2024-05-27 00:35:53 +03:30
|
|
|
|
using Brizco.Domain.Dtos.LargeDtos;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Brizco.Repository.Handlers.Activity;
|
2023-09-18 09:45:02 +03:30
|
|
|
|
|
|
|
|
|
|
public class CreateActivityCommandHandler : IRequestHandler<CreateActivityCommand, ActivityLDto>
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IRepositoryWrapper _repositoryWrapper;
|
2023-09-18 15:41:41 +03:30
|
|
|
|
private readonly ICurrentUserService _currentUserService;
|
|
|
|
|
|
|
2023-12-13 12:05:04 +03:30
|
|
|
|
public CreateActivityCommandHandler(IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService)
|
2023-09-18 09:45:02 +03:30
|
|
|
|
{
|
|
|
|
|
|
_repositoryWrapper = repositoryWrapper;
|
2023-09-18 15:41:41 +03:30
|
|
|
|
_currentUserService = currentUserService;
|
2023-09-18 09:45:02 +03:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<ActivityLDto> Handle(CreateActivityCommand request, CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
2023-09-18 15:41:41 +03:30
|
|
|
|
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);
|
2023-09-20 11:54:12 +03:30
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
await _repositoryWrapper.BeginTransaction(cancellationToken);
|
2023-11-26 16:56:38 +03:30
|
|
|
|
var activity = Domain.Entities.Task.Activity
|
2023-09-20 11:54:12 +03:30
|
|
|
|
.Create(
|
2023-11-26 16:56:38 +03:30
|
|
|
|
ActivityStatus.Created,
|
2023-12-13 12:05:04 +03:30
|
|
|
|
DateTime.MinValue,
|
2023-11-26 16:56:38 +03:30
|
|
|
|
string.Empty,
|
2023-09-20 11:54:12 +03:30
|
|
|
|
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-12-13 12:05:04 +03:30
|
|
|
|
activity.SetUser(request.UserId);
|
2023-09-18 15:41:41 +03:30
|
|
|
|
|
2024-05-20 22:51:01 +03:30
|
|
|
|
activity.SetShiftPlan(request.ShiftPlanId);
|
2023-09-18 09:45:02 +03:30
|
|
|
|
|
2023-11-26 16:56:38 +03:30
|
|
|
|
_repositoryWrapper.SetRepository<Domain.Entities.Task.Activity>().Add(activity);
|
2023-09-20 11:54:12 +03:30
|
|
|
|
await _repositoryWrapper.SaveChangesAsync(cancellationToken);
|
|
|
|
|
|
await _repositoryWrapper.CommitAsync(cancellationToken);
|
2023-11-26 16:56:38 +03:30
|
|
|
|
return activity.AdaptToLDto();
|
2023-09-20 11:54:12 +03:30
|
|
|
|
}
|
|
|
|
|
|
catch (Exception)
|
2023-09-18 09:45:02 +03:30
|
|
|
|
{
|
2023-09-20 11:54:12 +03:30
|
|
|
|
await _repositoryWrapper.RollBackAsync(cancellationToken);
|
|
|
|
|
|
throw;
|
2023-09-18 09:45:02 +03:30
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|