UpdateBrandRequest to include IsActive flag and update BrandService to handle inactive brands correctly

This commit is contained in:
2026-09-08 11:44:16 +05:30
parent 04004437bb
commit 68aea8105d
2 changed files with 8 additions and 5 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
namespace Distribution.Features.Brand;
public record CreateBrandRequest(string Name);
public record UpdateBrandRequest(string Name);
public record UpdateBrandRequest(string Name, bool IsActive = true);
public record BrandResponse(Guid Id, string Name, bool IsActive, DateTime CreatedAt);
+7 -4
View File
@@ -5,15 +5,16 @@ namespace Distribution.Features.Brand;
public class BrandService(IBrandRepository repo, IUnitOfWork uow)
{
public async Task<List<BrandResponse>> GetAllAsync() =>
// Deleted brands stay IsActive=false but remain visible showing Inactive — delete
// never removes the row, only marks it (same rule as Items/Stores/Categories).
(await repo.GetAllAsync())
.Where(b => b.IsActive)
.Select(b => new BrandResponse(b.Id, b.Name, b.IsActive, b.CreatedAt))
.ToList();
public async Task<BrandResponse?> GetByIdAsync(Guid id)
{
var brand = await repo.GetByIdAsync(id);
if (brand is null || !brand.IsActive) return null;
if (brand is null) return null;
return new BrandResponse(brand.Id, brand.Name, brand.IsActive, brand.CreatedAt);
}
@@ -29,9 +30,10 @@ public class BrandService(IBrandRepository repo, IUnitOfWork uow)
public async Task<BrandResponse?> UpdateAsync(Guid id, UpdateBrandRequest request, CancellationToken ct = default)
{
var brand = await repo.GetByIdAsync(id);
if (brand is null || !brand.IsActive) return null;
if (brand is null) return null;
if (brand.ExternalErpId.HasValue) return null;
brand.Name = request.Name;
brand.IsActive = request.IsActive;
await uow.BeginAsync(ct);
try { repo.Update(brand); await uow.CommitAsync(ct); }
catch { await uow.RollbackAsync(ct); throw; }
@@ -40,10 +42,11 @@ public class BrandService(IBrandRepository repo, IUnitOfWork uow)
public async Task<bool> DeleteAsync(Guid id, CancellationToken ct = default)
{
// Soft-delete only — never blocked by items still referencing this brand, same rule
// as Categories.
var brand = await repo.GetByIdAsync(id);
if (brand is null || !brand.IsActive) return false;
if (brand.ExternalErpId.HasValue) return false;
if (await repo.HasActiveItemsAsync(id)) return false;
await uow.BeginAsync(ct);
try { repo.Remove(brand); await uow.CommitAsync(ct); }
catch { await uow.RollbackAsync(ct); throw; }