PIbd-32_Artamonova_T.V._COP_2/UniversityBusinessLogic/BusinessLogics/StudentLogic.cs

122 lines
4.4 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 Microsoft.Extensions.Logging;
using UniversityContracts.BindingModels;
using UniversityContracts.BusinessLogicsContracts;
using UniversityContracts.SearchModels;
using UniversityContracts.StoragesContracts;
using UniversityContracts.ViewModels;
namespace UniversityBusinessLogic.BusinessLogics
{
public class StudentLogic : IStudentLogic
{
private readonly ILogger _logger;
private readonly IStudentStorage _studentStorage;
public StudentLogic(ILogger<StudentLogic> logger, IStudentStorage studentStorage)
{
_logger = logger;
_studentStorage = studentStorage;
}
public bool Create(StudentBindingModel model)
{
CheckModel(model);
model.DirectionName = model.DirectionName.Trim();
if (_studentStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(StudentBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_studentStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public StudentViewModel? ReadElement(StudentSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. FIO:{FIO}. Id:{ Id}", model.FIO, model.Id);
var element = _studentStorage.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<StudentViewModel>? ReadList(StudentSearchModel? model)
{
_logger.LogInformation("ReadList. FIO:{FIO}. Id:{ Id}", model?.FIO, model?.Id);
var list = model == null ? _studentStorage.GetFullList() : _studentStorage.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(StudentBindingModel model)
{
CheckModel(model);
if (_studentStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(StudentBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.FIO))
{
throw new ArgumentNullException("Нет ФИО студента", nameof(model.FIO));
}
if (string.IsNullOrEmpty(model.PhotoFilePath))
{
throw new ArgumentNullException("Нет пути к фото", nameof(model.PhotoFilePath));
}
if (string.IsNullOrEmpty(model.Email))
{
throw new ArgumentNullException("Нет электронной почты", nameof(model.Email));
}
if (string.IsNullOrEmpty(model.DirectionName))
{
throw new ArgumentNullException("Нет направления", nameof(model.DirectionName));
}
_logger.LogInformation("Student. FIO:{FIO}. PhotoFilePath:{PhotoFilePath}. Email:{Email}. DirectionName:{DirectionName}. Id: {Id} ", model.FIO, model.PhotoFilePath, model.Email, model.DirectionName, model.Id);
var element = _studentStorage.GetElement(new StudentSearchModel
{
FIO = model.FIO
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Студент с таким ФИО уже есть");
}
}
}
}