public async Task <Result> Handle(Command request, CancellationToken cancellationToken) { var transactionTemplate = await _writeDbContext.TransactionTemplates .FirstOrDefaultAsync(x => x.TransactionTemplateId == request.TransactionTemplateId, cancellationToken : cancellationToken) ?? throw new NotFoundException(Localization.For(() => ErrorMessages.TransactionNotFound)); if (!await _accessControlService.HasTransactionTemplateAccess(transactionTemplate.TransactionTemplateId)) { throw new NotFoundException(Localization.For(() => ErrorMessages.TransactionNotFound)); } transactionTemplate.SoftDelete(); await _writeDbContext.SaveChangesAsync(cancellationToken); _ = _mediator.Publish(new Notification() { TransactionTemplate = transactionTemplate }, cancellationToken); return(new Result() { }); }
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 }); }