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

31 lines
1.3 KiB
C#
Raw Permalink Normal View History

namespace Brizco.Repository.Handlers.Routines;
2023-11-14 16:21:49 +03:30
2024-08-09 21:42:04 +03:30
public class UpdateRoutineCommandHandler(IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService)
: IRequestHandler<UpdateRoutineCommand, bool>
2023-11-14 16:21:49 +03:30
{
public async Task<bool> Handle(UpdateRoutineCommand request, CancellationToken cancellationToken)
{
2024-08-09 21:42:04 +03:30
var shift = await repositoryWrapper.SetRepository<Domain.Entities.Routines.Routine>()
2023-11-14 16:21:49 +03:30
.TableNoTracking.FirstOrDefaultAsync(s => s.Id == request.Id, cancellationToken);
if (shift == null)
throw new AppException("Routine not found", ApiResultStatusCode.NotFound);
2024-08-09 21:42:04 +03:30
if (currentUserService.ComplexId == null)
2023-11-14 16:21:49 +03:30
throw new AppException("ComplexId is null", ApiResultStatusCode.NotFound);
2024-08-09 21:42:04 +03:30
if (!Guid.TryParse(currentUserService.ComplexId, out Guid complexId))
2023-11-14 16:21:49 +03:30
throw new AppException("ComplexId is wrong", ApiResultStatusCode.NotFound);
2024-07-06 21:20:10 +03:30
var newEntity = Domain.Entities.Routines.Routine.Create(request.Name,
2023-11-14 16:21:49 +03:30
request.Description,
complexId);
newEntity.Id = request.Id;
2024-08-09 21:42:04 +03:30
repositoryWrapper.SetRepository<Domain.Entities.Routines.Routine>()
2023-11-14 16:21:49 +03:30
.Update(newEntity);
2024-08-09 21:42:04 +03:30
await repositoryWrapper.SaveChangesAsync(cancellationToken);
2023-11-14 16:21:49 +03:30
return true;
}
}