public async Task <Result> Handle(Command request, CancellationToken cancellationToken) { if (!await _accessControlService.HasBudgetCategoryAccessAsync(request.BudgetCategoryId)) { throw new NotFoundException(Localization.For(() => ErrorMessages.BudgetCategoryNotFound)); } var budget = await _writeDbContext.Budgets .Include(x => x.BudgetCategories) .FirstAsync(x => x.BudgetCategories.Any(s => s.BudgetCategoryId == request.BudgetCategoryId), cancellationToken); var budgetCategory = budget.BudgetCategories.First(x => x.BudgetCategoryId == request.BudgetCategoryId); budget.BudgetCategories.MoveUp(budgetCategory); await _writeDbContext.SaveChangesAsync(cancellationToken); _ = _mediator.Publish(new Notification() { ReferenceBudgetCategory = budgetCategory, }, cancellationToken); return(new Result() { }); }
public async Task <Result> Handle(Command request, CancellationToken cancellationToken) { var transaction = await _writeDbContext.Transactions .Include(x => x.SubTransactions) .FirstOrDefaultAsync(x => x.SubTransactions.Any(s => s.SubTransactionId == request.SubTransactionId), cancellationToken: cancellationToken) ?? throw new NotFoundException(Localization.For(() => ErrorMessages.TransactionNotFound)); if (!await _accessControlService.HasBudgetCategoryAccessAsync(transaction.BudgetCategoryId)) { throw new NotFoundException(Localization.For(() => ErrorMessages.TransactionNotFound)); } var subTransaction = transaction.SubTransactions.First(x => x.SubTransactionId == request.SubTransactionId); subTransaction.SoftDelete(); await _writeDbContext.SaveChangesAsync(cancellationToken); _ = _mediator.Publish(new Notification() { Transaction = transaction, SubTransaction = subTransaction }, cancellationToken); return(new Result() { }); }
public async Task <Result> Handle(Command request, CancellationToken cancellationToken) { var budgetCategoryId = request.BudgetCategoryId; if (!await _accessControlService.HasBudgetCategoryAccessAsync(budgetCategoryId)) { throw new NotFoundException(Localization.For(() => ErrorMessages.BudgetCategoryNotFound)); } var budgetCategory = _writeDbContext.BudgetCategories .First(x => x.BudgetCategoryId == request.BudgetCategoryId); var transactionTemplate = TransactionTemplate.Create(request.Description, budgetCategory, request.Amount); _writeDbContext.TransactionTemplates.Add(transactionTemplate); await _writeDbContext.SaveChangesAsync(cancellationToken); _ = _mediator.Publish(new Notification() { TransactionTemplate = transactionTemplate }, cancellationToken); return(new Result() { Id = transactionTemplate.TransactionTemplateId }); }
public async Task <Result> Handle(Command request, CancellationToken cancellationToken) { if (!await _accessControlService.HasBudgetCategoryAccessAsync(request.BudgetCategoryId)) { throw new NotFoundException(Localization.For(() => ErrorMessages.BudgetCategoryNotFound)); } var budgetCategory = await _writeDbContext.BudgetCategories .FirstOrDefaultAsync(x => x.BudgetCategoryId == request.BudgetCategoryId, cancellationToken); var icon = await _writeDbContext.BudgetCategoryIcons .FirstOrDefaultAsync(x => x.BudgetCategoryIconId == request.BudgetCategoryIconId, cancellationToken); budgetCategory.SetIcon(icon); await _writeDbContext.SaveChangesAsync(cancellationToken); _ = _mediator.Publish(new Notification() { ReferenceBudgetCategory = budgetCategory, }, cancellationToken); return(new Result() { Data = new IconDto() { IconId = icon.BudgetCategoryIconId, IconKey = icon.IconKey } }); }
public async Task <Result> Handle(Command request, CancellationToken cancellationToken) { var budgetCategory = await _writeDbContext.BudgetCategories .Include(x => x.BudgetedAmounts) .FirstOrDefaultAsync(x => x.BudgetedAmounts.Any(s => s.BudgetedAmountId == request.BudgetedAmountId), cancellationToken); if (!await _accessControlService.HasBudgetCategoryAccessAsync(budgetCategory.BudgetCategoryId)) { throw new NotFoundException(Localization.For(() => ErrorMessages.BudgetCategoryNotFound)); } var budgetedAmount = budgetCategory.BudgetedAmounts.FirstOrDefault(x => x.BudgetedAmountId == request.BudgetedAmountId); budgetCategory.BudgetedAmounts.SetItemValidFromDate(budgetedAmount, request.ValidFrom); await _writeDbContext.SaveChangesAsync(cancellationToken); _ = _mediator.Publish(new Notification() { ReferenceBudgetCategory = budgetCategory, ReferenceBudgetedAmount = budgetedAmount }, cancellationToken); return(new Result() { Data = request.ValidFrom }); }
public async Task <Result> Handle(Command request, CancellationToken cancellationToken) { if (!await _accessControlService.HasBudgetCategoryAccessAsync(request.BudgetCategoryId)) { throw new NotFoundException(Localization.For(() => ErrorMessages.BudgetCategoryNotFound)); } var budgetCategory = await _writeDbContext.BudgetCategories .FirstOrDefaultAsync(x => x.BudgetCategoryId == request.BudgetCategoryId, cancellationToken); var budgetedAmount = budgetCategory.AddBudgetedAmount(request.Amount, request.ValidFrom); await _writeDbContext.SaveChangesAsync(cancellationToken); _ = _mediator.Publish(new Notification() { ReferenceBudgetCategory = budgetCategory, ReferenceBudgetedAmount = budgetedAmount }, cancellationToken); return(new Result() { Data = new BudgetedAmountDto() { Amount = budgetedAmount.Amount, BudgetedAmountId = budgetedAmount.BudgetedAmountId, ValidFrom = budgetedAmount.ValidFrom, ValidTo = budgetedAmount.ValidTo } }); }
public async Task <Result> Handle(Command request, CancellationToken cancellationToken) { var targetBudgetCategoryId = request.TargetBudgetCategoryId; if (!await _accessControlService.HasBudgetCategoryAccessAsync(targetBudgetCategoryId)) { throw new NotFoundException(Localization.For(() => ErrorMessages.BudgetNotFound)); } var sourceBudgetCategoryId = request.SourceBudgetCategoryId; if (sourceBudgetCategoryId != null && !await _accessControlService.HasBudgetCategoryAccessAsync(sourceBudgetCategoryId)) { throw new NotFoundException(Localization.For(() => ErrorMessages.BudgetNotFound)); } var targetBudgetCategory = _writeDbContext.BudgetCategories .First(x => x.BudgetCategoryId == request.TargetBudgetCategoryId); var sourceBudgetCategory = sourceBudgetCategoryId != null ? _writeDbContext.BudgetCategories .First(x => x.BudgetCategoryId == request.SourceBudgetCategoryId) : null; var allocation = Allocation.Create(request.Description, targetBudgetCategory, sourceBudgetCategory, request.Amount, request.AllocationDate); _writeDbContext.Allocations.Add(allocation); await _writeDbContext.SaveChangesAsync(cancellationToken); _ = _mediator.Publish(new Notification() { ReferenceAllocation = allocation }, cancellationToken); return(new Result() { Id = allocation.AllocationId }); }
public async Task <Result> Handle(Command request, CancellationToken cancellationToken) { if (!await _accessControlService.HasAllocationAccess(request.AllocationId)) { throw new NotFoundException(Localization.For(() => ErrorMessages.TransactionNotFound)); } if (request.SourceBudgetCategoryId != null && !await _accessControlService.HasBudgetCategoryAccessAsync(request.SourceBudgetCategoryId)) { throw new NotFoundException(Localization.For(() => ErrorMessages.BudgetCategoryNotFound)); } var allocation = await _writeDbContext.Allocations .FirstAsync(x => x.AllocationId == request.AllocationId, cancellationToken : cancellationToken); var oldBudgetCategory = allocation.SourceBudgetCategoryId != null ? await _writeDbContext.BudgetCategories.FirstAsync(x => x.BudgetCategoryId == allocation.SourceBudgetCategoryId, cancellationToken : cancellationToken) : null; var newBudgetCategory = request.SourceBudgetCategoryId != null ? await _writeDbContext.BudgetCategories.FirstAsync(x => x.BudgetCategoryId == request.SourceBudgetCategoryId, cancellationToken : cancellationToken) : null; if (oldBudgetCategory != null && newBudgetCategory != null && oldBudgetCategory?.BudgetCategoryType != newBudgetCategory?.BudgetCategoryType) { throw new BusinessException(Localization.For(() => ErrorMessages.NotSameBudgetCategoryType)); } var oldCategoryId = allocation.SourceBudgetCategoryId; allocation.SetSourceBudgetCategory(newBudgetCategory); await _writeDbContext.SaveChangesAsync(cancellationToken); _ = _mediator.Publish(new Notification() { ReferenceAllocation = allocation, OldBudgetCategoryId = oldCategoryId, NewBudgetCategoryId = allocation.SourceBudgetCategoryId }, cancellationToken); return(new Result() { Id = allocation.SourceBudgetCategoryId }); }
public async Task <Result> Handle(Command request, CancellationToken cancellationToken) { if (!await _accessControlService.HasTransactionTemplateAccess(request.TransactionTemplateId)) { throw new NotFoundException(Localization.For(() => ErrorMessages.TransactionNotFound)); } if (!await _accessControlService.HasBudgetCategoryAccessAsync(request.BudgetCategoryId)) { throw new NotFoundException(Localization.For(() => ErrorMessages.BudgetCategoryNotFound)); } var transactionTemplate = await _writeDbContext.TransactionTemplates .FirstAsync(x => x.TransactionTemplateId == request.TransactionTemplateId, cancellationToken : cancellationToken); var oldBudgetCategory = await _writeDbContext.BudgetCategories .FirstAsync(x => x.BudgetCategoryId == transactionTemplate.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)); } transactionTemplate.SetBudgetCategory(newBudgetCategory); await _writeDbContext.SaveChangesAsync(cancellationToken); _ = _mediator.Publish(new Notification() { NewBudgetCategoryId = newBudgetCategory.BudgetCategoryId, OldBudgetCategoryId = oldBudgetCategory.BudgetCategoryId, TransactionTemplate = transactionTemplate }, cancellationToken); return(new Result() { Data = transactionTemplate.BudgetCategoryId }); }