Api/NetinaShop.Repository/Handlers/Brands/UpdateBrandCommandHandler.cs

27 lines
1.1 KiB
C#

using NetinaShop.Domain.Entities.Brands;
namespace NetinaShop.Repository.Handlers.Brands;
public class UpdateBrandCommandHandler : IRequestHandler<UpdateBrandCommand,bool>
{
private readonly IRepositoryWrapper _repositoryWrapper;
public UpdateBrandCommandHandler(IRepositoryWrapper repositoryWrapper)
{
_repositoryWrapper = repositoryWrapper;
}
public async Task<bool> Handle(UpdateBrandCommand request, CancellationToken cancellationToken)
{
var ent = await _repositoryWrapper.SetRepository<Brand>().TableNoTracking
.FirstOrDefaultAsync(b => b.Id == request.Id, cancellationToken);
if (ent == null)
throw new AppException("Brand not found");
var newEnt = Brand.Create(request.Name, request.Description, request.HasSpecialPage, request.PageUrl);
newEnt.Id = ent.Id;
newEnt.CreatedAt = ent.CreatedAt;
newEnt.CreatedBy = ent.CreatedBy;
_repositoryWrapper.SetRepository<Brand>().Update(newEnt);
await _repositoryWrapper.SaveChangesAsync(cancellationToken);
return true;
}
}