using StudentEnrollmentContracts.BusinessLogicContracts; using StudentEnrollmentContracts.StorageContracts; using StudentEnrollmentContracts.ViewModels; using StudentEnrollmentContracts.BindingModels; using StudentEnrollmentContracts.SearchModels; using Microsoft.Extensions.Logging; namespace StudentEnrollmentBusinessLogic { public class FacultyLogic : IFacultyLogic { private readonly IFacultyStorage _facultyStorage; private readonly ILogger _logger; public FacultyLogic(IFacultyStorage facultyStorage, ILogger logger) { _facultyStorage = facultyStorage; _logger = logger; } public List? ReadList(FacultySearchModel? model) { _logger.LogInformation("ReadList. FacultyName:{FacultyName}.Id:{ Id}", model?.name, model?.faculty_id); var list = model == null ? _facultyStorage.GetFullList() : _facultyStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } public FacultyViewModel? ReadElement(FacultySearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. FacultyName:{FacultyName}.Id:{ Id}", model.name, model.faculty_id); var element = _facultyStorage.GetElement(model); if (element == null) { _logger.LogWarning("ReadElement element not found"); return null; } _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); return element; } public bool Create(FacultyBindingModel model) { CheckModel(model); if (_facultyStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Update(FacultyBindingModel model) { CheckModel(model); if (_facultyStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public bool Delete(FacultyBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id:{Id}", model.Id); if (_facultyStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } private void CheckModel(FacultyBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.FacultyName)) { throw new ArgumentNullException("Нет названия факультета!", nameof(model.FacultyName)); } _logger.LogInformation("Faculty. FacultyName:{FacultyName}. Id: { Id}", model.FacultyName, model.Id); var element = _facultyStorage.GetElement(new FacultySearchModel { name = model.FacultyName, }); ; if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Факультет с таким же названием уже есть"); } } } }