public async Task <IOperationResult> HandleAsync(IUpdateAssignmentToUser command, ICorrelationContext context)
        {
            var assignment = await _repository.GetAsync(command.Id);

            if (assignment is null)
            {
                throw new BaristaException("assignment_not_found", $"Assignment with ID '{command.Id}' was not found");
            }
            if (!(assignment is Domain.AssignmentToUser assignmentToUser))
            {
                throw new BaristaException("invalid_assignment_update_command", "This assignment does not assign means to an user and as such cannot be updated with this command.");
            }

            await Task.WhenAll(
                _meansVerifier.AssertExists(command.MeansId),
                _userVerifier.AssertExists(command.UserId)
                );

            assignment.SetMeansId(command.MeansId);
            assignment.SetValidity(command.ValidSince, command.ValidUntil);
            assignmentToUser.SetIsShared(command.IsShared);
            assignmentToUser.SetUserId(command.UserId);

            await _repository.UpdateAsync(assignmentToUser);

            await _repository.SaveChanges();

            try
            {
                await _exclVerifier.VerifyAssignmentExclusivity(command.MeansId);
            }
            catch (BaristaException)
            {
                await _repository.DeleteAsync(assignment);

                await _repository.SaveChanges();

                throw;
            }

            await _busPublisher.Publish(new AssignmentToUserUpdated(assignmentToUser.Id, assignmentToUser.MeansId,
                                                                    assignmentToUser.ValidSince, assignmentToUser.ValidUntil, assignmentToUser.UserId,
                                                                    assignmentToUser.IsShared));

            return(OperationResult.Ok());
        }
Пример #2
0
        public async Task <IIdentifierResult> HandleAsync(ICreateAssignmentToUser command, ICorrelationContext context)
        {
            await Task.WhenAll(
                _meansVerifier.AssertExists(command.MeansId),
                _userVerifier.AssertExists(command.UserId)
                );

            var assignment = new Domain.AssignmentToUser(command.Id, command.MeansId, command.ValidSince, command.ValidUntil, command.UserId, command.IsShared);
            await _repository.AddAsync(assignment);

            try
            {
                await _repository.SaveChanges();
            }
            catch (EntityAlreadyExistsException)
            {
                throw new BaristaException("assignment_to_user_already_exists", $"An assignment with the ID '{command.Id}' already exists.");
            }

            try
            {
                await _exclVerifier.VerifyAssignmentExclusivity(command.MeansId);
            }
            catch (BaristaException)
            {
                await _repository.DeleteAsync(assignment);

                await _repository.SaveChanges();

                throw;
            }

            await _busPublisher.Publish(new AssignmentToUserCreated(assignment.Id, assignment.MeansId, assignment.ValidSince, assignment.ValidUntil, assignment.UserId, assignment.IsShared));

            return(new IdentifierResult(command.Id));
        }