diff --git a/src/CMSMicroservice.Application/ProductsCQ/Commands/CreateNewProducts/CreateNewProductsCommand.cs b/src/CMSMicroservice.Application/ProductsCQ/Commands/CreateNewProducts/CreateNewProductsCommand.cs index 89639e4..4f7ec10 100644 --- a/src/CMSMicroservice.Application/ProductsCQ/Commands/CreateNewProducts/CreateNewProductsCommand.cs +++ b/src/CMSMicroservice.Application/ProductsCQ/Commands/CreateNewProducts/CreateNewProductsCommand.cs @@ -25,5 +25,7 @@ public record CreateNewProductsCommand : IRequest public int ViewCount { get; init; } // public int RemainingCount { get; init; } + // لیست شناسه دسته‌بندی‌های محصول + public ICollection? CategoryIds { get; init; } -} \ No newline at end of file +} diff --git a/src/CMSMicroservice.Application/ProductsCQ/Commands/CreateNewProducts/CreateNewProductsCommandHandler.cs b/src/CMSMicroservice.Application/ProductsCQ/Commands/CreateNewProducts/CreateNewProductsCommandHandler.cs index 704279f..d2cdad3 100644 --- a/src/CMSMicroservice.Application/ProductsCQ/Commands/CreateNewProducts/CreateNewProductsCommandHandler.cs +++ b/src/CMSMicroservice.Application/ProductsCQ/Commands/CreateNewProducts/CreateNewProductsCommandHandler.cs @@ -1,3 +1,4 @@ +using CMSMicroservice.Domain.Entities; using CMSMicroservice.Domain.Events; namespace CMSMicroservice.Application.ProductsCQ.Commands.CreateNewProducts; public class CreateNewProductsCommandHandler : IRequestHandler @@ -14,8 +15,30 @@ public class CreateNewProductsCommandHandler : IRequestHandler(); await _context.Productss.AddAsync(entity, cancellationToken); - entity.AddDomainEvent(new CreateNewProductsEvent(entity)); await _context.SaveChangesAsync(cancellationToken); + + // ثبت دسته‌بندی‌های محصول (در صورت ارسال) + if (request.CategoryIds is { Count: > 0 }) + { + var distinctCategoryIds = request.CategoryIds + .Where(id => id > 0) + .Distinct() + .ToList(); + + foreach (var categoryId in distinctCategoryIds) + { + var rel = new PruductCategory + { + ProductId = entity.Id, + CategoryId = categoryId + }; + await _context.PruductCategorys.AddAsync(rel, cancellationToken); + } + + await _context.SaveChangesAsync(cancellationToken); + } + + entity.AddDomainEvent(new CreateNewProductsEvent(entity)); return entity.Adapt(); } } diff --git a/src/CMSMicroservice.Application/ProductsCQ/Commands/UpdateProducts/UpdateProductsCommand.cs b/src/CMSMicroservice.Application/ProductsCQ/Commands/UpdateProducts/UpdateProductsCommand.cs index 49daa17..a562980 100644 --- a/src/CMSMicroservice.Application/ProductsCQ/Commands/UpdateProducts/UpdateProductsCommand.cs +++ b/src/CMSMicroservice.Application/ProductsCQ/Commands/UpdateProducts/UpdateProductsCommand.cs @@ -27,5 +27,7 @@ public record UpdateProductsCommand : IRequest public int ViewCount { get; init; } // public int RemainingCount { get; init; } + // لیست شناسه دسته‌بندی‌های محصول + public ICollection? CategoryIds { get; init; } -} \ No newline at end of file +} diff --git a/src/CMSMicroservice.Application/ProductsCQ/Commands/UpdateProducts/UpdateProductsCommandHandler.cs b/src/CMSMicroservice.Application/ProductsCQ/Commands/UpdateProducts/UpdateProductsCommandHandler.cs index aa19799..5883a7a 100644 --- a/src/CMSMicroservice.Application/ProductsCQ/Commands/UpdateProducts/UpdateProductsCommandHandler.cs +++ b/src/CMSMicroservice.Application/ProductsCQ/Commands/UpdateProducts/UpdateProductsCommandHandler.cs @@ -1,4 +1,8 @@ +using CMSMicroservice.Application.Common.Exceptions; +using CMSMicroservice.Application.Common.Interfaces; +using CMSMicroservice.Domain.Entities; using CMSMicroservice.Domain.Events; +using Microsoft.EntityFrameworkCore; namespace CMSMicroservice.Application.ProductsCQ.Commands.UpdateProducts; public class UpdateProductsCommandHandler : IRequestHandler { @@ -12,9 +16,47 @@ public class UpdateProductsCommandHandler : IRequestHandler Handle(UpdateProductsCommand request, CancellationToken cancellationToken) { var entity = await _context.Productss - .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) ?? throw new NotFoundException(nameof(Products), request.Id); + .FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken) + ?? throw new NotFoundException(nameof(Products), request.Id); + request.Adapt(entity); _context.Productss.Update(entity); + + // به‌روزرسانی دسته‌بندی‌های محصول در صورت ارسال CategoryIds + if (request.CategoryIds is not null) + { + var targetIds = (request.CategoryIds ?? Array.Empty()) + .Where(id => id > 0) + .Distinct() + .ToHashSet(); + + var existingRelations = await _context.PruductCategorys + .Where(x => x.ProductId == entity.Id) + .ToListAsync(cancellationToken); + + var existingIds = existingRelations + .Select(x => x.CategoryId) + .ToHashSet(); + + var toAdd = targetIds.Except(existingIds).ToList(); + var toRemove = existingRelations.Where(x => !targetIds.Contains(x.CategoryId)).ToList(); + + foreach (var categoryId in toAdd) + { + var rel = new PruductCategory + { + ProductId = entity.Id, + CategoryId = categoryId + }; + await _context.PruductCategorys.AddAsync(rel, cancellationToken); + } + + if (toRemove.Count > 0) + { + _context.PruductCategorys.RemoveRange(toRemove); + } + } + entity.AddDomainEvent(new UpdateProductsEvent(entity)); await _context.SaveChangesAsync(cancellationToken); return Unit.Value; diff --git a/src/CMSMicroservice.Application/ProductsCQ/Queries/GetAllProductsByFilter/GetAllProductsByFilterQueryHandler.cs b/src/CMSMicroservice.Application/ProductsCQ/Queries/GetAllProductsByFilter/GetAllProductsByFilterQueryHandler.cs index 90854c5..b7e7a81 100644 --- a/src/CMSMicroservice.Application/ProductsCQ/Queries/GetAllProductsByFilter/GetAllProductsByFilterQueryHandler.cs +++ b/src/CMSMicroservice.Application/ProductsCQ/Queries/GetAllProductsByFilter/GetAllProductsByFilterQueryHandler.cs @@ -32,11 +32,35 @@ public class GetAllProductsByFilterQueryHandler : IRequestHandler request.Filter.RemainingCount == null || x.RemainingCount == request.Filter.RemainingCount) ; } + var meta = await query.GetMetaData(request.PaginationState, cancellationToken); + + var models = await query + .PaginatedListAsync(paginationState: request.PaginationState) + .Select(x => new GetAllProductsByFilterResponseModel + { + Id = x.Id, + Title = x.Title, + Description = x.Description, + ShortInfomation = x.ShortInfomation, + FullInformation = x.FullInformation, + Price = x.Price, + Discount = x.Discount, + Rate = x.Rate, + ImagePath = x.ImagePath, + ThumbnailPath = x.ThumbnailPath, + SaleCount = x.SaleCount, + ViewCount = x.ViewCount, + RemainingCount = x.RemainingCount, + CategoryIds = x.PruductCategorys + .Select(pc => pc.CategoryId) + .ToList() + }) + .ToListAsync(cancellationToken); + return new GetAllProductsByFilterResponseDto { - MetaData = await query.GetMetaData(request.PaginationState, cancellationToken), - Models = await query.PaginatedListAsync(paginationState: request.PaginationState) - .ProjectToType().ToListAsync(cancellationToken) + MetaData = meta, + Models = models }; } } diff --git a/src/CMSMicroservice.Application/ProductsCQ/Queries/GetAllProductsByFilter/GetAllProductsByFilterResponseDto.cs b/src/CMSMicroservice.Application/ProductsCQ/Queries/GetAllProductsByFilter/GetAllProductsByFilterResponseDto.cs index 6525d21..78a7b57 100644 --- a/src/CMSMicroservice.Application/ProductsCQ/Queries/GetAllProductsByFilter/GetAllProductsByFilterResponseDto.cs +++ b/src/CMSMicroservice.Application/ProductsCQ/Queries/GetAllProductsByFilter/GetAllProductsByFilterResponseDto.cs @@ -6,7 +6,9 @@ public class GetAllProductsByFilterResponseDto //مدل خروجی public List? Models { get; set; } -}public class GetAllProductsByFilterResponseModel +} + +public class GetAllProductsByFilterResponseModel { // public long Id { get; set; } @@ -34,4 +36,6 @@ public class GetAllProductsByFilterResponseDto public int ViewCount { get; set; } // public int RemainingCount { get; set; } + // لیست شناسه دسته‌بندی‌های محصول + public List CategoryIds { get; set; } = new(); } diff --git a/src/CMSMicroservice.Application/ProductsCQ/Queries/GetProducts/GetProductsQueryHandler.cs b/src/CMSMicroservice.Application/ProductsCQ/Queries/GetProducts/GetProductsQueryHandler.cs index c1738cc..542d0b6 100644 --- a/src/CMSMicroservice.Application/ProductsCQ/Queries/GetProducts/GetProductsQueryHandler.cs +++ b/src/CMSMicroservice.Application/ProductsCQ/Queries/GetProducts/GetProductsQueryHandler.cs @@ -14,7 +14,25 @@ public class GetProductsQueryHandler : IRequestHandler x.Id == request.Id) - .ProjectToType() + .Select(x => new GetProductsResponseDto + { + Id = x.Id, + Title = x.Title, + Description = x.Description, + ShortInfomation = x.ShortInfomation, + FullInformation = x.FullInformation, + Price = x.Price, + Discount = x.Discount, + Rate = x.Rate, + ImagePath = x.ImagePath, + ThumbnailPath = x.ThumbnailPath, + SaleCount = x.SaleCount, + ViewCount = x.ViewCount, + RemainingCount = x.RemainingCount, + CategoryIds = x.PruductCategorys + .Select(pc => pc.CategoryId) + .ToList() + }) .FirstOrDefaultAsync(cancellationToken); return response ?? throw new NotFoundException(nameof(Products), request.Id); diff --git a/src/CMSMicroservice.Application/ProductsCQ/Queries/GetProducts/GetProductsResponseDto.cs b/src/CMSMicroservice.Application/ProductsCQ/Queries/GetProducts/GetProductsResponseDto.cs index ccd0ada..ea5ac1c 100644 --- a/src/CMSMicroservice.Application/ProductsCQ/Queries/GetProducts/GetProductsResponseDto.cs +++ b/src/CMSMicroservice.Application/ProductsCQ/Queries/GetProducts/GetProductsResponseDto.cs @@ -27,5 +27,7 @@ public class GetProductsResponseDto public int ViewCount { get; set; } // public int RemainingCount { get; set; } + // لیست شناسه دسته‌بندی‌های محصول + public List CategoryIds { get; set; } = new(); -} \ No newline at end of file +} diff --git a/src/CMSMicroservice.Protobuf/Protos/products.proto b/src/CMSMicroservice.Protobuf/Protos/products.proto index fab3a5d..4e12748 100644 --- a/src/CMSMicroservice.Protobuf/Protos/products.proto +++ b/src/CMSMicroservice.Protobuf/Protos/products.proto @@ -58,6 +58,8 @@ message CreateNewProductsRequest int32 sale_count = 10; int32 view_count = 11; int32 remaining_count = 12; + // لیست شناسه دسته‌بندی‌های محصول + repeated int64 category_ids = 13; } message CreateNewProductsResponse { @@ -78,6 +80,8 @@ message UpdateProductsRequest int32 sale_count = 11; int32 view_count = 12; int32 remaining_count = 13; + // لیست شناسه دسته‌بندی‌های محصول + repeated int64 category_ids = 14; } message DeleteProductsRequest { @@ -102,6 +106,8 @@ message GetProductsResponse int32 sale_count = 11; int32 view_count = 12; int32 remaining_count = 13; + // لیست شناسه دسته‌بندی‌های محصول + repeated int64 category_ids = 14; } message GetAllProductsByFilterRequest { @@ -145,4 +151,6 @@ message GetAllProductsByFilterResponseModel int32 sale_count = 11; int32 view_count = 12; int32 remaining_count = 13; + // لیست شناسه دسته‌بندی‌های محصول + repeated int64 category_ids = 14; }