151 lines
5.1 KiB
C#
151 lines
5.1 KiB
C#
using LCU.Library.Internal.ArchiverApiLib.Repositories;
|
|
using LCU.Library.Internal.ArchiverApiModel.Entities;
|
|
using System.Data.SqlClient;
|
|
using LCU.Library.Internal.Exceptions;
|
|
using Serilog;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace LCU.Api.Internal.ArchiverApi.Services;
|
|
|
|
public class RegexMappingService : IRegexMappingService
|
|
{
|
|
private readonly IRegexMappingRepository _regexMappingRepository;
|
|
private readonly IUnitOfWork _unitOfWork;
|
|
|
|
public RegexMappingService(IUnitOfWork unitOfWork,
|
|
IRegexMappingRepository regexMappingRepository)
|
|
{
|
|
_unitOfWork = unitOfWork;
|
|
_regexMappingRepository = regexMappingRepository;
|
|
}
|
|
public void DeleteAsync(int regexMappingId)
|
|
{
|
|
try
|
|
{
|
|
var regexMapping = _regexMappingRepository.FindByRegexMappingIdAsync(regexMappingId).Result;
|
|
ArgumentNullException.ThrowIfNull(regexMapping);
|
|
_regexMappingRepository.Remove(regexMapping);
|
|
_unitOfWork.Complete();
|
|
}
|
|
catch (SqlException sqlException)
|
|
{
|
|
Log.Error(sqlException, $"SQL failure removing regex mapping. RegexMappingId: {regexMappingId}");
|
|
throw new ServiceException($"SQL failure removing regex mapping. RegexMappingId: {regexMappingId}");
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception, $"Failure removing regex mapping. RegexMappingId: {regexMappingId}");
|
|
throw new ServiceException($"Failure removing regex mapping. RegexMappingId: {regexMappingId}");
|
|
}
|
|
}
|
|
|
|
public async Task<IEnumerable<RegexMapping>> GetAsync()
|
|
{
|
|
try
|
|
{
|
|
return await _regexMappingRepository.ListAsync();
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception, "Failure retrieving regex mappings.");
|
|
throw new ServiceException("Failure retrieving regex mappings.");
|
|
}
|
|
}
|
|
|
|
public async Task<RegexMapping> GetByApplicationMappingIdAsync(int id)
|
|
{
|
|
try
|
|
{
|
|
var regexMapping = await _regexMappingRepository.FindByApplicationMappingIdAsync(id);
|
|
ArgumentNullException.ThrowIfNull(regexMapping);
|
|
return regexMapping;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception, $"Failure retrieving regex mapping with id {id}.");
|
|
throw new ServiceException($"Failure retrieving regex mapping with id {id}.");
|
|
}
|
|
}
|
|
|
|
public async Task<RegexMapping> GetByRegexMappingIdAsync(int id)
|
|
{
|
|
try
|
|
{
|
|
var regexMapping = await _regexMappingRepository.FindByRegexMappingIdAsync(id);
|
|
ArgumentNullException.ThrowIfNull(regexMapping);
|
|
return regexMapping;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception, $"Failure retrieving regex mapping with id {id}.");
|
|
throw new ServiceException($"Failure retrieving regex mapping with id {id}.");
|
|
}
|
|
}
|
|
|
|
public async Task<RegexMapping> SaveAsync(RegexMapping regexMapping)
|
|
{
|
|
try
|
|
{
|
|
if (CheckTagInvalid(regexMapping.Tag))
|
|
{
|
|
throw new Exception("Invalid Tag: "+regexMapping.Tag);
|
|
}
|
|
var result = await _regexMappingRepository.AddAsync(regexMapping);
|
|
await _unitOfWork.CompleteAsync();
|
|
|
|
return result;
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception, "Failure saving regex mapping");
|
|
throw new ServiceException("Failure saving regex mapping");
|
|
}
|
|
}
|
|
|
|
public RegexMapping Update(RegexMapping regexMapping)
|
|
{
|
|
try
|
|
{
|
|
if (CheckTagInvalid(regexMapping.Tag))
|
|
{
|
|
throw new Exception("Invalid Tag: "+regexMapping.Tag);
|
|
}
|
|
var existing = GetByRegexMappingIdAsync(regexMapping.RegexMappingId).Result;
|
|
ArgumentNullException.ThrowIfNull(existing);
|
|
|
|
existing.ApplicationMappingId = regexMapping.ApplicationMappingId;
|
|
existing.Regex = regexMapping.Regex;
|
|
existing.Tag = regexMapping.Tag;
|
|
|
|
_regexMappingRepository.Update(existing);
|
|
_unitOfWork.Complete();
|
|
|
|
return existing;
|
|
}
|
|
catch (SqlException sqlException)
|
|
{
|
|
Log.Error(sqlException, "SQL Failure updating regex mapping");
|
|
throw new ServiceException("SQL Failure updating regex mapping");
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Log.Error(exception, "Failure updating regex mapping");
|
|
throw new ServiceException("Failure updating regex mapping");
|
|
}
|
|
}
|
|
|
|
private bool CheckTagInvalid(string tag)
|
|
{
|
|
var endIndex = tag.IndexOf('}');
|
|
var sub = tag.Substring(0, 6);
|
|
if (tag.Substring(0,6) == "{Regex" && endIndex >= 7)
|
|
{
|
|
var tagNumber = Int32.Parse(tag.Substring(6, endIndex - 6));
|
|
return tagNumber >= 0 && tagNumber <= 3;
|
|
}
|
|
return true;
|
|
}
|
|
}
|