public async Task <UserHistoryDto> EstimateBonus(Guid historyId, int estimate, CancellationToken cancellationToken = default) { var history = await _historyRepository.GetByIdAsync(historyId, cancellationToken); if (history is null) { throw new ArgumentNullException("", Resources.IdentifierIsNull); } history.Rating = estimate; var bonus = await _bonusRepository.GetByIdAsync(history.BonusId, cancellationToken); if (bonus is null) { throw new ArgumentNullException("", Resources.IdentifierIsNull); } double avg; if (bonus.Rating == 0) { avg = estimate; } else { int countEst = await _historyRepository.GetCountHistoryByBonusIdAsync(bonus.Id, cancellationToken); avg = (bonus.Rating + estimate) / (countEst + 1); } await _bonusRepository.UpdateBonusRatingAsync(bonus.Id, avg, cancellationToken); await _historyRepository.UpdateAsync(historyId, history, cancellationToken); return(_mapper.Map <UserHistoryDto>(history)); }
public async Task <BonusDto> FindBonusByIdAsync(Guid id, CancellationToken cancellationToken) { if (id == Guid.Empty) { throw new ArgumentNullException("", Resources.IdentifierIsNull); } var result = await _bonusRepository.GetByIdAsync(id, cancellationToken); return(result is null ? throw new ArgumentException("", Resources.FindbyIdError) : _mapper.Map <BonusDto>(result)); }
public async Task <UserHistoryDto> EstimateBonus(Guid historyId, int estimate, CancellationToken cancellationToken = default) { var history = await _historyRepository.GetByIdAsync(historyId, cancellationToken); if (history is null) { throw new ArgumentNullException("", Resources.IdentifierIsNull); } history.Rating = estimate; var bonus = await _bonusRepository.GetByIdAsync(history.BonusId, cancellationToken); if (bonus is null) { throw new ArgumentNullException("", Resources.IdentifierIsNull); } double avg; if (bonus.Rating == 0) { avg = estimate; } else { var estimatedHistory = await _historyRepository.GetEstmatatedHistoryByBonusIdAsync(bonus.Id, cancellationToken); int sumEstimated = 0; foreach (var h in estimatedHistory) { sumEstimated += h.Rating; } avg = (double)(sumEstimated + estimate) / (double)(estimatedHistory.Count() + 1); } await _bonusRepository.UpdateBonusRatingAsync(bonus.Id, avg, cancellationToken); await _historyRepository.UpdateAsync(historyId, history, cancellationToken); return(_mapper.Map <UserHistoryDto>(history)); }