43 lines
1.7 KiB
C#
43 lines
1.7 KiB
C#
namespace Brizco.Repository.Handlers.Routine;
|
|
|
|
public class UpdateRoutineCommandHandler : IRequestHandler<UpdateRoutineCommand, bool>
|
|
{
|
|
private readonly IRepositoryWrapper _repositoryWrapper;
|
|
private readonly ICurrentUserService _currentUserService;
|
|
|
|
public UpdateRoutineCommandHandler(IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService)
|
|
{
|
|
_repositoryWrapper = repositoryWrapper;
|
|
_currentUserService = currentUserService;
|
|
}
|
|
|
|
public async Task<bool> Handle(UpdateRoutineCommand request, CancellationToken cancellationToken)
|
|
{
|
|
var shift = await _repositoryWrapper.SetRepository<Domain.Entities.Routine.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.Routine.Routine.Create(request.Title,
|
|
request.Description,
|
|
complexId);
|
|
newEntity.Id = request.Id;
|
|
|
|
var users = await _repositoryWrapper.SetRepository<PositionUser>().TableNoTracking
|
|
.Where(pu => pu.PositionId == newEntity.Id)
|
|
.ToListAsync(cancellationToken);
|
|
|
|
|
|
_repositoryWrapper.SetRepository<Domain.Entities.Routine.Routine>()
|
|
.Update(newEntity);
|
|
|
|
await _repositoryWrapper.SaveChangesAsync(cancellationToken);
|
|
|
|
return true;
|
|
}
|
|
} |