/// <inheritdoc /> public override async Task <Response> Handle(Request request, CancellationToken cancellationToken) { var availableBudgets = await BudgetRepository.ListAvailableBudgets(); var budgetToUpdate = availableBudgets.FirstOrDefault(x => x.Id != request.BudgetId); if (budgetToUpdate == null) { throw new NotFoundException("Budget was not found"); } if (budgetToUpdate.BudgetShares.Any(x => x.SharedWithUserId == request.UserId)) { throw new SaveFailureException("Budget share is already created", request); } var shareEntity = Mapper.Map <BudgetShare>(request); await _budgetShareRepository.AddAsync(shareEntity); await _budgetShareRepository.SaveChangesAsync(cancellationToken); return(new Response() { Data = new Unit() }); }
/// <inheritdoc /> public override async Task <Response> Handle(Request request, CancellationToken cancellationToken) { var findShareTask = _budgetShareRepository.ListWithFilter(new Domain.Entities.Budget(request.BudgetId), new BudgetShareFilterModel() { UserIdFilter = request.UserId }); if (!await BudgetRepository.IsAccessibleToUser(request.BudgetId)) { throw new NotFoundException("Budget was not found"); } var budgetShares = await findShareTask; if (!budgetShares.Any()) { throw new NotFoundException("Budget share was not found"); } await Task.WhenAll(budgetShares.Select(i => _budgetShareRepository.DeleteAsync(i))); try { await _budgetShareRepository.SaveChangesAsync(cancellationToken); } catch (Exception) { throw new SaveFailureException("budget share", budgetShares); } return(new Response() { Data = new Unit() }); }