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 ImsAutoFileDataService : IImsAutoFileDataService { private readonly IImsAutoFileDataRepository _imsAutoFileDataRepository; private readonly IUnitOfWork _unitOfWork; public ImsAutoFileDataService( IImsAutoFileDataRepository imsAutoFileDataRepository, IUnitOfWork unitOfWork) { _imsAutoFileDataRepository = imsAutoFileDataRepository; _unitOfWork = unitOfWork; } public async Task> ListAll() { try { return await _imsAutoFileDataRepository.ListAllAsync(); } catch (Exception ex) { Log.Error(ex, "Failure retrieving ImsAutoFileData Set"); throw new ServerErrorException(); } } public async Task SaveAsync(ImsAutoFileData imsAutoFileData) { try { await _imsAutoFileDataRepository.AddAsync(imsAutoFileData); await _unitOfWork.CompleteAsync(); return imsAutoFileData; } catch (Exception ex) { Log.Error(ex, $"Failure saving IMS Auto File Data: {imsAutoFileData}", imsAutoFileData); throw new ServerErrorException(); } } public async Task Get(string vin, int repoId) { ImsAutoFileData imsAutoFileData; try { imsAutoFileData = await _imsAutoFileDataRepository.FindByIdAsync(vin, repoId); } catch (Exception ex) { Log.Error(ex, $"Failure retrieving IMS Auto File Data with vin: {vin} repoId: {repoId}"); throw new ServerErrorException(); } if (imsAutoFileData == null) { throw new NotFoundException($"IMS Auto File Data id {vin} / {repoId} not found."); } return imsAutoFileData; } public async Task GetByAuctionAssignmentId(int auctionAssignmentId) { ImsAutoFileData imsAutoFileData; try { imsAutoFileData = await _imsAutoFileDataRepository.FindByAuctionAssignmentIdAsync(auctionAssignmentId); } catch (Exception ex) { Log.Error(ex, $"Failure retrieving IMS Auto File Data by AuctionAssignmentId: {auctionAssignmentId}", auctionAssignmentId); throw new ServerErrorException(); } if (imsAutoFileData == null) { throw new NotFoundException($"IMS Auto File Data id {auctionAssignmentId} not found."); } return imsAutoFileData; } public async Task UpdateAsync(ImsAutoFileData imsAutoFileData) { ImsAutoFileData existingImsAutoFileData = null; try { existingImsAutoFileData = await _imsAutoFileDataRepository.FindByIdAsync(imsAutoFileData.VIN, imsAutoFileData.RepoId); } catch(Exception ex) { Log.Error(ex, $"Failed to retreive existingImsAutoFileData with vin: {imsAutoFileData.VIN} and {imsAutoFileData.RepoId}",imsAutoFileData); throw new ServerErrorException(); } if (existingImsAutoFileData == null) { throw new NotFoundException($"IMS Auto File Data with id {imsAutoFileData.VIN} / {imsAutoFileData.RepoId} not found."); } try { _imsAutoFileDataRepository.Update(imsAutoFileData); await _unitOfWork.CompleteAsync(); return (existingImsAutoFileData); } catch (SqlException ex) { Log.Error(ex, $"SQL Failure updating IMS Auto File Data: {imsAutoFileData}", imsAutoFileData); throw new ServerErrorException(); } catch (Exception ex) { Log.Error(ex, $"Failure updating IMS Auto File Data: {imsAutoFileData}", imsAutoFileData); throw new ServerErrorException(); } } public async Task DeleteAsync(string vin, int repoId) { ImsAutoFileData existingimsAutoFileData = null; try { existingimsAutoFileData = await _imsAutoFileDataRepository.FindByIdAsync(vin, repoId); } catch(Exception ex) { Log.Error(ex, $"Failed to retreive existingImsAutoFileData with vin: {vin} and {repoId}"); throw new ServerErrorException(); } if (existingimsAutoFileData == null) { throw new NotFoundException($"IMS Auto File Data with id {vin} / {repoId} not found."); } try { _imsAutoFileDataRepository.Remove(existingimsAutoFileData); await _unitOfWork.CompleteAsync(); return existingimsAutoFileData; } catch (SqlException ex) { Log.Error(ex, $"SQL Failure deleting IMS Auto File Data with vin: {vin}, repoId: {repoId}"); throw new ServerErrorException(); } catch (Exception ex) { Log.Error(ex, $"Failure deleting IMS Auto File Data with vin: {vin}, repoId: {repoId}"); throw new ServerErrorException(); } } } }