Api/Brizco.Repository/Handlers/Routines/UpdateRoutineCommandHandler.cs

31 lines
1.3 KiB
C#

namespace Brizco.Repository.Handlers.Routines;
public class UpdateRoutineCommandHandler(IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService)
: IRequestHandler<UpdateRoutineCommand, bool>
{
public async Task<bool> Handle(UpdateRoutineCommand request, CancellationToken cancellationToken)
{
var shift = await repositoryWrapper.SetRepository<Domain.Entities.Routines.Routine>()
.TableNoTracking.FirstOrDefaultAsync(s => s.Id == request.Id, cancellationToken);
if (shift == null)
throw new AppException("Routine not found", ApiResultStatusCode.NotFound);
if (currentUserService.ComplexId == null)
throw new AppException("ComplexId is null", ApiResultStatusCode.NotFound);
if (!Guid.TryParse(currentUserService.ComplexId, out Guid complexId))
throw new AppException("ComplexId is wrong", ApiResultStatusCode.NotFound);
var newEntity = Domain.Entities.Routines.Routine.Create(request.Name,
request.Description,
complexId);
newEntity.Id = request.Id;
repositoryWrapper.SetRepository<Domain.Entities.Routines.Routine>()
.Update(newEntity);
await repositoryWrapper.SaveChangesAsync(cancellationToken);
return true;
}
}