public async Task UpdateAsync(BonusType bonusType) { var oldBonusType = await _bonusTypeRepository.GetBonusTypeAsync(bonusType.Type); if (oldBonusType == null) { throw new EntityNotFoundException($"Bonus Type with name {bonusType.Type} does not exist."); } var bonusTypeWithSameDisplayName = await _bonusTypeRepository.GetBonusTypeByDisplayNameAsync(bonusType.DisplayName); if (bonusTypeWithSameDisplayName != null && oldBonusType.Type != bonusTypeWithSameDisplayName.Type) { throw new EntityAlreadyExistsException( $"Bonus Type with same Display Name '{bonusType.DisplayName}' already exists."); } // Copy old values to preserve them after update bonusType.CreationDate = oldBonusType.CreationDate; await _bonusTypeRepository.UpdateAsync(bonusType); _log.Info($"Bonus Type was updated: {bonusType.ToJson()}", process: nameof(UpdateAsync), context: bonusType.Type); }
public async Task <BonusType> InsertAsync(BonusType bonusType) { bonusType.CreationDate = DateTime.UtcNow; bonusType.Type = bonusType.Type.ToLower(); var bonusTypeWithSameType = await _bonusTypeRepository.GetBonusTypeAsync(bonusType.Type); if (bonusTypeWithSameType != null) { throw new EntityAlreadyExistsException($"Bonus Type '{bonusType.Type}' already exists."); } var bonusTypeWithSameDisplayName = await _bonusTypeRepository.GetBonusTypeByDisplayNameAsync(bonusType.DisplayName); if (bonusTypeWithSameDisplayName != null) { throw new EntityAlreadyExistsException( $"Bonus Type with same Display Name '{bonusType.DisplayName}' already exists."); } var bonusTypeResponse = await _bonusTypeRepository.InsertAsync(bonusType); _log.Info($"Bonus Type was added: {bonusType.ToJson()}", process: nameof(InsertAsync), context: bonusType.Type); return(bonusTypeResponse); }