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

118 lines
4.2 KiB
C#
Raw Normal View History

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 HospitalBusinessLogic.BusinessLogics
{
public class MedicinesLogic : IMedicinesLogic
{
private readonly ILogger _logger;
private readonly IMedicinesStorage _medicinesStorage;
public MedicinesLogic(ILogger<MedicinesLogic> logger, IMedicinesStorage medicinesStorage)
{
_logger = logger;
_medicinesStorage = medicinesStorage;
}
public bool Create(MedicinesBindingModel model)
{
CheckModel(model);
if (_medicinesStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(MedicinesBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_medicinesStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public MedicinesViewModel? ReadElement(MedicinesSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. MedicinesName:{MedicinesName}. Id:{ Id}", model.MedicinesName, model.Id);
var element = _medicinesStorage.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<MedicinesViewModel>? ReadList(MedicinesSearchModel? model)
{
_logger.LogInformation("ReadList. MedicinesName:{MedicinesName}. Id:{ Id}", model?.MedicinesName, model?.Id);
var list = model == null ? _medicinesStorage.GetFullList() : _medicinesStorage.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(MedicinesBindingModel model)
{
CheckModel(model);
if (_medicinesStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(MedicinesBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.MedicinesName))
{
throw new ArgumentNullException("Нет названия лекарства", nameof(model.MedicinesName));
}
if (string.IsNullOrEmpty(model.Group))
{
throw new ArgumentNullException("Нет группы лекарства", nameof(model.Group));
}
_logger.LogInformation("Medicines. MedicinesName:{MedicinesName}. Group:{ Group}. Id: { Id} ", model.MedicinesName, model.Group, model.Id);
var element = _medicinesStorage.GetElement(new MedicinesSearchModel
{
MedicinesName = model.MedicinesName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Лекарство с таким названием уже есть");
}
}
}
}