Пример #1
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);
            }
Пример #2
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
                });
            }
            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 <Unit> Handle(Command command, CancellationToken cancellationToken)
            {
                var availableBudgets = await BudgetCategoryRepository.ListAllAsync();

                int?budgetId = null;

                for (var index = 0; index < command.BudgetCategoryOrder.Count; index++)
                {
                    var budgetCategoryId     = command.BudgetCategoryOrder[index].BudgetCategoryId;
                    var budgetCategoryEntity = availableBudgets.FirstOrDefault(x => x.Id == budgetCategoryId);
                    if (budgetCategoryEntity == null)
                    {
                        throw new NotFoundException("Budget category was not found");
                    }

                    budgetId = budgetCategoryEntity.BudgetId;
                    budgetCategoryEntity.Order = index;
                    await BudgetCategoryRepository.UpdateAsync(budgetCategoryEntity);
                }

                await BudgetCategoryRepository.SaveChangesAsync(cancellationToken);

                if (budgetId != null)
                {
                    _ = _mediator.Publish(new Notification()
                    {
                        BudgetId = budgetId.Value
                    },
                                          cancellationToken);
                }

                return(Unit.Value);
            }
Пример #5
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
                });
            }
Пример #6
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);
            }
Пример #7
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));
            }
Пример #8
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));
            }
Пример #9
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));
            }
Пример #10
0
            public override async Task <BudgetCategoryDto> Handle(Command command, CancellationToken cancellationToken)
            {
                var availableBudgets = await _budgetRepository.ListAvailableBudgets();

                if (availableBudgets.All(s => s.Id != command.BudgetId))
                {
                    throw new NotFoundException("Specified budget does not exist");
                }

                var maxOrder = (await BudgetCategoryRepository.ListWithFilter(new Domain.Entities.Budget(command.BudgetId), new BudgetCategoryFilterModel())).Max(x => x.Order);

                var budgetCategoryEntity = Mapper.Map <BudgetCategory>(command);

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

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

                budgetCategoryEntity.BudgetCategoryBudgetedAmounts = amountConfigs;

                var savedBudgetCategory = await BudgetCategoryRepository.AddAsync(budgetCategoryEntity);

                var addedRows = await BudgetCategoryRepository.SaveChangesAsync(cancellationToken);

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

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

                _ = _mediator.Publish(new Notification()
                {
                    BudgetId       = budgetCategoryEntity.BudgetId,
                    BudgetCategory = dto
                }, cancellationToken);
                return(dto);
            }
            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));
            }
Пример #12
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);
            }
Пример #13
0
            public override async Task <IEnumerable <BudgetCategoryDto> > Handle(Query query, CancellationToken cancellationToken)
            {
                var availableBudgets = await _budgetRepository.ListAvailableBudgets();

                if (availableBudgets.All(x => x.Id == query.BudgetId))
                {
                    throw new NotFoundException("Target budget was not found");
                }

                var categories = await BudgetCategoryRepository.ListWithFilter(new Domain.Entities.Budget(query.BudgetId), new BudgetCategoryFilterModel()
                {
                    OrderBy   = x => x.Order,
                    DataOrder = eDataOrder.Ascending
                });

                return(Mapper.Map <IEnumerable <BudgetCategoryDto> >(categories));
            }
Пример #14
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));
            }
Пример #15
0
        public void Initialize(string filename)
        {
            IDatabaseFactory databaseFactory = new DatabaseFactory();
            IUnitOfWork unitOfWork = new UnitOfWork(databaseFactory);
            IAccountRepository accountRepository = new AccountRepository(databaseFactory);
            ITransactionRepository transactionRepository = new TransactionRepository(databaseFactory);
            ICategoryRepository categoryRepository = new CategoryRepository(databaseFactory);
            IVendorRepository vendorRepository = new VendorRepository(databaseFactory);
            ICategoryGroupRepository categoryGroupRepository = new CategoryGroupRepository(databaseFactory);
            IBillRepository billRepository = new BillRepository(databaseFactory);
            IBillTransactionRepository billTransactionRepository = new BillTransactionRepository(databaseFactory);
            IBillGroupRepository billGroupRepository = new BillGroupRepository(databaseFactory);
            IBudgetCategoryRepository budgetCategoryRepository = new BudgetCategoryRepository(databaseFactory);
            IAccountGroupRepository accountGroupRepository = new AccountGroupRepository(databaseFactory);
            IImportDescriptionVendorMapRepository importDescriptionVendorMapRepository = new ImportDescriptionVendorMapRepository(databaseFactory);
            IAccountService accountService = new AccountService(unitOfWork, accountRepository, transactionRepository, categoryRepository, vendorRepository, billRepository, billTransactionRepository, billGroupRepository, categoryGroupRepository, budgetCategoryRepository, importDescriptionVendorMapRepository);
            TransactionImporter importer = new TransactionImporter(unitOfWork, accountService, accountRepository, transactionRepository, vendorRepository, categoryGroupRepository, accountGroupRepository, importDescriptionVendorMapRepository);

            importer.Import(filename);
        }
Пример #16
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());
            }
Пример #17
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());
            }
Пример #18
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));
            }