127 lines
4.7 KiB
C#
127 lines
4.7 KiB
C#
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.Phone))
|
||
{
|
||
throw new ArgumentNullException("Не задан телефон", nameof(model.Phone));
|
||
}
|
||
if (string.IsNullOrEmpty(model.Name))
|
||
{
|
||
throw new ArgumentNullException("Не задано ФИО", nameof(model.Name));
|
||
}
|
||
_logger.LogInformation("Teacher. Name:{Name}.Phone:{ Phone}. Post{Post}.Login{Login} .Password{Password} Id: {Id}", model.Name, model.Phone, 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("Учитель с таким логином уже есть");
|
||
}
|
||
}
|
||
}
|
||
}
|