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); }
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); }
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 <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()); }