PIbd-21_MasenkinMS_Coursewo.../Hospital/HospitalBusinessLogics/BusinessLogics/MedicineLogic.cs
2024-04-25 23:50:21 +04:00

176 lines
4.6 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 HospitalContracts.BindingModels;
using HospitalContracts.BusinessLogicsContracts;
using HospitalContracts.SearchModels;
using HospitalContracts.StoragesContracts;
using HospitalContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HospitalBusinessLogics.BusinessLogics
{
/// <summary>
/// Бизнес-логика для сущности "Лекарство"
/// </summary>
public class MedicineLogic : IMedicineLogic
{
/// <summary>
/// Логгер
/// </summary>
private readonly ILogger _logger;
/// <summary>
/// Хранилище
/// </summary>
private readonly IMedicineStorage _medicineStorage;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="logger"></param>
/// <param name="medicineStorage"></param>
public MedicineLogic(ILogger<MedicineLogic> logger, IMedicineStorage medicineStorage)
{
_logger = logger;
_medicineStorage = medicineStorage;
}
/// <summary>
/// Получить список
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public List<MedicineViewModel>? ReadList(MedicineSearchModel? model)
{
_logger.LogInformation("ReadList. Medicine.Id: {Id}. Name: {Name}", model?.Id, model?.Name);
var list = model == null ? _medicineStorage.GetFullList() : _medicineStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList. Returned null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
/// <summary>
/// Получить отдельную запись
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
public MedicineViewModel? ReadElement(MedicineSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Medicine.Id: {Id}. Name: {Name}", model?.Id, model?.Name);
var element = _medicineStorage.GetElement(model!);
if (element == null)
{
_logger.LogWarning("ReadElement. Element not found");
return null;
}
_logger.LogInformation("ReadElement. Find Medicine.Id: {Id}", element?.Id);
return element;
}
/// <summary>
/// Создать запись
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public bool Create(MedicineBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Create. Medicine.Id: {Id}", model.Id);
if (_medicineStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
/// <summary>
/// Изменить запись
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public bool Update(MedicineBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Update. Medicine.Id: {Id}", model.Id);
if (_medicineStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
/// <summary>
/// Удалить запись
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public bool Delete(MedicineBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Medicine.Id: {Id}", model.Id);
if (_medicineStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
/// <summary>
/// Проверка модели
/// </summary>
/// <param name="model"></param>
/// <param name="withParams"></param>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="InvalidOperationException"></exception>
private void CheckModel(MedicineBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Не указано название лекарства", nameof(model.Name));
}
_logger.LogInformation("CheckModel. Medicine.Id: {Id}", model.Id);
var element = _medicineStorage.GetElement(new MedicineSearchModel
{
Name = model.Name
});
if (element != null && !element.Id.Equals(model.Id))
{
throw new InvalidOperationException("Лекарство с таким названием уже существует");
}
}
}
}