Files
monorepo/LCU.API.Collections.RepoApi-old/LCU.API.Collections.RepoApi/Services/RepoCompanyService.cs
T
2026-04-20 14:47:24 -05:00

158 lines
5.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Threading.Tasks;
using LCU.API.Collections.RepoApi.Exceptions;
using LCU.API.Collections.RepoApi.Models;
using LCU.Shared.Collections.RepoApiLib.Models;
using LCU.Shared.Collections.RepoApiLib.Repositories;
using Serilog;
namespace LCU.API.Collections.RepoApi.Services
{
public class RepoCompanyService : IRepoCompanyService
{
private IRepoCompanyRepository _repoCompanyRepository;
private IUnitOfWork _unitOfWork;
public RepoCompanyService(
IRepoCompanyRepository repoCompanyRepository,
IUnitOfWork unitOfWork)
{
_repoCompanyRepository = repoCompanyRepository;
_unitOfWork = unitOfWork;
}
public async Task<IEnumerable<RepoCompany>> ListAll()
{
try
{
return await _repoCompanyRepository.ListAsync();
}
catch (Exception ex)
{
Log.Error(ex, "Failure retrieving Repo Companies");
throw new ServerErrorException();
}
}
public async Task<RepoCompany> Get(int id)
{
RepoCompany repoCompany;
try
{
repoCompany = await _repoCompanyRepository.FindByIdAsync(id);
}
catch (Exception ex)
{
Log.Error(ex, $"Failure retrieving Repo Company with id: {id}");
throw new NotFoundException($"Repo Company with id {id} not found");
}
if (repoCompany == null)
{
throw new NotFoundException($"Repo Company with id {id} not found");
}
return repoCompany;
}
public async Task<RepoCompany> Update(RepoCompany repoCompany, AppUser user)
{
var existing = await _repoCompanyRepository.FindByIdAsync(repoCompany.ID);
if (existing == null)
{
throw new NotFoundException($"Repo Company with id {repoCompany.ID} not found");
}
existing.Name = repoCompany.Name;
existing.Email = repoCompany.Email;
existing.Address_Line_1 = repoCompany.Address_Line_1;
existing.Address_Line_2 = repoCompany.Address_Line_2;
existing.City = repoCompany.City;
existing.State = repoCompany.State;
existing.ZIP = repoCompany.ZIP;
existing.Country = repoCompany.Country;
existing.Phone = repoCompany.Phone;
existing.Fax = repoCompany.Fax;
existing.ReferenceId = repoCompany.ReferenceId;
if (existing.DisabledDate != repoCompany.DisabledDate)
{
if (repoCompany.DisabledDate == null)
{
existing.DisabledDate = null;
existing.DisabledBy = null;
} else
{
existing.DisabledDate = repoCompany.DisabledDate;
existing.DisabledBy = user.FirstName + " " + user.LastName;
}
}
try
{
_repoCompanyRepository.Update(existing);
await _unitOfWork.CompleteAsync();
return existing;
}
catch (SqlException ex)
{
Log.Error(ex, $"SQL Filure updating Repo Company: {repoCompany}", repoCompany);
throw new ServerErrorException();
}
catch (Exception ex)
{
Log.Error(ex, $"Failure updating Repo Repo Company: {repoCompany}", repoCompany);
throw new ServerErrorException();
}
}
public async Task<RepoCompany> Save(RepoCompany repoCompany)
{
try
{
var newCompany = await _repoCompanyRepository.AddAsync(repoCompany);
await _unitOfWork.CompleteAsync();
return newCompany;
}
catch (Exception ex)
{
Log.Error(ex, $"Failure saving Repo Company: {repoCompany}", repoCompany);
throw new ServerErrorException();
}
}
public async Task<RepoCompany> Delete(int id)
{
var existingRepoCompany = await _repoCompanyRepository.FindByIdAsync(id);
if (existingRepoCompany == null)
{
throw new NotFoundException($" Repo Company with id {id} not found.");
}
try
{
_repoCompanyRepository.Remove(existingRepoCompany);
await _unitOfWork.CompleteAsync();
return existingRepoCompany;
}
catch (SqlException ex)
{
Log.Error(ex, $"SQL Failure deleting Repo Company with id: {id}");
throw new ServerErrorException();
}
catch (Exception ex)
{
Log.Error(ex, $"Failure deleting Repo Company with id: {id}");
throw new ServerErrorException();
}
}
}
}