2024-07-08 17:45:17 +03:30
|
|
|
|
namespace Brizco.Repository.Handlers.Activities;
|
2023-09-18 09:45:02 +03:30
|
|
|
|
|
2024-08-09 21:42:04 +03:30
|
|
|
|
public class UpdateActivityCommandHandler(IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService)
|
|
|
|
|
|
: IRequestHandler<UpdateActivityCommand, bool>
|
2023-09-18 09:45:02 +03:30
|
|
|
|
{
|
|
|
|
|
|
public async Task<bool> Handle(UpdateActivityCommand request, CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
2024-08-09 21:42:04 +03:30
|
|
|
|
var task = await repositoryWrapper.SetRepository<Domain.Entities.Tasks.Activity>()
|
2023-09-18 09:45:02 +03:30
|
|
|
|
.TableNoTracking.FirstOrDefaultAsync(s => s.Id == request.Id,cancellationToken);
|
|
|
|
|
|
if (task == null)
|
|
|
|
|
|
throw new AppException("Task not found", ApiResultStatusCode.NotFound);
|
2024-08-09 21:42:04 +03:30
|
|
|
|
if (currentUserService.ComplexId == null)
|
2023-09-18 15:41:41 +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-09-18 15:41:41 +03:30
|
|
|
|
throw new AppException("ComplexId is wrong", ApiResultStatusCode.NotFound);
|
2023-09-18 09:45:02 +03:30
|
|
|
|
|
2023-09-20 11:54:12 +03:30
|
|
|
|
try
|
|
|
|
|
|
{
|
2024-08-09 21:42:04 +03:30
|
|
|
|
await repositoryWrapper.BeginTransaction(cancellationToken);
|
2024-07-06 21:20:10 +03:30
|
|
|
|
var newTask = Domain.Entities.Tasks.Activity.Create(
|
2023-09-20 11:54:12 +03:30
|
|
|
|
request.Status,
|
|
|
|
|
|
request.DoneAt,
|
|
|
|
|
|
request.PerformanceDescription,
|
|
|
|
|
|
request.Title,
|
|
|
|
|
|
request.Description,
|
|
|
|
|
|
request.Type,
|
|
|
|
|
|
request.IsDisposable,
|
|
|
|
|
|
request.SetFor,
|
|
|
|
|
|
request.HasDisposed,
|
|
|
|
|
|
request.Amount,
|
|
|
|
|
|
request.AmountType,
|
2023-11-18 22:23:23 +03:30
|
|
|
|
complexId,
|
|
|
|
|
|
request.ScheduleType);
|
2023-09-18 09:45:02 +03:30
|
|
|
|
|
2023-09-20 11:54:12 +03:30
|
|
|
|
newTask.Id = request.Id;
|
2024-08-14 15:15:44 +03:30
|
|
|
|
newTask.CreatedAt = task.CreatedAt;
|
|
|
|
|
|
newTask.CreatedBy = task.CreatedBy;
|
2023-09-18 09:45:02 +03:30
|
|
|
|
|
2023-12-13 12:05:04 +03:30
|
|
|
|
newTask.SetUser(request.UserId);
|
2024-05-20 22:51:01 +03:30
|
|
|
|
newTask.SetShiftPlan(request.ShiftPlanId);
|
2023-12-13 12:05:04 +03:30
|
|
|
|
|
2024-08-09 21:42:04 +03:30
|
|
|
|
repositoryWrapper.SetRepository<Domain.Entities.Tasks.Activity>()
|
2023-09-20 11:54:12 +03:30
|
|
|
|
.Update(newTask);
|
2024-08-09 21:42:04 +03:30
|
|
|
|
await repositoryWrapper.SaveChangesAsync(cancellationToken);
|
|
|
|
|
|
await repositoryWrapper.CommitAsync(cancellationToken);
|
2023-09-18 09:45:02 +03:30
|
|
|
|
|
2023-09-20 11:54:12 +03:30
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception )
|
|
|
|
|
|
{
|
2024-08-09 21:42:04 +03:30
|
|
|
|
await repositoryWrapper.RollBackAsync(cancellationToken);
|
2023-09-20 11:54:12 +03:30
|
|
|
|
throw;
|
|
|
|
|
|
}
|
2023-09-18 09:45:02 +03:30
|
|
|
|
}
|
|
|
|
|
|
}
|