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 AuctionHouseService : IAuctionHouseService { private readonly IAuctionHouseRepository _auctionHouseRepository; private readonly IUnitOfWork _unitOfWork; public AuctionHouseService( IAuctionHouseRepository auctionHouseRepository, IUnitOfWork unitOfWork) { _auctionHouseRepository = auctionHouseRepository; _unitOfWork = unitOfWork; } public async Task> ListAllAsync() { try { return await _auctionHouseRepository.ListAllAsync(); } catch (Exception ex) { Log.Error(ex, "Failure retrieving Auctions"); throw new ServerErrorException(); } } public async Task GetAsync(string id) { AuctionHouse auction; try { auction = await _auctionHouseRepository.FindByIdAsync(id); } catch (Exception ex) { Log.Error(ex, $"Failure retrieving Auction with id: {id}"); throw new ServerErrorException(); } if (auction == null) { throw new NotFoundException($"Auction with id {id} not found."); } return auction; } public async Task SaveAsync(AuctionHouse auction) { try { await _auctionHouseRepository.AddAsync(auction); await _unitOfWork.CompleteAsync(); return auction; } catch (Exception ex) { Log.Error(ex, "Failure saving Auction", auction); throw new ServerErrorException(); } } public async Task UpdateAsync(AuctionHouse auction) { AuctionHouse existing; try { existing = await _auctionHouseRepository.FindByIdAsync(auction.AuctionID); } catch (Exception ex) { Log.Error(ex, $"Failed to find existing Auction House to update with id: {auction.AuctionID}", auction); throw new ServerErrorException(); } if (existing == null) { throw new NotFoundException($"Auction with id {auction.AuctionID} not found."); } existing.Name = auction.Name; existing.Active = auction.Active; existing.Address_Line_1 = auction.Address_Line_1; existing.Address_Line_2 = auction.Address_Line_2; existing.City = auction.City; existing.State = auction.State; existing.ZIP = auction.ZIP; existing.Country = auction.Country; existing.Phone = auction.Phone; existing.Fax = auction.Fax; existing.Latitude = auction.Latitude; existing.Longitude = auction.Longitude; try { _auctionHouseRepository.Update(existing); await _unitOfWork.CompleteAsync(); return (existing); } catch (SqlException ex) { Log.Error(ex, $"SQL Failure updating Auction: {auction}", auction); throw new ServerErrorException(); } catch (Exception ex) { Log.Error(ex, $"Failure updating Auction {auction}", auction); throw new ServerErrorException(); } } public async Task DeleteAsync(string id) { AuctionHouse existingAuction; try { existingAuction = await _auctionHouseRepository.FindByIdAsync(id); } catch (Exception ex) { Log.Error(ex, $"Failed to find existing Auction House to delete with id: {id}"); throw new ServerErrorException(); } if (existingAuction == null) { throw new NotFoundException($"Auction with id {id} not found."); } try { _auctionHouseRepository.Remove(existingAuction); await _unitOfWork.CompleteAsync(); return existingAuction; } catch (SqlException ex) { Log.Error(ex, $"SQL Failure deleting Auction with id: {id}"); throw new ServerErrorException(); } catch (Exception ex) { Log.Error(ex, $"Failure deleting Auction with id : {id}"); throw new ServerErrorException(); } } } }