using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Threading.Tasks; using LCU.API.Collections.RepoApi.Exceptions; using LCU.Shared.Collections.RepoApiLib.Models; using LCU.Shared.Collections.RepoApiLib.Repositories; using Serilog; namespace LCU.API.Collections.RepoApi.Services { public class VehicleFloorPricingParametersService : IVehicleFloorPricingParametersService { private readonly IVehicleFloorPricingParametersRepository _parametersRepository; private readonly IUnitOfWork _unitOfWork; public VehicleFloorPricingParametersService( IVehicleFloorPricingParametersRepository parametersRepository, IUnitOfWork unitOfWork) { _parametersRepository = parametersRepository; _unitOfWork = unitOfWork; } public async Task> ListAll() { try { return await _parametersRepository.ListAllAsync(); } catch (Exception ex) { Log.Error(ex, "Failure retrieving VehicleFloorPricingParameters"); throw new ServerErrorException(); } } public async Task Get(int id) { VehicleFloorPricingParameters parameters; try { parameters = await _parametersRepository.FindByIdAsync(id); await _unitOfWork.CompleteAsync(); } catch (Exception ex) { Log.Error(ex, $"Failure retrieving VehicleFloorPricingParameters: {id}", id); throw new ServerErrorException(); } if (parameters == null) { throw new NotFoundException($"VehicleFloorPricingParameters with id {id} not found."); } return parameters; } public async Task UpdateAsync(VehicleFloorPricingParameters parameters) { VehicleFloorPricingParameters existing = null; try { existing = await _parametersRepository.FindByIdAsync(parameters.Id); } catch (Exception ex) { Log.Error(ex, $"Failure retrieving existing VehicleFloorPricingParameters with Id: {parameters.Id}", parameters.Id); throw new ServerErrorException(); } if (existing == null) { throw new NotFoundException($"Vehicle Floor Pricing Parameters with id {parameters.Id} not found."); } existing.LowCondition = parameters.LowCondition; existing.HighCondition = parameters.HighCondition; existing.NADAFirstValue = parameters.NADAFirstValue; existing.NADASecondValue = parameters.NADASecondValue; existing.MMRAdjustedValue = parameters.MMRAdjustedValue; existing.PercentageOf = parameters.PercentageOf; existing.ValuationServiceType = parameters.ValuationServiceType; try { _parametersRepository.Update(existing); await _unitOfWork.CompleteAsync(); return (existing); } catch (SqlException ex) { Log.Error(ex, $"SQL Failure updating Vehicle Floor Pricing Parameters: {parameters}", parameters); throw new ServerErrorException(); } catch (Exception ex) { Log.Error(ex, $"Failure updating Vehicle Floor Pricing Parameters: {parameters}", parameters); throw new ServerErrorException(); } } } }