From e0ce7e52cb4012a82f707067b47796969f3ba0d4 Mon Sep 17 00:00:00 2001 From: GokaPek Date: Sun, 28 Apr 2024 13:28:25 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B5=D0=B3=D0=BE=D1=80=20=D0=B1=D0=B8=D0=B7?= =?UTF-8?q?=D0=BD=D0=B5=D1=81=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/DisciplineLogic.cs | 112 ++++++++++++++++ .../BusinessLogics/StatementLogic.cs | 114 ++++++++++++++++ .../BusinessLogics/StudentLogic.cs | 3 +- .../BusinessLogics/TeacherLogic .cs | 123 ++++++++++++++++++ .../IStorekeeperLogic.cs | 20 +++ 5 files changed, 370 insertions(+), 2 deletions(-) create mode 100644 University/UniversityBusinessLogic/BusinessLogics/DisciplineLogic.cs create mode 100644 University/UniversityBusinessLogic/BusinessLogics/StatementLogic.cs create mode 100644 University/UniversityBusinessLogic/BusinessLogics/TeacherLogic .cs create mode 100644 University/UniversityContracts/BusinessLogicsContracts/IStorekeeperLogic.cs diff --git a/University/UniversityBusinessLogic/BusinessLogics/DisciplineLogic.cs b/University/UniversityBusinessLogic/BusinessLogics/DisciplineLogic.cs new file mode 100644 index 0000000..6c62dd7 --- /dev/null +++ b/University/UniversityBusinessLogic/BusinessLogics/DisciplineLogic.cs @@ -0,0 +1,112 @@ +using Microsoft.Extensions.Logging; +using UniversityContracts.BindingModels; +using UniversityContracts.BusinessLogicsContracts; +using UniversityContracts.SearchModels; +using UniversityContracts.StorageContracts; +using UniversityContracts.ViewModels; + +namespace UniversityBusinessLogic.BusinessLogics +{ + partial class DisciplineLogic: IDisciplineLogic + { + private readonly ILogger _logger; + private readonly IDisciplineStorage _disciplineStorage; + public DisciplineLogic(ILogger logger, IDisciplineStorage disciplineStorage) + { + _logger = logger; + _disciplineStorage = disciplineStorage; + } + public List? ReadList(DisciplineSearchModel? model) + { + _logger.LogInformation("ReadList. Name: {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 DisciplineViewModel? ReadElement(DisciplineSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Name:{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 bool Create(DisciplineBindingModel model) + { + CheckModel(model); + if (_disciplineStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(DisciplineBindingModel model) + { + CheckModel(model); + if (_disciplineStorage.Update(model) == null) + { + _logger.LogWarning("Update 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; + } + 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.Description)) + { + throw new ArgumentNullException("Должна быть дисциплина", nameof(model.Description)); + } + _logger.LogInformation("Discipline. Name:{Name}.Description:{Description}. Id: {Id}", + model.Name, model.Description, model.Id); + var element = _disciplineStorage.GetElement(new DisciplineSearchModel + { + Name = model.Name + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Данная дисциплина уже существует"); + } + } + } +} diff --git a/University/UniversityBusinessLogic/BusinessLogics/StatementLogic.cs b/University/UniversityBusinessLogic/BusinessLogics/StatementLogic.cs new file mode 100644 index 0000000..9f4412f --- /dev/null +++ b/University/UniversityBusinessLogic/BusinessLogics/StatementLogic.cs @@ -0,0 +1,114 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UniversityContracts.BindingModels; +using UniversityContracts.BusinessLogicsContracts; +using UniversityContracts.SearchModels; +using UniversityContracts.StorageContracts; +using UniversityContracts.ViewModels; +using UniversityDataModels.Enums; + +namespace UniversityBusinessLogic.BusinessLogics +{ + public class StatementLogic : IStatementLogic + { + private readonly ILogger _logger; + private readonly IStatementStorage _statementStorage; + public StatementLogic(ILogger logger, IStatementStorage + statementStorage) + { + _logger = logger; + _statementStorage = statementStorage; + } + public List? ReadList(StatementSearchModel? model) + { + _logger.LogInformation("ReadList.StatementId:{Id} ", + model?.Id); + var list = model == null ? _statementStorage.GetFullList() : + _statementStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public StatementViewModel? ReadElement(StatementSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement.Id:{Id}", + model.Id); + var element = _statementStorage.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(StatementBindingModel model) + { + CheckModel(model); + + if (_statementStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(StatementBindingModel model) + { + CheckModel(model); + if (_statementStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(StatementBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_statementStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(StatementBindingModel 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 (model.TeacherId <= 0) + { + throw new ArgumentNullException("Некорректный идентификатор преподавателя", nameof(model.TeacherId)); + } + _logger.LogInformation("Statement. StatementId:{Id}.Name:{Name}. TeacherId: { TeacherId}", + model.Id, model.Name, model.TeacherId); + } + } +} diff --git a/University/UniversityBusinessLogic/BusinessLogics/StudentLogic.cs b/University/UniversityBusinessLogic/BusinessLogics/StudentLogic.cs index a331058..129ffbc 100644 --- a/University/UniversityBusinessLogic/BusinessLogics/StudentLogic.cs +++ b/University/UniversityBusinessLogic/BusinessLogics/StudentLogic.cs @@ -12,8 +12,7 @@ namespace UniversityBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IStudentStorage _studentStorage; - public StudentLogic(ILogger logger, IStudentStorage - studentStorage) + public StudentLogic(ILogger logger, IStudentStorage studentStorage) { _logger = logger; _studentStorage = studentStorage; diff --git a/University/UniversityBusinessLogic/BusinessLogics/TeacherLogic .cs b/University/UniversityBusinessLogic/BusinessLogics/TeacherLogic .cs new file mode 100644 index 0000000..15baa7a --- /dev/null +++ b/University/UniversityBusinessLogic/BusinessLogics/TeacherLogic .cs @@ -0,0 +1,123 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UniversityContracts.BindingModels; +using UniversityContracts.BusinessLogicsContracts; +using UniversityContracts.SearchModels; +using UniversityContracts.StorageContracts; +using UniversityContracts.ViewModels; + +namespace UniversityBusinessLogic.BusinessLogics +{ + public class TeacherLogic : ITeacherLogic + { + private readonly ILogger _logger; + private readonly ITeacherStorage _teacherStorage; + public TeacherLogic(ILogger logger, ITeacherStorage + teacherStorage) + { + _logger = logger; + _teacherStorage = teacherStorage; + } + public List? ReadList(TeacherSearchModel? model) + { + _logger.LogInformation("ReadList. Name: {Name}.Id:{Id} ", + model?.Name, 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. Name:{Name}.Id:{Id}", + model.Name, 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.Name)) + { + throw new ArgumentNullException("Нет названия", + nameof(model.Name)); + } + if (string.IsNullOrEmpty(model.AcademicDegree)) + { + throw new ArgumentNullException("Не указана учёная степень", nameof(model.AcademicDegree)); + } + if (string.IsNullOrEmpty(model.Position)) + { + throw new ArgumentNullException("Нет должности", + nameof(model.Position)); + } + _logger.LogInformation("Teacher. Name:{Name}.AcademicDegree:{AcademicDegree}. Position:{Position}. Id: {Id}", + model.Name, model.AcademicDegree, model.Position, model.Id); + var element = _teacherStorage.GetElement(new TeacherSearchModel + { + Name = model.Name + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Данный преподаватель уже существует"); + } + } + } +} diff --git a/University/UniversityContracts/BusinessLogicsContracts/IStorekeeperLogic.cs b/University/UniversityContracts/BusinessLogicsContracts/IStorekeeperLogic.cs new file mode 100644 index 0000000..b051e94 --- /dev/null +++ b/University/UniversityContracts/BusinessLogicsContracts/IStorekeeperLogic.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UniversityContracts.BindingModels; +using UniversityContracts.SearchModels; +using UniversityContracts.ViewModels; + +namespace UniversityContracts.BusinessLogicsContracts +{ + public interface IStorekeeperLogic + { + List? ReadList(StorekeeperSearchModel? model); + StorekeeperViewModel? ReadElement(StorekeeperSearchModel model); + bool Create(StorekeeperBindingModel model); + bool Update(StorekeeperBindingModel model); + bool Delete(StorekeeperBindingModel model); + } +}