Skip to content

Commit

Permalink
Son kontrol öncesi
Browse files Browse the repository at this point in the history
  • Loading branch information
mervanerdem committed Jun 25, 2023
1 parent c8089ab commit 08c46b9
Show file tree
Hide file tree
Showing 50 changed files with 1,121 additions and 350 deletions.
19 changes: 8 additions & 11 deletions ECOmmerceAPI.Business/Base/BaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public BaseService(IUnitOfWork unitOfWork, IMapper mapper)
this.unitOfWork = unitOfWork;
this.mapper = mapper;
}
public ApiResponse Delete(int Id)
public virtual ApiResponse Delete(int Id)
{
try
{
Expand All @@ -39,11 +39,11 @@ public ApiResponse Delete(int Id)
}
}

public ApiResponse<List<TResponse>> GetAll()
public virtual ApiResponse<List<TResponse>> GetAll()
{
try
{
var entityList = unitOfWork.Repository<TEntity>().GetAll();
var entityList = unitOfWork.Repository<TEntity>().GetAll().ToList();
var mapped = mapper.Map<List<TEntity>, List<TResponse>>(entityList);
return new ApiResponse<List<TResponse>>(mapped);
}
Expand All @@ -54,7 +54,7 @@ public ApiResponse<List<TResponse>> GetAll()
}
}

public ApiResponse<TResponse> GetById(int id)
public virtual ApiResponse<TResponse> GetById(int id)
{
try
{
Expand Down Expand Up @@ -94,27 +94,24 @@ public virtual ApiResponse Insert(TRequest request)
}
}

public ApiResponse Update(int Id, TRequest request)
public virtual ApiResponse Update(int Id, TRequest request)
{
try
{
var entity = mapper.Map<TRequest, User>(request);
var entity = mapper.Map<TRequest, TEntity>(request);

var exist = unitOfWork.Repository<User>().GetByIdAsNoTracking(Id);
var exist = unitOfWork.Repository<TEntity>().GetByIdAsNoTracking(Id);
if (exist is null)
{
return new ApiResponse("Record not found");
}

entity.Id = Id;
entity.UpdatedAt = DateTime.UtcNow;
entity.Role = exist.Role;
entity.CreatedBy = exist.CreatedBy;
entity.CreatedAt = exist.CreatedAt;
entity.UpdatedBy = exist.UserName;
entity.Password = JwtHelper.CreateMD5(entity.Password);

unitOfWork.Repository<User>().Update(entity);
unitOfWork.Repository<TEntity>().Update(entity);
if (unitOfWork.Complete() > 0)
{
return new ApiResponse();
Expand Down
1 change: 0 additions & 1 deletion ECOmmerceAPI.Business/ECommerceAPI.Business.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\ECommerceAPI.Data\ECommerceAPI.Data.csproj" />
<ProjectReference Include="..\ECommerceAPI.Schema\ECommerceAPI.Schema.csproj" />
</ItemGroup>

Expand Down
47 changes: 47 additions & 0 deletions ECOmmerceAPI.Business/Services/Category/CategoryService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using AutoMapper;
using ECommerceAPI.Base;
using ECommerceAPI.Data;
using ECommerceAPI.Data.Domain;
using ECommerceAPI.Schema.DataSets.Category;
using Microsoft.EntityFrameworkCore;

namespace ECommerceAPI.Business.Services
{
public class CategoryService : BaseService<Category, CategoryRequest, CategoryResponse>, ICategoryService
{

private readonly IUnitOfWork unitOfWork;
private readonly IMapper mapper;
public CategoryService(IUnitOfWork unitOfWork, IMapper mapper) : base(unitOfWork, mapper)
{
this.unitOfWork = unitOfWork;
this.mapper = mapper;
}

public override ApiResponse<List<CategoryResponse>> GetAll()
{
var categories = unitOfWork.Repository<Category>()
.GetAsQueryable()
.Include(x => x.Products)
.ThenInclude(pc => pc.Product)
.ToList();
var categoryResponses = mapper.Map<List<CategoryResponse>>(categories);
return new ApiResponse<List<CategoryResponse>>(categoryResponses);
}

public override ApiResponse<CategoryResponse> GetById(int id)
{
var category = unitOfWork.Repository<Category>()
.Where(x => x.Id.Equals(id))
.Include(x => x.Products)
.ThenInclude(pc => pc.Product)
.FirstOrDefault();
if (category is null)
{
return new ApiResponse<CategoryResponse>("Not Found");
}
var categoryResponse = mapper.Map<CategoryResponse>(category);
return new ApiResponse<CategoryResponse>(categoryResponse);
}
}
}
9 changes: 9 additions & 0 deletions ECOmmerceAPI.Business/Services/Category/ICategoryService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using ECommerceAPI.Data.Domain;
using ECommerceAPI.Schema.DataSets.Category;

namespace ECommerceAPI.Business.Services
{
public interface ICategoryService : IBaseService<Category, CategoryRequest, CategoryResponse>
{
}
}
24 changes: 24 additions & 0 deletions ECOmmerceAPI.Business/Services/Coupon/CouponService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using AutoMapper;
using ECommerceAPI.Data;
using ECommerceAPI.Data.Domain;
using ECommerceAPI.Schema.DataSets.Coupon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ECommerceAPI.Business.Services
{
public class CouponService : BaseService<Coupon, CouponRequest, CouponResponse>, ICouponService
{
private readonly IUnitOfWork unitOfWork;
private readonly IMapper mapper;
public CouponService(IUnitOfWork unitOfWork, IMapper mapper) : base(unitOfWork, mapper)
{
this.unitOfWork = unitOfWork;
this.mapper = mapper;
}

}
}
16 changes: 16 additions & 0 deletions ECOmmerceAPI.Business/Services/Coupon/ICoupunService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using ECommerceAPI.Data.Domain;
using ECommerceAPI.Schema.DataSets.Coupon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ECommerceAPI.Business.Services
{
public interface ICouponService : IBaseService<Coupon, CouponRequest, CouponResponse>
{


}
}
20 changes: 20 additions & 0 deletions ECOmmerceAPI.Business/Services/Order/IOrderService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using ECommerceAPI.Base;
using ECommerceAPI.Data.Domain;
using ECommerceAPI.Schema.DataSets.Order;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ECommerceAPI.Business.Services
{
public interface IOrderService : IBaseService<Order, OrderRequest, OrderResponse>
{
ApiResponse Add(OrderRequest request, int userId);

ApiResponse<List<OrderResponse>> GetAllMyOrder(int userId);


}
}
203 changes: 203 additions & 0 deletions ECOmmerceAPI.Business/Services/Order/OrderService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
using AutoMapper;
using ECommerceAPI.Base;
using ECommerceAPI.Data.Domain;
using ECommerceAPI.Data;
using ECommerceAPI.Schema.DataSets.Order;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ECommerceAPI.Base.Helper;

namespace ECommerceAPI.Business.Services
{

public class OrderService : BaseService<Category, OrderRequest, OrderResponse>, IOrderService
{
private readonly IUnitOfWork unitOfWork;
private readonly IMapper mapper;
public OrderService(IUnitOfWork unitOfWork, IMapper mapper) : base(unitOfWork, mapper)
{
this.unitOfWork = unitOfWork;
this.mapper = mapper;
}

// Sipariş Oluşturmak için kullanılır.
public ApiResponse Add(OrderRequest request, int userId)
{
if (request is null)
{
return new ApiResponse("Bad Request");
}
decimal pointSum = 0;
decimal couponPercentAmount = 0;
string couponCod = "";
var products = unitOfWork.Repository<Product>().Where(x => request.ProductIds.Contains(x.Id));
var productPrices = products.Select(x => x.Price).Sum();
var productCount = products.Count();
if (StockControl(products))
{
return new ApiResponse("No Stock");
}
var user = unitOfWork.Repository<User>().GetByIdAsNoTracking(userId);
var userPointRatio = user.DigitalWallet / productCount;
decimal productsPrice = 0;
if (string.IsNullOrWhiteSpace(request.CouponCode))
{
foreach (var item in products)
{
pointSum += PointCalculator(item, userPointRatio, item.Price);
productsPrice += item.Price;
}
}
else
{
var coupon = unitOfWork.Repository<Coupon>().Where(x => x.Code.Equals(request.CouponCode)).FirstOrDefault();
if (CouponControl(coupon))
{
return new ApiResponse("Coupon Wrong");
}
couponPercentAmount = coupon.PercentAmount;
couponCod = coupon.Code;
foreach (var item in products)
{
var price = item.Price - ((coupon.PercentAmount * item.Price) / 100);
pointSum += PointCalculator(item, userPointRatio, price);
productsPrice += price;
}
}
var sumPrice = productsPrice - user.DigitalWallet;
var couponAmount = (productPrices * couponPercentAmount) / 100;
if (sumPrice > 0)
{
if (Payment(sumPrice))
{
var orderNumber = GenerateUniqueOrderNumber();
OrderAdd(userId, sumPrice, user.DigitalWallet, couponAmount, couponCod, orderNumber);
PointAdd(user, pointSum);
StockReduction(products);
OrderDetailAdd(products, orderNumber);
if (unitOfWork.Complete() > 0)
{
return new ApiResponse();
}
else
{
return new ApiResponse("Internal Server Error");
}
}
return new ApiResponse("Payment Failed.");
}
else
{
return new ApiResponse();
}
}
// Benzersiz Sipariş Numarası Üretir.
private string GenerateUniqueOrderNumber()
{
while (true)
{
var orderNumber = JwtHelper.GenerateOrderNumber();
if (!unitOfWork.Repository<Order>().Where(x => x.OrderNumber.Equals(orderNumber)).Any())
{
return orderNumber;
}
}
}
// Sipariş Detaylarını OrderDetail tablosuna ekler.
private void OrderDetailAdd(IQueryable<Product> products, string orderNumber)
{
foreach (var item in products)
{
unitOfWork.Repository<OrderDetail>().Add(new OrderDetail
{
OrderNumber = orderNumber,
Price = item.Price,
ProductId = item.Id,
ProductName = item.Name
});

}
}
// Stok azaltır.
private void StockReduction(IQueryable<Product> products)
{
foreach (var item in products)
{
item.Stock--;
unitOfWork.Repository<Product>().Update(item);
}
}
// Sipariş Oluşturur.
private void OrderAdd(int userId, decimal sumPrice, decimal pointBalance, decimal couponAmount, string couponCode, string orderNumber)
{
unitOfWork.Repository<Order>().Add(new Order
{
UserID = userId,
OrderNumber = orderNumber,
CartAmount = sumPrice,
PointAmount = pointBalance,
CouponAmount = couponAmount,
CouponCode = couponCode
});
}
// Verilen kuponun geçerli olup olmadığını kontrol eder.
private bool CouponControl(Coupon coupon)
{
if (coupon is null)
{
return true;
}
if (coupon.IsActive.Equals(false) || DateTime.Now > coupon.ExpirationDate)
{
return true;
}
return false;

}
// Yeterli sayıda stok olup olmadığını kontrol eder.
private bool StockControl(IQueryable<Product> products)
{
foreach (var item in products)
{
if (item.Stock < 1)
{
return true;
}
}
return false;
}

private void PointAdd(User user, decimal pointSum)
{
user.DigitalWallet = pointSum;
unitOfWork.Repository<User>().Update(user);
}
// Ödeme entegrasyonu için metod.
private bool Payment(decimal sumPrice)
{
// Ödeme sistemi entegresi..
return true;

}
// Puan bakiyesinin hesaplama işlemi yapılır.
private decimal PointCalculator(Product item, decimal userPointRatio, decimal price)
{
var newPrice = price - userPointRatio;
var percentage = (newPrice * item.PointEarningPercentage) / 100;
var point = percentage > item.MaxPointAmount ? item.MaxPointAmount : percentage;
return point;
}

// Request header kısmında bulunan JWT keyindeki user bilgisine göre siparişleri getirir. Kullanıcının kendisine ait siparişleri çekmesini sağlar.
public ApiResponse<List<OrderResponse>> GetAllMyOrder(int userId)
{
var orders = unitOfWork.Repository<Order>().Where(x => x.UserID.Equals(userId)).ToList();
var orderResponses = mapper.Map<List<OrderResponse>>(orders);
return new ApiResponse<List<OrderResponse>>(orderResponses);

}
}
}
Loading

0 comments on commit 08c46b9

Please sign in to comment.