using Microsoft.Extensions.Logging; using HospitalContracts.BindingModels; using HospitalContracts.BusinessLogicsContracts; using HospitalContracts.SearchModels; using HospitalContracts.StoragesContracts; using HospitalContracts.ViewModels; namespace HospitalBusinessLogic.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? 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.Days < 1) { throw new ArgumentNullException("Количество дней приема должно быть не меньше 1", nameof(model.Days)); } if (model.PillsInDay < 1) { throw new ArgumentNullException("Количество препарата в день должно быть не меньше 1", nameof(model.PillsInDay)); } _logger.LogInformation("Course. Id: {Id}", model.Id); } } }