StudentEnrollmentNew/StudentEnrollment/StudentEnrollmentBusinessLogic/FacultyLogic.cs

109 lines
3.0 KiB
C#
Raw Normal View History

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;
2024-05-06 20:26:16 +04:00
public FacultyLogic(IFacultyStorage facultyStorage, ILogger<FacultyLogic> logger)
{
_facultyStorage = facultyStorage;
_logger = logger;
}
public List<FacultyViewModel>? ReadList(FacultySearchModel? model)
{
2024-05-06 20:26:16 +04:00
_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));
}
2024-05-06 20:26:16 +04:00
_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
{
2024-05-06 20:26:16 +04:00
name = model.FacultyName,
}); ;
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Факультет с таким же названием уже есть");
}
}
}
}