public async Task <Result> Handle(Command request, CancellationToken cancellationToken)
            {
                var subTransaction = await _writeDbContext.Transactions
                                     .Include(x => x.SubTransactions)
                                     .SelectMany(x => x.SubTransactions)
                                     .FirstOrDefaultAsync(x => x.SubTransactionId == request.SubTransactionId, cancellationToken: cancellationToken)
                                     ?? throw new NotFoundException(Localization.For(() => ErrorMessages.TransactionNotFound));

                if (!await _accessControlService.HasTransactionAccess(subTransaction.ParentTransactionId))
                {
                    throw new NotFoundException(Localization.For(() => ErrorMessages.TransactionNotFound));
                }

                subTransaction.SetDescription(request.Description);

                await _writeDbContext.SaveChangesAsync(cancellationToken);

                var transaction = _writeDbContext.Transactions.FirstOrDefault(x => x.TransactionId == subTransaction.ParentTransactionId);

                _ = _mediator.Publish(new Notification()
                {
                    Transaction    = transaction,
                    SubTransaction = subTransaction
                }, cancellationToken);
                return(new Result()
                {
                    Data = subTransaction.Description
                });
            }
            public async Task <Result> Handle(Command request, CancellationToken cancellationToken)
            {
                if (!await _accessControlService.HasTransactionAccess(request.TransactionId))
                {
                    throw new NotFoundException(Localization.For(() => ErrorMessages.TransactionNotFound));
                }
                var transaction = await _writeDbContext.Transactions
                                  .FirstOrDefaultAsync(x => x.TransactionId == request.TransactionId, cancellationToken : cancellationToken)
                                  ?? throw new NotFoundException(Localization.For(() => ErrorMessages.TransactionNotFound));

                var oldDate = transaction.TransactionDate;

                transaction.SetTransactionDate(request.TransactionDate);

                await _writeDbContext.SaveChangesAsync(cancellationToken);

                _ = _mediator.Publish(new Notification()
                {
                    OldTransactionDate = oldDate,
                    NewTransactionDate = transaction.TransactionDate,
                    Transaction        = transaction
                }, cancellationToken);

                return(new Result()
                {
                    Data = transaction.TransactionDate
                });
            }
Esempio n. 3
0
            public async Task <Result> Handle(Command request, CancellationToken cancellationToken)
            {
                if (!await _accessControlService.HasTransactionAccess(request.TransactionId))
                {
                    throw new NotFoundException(Localization.For(() => ErrorMessages.TransactionNotFound));
                }

                if (!await _accessControlService.HasBudgetCategoryAccessAsync(request.BudgetCategoryId))
                {
                    throw new NotFoundException(Localization.For(() => ErrorMessages.BudgetCategoryNotFound));
                }

                var transaction = await _writeDbContext.Transactions
                                  .FirstAsync(x => x.TransactionId == request.TransactionId, cancellationToken : cancellationToken);

                var oldBudgetCategory = await _writeDbContext.BudgetCategories
                                        .FirstAsync(x => x.BudgetCategoryId == transaction.BudgetCategoryId, cancellationToken : cancellationToken);

                var newBudgetCategory = await _writeDbContext.BudgetCategories
                                        .FirstAsync(x => x.BudgetCategoryId == request.BudgetCategoryId, cancellationToken : cancellationToken);

                if (oldBudgetCategory.BudgetCategoryType != newBudgetCategory.BudgetCategoryType)
                {
                    throw new BusinessException(Localization.For(() => ErrorMessages.NotSameBudgetCategoryType));
                }

                transaction.SetBudgetCategory(newBudgetCategory);

                await _writeDbContext.SaveChangesAsync(cancellationToken);

                _ = _mediator.Publish(new Notification()
                {
                    NewBudgetCategoryId = newBudgetCategory.BudgetCategoryId,
                    OldBudgetCategoryId = oldBudgetCategory.BudgetCategoryId,
                    Transaction         = transaction
                }, cancellationToken);

                return(new Result()
                {
                    Data = transaction.BudgetCategoryId
                });
            }