Compare commits

...

2 Commits

3 changed files with 141 additions and 6 deletions

View File

@ -0,0 +1,135 @@
using MedicalDatabaseContracts;
using MedicalDatabaseContracts.Models;
using MedicalDatabaseContracts.SearchModels;
using MedicalDatabaseContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace MedicalBusinessLogic.BusinessLogics
{
public abstract class AbstractLogic<M, V, S> : ILogic<M, V, S>
where M : AbstractModel
where V : AbstractViewModel
where S : AbstractSearchModel
{
protected readonly ILogger<AbstractLogic<M, V, S>> _logger;
protected readonly IStorage<M> _storage;
protected AbstractLogic(ILogger<AbstractLogic<M, V, S>> logger, IStorage<M> storage)
{
_logger = logger;
_storage = storage;
}
protected abstract bool CheckModelIsValid(M model);
protected abstract bool CheckModelBySearchModel(M model, S searchModel);
protected abstract V GetViewModel(M model);
public bool Create(M model)
{
return Create(model, out _);
}
public bool Delete(int id)
{
return Delete(id, out _);
}
public V? ReadElement(int id)
{
return ReadElement(id, out _);
}
public List<V> ReadList(S? searchModel)
{
return ReadList(searchModel, out _);
}
public bool Update(M model)
{
return Update(model, out _);
}
public virtual List<V> ReadList(S? searchModel, out long elapsedMilliseconds)
{
var elements = _storage.GetAll(out elapsedMilliseconds);
if (searchModel != null)
{
elements = elements.Where(x => CheckModelBySearchModel(x, searchModel)).ToList();
}
_logger.LogInformation($"ReadList count:{elements.Count}");
return elements.Select(x => GetViewModel(x)).ToList();
}
public virtual V? ReadElement(int id, out long elapsedMilliseconds)
{
var element = _storage.Get(id, out elapsedMilliseconds);
if (element == null)
{
_logger.LogWarning($"ReadElement record id:{id} failed: element not found");
return null;
}
_logger.LogInformation($"ReadElement record id:{id} success");
return GetViewModel(element);
}
public virtual bool Create(M model, out long elapsedMilliseconds)
{
elapsedMilliseconds = 0;
if (!CheckModelIsValid(model))
{
return false;
}
try
{
_storage.Insert(model, out elapsedMilliseconds);
_logger.LogInformation($"Insert operation success");
return true;
}
catch (Exception ex)
{
_logger.LogWarning($"Insert operation failed: {ex.Message}");
return false;
}
}
public virtual bool Update(M model, out long elapsedMilliseconds)
{
elapsedMilliseconds = 0;
if (!CheckModelIsValid(model))
{
return false;
}
try
{
_storage.Update(model, out elapsedMilliseconds);
_logger.LogInformation($"Update record id:{model.Id} operation success");
return true;
}
catch (Exception ex)
{
_logger.LogWarning($"Update record id:{model.Id} operation failed: {ex.Message}");
return false;
}
}
public virtual bool Delete(int id, out long elapsedMilliseconds)
{
elapsedMilliseconds = 0;
try
{
_storage.Delete(id, out elapsedMilliseconds);
_logger.LogInformation($"Delete operation success");
return true;
}
catch (Exception ex)
{
_logger.LogWarning($"Delete operation failed: {ex.Message}");
return false;
}
}
}
}

View File

@ -9,10 +9,15 @@ namespace MedicalDatabaseContracts
where V : AbstractViewModel
where S : AbstractSearchModel
{
List<V>? ReadList(S? searchModel);
List<V> ReadList(S? searchModel);
List<V> ReadList(S? searchModel, out long elapsedMilliseconds);
V? ReadElement(int id);
V? ReadElement(int id, out long elapsedMilliseconds);
bool Create(M model);
bool Create(M model, out long elapsedMilliseconds);
bool Update(M model);
bool Update(M model, out long elapsedMilliseconds);
bool Delete(int id);
bool Delete(int id, out long elapsedMilliseconds);
}
}

View File

@ -31,7 +31,6 @@ namespace MedicalPostgresqlDatabase
public virtual void Delete(int id)
{
long _;
Delete(id, out _);
}
public virtual void Delete(int id, out long elapsedMilliseconds)
@ -48,7 +47,6 @@ namespace MedicalPostgresqlDatabase
}
public virtual T? Get(int id)
{
long _;
return Get(id, out _);
}
public virtual T? Get(int id, out long elapsedMilliseconds)
@ -70,7 +68,6 @@ namespace MedicalPostgresqlDatabase
}
public virtual List<T> GetAll()
{
long _;
return GetAll(out _);
}
public virtual List<T> GetAll(out long elapsedMilliseconds)
@ -92,7 +89,6 @@ namespace MedicalPostgresqlDatabase
}
public virtual void Insert(T item)
{
long _;
Insert(item, out _);
}
public virtual void Insert(T item, out long elapsedMilliseconds)
@ -137,7 +133,6 @@ namespace MedicalPostgresqlDatabase
}
public virtual void Update(T item)
{
long _;
Update(item, out _);
}
public virtual void Update(T item, out long elapsedMilliseconds)