PIbd-22_Karamushko_M_K_Univ.../University/UniversityBuisnessLogic/DisciplineLogic.cs
2023-04-05 20:24:16 +04:00

118 lines
4.0 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 UniversityContracts.BindingModels;
using UniversityContracts.BuisnessLogicContracts;
using UniversityContracts.SearchModels;
using UniversityContracts.StoragesContracts;
using UniversityContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace UniversityBuisnessLogic
{
public class DisciplineLogic : IDisciplineLogic
{
private readonly ILogger _logger;
private readonly IDisciplineStorage _disciplineStorage;
public DisciplineLogic(ILogger<DisciplineLogic> logger, IDisciplineStorage disciplineStorage)
{
_logger = logger;
_disciplineStorage = disciplineStorage;
}
public bool Create(DisciplineBindingModel model)
{
CheckModel(model);
if (_disciplineStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(DisciplineBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_disciplineStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public DisciplineViewModel? ReadElement(DisciplineSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. DisciplineName:{Name}. Id:{Id}", model.Name, model.Id);
var element = _disciplineStorage.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<DisciplineViewModel>? ReadList(DisciplineSearchModel? model)
{
_logger.LogInformation("ReadList. DisciplineName: {Name}. Id:{Id}", model?.Name, model?.Id);
var list = model == null ? _disciplineStorage.GetFullList() : _disciplineStorage.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(DisciplineBindingModel model)
{
CheckModel(model);
if (_disciplineStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(DisciplineBindingModel 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));
}
if (string.IsNullOrEmpty(model.Department))
{
throw new ArgumentNullException("Нет названия кафедры", nameof(model.Name));
}
_logger.LogInformation("Discipline. Name:{Name}. Department:{Department}. Id: {Id}", model.Name, model.Department, model.Id);
var element = _disciplineStorage.GetElement(new DisciplineSearchModel
{
Name = model.Name
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Дисциплина с таким названием уже есть");
}
}
}
}