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

145 lines
4.7 KiB
C#
Raw Normal View History

using HospitalContracts.BindingModels;
using HospitalContracts.BusinessLogicsContracts;
using HospitalContracts.SearchModels;
using HospitalContracts.StoragesContracts;
using HospitalContracts.ViewModels;
2023-05-20 06:49:33 +04:00
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;
public RecipesLogic(ILogger<RecipesLogic> logger, IRecipesStorage recipesStorage)
{
_logger = logger;
_recipesStorage = recipesStorage;
}
2023-05-20 06:49:33 +04:00
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,
RecipeProcedures = element.RecipeProcedures
});
return true;
}
public bool Create(RecipesBindingModel model)
{
CheckModel(model);
if (_recipesStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
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 (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}", model.Id, model.Dose, model.ModeOfApplication);
}
}
}