Пример #1
0
            public override async Task <Response> Handle(Command command, CancellationToken cancellationToken)
            {
                var transaction = await TransactionRepository.GetByIdAsync(command.TransactionId);

                var budgetCategory = await BudgetCategoryRepository.GetByIdAsync(command.BudgetCategoryId);

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

                transaction.Description           = command.Description;
                transaction.Amount                = command.Amount;
                transaction.TransactionDateTime   = command.TransactionDate;
                transaction.BudgetCategoryId      = command.BudgetCategoryId;
                transaction.TransactionScheduleId = command.TransactionScheduleId;

                await TransactionRepository.UpdateAsync(transaction);

                await TransactionRepository.SaveChangesAsync(cancellationToken);

                var updated = Mapper.Map <TransactionDetailsDto>(transaction);

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

                return(new Response {
                    Data = updated
                });
            }
Пример #2
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
                });
            }
Пример #3
0
            public override async Task <AllocationDto> Handle(Command request, CancellationToken cancellationToken)
            {
                var category = await BudgetCategoryRepository.GetByIdAsync(request.TargetBudgetCategoryId);

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


                var allocationEntity = Mapper.Map <Domain.Entities.Allocation>(request);

                allocationEntity.CreatedByUserId  = AuthenticationProvider.User.UserId;
                allocationEntity.CreationDateTime = DateTime.Now;

                var savedAllocation = await AllocationRepository.AddAsync(allocationEntity);

                var addedRows = await AllocationRepository.SaveChangesAsync(cancellationToken);

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

                var dto = Mapper.Map <AllocationDto>(savedAllocation);

                _ = _mediator.Publish(new Notification()
                {
                    BudgetId   = category.BudgetId,
                    Allocation = dto
                }, cancellationToken);
                return(dto);
            }
Пример #4
0
            public override async Task <BudgetCategoryDto> Handle(Command command, CancellationToken cancellationToken)
            {
                var isAccessible = await BudgetCategoryRepository.IsAccessibleToUser(command.BudgetCategoryId);

                var budgetCategoryEntity = await BudgetCategoryRepository.GetByIdAsync(command.BudgetCategoryId);

                budgetCategoryEntity.Name = command.Name;
                budgetCategoryEntity.Icon = command.Icon;

                for (int i = 0; i < command.AmountConfigs.Count - 1; i++)
                {
                    command.AmountConfigs[i].ValidTo = command.AmountConfigs[i + 1]
                                                       .ValidFrom
                                                       .AddDays(-1)
                                                       .FirstDayOfMonth();
                    command.AmountConfigs[i + 1].ValidTo = null;
                }

                var amountConfigs = command.AmountConfigs
                                    .Select(x => new BudgetCategoryBudgetedAmount()
                {
                    BudgetCategoryId = budgetCategoryEntity.Id,
                    MonthlyAmount    = x.MonthlyAmount,
                    ValidFrom        = x.ValidFrom,
                    ValidTo          = x.ValidTo
                })
                                    .ToList();

                budgetCategoryEntity.BudgetCategoryBudgetedAmounts = amountConfigs;

                if (!(isAccessible))
                {
                    throw new NotFoundException("Budget category was not found");
                }

                await BudgetCategoryRepository.UpdateAsync(budgetCategoryEntity);

                var addedRows = await BudgetCategoryRepository.SaveChangesAsync(cancellationToken);

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

                var dto = Mapper.Map <BudgetCategoryDto>(budgetCategoryEntity);

                _ = _mediator.Publish(new Notification()
                {
                    BudgetId       = budgetCategoryEntity.BudgetId,
                    BudgetCategory = dto
                }, cancellationToken);

                return(dto);
            }
Пример #5
0
            public override async Task <BudgetCategoryDto> Handle(Query query, CancellationToken cancellationToken)
            {
                var isAccessible = await BudgetCategoryRepository.IsAccessibleToUser(query.BudgetCategoryId);

                if (!isAccessible)
                {
                    throw new NotFoundException("Specified budget does not exist");
                }

                var budgetCategory = await BudgetCategoryRepository.GetByIdAsync(query.BudgetCategoryId);

                return(Mapper.Map <BudgetCategoryDto>(budgetCategory));
            }
Пример #6
0
            public override async Task <Unit> Handle(Command command, CancellationToken cancellationToken)
            {
                var isAccessible = await BudgetCategoryRepository.IsAccessibleToUser(command.BudgetCategoryId);

                if (!isAccessible)
                {
                    throw new NotFoundException("Specified budget category does not exist");
                }

                var budgetCategoryToDelete = await BudgetCategoryRepository.GetByIdAsync(command.BudgetCategoryId);

                await BudgetCategoryRepository.DeleteAsync(budgetCategoryToDelete);

                await BudgetCategoryRepository.SaveChangesAsync(cancellationToken);

                _ = _mediator.Publish(new Notification()
                {
                    BudgetId = budgetCategoryToDelete.BudgetId
                }, cancellationToken);
                return(new Unit());
            }