PIbd-21_CourseWork_Polyclin.../Polyclinic/PolyclinicBusinessLogic/BusinessLogics/CourseLogic.cs

106 lines
3.5 KiB
C#

using Microsoft.Extensions.Logging;
using PolyclinicContracts.BindingModels;
using PolyclinicContracts.BusinessLogicsContracts;
using PolyclinicContracts.SearchModels;
using PolyclinicContracts.StoragesContracts;
using PolyclinicContracts.ViewModels;
namespace PolyclinicBusinessLogic.BusinessLogics
{
public class CourseLogic : ICourseLogic
{
private ILogger _logger;
private ICourseStorage _courseStorage;
public CourseLogic(ILogger logger, ICourseStorage courseStorage)
{
_logger = logger;
_courseStorage = courseStorage;
}
public bool Create(CourseBindingModel model)
{
CheckModel(model);
if (_courseStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(CourseBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_courseStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public CourseViewModel? ReadElement(CourseSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id:{Id}", model.Id);
var element = _courseStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement. Id:{Id}", element.Id);
return element;
}
public List<CourseViewModel>? ReadList(CourseSearchModel? model)
{
_logger.LogInformation("ReadList. Id:{Id}", model?.Id);
var list = model == null ? _courseStorage.GetFullList() : _courseStorage.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(CourseBindingModel model)
{
CheckModel(model);
if (_courseStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(CourseBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.DaysCount < 1)
{
throw new ArgumentNullException("Количество дней приема должно быть больше нуля", nameof(model.DaysCount));
}
if (model.PillsPerDay < 1)
{
throw new ArgumentNullException("Количество препарата в день должно быть больше нуля", nameof(model.PillsPerDay));
}
_logger.LogInformation("Course. Id: {Id}", model.Id);
}
}
}