Skip to content

Commit

Permalink
[Product details management][Duy] Delete product detail
Browse files Browse the repository at this point in the history
- Add 'IsDeleted' field in entity
- Add script to add 'IsDeleted' column in database
- Implement delete endpoint
  • Loading branch information
duykasama committed Jan 24, 2024
1 parent 25575ba commit 3650320
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Domus.Api/Domus.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
<EmbeddedResource Include="Scripts\7.Add_Package_Discount.sql" />
<None Remove="Scripts\8.Add_ProductDetail_DisplayPrice.sql" />
<EmbeddedResource Include="Scripts\8.Add_ProductDetail_DisplayPrice.sql" />
<None Remove="Scripts\9.Add_ProductDetail_IsDeleted.sql" />
<EmbeddedResource Include="Scripts\9.Add_ProductDetail_IsDeleted.sql" />
</ItemGroup>

</Project>
2 changes: 2 additions & 0 deletions Domus.Api/Scripts/9.Add_ProductDetail_IsDeleted.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE [ProductDetail]
ADD [IsDeleted] BIT DEFAULT 0
1 change: 1 addition & 0 deletions Domus.Domain/DatabaseMappings/ProductDetailModelMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Domus.Domain/Entities/ProductDetail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Domus.Domain.Entities;

public partial class ProductDetail : BaseEntity<Guid>
public partial class ProductDetail : DeletableEntity<Guid>
{
public Guid ProductId { get; set; }

Expand Down
5 changes: 4 additions & 1 deletion Domus.Service/Implementations/ProductDetailService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,12 @@ public async Task<ServiceActionResult> CreateProductDetail(CreateProductDetailRe

public async Task<ServiceActionResult> 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);
Expand Down

0 comments on commit 3650320

Please sign in to comment.