예제 #1
0
        public async Task <Guid> Handle(CreateMovementCommand command,
                                        CancellationToken cancellationToken)
        {
            if (await _userRepository.Get(command.UserId) is var user && user is null)
            {
                throw new Exception("Invalid user");
            }

            if (await _accountRepository.Get(command.AccountId) is var account && account is null)
            {
                throw new Exception("Invalid account");
            }

            var movement = new Movement(command.Value, command.Description, (EMovementCategory)command.Category,
                                        (EMovementType)command.Type, command.AccountId, command.UserId);

            var movementValidator = new MovementValidator();
            var result            = await movementValidator.ValidateAsync(movement, default);

            if (!result.IsValid)
            {
                throw new Exception(String.Join("--", result.Errors));
            }

            var balance = await _transactionService.CreateOrUpdateBalance(movement);

            return(balance.Id);
        }
예제 #2
0
        public async Task <Guid> Handle(EditMovementCommand command, CancellationToken cancellationToken)
        {
            var movement = await _expenseRepository.Get(command.MovementId);

            movement.SetCategory((EMovementCategory)command.Category);
            movement.SetValue(command.Value);
            movement.SetDescription(command.Description);
            movement.SetMovementType((EMovementType)command.Type);

            var movementValidatorValidator = new MovementValidator();
            var result = await movementValidatorValidator.ValidateAsync(movement, default);

            if (!result.IsValid)
            {
                throw new Exception(String.Join("--", result.Errors));
            }

            await _expenseRepository.Update(movement.Id, movement);

            return(movement.Id);
        }