CourseWork_SchoolStudyAgain/SchoolAgainStudy/SchoolAgainStudyBusinessLogic/BusinessLogic/TeacherLogic.cs
2023-05-17 21:29:39 +04:00

127 lines
4.7 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 SchoolAgainStudyBusinessLogic.Новая_папка;
using SchoolAgainStudyContracts.BindingModel;
using SchoolAgainStudyContracts.BusinessLogicContracts;
using SchoolAgainStudyContracts.SearchModel;
using SchoolAgainStudyContracts.StorageContracts;
using SchoolAgainStudyContracts.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SchoolAgainStudyBusinessLogic.BusinessLogic
{
public class TeacherLogic : ITeacherLogic
{
private readonly ILogger _logger;
private readonly ITeacherStorage _teacherStorage;
public TeacherLogic(ILogger<TeacherLogic> logger, ITeacherStorage teacherStorage)
{
_logger = logger;
_teacherStorage = teacherStorage;
}
public List<TeacherViewModel>? ReadList(TeacherSearchModel? model)
{
_logger.LogInformation("ReadList. Login:{Login}.Id:{ Id}", model?.Login, model?.Id);
var list = model == null ? _teacherStorage.GetFullList() : _teacherStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public TeacherViewModel? ReadElement(TeacherSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Login:{Login}. Id:{ Id}", model.Login, model.Id);
var element = _teacherStorage.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(TeacherBindingModel model)
{
CheckModel(model);
if (_teacherStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(TeacherBindingModel model)
{
CheckModel(model);
if (_teacherStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(TeacherBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_teacherStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(TeacherBindingModel model, bool withParams =
true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Login))
{
throw new ArgumentNullException("Не задан логин", nameof(model.Login));
}
if (string.IsNullOrEmpty(model.Password))
{
throw new ArgumentNullException("Не задан пароль", nameof(model.Password));
}
if (string.IsNullOrEmpty(model.Post))
{
throw new ArgumentNullException("Не задана должность", nameof(model.Post));
}
if (string.IsNullOrEmpty(model.Email))
{
throw new ArgumentNullException("Не задана почта", nameof(model.Email));
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Не задано ФИО", nameof(model.Name));
}
_logger.LogInformation("Teacher. Name:{Name}.Email:{ Email}. Post{Post}.Login{Login} .Password{Password} Id: {Id}", model.Name, model.Email, model.Post, model.Login, model.Password, model.Id);
var element = _teacherStorage.GetElement(new TeacherSearchModel
{
Login = model.Login
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Учитель с таким логином уже есть");
}
}
}
}