Add validators and services for Product Galleries and Product Tags

- Implemented Create, Delete, Get, and Update validators for Product Galleries.
- Added Create, Delete, Get, and Update validators for Product Tags.
- Created service classes for handling Discount Categories, Discount Orders, Discount Products, Discount Shopping Cart, Product Categories, Product Galleries, and Product Tags.
- Each service class integrates with CQRS for command and query handling.
- Established mapping profiles for Product Galleries.
This commit is contained in:
masoodafar-web
2025-12-04 02:40:49 +03:30
parent 40d54d08fc
commit f0f48118e7
436 changed files with 33159 additions and 2005 deletions
@@ -0,0 +1,28 @@
using MediatR;
namespace CMSMicroservice.Application.DiscountShopCQ.Queries.GetDiscountCategories;
public class GetDiscountCategoriesQuery : IRequest<GetDiscountCategoriesResponseDto>
{
public long? ParentCategoryId { get; set; }
public bool? IsActive { get; set; }
}
public class GetDiscountCategoriesResponseDto
{
public List<DiscountCategoryDto> Categories { get; set; }
}
public class DiscountCategoryDto
{
public long Id { get; set; }
public string Name { get; set; }
public string Title { get; set; }
public string? Description { get; set; }
public string? ImagePath { get; set; }
public long? ParentCategoryId { get; set; }
public int SortOrder { get; set; }
public bool IsActive { get; set; }
public int ProductCount { get; set; }
public List<DiscountCategoryDto>? Children { get; set; }
}
@@ -0,0 +1,99 @@
using CMSMicroservice.Application.Common.Interfaces;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace CMSMicroservice.Application.DiscountShopCQ.Queries.GetDiscountCategories;
public class GetDiscountCategoriesQueryHandler : IRequestHandler<GetDiscountCategoriesQuery, GetDiscountCategoriesResponseDto>
{
private readonly IApplicationDbContext _context;
public GetDiscountCategoriesQueryHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<GetDiscountCategoriesResponseDto> Handle(GetDiscountCategoriesQuery request, CancellationToken cancellationToken)
{
var query = _context.DiscountCategories.AsQueryable();
// فیلتر بر اساس ParentCategoryId
if (request.ParentCategoryId.HasValue)
{
query = query.Where(c => c.ParentCategoryId == request.ParentCategoryId.Value);
}
else
{
// اگر ParentCategoryId مشخص نشده، فقط دسته‌های اصلی (بدون والد) را برگردان
query = query.Where(c => c.ParentCategoryId == null);
}
// فیلتر بر اساس وضعیت فعال
if (request.IsActive.HasValue)
{
query = query.Where(c => c.IsActive == request.IsActive.Value);
}
var categories = await query
.OrderBy(c => c.SortOrder)
.ThenBy(c => c.Title)
.Select(c => new DiscountCategoryDto
{
Id = c.Id,
Name = c.Name,
Title = c.Title,
Description = c.Description,
ImagePath = c.ImagePath,
ParentCategoryId = c.ParentCategoryId,
SortOrder = c.SortOrder,
IsActive = c.IsActive,
ProductCount = _context.DiscountProductCategories.Count(pc => pc.CategoryId == c.Id)
})
.ToListAsync(cancellationToken);
// بارگذاری زیرمجموعه‌ها به صورت بازگشتی
foreach (var category in categories)
{
category.Children = await LoadChildren(category.Id, request.IsActive, cancellationToken);
}
return new GetDiscountCategoriesResponseDto
{
Categories = categories
};
}
private async Task<List<DiscountCategoryDto>> LoadChildren(long parentId, bool? isActive, CancellationToken cancellationToken)
{
var query = _context.DiscountCategories.Where(c => c.ParentCategoryId == parentId);
if (isActive.HasValue)
{
query = query.Where(c => c.IsActive == isActive.Value);
}
var children = await query
.OrderBy(c => c.SortOrder)
.ThenBy(c => c.Title)
.Select(c => new DiscountCategoryDto
{
Id = c.Id,
Name = c.Name,
Title = c.Title,
Description = c.Description,
ImagePath = c.ImagePath,
ParentCategoryId = c.ParentCategoryId,
SortOrder = c.SortOrder,
IsActive = c.IsActive,
ProductCount = _context.DiscountProductCategories.Count(pc => pc.CategoryId == c.Id)
})
.ToListAsync(cancellationToken);
foreach (var child in children)
{
child.Children = await LoadChildren(child.Id, isActive, cancellationToken);
}
return children;
}
}
@@ -0,0 +1,33 @@
using MediatR;
namespace CMSMicroservice.Application.DiscountShopCQ.Queries.GetDiscountProductById;
public class GetDiscountProductByIdQuery : IRequest<DiscountProductDetailDto?>
{
public long ProductId { get; set; }
}
public class DiscountProductDetailDto
{
public long Id { get; set; }
public string Title { get; set; }
public string ShortInfomation { get; set; }
public string FullInformation { get; set; }
public long Price { get; set; }
public int MaxDiscountPercent { get; set; }
public int Rate { get; set; }
public string ImagePath { get; set; }
public string ThumbnailPath { get; set; }
public int SaleCount { get; set; }
public int ViewCount { get; set; }
public int RemainingCount { get; set; }
public bool IsActive { get; set; }
public List<CategoryDto> Categories { get; set; } = new();
}
public class CategoryDto
{
public long Id { get; set; }
public string Name { get; set; }
public string Title { get; set; }
}
@@ -0,0 +1,64 @@
using CMSMicroservice.Application.Common.Interfaces;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace CMSMicroservice.Application.DiscountShopCQ.Queries.GetDiscountProductById;
public class GetDiscountProductByIdQueryHandler : IRequestHandler<GetDiscountProductByIdQuery, DiscountProductDetailDto?>
{
private readonly IApplicationDbContext _context;
public GetDiscountProductByIdQueryHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<DiscountProductDetailDto?> Handle(GetDiscountProductByIdQuery request, CancellationToken cancellationToken)
{
var product = await _context.DiscountProducts
.Where(p => p.Id == request.ProductId)
.Select(p => new DiscountProductDetailDto
{
Id = p.Id,
Title = p.Title,
ShortInfomation = p.ShortInfomation,
FullInformation = p.FullInformation,
Price = p.Price,
MaxDiscountPercent = p.MaxDiscountPercent,
Rate = p.Rate,
ImagePath = p.ImagePath,
ThumbnailPath = p.ThumbnailPath,
SaleCount = p.SaleCount,
ViewCount = p.ViewCount,
RemainingCount = p.RemainingCount,
IsActive = p.IsActive
})
.FirstOrDefaultAsync(cancellationToken);
if (product == null)
return null;
// Get categories
var categories = await _context.DiscountProductCategories
.Where(pc => pc.ProductId == request.ProductId)
.Select(pc => new CategoryDto
{
Id = pc.Category.Id,
Name = pc.Category.Name,
Title = pc.Category.Title
})
.ToListAsync(cancellationToken);
product.Categories = categories;
// Increment view count
var productEntity = await _context.DiscountProducts.FindAsync(new object[] { request.ProductId }, cancellationToken);
if (productEntity != null)
{
productEntity.ViewCount++;
await _context.SaveChangesAsync(cancellationToken);
}
return product;
}
}
@@ -0,0 +1,36 @@
using CMSMicroservice.Application.Common.Models;
using MediatR;
namespace CMSMicroservice.Application.DiscountShopCQ.Queries.GetDiscountProducts;
public class GetDiscountProductsQuery : IRequest<GetDiscountProductsResponseDto>
{
public PaginationState? PaginationQuery { get; set; }
public long? CategoryId { get; set; }
public string? SearchTerm { get; set; }
public bool? IsActive { get; set; }
public int? MinPrice { get; set; }
public int? MaxPrice { get; set; }
}
public class GetDiscountProductsResponseDto
{
public MetaData MetaData { get; set; }
public List<DiscountProductDto> Models { get; set; }
}
public class DiscountProductDto
{
public long Id { get; set; }
public string Title { get; set; }
public string ShortInfomation { get; set; }
public long Price { get; set; }
public int MaxDiscountPercent { get; set; }
public int Rate { get; set; }
public string ImagePath { get; set; }
public string ThumbnailPath { get; set; }
public int SaleCount { get; set; }
public int ViewCount { get; set; }
public int RemainingCount { get; set; }
public bool IsActive { get; set; }
}
@@ -0,0 +1,92 @@
using CMSMicroservice.Application.Common.Interfaces;
using CMSMicroservice.Application.Common.Models;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace CMSMicroservice.Application.DiscountShopCQ.Queries.GetDiscountProducts;
public class GetDiscountProductsQueryHandler : IRequestHandler<GetDiscountProductsQuery, GetDiscountProductsResponseDto>
{
private readonly IApplicationDbContext _context;
public GetDiscountProductsQueryHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<GetDiscountProductsResponseDto> Handle(GetDiscountProductsQuery request, CancellationToken cancellationToken)
{
var query = _context.DiscountProducts.AsQueryable();
// Apply filters
if (request.CategoryId.HasValue)
{
var productIds = await _context.DiscountProductCategories
.Where(pc => pc.CategoryId == request.CategoryId.Value)
.Select(pc => pc.ProductId)
.ToListAsync(cancellationToken);
query = query.Where(p => productIds.Contains(p.Id));
}
if (!string.IsNullOrWhiteSpace(request.SearchTerm))
{
query = query.Where(p =>
p.Title.Contains(request.SearchTerm) ||
p.ShortInfomation.Contains(request.SearchTerm));
}
if (request.IsActive.HasValue)
{
query = query.Where(p => p.IsActive == request.IsActive.Value);
}
if (request.MinPrice.HasValue)
{
query = query.Where(p => p.Price >= request.MinPrice.Value);
}
if (request.MaxPrice.HasValue)
{
query = query.Where(p => p.Price <= request.MaxPrice.Value);
}
var totalCount = await query.CountAsync(cancellationToken);
// Apply pagination
var pagination = request.PaginationQuery ?? new PaginationState { PageNumber = 1, PageSize = 10 };
var products = await query
.OrderByDescending(p => p.Created)
.Skip((pagination.PageNumber - 1) * pagination.PageSize)
.Take(pagination.PageSize)
.Select(p => new DiscountProductDto
{
Id = p.Id,
Title = p.Title,
ShortInfomation = p.ShortInfomation,
Price = p.Price,
MaxDiscountPercent = p.MaxDiscountPercent,
Rate = p.Rate,
ImagePath = p.ImagePath,
ThumbnailPath = p.ThumbnailPath,
SaleCount = p.SaleCount,
ViewCount = p.ViewCount,
RemainingCount = p.RemainingCount,
IsActive = p.IsActive
})
.ToListAsync(cancellationToken);
return new GetDiscountProductsResponseDto
{
MetaData = new MetaData
{
TotalCount = totalCount,
PageSize = pagination.PageSize,
CurrentPage = pagination.PageNumber,
TotalPage = (int)Math.Ceiling(totalCount / (double)pagination.PageSize)
},
Models = products
};
}
}
@@ -0,0 +1,46 @@
using CMSMicroservice.Domain.Enums;
using MediatR;
namespace CMSMicroservice.Application.DiscountShopCQ.Queries.GetOrderById;
public class GetOrderByIdQuery : IRequest<OrderDetailDto?>
{
public long OrderId { get; set; }
public long UserId { get; set; }
}
public class OrderDetailDto
{
public long Id { get; set; }
public long UserId { get; set; }
public long TotalAmount { get; set; }
public long DiscountBalanceUsed { get; set; }
public long GatewayAmountPaid { get; set; }
public long VatAmount { get; set; }
public PaymentStatus PaymentStatus { get; set; }
public DateTime? PaymentDate { get; set; }
public DeliveryStatus DeliveryStatus { get; set; }
public string? TrackingCode { get; set; }
public string? DeliveryDescription { get; set; }
public DateTime Created { get; set; }
public UserAddressDto Address { get; set; }
public List<OrderItemDto> Items { get; set; } = new();
}
public class UserAddressDto
{
public string Title { get; set; }
public string Address { get; set; }
public string PostalCode { get; set; }
}
public class OrderItemDto
{
public long ProductId { get; set; }
public string ProductTitle { get; set; }
public int Count { get; set; }
public long UnitPrice { get; set; }
public int DiscountPercentUsed { get; set; }
public long DiscountAmount { get; set; }
public long FinalPrice { get; set; }
}
@@ -0,0 +1,60 @@
using CMSMicroservice.Application.Common.Interfaces;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace CMSMicroservice.Application.DiscountShopCQ.Queries.GetOrderById;
public class GetOrderByIdQueryHandler : IRequestHandler<GetOrderByIdQuery, OrderDetailDto?>
{
private readonly IApplicationDbContext _context;
public GetOrderByIdQueryHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<OrderDetailDto?> Handle(GetOrderByIdQuery request, CancellationToken cancellationToken)
{
var order = await _context.DiscountOrders
.Where(o => o.Id == request.OrderId && o.UserId == request.UserId)
.Include(o => o.UserAddress)
.Include(o => o.OrderDetails)
.ThenInclude(od => od.Product)
.FirstOrDefaultAsync(cancellationToken);
if (order == null)
return null;
return new OrderDetailDto
{
Id = order.Id,
UserId = order.UserId,
TotalAmount = order.TotalAmount,
DiscountBalanceUsed = order.DiscountBalanceUsed,
GatewayAmountPaid = order.GatewayAmountPaid,
VatAmount = order.VatAmount,
PaymentStatus = order.PaymentStatus,
PaymentDate = order.PaymentDate,
DeliveryStatus = order.DeliveryStatus,
TrackingCode = order.TrackingCode,
DeliveryDescription = order.DeliveryDescription,
Created = order.Created,
Address = new UserAddressDto
{
Title = order.UserAddress.Title,
Address = order.UserAddress.Address,
PostalCode = order.UserAddress.PostalCode
},
Items = order.OrderDetails.Select(od => new OrderItemDto
{
ProductId = od.ProductId,
ProductTitle = od.Product.Title,
Count = od.Count,
UnitPrice = od.UnitPrice,
DiscountPercentUsed = od.DiscountPercentUsed,
DiscountAmount = od.DiscountAmount,
FinalPrice = od.FinalPrice
}).ToList()
};
}
}
@@ -0,0 +1,32 @@
using MediatR;
namespace CMSMicroservice.Application.DiscountShopCQ.Queries.GetUserCart;
public class GetUserCartQuery : IRequest<UserCartDto>
{
public long UserId { get; set; }
}
public class UserCartDto
{
public List<CartItemDto> Items { get; set; } = new();
public long TotalAmount { get; set; }
public long MaxDiscountAmount { get; set; }
public long MinPayableAmount { get; set; }
}
public class CartItemDto
{
public long CartItemId { get; set; }
public long ProductId { get; set; }
public string ProductTitle { get; set; }
public string ProductImagePath { get; set; }
public long UnitPrice { get; set; }
public int Count { get; set; }
public long SubTotal { get; set; }
public int MaxDiscountPercent { get; set; }
public long MaxDiscountAmount { get; set; }
public long MinPayable { get; set; }
public int RemainingStock { get; set; }
public bool IsActive { get; set; }
}
@@ -0,0 +1,50 @@
using CMSMicroservice.Application.Common.Interfaces;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace CMSMicroservice.Application.DiscountShopCQ.Queries.GetUserCart;
public class GetUserCartQueryHandler : IRequestHandler<GetUserCartQuery, UserCartDto>
{
private readonly IApplicationDbContext _context;
public GetUserCartQueryHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<UserCartDto> Handle(GetUserCartQuery request, CancellationToken cancellationToken)
{
var cartItems = await _context.DiscountShoppingCarts
.Where(c => c.UserId == request.UserId)
.Include(c => c.Product)
.Select(c => new CartItemDto
{
CartItemId = c.Id,
ProductId = c.ProductId,
ProductTitle = c.Product.Title,
ProductImagePath = c.Product.ThumbnailPath,
UnitPrice = c.Product.Price,
Count = c.Count,
SubTotal = c.Product.Price * c.Count,
MaxDiscountPercent = c.Product.MaxDiscountPercent,
MaxDiscountAmount = (c.Product.Price * c.Count * c.Product.MaxDiscountPercent) / 100,
MinPayable = c.Product.Price * c.Count - ((c.Product.Price * c.Count * c.Product.MaxDiscountPercent) / 100),
RemainingStock = c.Product.RemainingCount,
IsActive = c.Product.IsActive
})
.ToListAsync(cancellationToken);
var totalAmount = cartItems.Sum(i => i.SubTotal);
var maxDiscountAmount = cartItems.Sum(i => i.MaxDiscountAmount);
var minPayableAmount = cartItems.Sum(i => i.MinPayable);
return new UserCartDto
{
Items = cartItems,
TotalAmount = totalAmount,
MaxDiscountAmount = maxDiscountAmount,
MinPayableAmount = minPayableAmount
};
}
}
@@ -0,0 +1,34 @@
using CMSMicroservice.Application.Common.Models;
using CMSMicroservice.Domain.Enums;
using MediatR;
namespace CMSMicroservice.Application.DiscountShopCQ.Queries.GetUserOrders;
public class GetUserOrdersQuery : IRequest<GetUserOrdersResponseDto>
{
public long UserId { get; set; }
public PaginationState? PaginationQuery { get; set; }
public PaymentStatus? PaymentStatus { get; set; }
public DeliveryStatus? DeliveryStatus { get; set; }
}
public class GetUserOrdersResponseDto
{
public MetaData MetaData { get; set; }
public List<OrderSummaryDto> Models { get; set; }
}
public class OrderSummaryDto
{
public long Id { get; set; }
public long TotalAmount { get; set; }
public long DiscountBalanceUsed { get; set; }
public long GatewayAmountPaid { get; set; }
public long VatAmount { get; set; }
public PaymentStatus PaymentStatus { get; set; }
public DateTime? PaymentDate { get; set; }
public DeliveryStatus DeliveryStatus { get; set; }
public string? TrackingCode { get; set; }
public DateTime Created { get; set; }
public int ItemsCount { get; set; }
}
@@ -0,0 +1,70 @@
using CMSMicroservice.Application.Common.Interfaces;
using CMSMicroservice.Application.Common.Models;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace CMSMicroservice.Application.DiscountShopCQ.Queries.GetUserOrders;
public class GetUserOrdersQueryHandler : IRequestHandler<GetUserOrdersQuery, GetUserOrdersResponseDto>
{
private readonly IApplicationDbContext _context;
public GetUserOrdersQueryHandler(IApplicationDbContext context)
{
_context = context;
}
public async Task<GetUserOrdersResponseDto> Handle(GetUserOrdersQuery request, CancellationToken cancellationToken)
{
var query = _context.DiscountOrders
.Where(o => o.UserId == request.UserId);
// Apply filters
if (request.PaymentStatus.HasValue)
{
query = query.Where(o => o.PaymentStatus == request.PaymentStatus.Value);
}
if (request.DeliveryStatus.HasValue)
{
query = query.Where(o => o.DeliveryStatus == request.DeliveryStatus.Value);
}
var totalCount = await query.CountAsync(cancellationToken);
// Apply pagination
var pagination = request.PaginationQuery ?? new PaginationState { PageNumber = 1, PageSize = 10 };
var orders = await query
.OrderByDescending(o => o.Created)
.Skip((pagination.PageNumber - 1) * pagination.PageSize)
.Take(pagination.PageSize)
.Select(o => new OrderSummaryDto
{
Id = o.Id,
TotalAmount = o.TotalAmount,
DiscountBalanceUsed = o.DiscountBalanceUsed,
GatewayAmountPaid = o.GatewayAmountPaid,
VatAmount = o.VatAmount,
PaymentStatus = o.PaymentStatus,
PaymentDate = o.PaymentDate,
DeliveryStatus = o.DeliveryStatus,
TrackingCode = o.TrackingCode,
Created = o.Created,
ItemsCount = o.OrderDetails.Count
})
.ToListAsync(cancellationToken);
return new GetUserOrdersResponseDto
{
MetaData = new MetaData
{
TotalCount = totalCount,
PageSize = pagination.PageSize,
CurrentPage = pagination.PageNumber,
TotalPage = (int)Math.Ceiling(totalCount / (double)pagination.PageSize)
},
Models = orders
};
}
}