From 27a285e5734487d53c8f43ebdd4319eae009effe Mon Sep 17 00:00:00 2001 From: "ns.potapov" Date: Wed, 15 May 2024 01:13:04 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=BB=20?= =?UTF-8?q?=D0=B0=D0=B1=D1=81=D1=82=D1=80=D0=B0=D0=BA=D1=82=D0=BD=D1=8B?= =?UTF-8?q?=D0=B9=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20=D0=BB=D0=BE=D0=B3?= =?UTF-8?q?=D0=B8=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/AbstractLogic.cs | 135 ++++++++++++++++++ Medical/MedicalDatabaseContracts/ILogic.cs | 7 +- 2 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 Medical/MedicalBusinessLogic/BusinessLogics/AbstractLogic.cs diff --git a/Medical/MedicalBusinessLogic/BusinessLogics/AbstractLogic.cs b/Medical/MedicalBusinessLogic/BusinessLogics/AbstractLogic.cs new file mode 100644 index 0000000..37f7510 --- /dev/null +++ b/Medical/MedicalBusinessLogic/BusinessLogics/AbstractLogic.cs @@ -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 : ILogic + where M : AbstractModel + where V : AbstractViewModel + where S : AbstractSearchModel + { + protected readonly ILogger> _logger; + protected readonly IStorage _storage; + + protected AbstractLogic(ILogger> logger, IStorage 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 ReadList(S? searchModel) + { + return ReadList(searchModel, out _); + } + + public bool Update(M model) + { + return Update(model, out _); + } + + public virtual List 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; + } + } + } +} diff --git a/Medical/MedicalDatabaseContracts/ILogic.cs b/Medical/MedicalDatabaseContracts/ILogic.cs index d8e520b..2b9f298 100644 --- a/Medical/MedicalDatabaseContracts/ILogic.cs +++ b/Medical/MedicalDatabaseContracts/ILogic.cs @@ -9,10 +9,15 @@ namespace MedicalDatabaseContracts where V : AbstractViewModel where S : AbstractSearchModel { - List? ReadList(S? searchModel); + List ReadList(S? searchModel); + List 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); } }