using System; using System.Collections.Generic; using System.Threading.Tasks; using Newtonsoft.Json; using Serilog; using LCU.Api.Internal.MaidBotLib.Models; using LCU.Api.Internal.MaidBotLib.Repositories; using LCU.Library.Internal.Exceptions; namespace LCU.Api.Internal.MaidBotApi.Services; public class LoggingMatrixService : ILoggingMatrixService { private readonly ILoggingMatrixRepository _loggingMatrixRepository; private readonly IUnitOfWork _unitOfWork; public LoggingMatrixService(IUnitOfWork unitOfWork, ILoggingMatrixRepository loggingMatrixRepository) { _unitOfWork = unitOfWork; _loggingMatrixRepository = loggingMatrixRepository; } public async Task> GetAsync() { try { return await _loggingMatrixRepository.ListAsync(); } catch (Exception exception) { var message = "Failure retrieving Logging Matrixes."; Log.Error(exception, message); throw new ServiceException($"{message} Exception Message: {exception.Message}. "); } } public async Task> GetActiveAsync() { try { return await _loggingMatrixRepository.ListActiveAsync(); } catch (Exception exception) { var message = "Failure retrieving active Logging Matrixes."; Log.Error(exception, message); throw new ServiceException($"{message} Exception Message: {exception.Message}. "); } } public async Task> GetByApplicationLevelAndGroupAsync(string application, string level, string group) { try { return await _loggingMatrixRepository.RetrieveByApplicationLevelAndGroup(application, level, group); } catch (Exception exception) { var message = $"Failure retrieving Logging Matrixes with application {application} and level {level}."; Log.Error(exception, message); throw new ServiceException($"{message} Exception Message: {exception.Message}. "); } } public async Task> GetByApplicationAndLevelAsync(string application, string level) { try { return await _loggingMatrixRepository.RetrieveByApplciationAndLevel(application, level); } catch (Exception exception) { var message = $"Failure retrieving Logging Matrixes with application {application} and level {level}."; Log.Error(exception, message); throw new ServiceException($"{message} Exception Message: {exception.Message}. "); } } public async Task GetByIdAsync(int id) { LoggingMatrix loggingMatrix = null; try { loggingMatrix = await _loggingMatrixRepository.FindByIdAsync(id); return loggingMatrix; } catch (Exception exception) { var message = $"Failure retrieving Logging Matrix with id {id}."; Log.Error(exception, message); throw new ServiceException($"{message} Exception Message: {exception.Message}."); } } public async Task SaveAsync(LoggingMatrix loggingMatrix) { try { var result = await _loggingMatrixRepository.AddAsync(loggingMatrix); await _unitOfWork.CompleteAsync(); return result; } catch (Exception exception) { var message = $"Failure saving Logging Matrix. LoggingMatrix: { JsonConvert.SerializeObject(loggingMatrix)}"; Log.Error(exception, message); throw new ServiceException($"{message} Exception Message: {exception.Message}."); } } public async Task UpdateAsync(LoggingMatrix loggingMatrix) { try { var existing = await GetByIdAsync(loggingMatrix.LoggingMatrixId); if (existing == null) { return null; } existing.Application = loggingMatrix.Application; existing.Level = loggingMatrix.Level; existing.Group = loggingMatrix.Group; existing.Email = loggingMatrix.Email; existing.Disabled = loggingMatrix.Disabled; _loggingMatrixRepository.Update(existing); await _unitOfWork.CompleteAsync(); return existing; } catch (Exception exception) { var message = $"Failure updating Logging Matrix. LoggingMatrix: { JsonConvert.SerializeObject(loggingMatrix)}"; Log.Error(exception, message); throw new ServiceException($"{message} Exception Message: {exception.Message}."); } } }