-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c8089ab
commit 08c46b9
Showing
50 changed files
with
1,121 additions
and
350 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
ECOmmerceAPI.Business/Services/Category/CategoryService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
{ | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
} | ||
} |
Oops, something went wrong.