From 7d9eedb01b451bda66b3dbeb7b595c7377f70077 Mon Sep 17 00:00:00 2001 From: Danil Markov Date: Fri, 7 Apr 2023 19:09:06 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D1=80=D0=BE=D0=BB=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogics/RoleLogic.cs | 100 ++++++++++++++++++ .../BindingModels/RoleBindingModel.cs | 15 +++ .../BusinessLogicContracts/IRoleLogic.cs | 20 ++++ .../SearchModels/RoleSearchModel.cs | 14 +++ .../SearchModels/StudentSearchModel.cs | 1 + .../StoragesContracts/IRoleStorage.cs | 21 ++++ .../ViewModels/RoleViewModel.cs | 18 ++++ UniversityDataBaseImplemet/Database.cs | 1 + .../Implements/RoleStorage.cs | 89 ++++++++++++++++ .../Implements/StudentStorage.cs | 36 ++++--- UniversityDataBaseImplemet/Models/Role.cs | 58 ++++++++++ UniversityModels/Models/IRoleModel.cs | 13 +++ 12 files changed, 373 insertions(+), 13 deletions(-) create mode 100644 UniversityBusinessLogic/BusinessLogics/RoleLogic.cs create mode 100644 UniversityContracts/BindingModels/RoleBindingModel.cs create mode 100644 UniversityContracts/BusinessLogicContracts/IRoleLogic.cs create mode 100644 UniversityContracts/SearchModels/RoleSearchModel.cs create mode 100644 UniversityContracts/StoragesContracts/IRoleStorage.cs create mode 100644 UniversityContracts/ViewModels/RoleViewModel.cs create mode 100644 UniversityDataBaseImplemet/Implements/RoleStorage.cs create mode 100644 UniversityDataBaseImplemet/Models/Role.cs create mode 100644 UniversityModels/Models/IRoleModel.cs diff --git a/UniversityBusinessLogic/BusinessLogics/RoleLogic.cs b/UniversityBusinessLogic/BusinessLogics/RoleLogic.cs new file mode 100644 index 0000000..ff1e37d --- /dev/null +++ b/UniversityBusinessLogic/BusinessLogics/RoleLogic.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UniversityContracts.BindingModels; +using UniversityContracts.BusinessLogicContracts; +using UniversityContracts.SearchModels; +using UniversityContracts.StoragesContracts; +using UniversityContracts.ViewModels; + +namespace UniversityBusinessLogic.BusinessLogics +{ + public class RoleLogic : IRoleLogic + { + private readonly IRoleStorage _roleStorage; + public RoleLogic(IRoleStorage roleStorage) + { + _roleStorage = roleStorage; + } + public bool Create(RoleBindingModel model) + { + CheckModel(model); + if (_roleStorage.Insert(model) == null) + { + return false; + } + return true; + } + + public bool Delete(RoleBindingModel model) + { + CheckModel(model, false); + if (_roleStorage.Delete(model) == null) + { + return false; + } + return true; + } + + public RoleViewModel? ReadElement(RoleSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + var element = _roleStorage.GetElement(model); + if (element == null) + { + return null; + } + return element; + } + + public List? ReadList(RoleSearchModel? model) + { + var list = model == null ? _roleStorage.GetFullList() : _roleStorage.GetFilteredList(model); + if (list == null) + { + return null; + } + return list; + } + + public bool Update(RoleBindingModel model) + { + CheckModel(model); + if (_roleStorage.Update(model) == null) + { + return false; + } + return true; + } + + private void CheckModel(RoleBindingModel 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)); + } + + var element = _roleStorage.GetElement(new RoleSearchModel + { + Name = model.Name + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Роль с таким названием уже есть"); + } + } + } +} diff --git a/UniversityContracts/BindingModels/RoleBindingModel.cs b/UniversityContracts/BindingModels/RoleBindingModel.cs new file mode 100644 index 0000000..1869bb0 --- /dev/null +++ b/UniversityContracts/BindingModels/RoleBindingModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UniversityModels.Models; + +namespace UniversityContracts.BindingModels +{ + public class RoleBindingModel : IRoleModel + { + public int Id { get; set; } + public string Name { get; set; } = string.Empty; + } +} diff --git a/UniversityContracts/BusinessLogicContracts/IRoleLogic.cs b/UniversityContracts/BusinessLogicContracts/IRoleLogic.cs new file mode 100644 index 0000000..387383b --- /dev/null +++ b/UniversityContracts/BusinessLogicContracts/IRoleLogic.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.BusinessLogicContracts +{ + public interface IRoleLogic + { + List? ReadList(RoleSearchModel? model); + RoleViewModel? ReadElement(RoleSearchModel model); + bool Create(RoleBindingModel model); + bool Update(RoleBindingModel model); + bool Delete(RoleBindingModel model); + } +} diff --git a/UniversityContracts/SearchModels/RoleSearchModel.cs b/UniversityContracts/SearchModels/RoleSearchModel.cs new file mode 100644 index 0000000..6376ff0 --- /dev/null +++ b/UniversityContracts/SearchModels/RoleSearchModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace UniversityContracts.SearchModels +{ + public class RoleSearchModel + { + public string? Name { get; set; } + public int? Id { get; set; } + } +} diff --git a/UniversityContracts/SearchModels/StudentSearchModel.cs b/UniversityContracts/SearchModels/StudentSearchModel.cs index cebac82..8b8b539 100644 --- a/UniversityContracts/SearchModels/StudentSearchModel.cs +++ b/UniversityContracts/SearchModels/StudentSearchModel.cs @@ -13,5 +13,6 @@ namespace UniversityContracts.SearchModels public int? UserId { get; set; } public DateTime? DateFrom { get; set; } public DateTime? DateTo { get; set; } + public bool? Disciplines { get; set; } } } diff --git a/UniversityContracts/StoragesContracts/IRoleStorage.cs b/UniversityContracts/StoragesContracts/IRoleStorage.cs new file mode 100644 index 0000000..805f93c --- /dev/null +++ b/UniversityContracts/StoragesContracts/IRoleStorage.cs @@ -0,0 +1,21 @@ +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.StoragesContracts +{ + public interface IRoleStorage + { + List GetFullList(); + List GetFilteredList(RoleSearchModel model); + RoleViewModel? GetElement(RoleSearchModel model); + RoleViewModel? Insert(RoleBindingModel model); + RoleViewModel? Update(RoleBindingModel model); + RoleViewModel? Delete(RoleBindingModel model); + } +} diff --git a/UniversityContracts/ViewModels/RoleViewModel.cs b/UniversityContracts/ViewModels/RoleViewModel.cs new file mode 100644 index 0000000..ab6c07d --- /dev/null +++ b/UniversityContracts/ViewModels/RoleViewModel.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UniversityModels.Models; + +namespace UniversityContracts.ViewModels +{ + public class RoleViewModel : IRoleModel + { + [DisplayName("Название")] + public string Name { get; set; } = string.Empty; + + public int Id { get; set; } + } +} diff --git a/UniversityDataBaseImplemet/Database.cs b/UniversityDataBaseImplemet/Database.cs index 32d2753..2994cec 100644 --- a/UniversityDataBaseImplemet/Database.cs +++ b/UniversityDataBaseImplemet/Database.cs @@ -22,5 +22,6 @@ namespace UniversityDataBaseImplemet public virtual DbSet EducationStatuses { get; set; } public virtual DbSet Students { get; set; } public virtual DbSet StudentDocuments { get; set; } + public virtual DbSet Roles { get; set; } } } diff --git a/UniversityDataBaseImplemet/Implements/RoleStorage.cs b/UniversityDataBaseImplemet/Implements/RoleStorage.cs new file mode 100644 index 0000000..68a8f0e --- /dev/null +++ b/UniversityDataBaseImplemet/Implements/RoleStorage.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UniversityContracts.BindingModels; +using UniversityContracts.SearchModels; +using UniversityContracts.StoragesContracts; +using UniversityContracts.ViewModels; +using UniversityDataBaseImplemet.Models; + +namespace UniversityDataBaseImplemet.Implements +{ + public class RoleStorage : IRoleStorage + { + public RoleViewModel? Delete(RoleBindingModel model) + { + using var context = new Database(); + var element = context.Roles + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Roles.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public RoleViewModel? GetElement(RoleSearchModel model) + { + using var context = new Database(); + if (model.Id.HasValue) + return context.Roles + .FirstOrDefault(record => record.Id == model.Id) + ?.GetViewModel; + if (!string.IsNullOrEmpty(model.Name)) + return context.Roles + .FirstOrDefault(record => record.Name.Equals(model.Name)) + ?.GetViewModel; + return null; + } + + public List GetFilteredList(RoleSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + using var context = new Database(); + return context.Roles + .Where(record => record.Name.Contains(model.Name)) + .Select(record => record.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new Database(); + return context.Roles.Select(record => record.GetViewModel).ToList(); + } + + public RoleViewModel? Insert(RoleBindingModel model) + { + var newRole = Role.Create(model); + if (newRole == null) + { + return null; + } + using var context = new Database(); + context.Roles.Add(newRole); + context.SaveChanges(); + return newRole.GetViewModel; + } + + public RoleViewModel? Update(RoleBindingModel model) + { + using var context = new Database(); + var client = context.Roles.FirstOrDefault(record => record.Id == model.Id); + if (client == null) + { + return null; + } + client.Update(model); + context.SaveChanges(); + return client.GetViewModel; + } + } +} diff --git a/UniversityDataBaseImplemet/Implements/StudentStorage.cs b/UniversityDataBaseImplemet/Implements/StudentStorage.cs index bc98658..99cee30 100644 --- a/UniversityDataBaseImplemet/Implements/StudentStorage.cs +++ b/UniversityDataBaseImplemet/Implements/StudentStorage.cs @@ -51,28 +51,38 @@ namespace UniversityDataBaseImplemet.Implements .Select(record => record.GetViewModel) .ToList(); } + /*else if (model.Disciplines == true) // для отчета#1 Поставщик + { + return context.Students + .Join(context.StudentStreams, student => student.Id, studentStream => studentStream.StudentId, (student, studentStream) => new { Student = student, StreamId = studentStream }) + .Join(context.Streams, studentStream => studentStream.StreamId, stream => stream.Id, (studentStream, stream) => new { Stream = stream }) + .Join(context.Disciplines, record => record.stream.Id, discipline => discipline.StreamId, (record, discipline) => new { record.student, discipline }) + .GroupBy(sd => sd.student) + .Select(g => new { + Student = g.Key.Name, + Disciplines = g.Select(sd => sd.discipline.Name).ToList() + }) + .ToList(); + }*/ /*else if (model.DateFrom != null && model.DateTo != null) // для отчета#2 Поставщик { return context.Students .Include(record => record.User) .Include(record => record.EducationStatus) - .Join(context.StudentStreams, student => student.Id, studentStream => studentStream.StudentId, (student, studentStream) => new { Student = student, StreamId = studentStream.StreamId }) - .Join(context.Streams, studentStream => studentStream.StreamId, stream => stream.Id, (studentStream, stream) => new { Student = studentStream.Student, Stream = stream }) - .GroupBy(record => record.Stream) + .Join(context.StudentStreams, + student => student.Id, + studentStream => studentStream.StudentId, + (student, studentStream) => new { Student = student, StreamId = studentStream }) + .Join(context.Streams, + studentStream => studentStream.StreamId, + stream => stream.Id, + (studentStream, stream) => new { Stream = stream }) .Select(result => new { - Stream = result.Key, + Stream = result.Stream, Students = result.Select(record => (record.Student, record.EducationStatus)).ToList() }) - .ToList(); - var result = context.Students - .Include(student => student.User) - .Include(student => student.StudentStreams) - .ThenInclude(studentStream => studentStream.Stream) - .ThenInclude(stream => stream.Disciplines) - .SelectMany(student => student.StudentStreams, (student, studentStream) => new { Student = student, Stream = studentStream.Stream }) - .Select(record => new { StudentName = record.Student.User.Name + " " + record.Student.User.Surname, Discipline = record.Stream.Disciplines }) - .ToList(); + .ToList(); }*/ else { diff --git a/UniversityDataBaseImplemet/Models/Role.cs b/UniversityDataBaseImplemet/Models/Role.cs new file mode 100644 index 0000000..d96102d --- /dev/null +++ b/UniversityDataBaseImplemet/Models/Role.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UniversityContracts.BindingModels; +using UniversityContracts.ViewModels; +using UniversityModels.Models; + +namespace UniversityDataBaseImplemet.Models +{ + public class Role : IRoleModel + { + [Required] + public string Name { get; set; } = string.Empty; + public int Id { get; set; } + + [ForeignKey("RoleId")] + public virtual List Users { get; set; } = new(); + + public static Role? Create(RoleBindingModel model) + { + if (model == null) + { + return null; + } + return new Role() + { + Id = model.Id, + Name = model.Name + }; + } + public static Role Create(RoleViewModel model) + { + return new Role + { + Id = model.Id, + Name = model.Name + }; + } + public void Update(RoleBindingModel model) + { + if (model == null) + { + return; + } + Name = model.Name; + + } + public RoleViewModel GetViewModel => new() + { + Id = Id, + Name = Name + }; + } +} diff --git a/UniversityModels/Models/IRoleModel.cs b/UniversityModels/Models/IRoleModel.cs new file mode 100644 index 0000000..cdf6be0 --- /dev/null +++ b/UniversityModels/Models/IRoleModel.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace UniversityModels.Models +{ + public interface IRoleModel : IId + { + string Name { get; } + } +}