53 lines
2.1 KiB
C#
53 lines
2.1 KiB
C#
namespace Brizco.Repository.Handlers.Activity;
|
|
|
|
public class CreateActivityCommandHandler : IRequestHandler<CreateActivityCommand, ActivityLDto>
|
|
{
|
|
private readonly IRepositoryWrapper _repositoryWrapper;
|
|
private readonly ICurrentUserService _currentUserService;
|
|
|
|
public CreateActivityCommandHandler(IRepositoryWrapper repositoryWrapper, ICurrentUserService currentUserService)
|
|
{
|
|
_repositoryWrapper = repositoryWrapper;
|
|
_currentUserService = currentUserService;
|
|
}
|
|
|
|
public async Task<ActivityLDto> Handle(CreateActivityCommand request, CancellationToken cancellationToken)
|
|
{
|
|
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);
|
|
try
|
|
{
|
|
await _repositoryWrapper.BeginTransaction(cancellationToken);
|
|
var activity = Domain.Entities.Task.Activity
|
|
.Create(
|
|
ActivityStatus.Created,
|
|
DateTime.MinValue,
|
|
string.Empty,
|
|
request.Title,
|
|
request.Description,
|
|
request.Type,
|
|
request.IsDisposable,
|
|
request.SetFor,
|
|
request.HasDisposed,
|
|
request.Amount,
|
|
request.AmountType,
|
|
complexId,
|
|
request.ScheduleType);
|
|
activity.SetUser(request.UserId);
|
|
|
|
activity.SetShift(request.ShiftId);
|
|
|
|
_repositoryWrapper.SetRepository<Domain.Entities.Task.Activity>().Add(activity);
|
|
await _repositoryWrapper.SaveChangesAsync(cancellationToken);
|
|
await _repositoryWrapper.CommitAsync(cancellationToken);
|
|
return activity.AdaptToLDto();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
await _repositoryWrapper.RollBackAsync(cancellationToken);
|
|
throw;
|
|
}
|
|
}
|
|
} |