UpdateBrandRequest to include IsActive flag and update BrandService to handle inactive brands correctly
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
namespace Distribution.Features.Brand;
|
namespace Distribution.Features.Brand;
|
||||||
|
|
||||||
public record CreateBrandRequest(string Name);
|
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);
|
public record BrandResponse(Guid Id, string Name, bool IsActive, DateTime CreatedAt);
|
||||||
|
|||||||
@@ -5,15 +5,16 @@ namespace Distribution.Features.Brand;
|
|||||||
public class BrandService(IBrandRepository repo, IUnitOfWork uow)
|
public class BrandService(IBrandRepository repo, IUnitOfWork uow)
|
||||||
{
|
{
|
||||||
public async Task<List<BrandResponse>> GetAllAsync() =>
|
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())
|
(await repo.GetAllAsync())
|
||||||
.Where(b => b.IsActive)
|
|
||||||
.Select(b => new BrandResponse(b.Id, b.Name, b.IsActive, b.CreatedAt))
|
.Select(b => new BrandResponse(b.Id, b.Name, b.IsActive, b.CreatedAt))
|
||||||
.ToList();
|
.ToList();
|
||||||
|
|
||||||
public async Task<BrandResponse?> GetByIdAsync(Guid id)
|
public async Task<BrandResponse?> GetByIdAsync(Guid id)
|
||||||
{
|
{
|
||||||
var brand = await repo.GetByIdAsync(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);
|
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)
|
public async Task<BrandResponse?> UpdateAsync(Guid id, UpdateBrandRequest request, CancellationToken ct = default)
|
||||||
{
|
{
|
||||||
var brand = await repo.GetByIdAsync(id);
|
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;
|
if (brand.ExternalErpId.HasValue) return null;
|
||||||
brand.Name = request.Name;
|
brand.Name = request.Name;
|
||||||
|
brand.IsActive = request.IsActive;
|
||||||
await uow.BeginAsync(ct);
|
await uow.BeginAsync(ct);
|
||||||
try { repo.Update(brand); await uow.CommitAsync(ct); }
|
try { repo.Update(brand); await uow.CommitAsync(ct); }
|
||||||
catch { await uow.RollbackAsync(ct); throw; }
|
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)
|
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);
|
var brand = await repo.GetByIdAsync(id);
|
||||||
if (brand is null || !brand.IsActive) return false;
|
if (brand is null || !brand.IsActive) return false;
|
||||||
if (brand.ExternalErpId.HasValue) return false;
|
if (brand.ExternalErpId.HasValue) return false;
|
||||||
if (await repo.HasActiveItemsAsync(id)) return false;
|
|
||||||
await uow.BeginAsync(ct);
|
await uow.BeginAsync(ct);
|
||||||
try { repo.Remove(brand); await uow.CommitAsync(ct); }
|
try { repo.Remove(brand); await uow.CommitAsync(ct); }
|
||||||
catch { await uow.RollbackAsync(ct); throw; }
|
catch { await uow.RollbackAsync(ct); throw; }
|
||||||
|
|||||||
Reference in New Issue
Block a user