From 36503207e298914ed479d77841087a5638135cbd Mon Sep 17 00:00:00 2001 From: duykasama Date: Thu, 25 Jan 2024 04:56:13 +0700 Subject: [PATCH] [Product details management][Duy] Delete product detail - Add 'IsDeleted' field in entity - Add script to add 'IsDeleted' column in database - Implement delete endpoint --- Domus.Api/Domus.Api.csproj | 2 ++ Domus.Api/Scripts/9.Add_ProductDetail_IsDeleted.sql | 2 ++ Domus.Domain/DatabaseMappings/ProductDetailModelMapper.cs | 1 + Domus.Domain/Entities/ProductDetail.cs | 2 +- Domus.Service/Implementations/ProductDetailService.cs | 5 ++++- 5 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 Domus.Api/Scripts/9.Add_ProductDetail_IsDeleted.sql diff --git a/Domus.Api/Domus.Api.csproj b/Domus.Api/Domus.Api.csproj index 0749ed0..f35c471 100644 --- a/Domus.Api/Domus.Api.csproj +++ b/Domus.Api/Domus.Api.csproj @@ -38,6 +38,8 @@ + + diff --git a/Domus.Api/Scripts/9.Add_ProductDetail_IsDeleted.sql b/Domus.Api/Scripts/9.Add_ProductDetail_IsDeleted.sql new file mode 100644 index 0000000..d6157bc --- /dev/null +++ b/Domus.Api/Scripts/9.Add_ProductDetail_IsDeleted.sql @@ -0,0 +1,2 @@ +ALTER TABLE [ProductDetail] +ADD [IsDeleted] BIT DEFAULT 0 diff --git a/Domus.Domain/DatabaseMappings/ProductDetailModelMapper.cs b/Domus.Domain/DatabaseMappings/ProductDetailModelMapper.cs index 982fcbd..77326ab 100644 --- a/Domus.Domain/DatabaseMappings/ProductDetailModelMapper.cs +++ b/Domus.Domain/DatabaseMappings/ProductDetailModelMapper.cs @@ -14,6 +14,7 @@ public void Map(ModelBuilder modelBuilder) entity.Property(e => e.Id).ValueGeneratedOnAdd(); entity.Property(e => e.DisplayPrice).HasColumnType("float"); + entity.Property(e => e.IsDeleted).HasDefaultValueSql("((0))"); entity.HasOne(d => d.Product).WithMany(p => p.ProductDetails) .HasForeignKey(d => d.ProductId) diff --git a/Domus.Domain/Entities/ProductDetail.cs b/Domus.Domain/Entities/ProductDetail.cs index 9dccf2c..80e4b44 100644 --- a/Domus.Domain/Entities/ProductDetail.cs +++ b/Domus.Domain/Entities/ProductDetail.cs @@ -2,7 +2,7 @@ namespace Domus.Domain.Entities; -public partial class ProductDetail : BaseEntity +public partial class ProductDetail : DeletableEntity { public Guid ProductId { get; set; } diff --git a/Domus.Service/Implementations/ProductDetailService.cs b/Domus.Service/Implementations/ProductDetailService.cs index 55a542d..7c99db9 100644 --- a/Domus.Service/Implementations/ProductDetailService.cs +++ b/Domus.Service/Implementations/ProductDetailService.cs @@ -92,9 +92,12 @@ public async Task CreateProductDetail(CreateProductDetailRe public async Task DeleteProductDetail(Guid id) { - if (!await _productDetailRepository.ExistsAsync(pd => pd.Id == id)) + var productDetail = await _productDetailRepository.GetAsync(pd => pd.Id == id); + if (productDetail == null) throw new ProductDetailNotFoundException(); + productDetail.IsDeleted = true; + await _productDetailRepository.UpdateAsync(productDetail); await _unitOfWork.CommitAsync(); return new ServiceActionResult(true);