Files
2026-04-20 14:47:24 -05:00

140 lines
4.7 KiB
C#

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 SavedLocationService : ISavedLocationService
{
private ISavedLocationRepository _savedLocationRepository;
private IUnitOfWork _unitOfWork;
public SavedLocationService(
ISavedLocationRepository savedLocationRepository,
IUnitOfWork unitOfWork)
{
_savedLocationRepository = savedLocationRepository;
_unitOfWork = unitOfWork;
}
public async Task<IEnumerable<SavedLocation>> ListAllAsync()
{
try
{
return await _savedLocationRepository.ListAsync();
}
catch (Exception ex)
{
Log.Error(ex, "Failure retrieving Saved Locations");
throw new ServerErrorException();
}
}
public async Task<SavedLocation> GetAsync(int id)
{
SavedLocation savedLocation;
try
{
savedLocation = await _savedLocationRepository.FindByIdAsync(id);
}
catch (Exception ex)
{
Log.Error(ex, $"Failure retrieving Saved Location with id: {id}");
throw new NotFoundException($"Saved Location with id {id} not found");
}
if (savedLocation == null)
{
throw new NotFoundException($"Saved Location with id {id} not found");
}
return savedLocation;
}
public async Task<SavedLocation> SaveAsync(SavedLocation savedLocation)
{
try
{
var newSavedLocation = await _savedLocationRepository.AddAsync(savedLocation);
await _unitOfWork.CompleteAsync();
return newSavedLocation;
}
catch (Exception ex)
{
Log.Error(ex, $"Failure saving Saved Location {savedLocation}", savedLocation);
throw new ServerErrorException();
}
}
public async Task<SavedLocation> UpdateAsync(SavedLocation savedLocation)
{
var existing = await _savedLocationRepository.FindByIdAsync(savedLocation.ID);
if (existing == null)
{
throw new NotFoundException($"Saved Location with id {savedLocation.ID} not found");
}
existing.Name = savedLocation.Name;
existing.LocationType = savedLocation.LocationType;
existing.Address_Line_1 = savedLocation.Address_Line_1;
existing.Address_Line_2 = savedLocation.Address_Line_2;
existing.City = savedLocation.City;
existing.State = savedLocation.State;
existing.ZIP = savedLocation.ZIP;
existing.Country = savedLocation.Country;
existing.Phone = savedLocation.Phone;
existing.Fax = savedLocation.Fax;
try
{
_savedLocationRepository.Update(existing);
await _unitOfWork.CompleteAsync();
return existing;
}
catch (SqlException ex)
{
Log.Error(ex, $"SQL Filure updating Saved Location: {savedLocation}", savedLocation);
throw new ServerErrorException();
}
catch (Exception ex)
{
Log.Error(ex, $"Failure updating Saved Location {savedLocation}", savedLocation);
throw new ServerErrorException();
}
}
public async Task<SavedLocation> DeleteAsync(int id)
{
var existingSavedLocation = await _savedLocationRepository.FindByIdAsync(id);
if (existingSavedLocation == null)
{
throw new NotFoundException($" Saved Location with id {id} not found.");
}
try
{
_savedLocationRepository.Remove(existingSavedLocation);
await _unitOfWork.CompleteAsync();
return existingSavedLocation;
}
catch (SqlException ex)
{
Log.Error(ex, $"SQL Failure deleting Saved Location with id: {id}");
throw new ServerErrorException();
}
catch (Exception ex)
{
Log.Error(ex, $"Failure deleting Saved Location with id: {id}");
throw new ServerErrorException();
}
}
}
}