Example #1
0
            public async Task <Response> Handle(Query request, CancellationToken cancellationToken)
            {
                if (!await _accessControlService.HasBudgetAccessAsync(request.BudgetId))
                {
                    throw new NotFoundException(Localization.For(() => ErrorMessages.BudgetNotFound));
                }

                var balance = await _balanceService.GetBudgetBalance(request.BudgetId, cancellationToken);

                return(new Response()
                {
                    Data = _mapper.Map <BudgetBalanceDto>(balance)
                });
            }
            public async Task <Result> Handle(Command request, CancellationToken cancellationToken)
            {
                if (!await _accessControlService.HasBudgetAccessAsync(request.BudgetId))
                {
                    throw new NotFoundException(Localization.For(() => ErrorMessages.BudgetNotFound));
                }

                var stored = await _writeDbContext.Budgets.FirstOrDefaultAsync(x => x.BudgetId == request.BudgetId &&
                                                                               x.OwnerUserId == _userContext.UserId, cancellationToken);

                stored.SetName(request.Name);
                await _writeDbContext.SaveChangesAsync(cancellationToken);

                return(new Result()
                {
                    Data = stored.Name
                });
            }
            public async Task <Result> Handle(Command request, CancellationToken cancellationToken)
            {
                var budgetId = request.BudgetId;

                if (!await _accessControlService.HasBudgetAccessAsync(budgetId))
                {
                    throw new NotFoundException(Localization.For(() => ErrorMessages.BudgetNotFound));
                }

                var budget = await _writeDbContext.Budgets
                             .FirstOrDefaultAsync(x => x.BudgetId == budgetId &&
                                                  x.OwnerUserId == _userContext.UserId, cancellationToken);

                var icon = await _writeDbContext.BudgetCategoryIcons
                           .FirstOrDefaultAsync(x => x.BudgetCategoryIconId == request.BudgetCategoryIconId, cancellationToken);

                var budgetCategory = Domain.Entities.BudgetCategory.Create(budget, request.Name, icon, request.BudgetCategoryType);

                foreach (var requestBudgetedAmount in request.BudgetedAmounts)
                {
                    budgetCategory.AddBudgetedAmount(requestBudgetedAmount.Amount, requestBudgetedAmount.ValidFrom);
                }
                _writeDbContext.BudgetCategories.Add(budgetCategory);

                await _writeDbContext.SaveChangesAsync(cancellationToken);

                _ = _mediator.Publish(new Notification()
                {
                    ReferenceBudgetCategory = budgetCategory,
                }, cancellationToken);

                return(new Result()
                {
                    Id = budgetCategory.BudgetCategoryId.Value
                });
            }