Esempio n. 1
0
            public override async Task <Response> Handle(Command command, CancellationToken cancellationToken)
            {
                var budgetCategory = await BudgetCategoryRepository.GetByIdAsync(command.BudgetCategoryId);

                if (budgetCategory == null)
                {
                    throw new NotFoundException("Target budget category was not found.");
                }

                var transactionEntity = Mapper.Map <Domain.Entities.Transaction>(command);

                transactionEntity.CreatedByUserId = AuthenticationProvider.User.UserId;
                var savedTransaction = await TransactionRepository.AddAsync(transactionEntity);

                var addedRows = await TransactionRepository.SaveChangesAsync(cancellationToken);

                if (addedRows.IsNullOrDefault())
                {
                    throw new SaveFailureException(nameof(transactionEntity), transactionEntity);
                }

                var createdTransaction = Mapper.Map <TransactionDetailsDto>(savedTransaction);

                createdTransaction.Type = budgetCategory.Type;
                _ = _mediator.Publish(new Notification()
                {
                    BudgetId    = budgetCategory.BudgetId,
                    Transaction = createdTransaction
                }, cancellationToken);

                return(new Response {
                    Data = createdTransaction
                });
            }