public override async Task <TransactionScheduleDto> Handle(Command request, CancellationToken cancellationToken)
            {
                var transactionSchedule = await TransactionScheduleRepository.GetByIdAsync(request.TransactionScheduleId);

                if (transactionSchedule == null)
                {
                    throw new NotFoundException("Transaction schedule was not found.");
                }
                var sourceCategoryAccessible = BudgetCategoryRepository.IsAccessibleToUser(transactionSchedule.Id);

                if (!await sourceCategoryAccessible)
                {
                    throw new NotFoundException("Source budget category was not found.");
                }

                transactionSchedule.Description      = request.Description;
                transactionSchedule.StartDate        = request.StartDate;
                transactionSchedule.Frequency        = request.Frequency;
                transactionSchedule.PeriodStep       = request.PeriodStep;
                transactionSchedule.EndDate          = request.EndDate;
                transactionSchedule.BudgetCategoryId = request.BudgetCategoryId;

                await TransactionScheduleRepository.UpdateAsync(transactionSchedule);

                await TransactionScheduleRepository.SaveChangesAsync(cancellationToken);

                return(Mapper.Map <TransactionScheduleDto>(transactionSchedule));
            }
            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);
            }
Example #3
0
            public override async Task <AllocationDto> Handle(Query request, CancellationToken cancellationToken)
            {
                var allocationEntity = await AllocationRepository.GetByIdAsync(request.AllocationId);

                if (allocationEntity.IsNullOrDefault() || !await BudgetCategoryRepository.IsAccessibleToUser(allocationEntity.Id))
                {
                    throw new NotFoundException("Target allocation was not found.");
                }

                return(Mapper.Map <AllocationDto>(allocationEntity));
            }
Example #4
0
            public override async Task <TransactionScheduleDetailsDto> Handle(Query request, CancellationToken cancellationToken)
            {
                var transactionScheduleEntity = await TransactionScheduleRepository.GetByIdAsync(request.TransactionScheduleId);

                if (transactionScheduleEntity.IsNullOrDefault() || !await BudgetCategoryRepository.IsAccessibleToUser(transactionScheduleEntity.Id))
                {
                    throw new NotFoundException("Target Transaction Schedule was not found.");
                }

                return(Mapper.Map <TransactionScheduleDetailsDto>(transactionScheduleEntity));
            }
Example #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));
            }
            public override async Task <TransactionScheduleDto> Handle(Command request, CancellationToken cancellationToken)
            {
                var transactionScheduleEntity = await TransactionScheduleRepository.GetByIdAsync(request.TransactionScheduleId);

                if (transactionScheduleEntity.IsNullOrDefault() || !await BudgetCategoryRepository.IsAccessibleToUser(transactionScheduleEntity.BudgetCategoryId))
                {
                    throw new NotFoundException("Target transaction schedule was not found.");
                }

                await TransactionScheduleRepository.DeleteAsync(transactionScheduleEntity);

                await TransactionScheduleRepository.SaveChangesAsync(cancellationToken);

                return(Mapper.Map <TransactionScheduleDto>(transactionScheduleEntity));
            }
Example #7
0
            public override async Task <AllocationDto> Handle(Command request, CancellationToken cancellationToken)
            {
                var allocation = await AllocationRepository.GetByIdAsync(request.AllocationId);

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

                var originalTargetCategoryAccessible = await BudgetCategoryRepository.IsAccessibleToUser(allocation.TargetBudgetCategoryId);

                var targetCategoryAccessible = await BudgetCategoryRepository.IsAccessibleToUser(request.TargetBudgetCategoryId);

                if (!targetCategoryAccessible || !originalTargetCategoryAccessible)
                {
                    throw new NotFoundException("Target budget category was not found.");
                }

                if (request.SourceBudgetCategoryId != null)
                {
                    var sourceCategoryAccessible = BudgetCategoryRepository.IsAccessibleToUser(request.SourceBudgetCategoryId.Value);
                    if (!await sourceCategoryAccessible)
                    {
                        throw new NotFoundException("Source budget category was not found.");
                    }
                }

                allocation.Description            = request.Description;
                allocation.AllocationDateTime     = request.AllocationDate;
                allocation.TargetBudgetCategoryId = request.TargetBudgetCategoryId;
                allocation.SourceBudgetCategoryId = request.SourceBudgetCategoryId;
                allocation.Amount = request.Amount;

                await AllocationRepository.UpdateAsync(allocation);

                await AllocationRepository.SaveChangesAsync(cancellationToken);

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

                _ = _mediator.Publish(new Notification()
                {
                    BudgetId   = allocation.TargetBudgetCategory.BudgetId,
                    Allocation = dto
                }, cancellationToken);
                return(dto);
            }
Example #8
0
            public override async Task <AllocationDto> Handle(Command request, CancellationToken cancellationToken)
            {
                var allocationEntity = await AllocationRepository.GetByIdAsync(request.AllocationId);

                if (allocationEntity.IsNullOrDefault() || !await BudgetCategoryRepository.IsAccessibleToUser(allocationEntity.TargetBudgetCategoryId))
                {
                    throw new NotFoundException("Target allocation was not found.");
                }

                await AllocationRepository.DeleteAsync(allocationEntity);

                await AllocationRepository.SaveChangesAsync(cancellationToken);

                _ = _mediator.Publish(new Notification()
                {
                    BudgetId = allocationEntity.TargetBudgetCategory.BudgetId,
                }, cancellationToken);
                return(Mapper.Map <AllocationDto>(allocationEntity));
            }
Example #9
0
            public override async Task <Response> Handle(Request request, CancellationToken cancellationToken)
            {
                var transactionEntity = await TransactionRepository.GetByIdAsync(request.TransactionId);

                if (transactionEntity.IsNullOrDefault() || !await BudgetCategoryRepository.IsAccessibleToUser(transactionEntity.BudgetCategoryId))
                {
                    throw new NotFoundException("Target transaction was not found.");
                }

                await TransactionRepository.DeleteAsync(transactionEntity);

                await TransactionRepository.SaveChangesAsync(cancellationToken);

                _ = _mediator.Publish(new Notification()
                {
                    BudgetId      = transactionEntity.BudgetCategory.BudgetId,
                    TransactionId = transactionEntity.Id
                }, cancellationToken);

                return(new Response());
            }
            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());
            }
Example #11
0
            public override async Task <TransactionScheduleDto> Handle(Command request, CancellationToken cancellationToken)
            {
                if (!await BudgetCategoryRepository.IsAccessibleToUser(request.BudgetCategoryId))
                {
                    throw new NotFoundException("Target budget category was not found.");
                }

                var transactionScheduleEntity = Mapper.Map <Domain.Entities.TransactionSchedule>(request);

                transactionScheduleEntity.CreatedByUserId = AuthenticationProvider.User.UserId;

                var savedTransactionSchedule = await TransactionScheduleRepository.AddAsync(transactionScheduleEntity);

                var addedRows = await TransactionScheduleRepository.SaveChangesAsync(cancellationToken);

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

                return(Mapper.Map <TransactionScheduleDto>(savedTransactionSchedule));
            }