Rogashova_E.A._CourseWork_H.../Hospital/HospitalBusinessLogic/BusinessLogics/RecipesLogic.cs

176 lines
6.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using HospitalBusinessLogic.MailWorker;
using HospitalContracts.BindingModels;
using HospitalContracts.BusinessLogicsContracts;
using HospitalContracts.SearchModels;
using HospitalContracts.StoragesContracts;
using HospitalContracts.ViewModels;
using HospitalDataModels.Models;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HospitalBusinessLogic.BusinessLogics
{
public class RecipesLogic : IRecipesLogic
{
private readonly ILogger _logger;
private readonly IRecipesStorage _recipesStorage;
private readonly AbstractMailWorker _mailWorker;
private readonly IClientLogic _clientLogic;
public RecipesLogic(ILogger<RecipesLogic> logger, IRecipesStorage recipesStorage, AbstractMailWorker mailWorker, IClientLogic clientLogic)
{
_logger = logger;
_recipesStorage = recipesStorage;
_mailWorker = mailWorker;
_clientLogic = clientLogic;
}
public bool AddProcedures(RecipesSearchModel model, IProceduresModel procedure)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("AddProcedure.Id:{ Id}", model.Id);
var element = _recipesStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("AddProcedure element not found");
return false;
}
_logger.LogInformation("AddProcedure find. Id:{Id}", element.Id);
element.RecipeProcedures[procedure.Id] = procedure;
_recipesStorage.Update(new()
{
Id = element.Id,
Dose = element.Dose,
ModeOfApplication = element.ModeOfApplication,
Date = element.Date,
ClientId = element.ClientId,
SymptomsId = element.SymptomsId,
RecipeProcedures = element.RecipeProcedures
});
return true;
}
public bool Create(RecipesBindingModel model)
{
CheckModel(model);
var result = _recipesStorage.Insert(model);
if (result == null) {
_logger.LogWarning("Insert operation failed");
return false;
}
SendRecipeMessage(result.ClientId, $"Поликлиника \"Вы больны\", Рецепт №{result.Id}", $"Рецепт №{result.Id} созданный {result.Date} с дозировкой {result.Dose} и способом применения {result.ModeOfApplication} добавлен");
return true;
}
public bool Delete(RecipesBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_recipesStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public RecipesViewModel? ReadElement(RecipesSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id:{ Id}", model.Id);
var element = _recipesStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public List<RecipesViewModel>? ReadList(RecipesSearchModel? model)
{
_logger.LogInformation("ReadElement. Id:{ Id}", model?.Id);
var list = model == null ? _recipesStorage.GetFullList() : _recipesStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public bool Update(RecipesBindingModel model)
{
CheckModel(model);
if (_recipesStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(RecipesBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.SymptomsId < 0)
{
throw new ArgumentNullException("Некорректный идентификатор симптома", nameof(model.SymptomsId));
}
if (string.IsNullOrEmpty(model.Dose))
{
throw new ArgumentNullException("В рецепте нет дозировки", nameof(model.Dose));
}
if (string.IsNullOrEmpty(model.ModeOfApplication))
{
throw new ArgumentNullException("Нет способа приема", nameof(model.ModeOfApplication));
}
_logger.LogInformation("Recipes.RecipesId:{Id}.Dose:{ Dose}.ModeOfApplication:{ ModeOfApplication}. MedicineId: { MedicineId}", model.Id, model.Dose, model.ModeOfApplication, model.SymptomsId);
}
private bool SendRecipeMessage(int clientId, string subject, string text)
{
var client = _clientLogic.ReadElement(new() { Id = clientId });
if (client == null)
{
return false;
}
_mailWorker.MailSendAsync(new()
{
MailAddress = client.Email,
Subject = subject,
Text = text
});
return true;
}
}
}