using System; using System.Data.SqlClient; using System.Threading.Tasks; using LCU.API.Collections.RepoApi.Exceptions; using LCU.API.Collections.RepoApi.Models.Filters; using LCU.Shared.Collections.RepoApiLib.Models; using LCU.Shared.Collections.RepoApiLib.Models.Results; using LCU.Shared.Collections.RepoApiLib.Repositories; using Serilog; namespace LCU.API.Collections.RepoApi.Services { public class AuditRecordService : IAuditRecordService { private readonly IAuditRecordRepository _repoAccountRepository; private readonly IUnitOfWork _unitOfWork; public AuditRecordService( IAuditRecordRepository repoAccountRepository, IUnitOfWork unitOfWork) { _repoAccountRepository = repoAccountRepository; _unitOfWork = unitOfWork; } public async Task> ListAll(PageFilter filter) { try { return await _repoAccountRepository.ListAllAsync(filter.Page, filter.Limit); } catch (Exception ex) { Log.Error(ex, $"Failure retrieving AuditRecords with page filter {filter}",filter); throw new ServerErrorException(); } } public async Task SaveAsync(AuditRecord repoAccount) { try { await _repoAccountRepository.AddAsync(repoAccount); await _unitOfWork.CompleteAsync(); return repoAccount; } catch (Exception ex) { Log.Error(ex, $"Failure saving AuditRecord for repoAccount: {repoAccount}", repoAccount); throw new ServerErrorException(); } } public async Task Get(int id) { AuditRecord repoAccount; try { repoAccount = await _repoAccountRepository.FindByIdAsync(id); } catch (Exception ex) { Log.Error(ex, $"Failure retrieving AuditRecord wtih id: {id}"); throw new ServerErrorException(); } if (repoAccount == null) { throw new NotFoundException($" AuditRecords with id {id} not found."); } return repoAccount; } public async Task UpdateAsync(AuditRecord auditRecord) { AuditRecord existingAuditRecord = null; try { existingAuditRecord = await _repoAccountRepository.FindByIdAsync(auditRecord.ID); } catch(Exception ex) { Log.Error(ex, $"Failure retreving existing auditRecord wtih auditrecordId: AuditRecordID: {auditRecord.ID}", auditRecord); throw new ServerErrorException(); } if (existingAuditRecord == null) { throw new NotFoundException($" AuditRecord with id {auditRecord.ID} not found."); } try { _repoAccountRepository.Update(existingAuditRecord); await _unitOfWork.CompleteAsync(); return (existingAuditRecord); } catch (SqlException ex) { Log.Error(ex, $"SQL Failure updating AuditRecord: {auditRecord}", auditRecord); throw new ServerErrorException(); } catch (Exception ex) { Log.Error(ex, $"Failure updating AuditRecord: {auditRecord}", auditRecord); throw new ServerErrorException(); } } public async Task DeleteAsync(int id) { AuditRecord existingAuditRecord = null; try { existingAuditRecord = await _repoAccountRepository.FindByIdAsync(id); } catch(Exception ex) { Log.Error(ex, $"Failure retreving existing auditRecord wtih auditrecordId: AuditRecordID: {id}"); throw new ServerErrorException(); } if (existingAuditRecord == null) { throw new NotFoundException($" AuditRecord with id {id} not found."); } try { _repoAccountRepository.Remove(existingAuditRecord); await _unitOfWork.CompleteAsync(); return existingAuditRecord; } catch (SqlException ex) { Log.Error(ex, $"SQL Failure deleting AuditRecord with id: {id}", id); throw new ServerErrorException(); } catch (Exception ex) { Log.Error(ex, $"Failure deleting AuditRecord with id: {id}", id); throw new ServerErrorException(); } } } }