From 1c94f49c82781df0b005207793dd5d0f32c062c1 Mon Sep 17 00:00:00 2001 From: ker73rus Date: Fri, 7 Apr 2023 19:22:59 +0400 Subject: [PATCH 1/2] UserAdded --- .../BusinessLogics/UserLogic.cs | 18 +++ .../BindingModels/UserBindingModel.cs | 17 +++ .../BusinessLogicContracts/IUserLogic.cs | 20 ++++ .../SearchModels/UserSearchModel.cs | 16 +++ .../StoragesContracts/IUserStorage.cs | 21 ++++ .../ViewModels/UserViewModel.cs | 19 +++ UniversityDataBaseImplemet/Database.cs | 1 + .../Implements/UserStorage.cs | 113 ++++++++++++++++++ UniversityDataBaseImplemet/Models/User.cs | 46 +++++++ UniversityModels/Models/IUserModel.cs | 16 +++ 10 files changed, 287 insertions(+) create mode 100644 UniversityBusinessLogic/BusinessLogics/UserLogic.cs create mode 100644 UniversityContracts/BindingModels/UserBindingModel.cs create mode 100644 UniversityContracts/BusinessLogicContracts/IUserLogic.cs create mode 100644 UniversityContracts/SearchModels/UserSearchModel.cs create mode 100644 UniversityContracts/StoragesContracts/IUserStorage.cs create mode 100644 UniversityContracts/ViewModels/UserViewModel.cs create mode 100644 UniversityDataBaseImplemet/Implements/UserStorage.cs create mode 100644 UniversityDataBaseImplemet/Models/User.cs create mode 100644 UniversityModels/Models/IUserModel.cs diff --git a/UniversityBusinessLogic/BusinessLogics/UserLogic.cs b/UniversityBusinessLogic/BusinessLogics/UserLogic.cs new file mode 100644 index 0000000..866aa64 --- /dev/null +++ b/UniversityBusinessLogic/BusinessLogics/UserLogic.cs @@ -0,0 +1,18 @@ +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 UserLogic : IUserLogic + { + + } +} diff --git a/UniversityContracts/BindingModels/UserBindingModel.cs b/UniversityContracts/BindingModels/UserBindingModel.cs new file mode 100644 index 0000000..f03f5e3 --- /dev/null +++ b/UniversityContracts/BindingModels/UserBindingModel.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UniversityModels.Models; + +namespace UniversityContracts.BindingModels +{ + public class UserBindingModel : IUserModel + { + public int Id { get; set; } + public string Login { get; set; } = string.Empty; + public string Password { get; set; } = string.Empty; + public int RoleId { get; set; } + } +} diff --git a/UniversityContracts/BusinessLogicContracts/IUserLogic.cs b/UniversityContracts/BusinessLogicContracts/IUserLogic.cs new file mode 100644 index 0000000..ade9886 --- /dev/null +++ b/UniversityContracts/BusinessLogicContracts/IUserLogic.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 IUserLogic + { + bool Create(UserBindingModel model); + bool Update(UserBindingModel model); + bool Delete(UserBindingModel model); + List? ReadList(UserSearchModel? model); + UserViewModel? ReadElement(UserSearchModel model); + } +} diff --git a/UniversityContracts/SearchModels/UserSearchModel.cs b/UniversityContracts/SearchModels/UserSearchModel.cs new file mode 100644 index 0000000..05d95e7 --- /dev/null +++ b/UniversityContracts/SearchModels/UserSearchModel.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace UniversityContracts.SearchModels +{ + public class UserSearchModel + { + public int? Id { get; set; } + public string? Login { get; set; } + public DateTime? DateFrom { get; set; } + public DateTime? DateTo { get; set; } + } +} diff --git a/UniversityContracts/StoragesContracts/IUserStorage.cs b/UniversityContracts/StoragesContracts/IUserStorage.cs new file mode 100644 index 0000000..02438fa --- /dev/null +++ b/UniversityContracts/StoragesContracts/IUserStorage.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 IUserStorage + { + List GetFullList(); + List GetFilteredList(UserSearchModel model); + UserViewModel? GetElement(UserSearchModel model); + UserViewModel? Insert(UserBindingModel model); + UserViewModel? Update(UserBindingModel model); + UserViewModel? Delete(UserBindingModel model); + } +} diff --git a/UniversityContracts/ViewModels/UserViewModel.cs b/UniversityContracts/ViewModels/UserViewModel.cs new file mode 100644 index 0000000..a5d96fc --- /dev/null +++ b/UniversityContracts/ViewModels/UserViewModel.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace UniversityContracts.ViewModels +{ + public class UserViewModel + { + public int Id { get; set; } + public int RoleId { get; set; } + [DisplayName("Логин")] + public string Login { get; set; } = string.Empty; + [DisplayName("Пароль")] + public string Password { get; set; } = string.Empty; + } +} diff --git a/UniversityDataBaseImplemet/Database.cs b/UniversityDataBaseImplemet/Database.cs index 32d2753..879921e 100644 --- a/UniversityDataBaseImplemet/Database.cs +++ b/UniversityDataBaseImplemet/Database.cs @@ -18,6 +18,7 @@ namespace UniversityDataBaseImplemet } base.OnConfiguring(optionsBuilder); } + public virtual DbSet User { get; set; } public virtual DbSet Documents { get; set; } public virtual DbSet EducationStatuses { get; set; } public virtual DbSet Students { get; set; } diff --git a/UniversityDataBaseImplemet/Implements/UserStorage.cs b/UniversityDataBaseImplemet/Implements/UserStorage.cs new file mode 100644 index 0000000..d33aeb1 --- /dev/null +++ b/UniversityDataBaseImplemet/Implements/UserStorage.cs @@ -0,0 +1,113 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Security.Cryptography.X509Certificates; +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 UserStorage : IUserStorage + { + public UserViewModel? Delete(UserBindingModel model) + { + using var context = new Database(); + var user = context.User + .Include(record => record.Login) + .Include(record => record.RoleId) + .FirstOrDefault(record => record.Id.Equals(model.Id)); + if (user == null) + { + return null; + } + context.User.Remove(user); + context.SaveChanges(); + return user.GetViewModel; + } + + public UserViewModel? GetElement(UserSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new Database(); + return context.User + .Include(record => record.Login) + .Include(record => record.Id) + .FirstOrDefault(record => record.Id.Equals(model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(UserSearchModel model) + { + using var context = new Database(); + if (model.Id.HasValue) + { + return context.User + .Include(record => record.Login) + .Include(record => record.RoleId) + .Where(record => record.Id.Equals(model.Id)) + .Select(record => record.GetViewModel) + .ToList(); + } + else return new(); + } + + public List GetFullList() + { + using var context = new Database(); + return context.User + .Include(record => record.Login) + .Include(record => record.RoleId) + .Select(record => record.GetViewModel) + .ToList(); + } + + public UserViewModel? Insert(UserBindingModel model) + { + var newUser = User.Create(model); + if (newUser == null) + { + return null; + } + using var context = new Database(); + context.User.Add(newUser); + context.SaveChanges(); + return newUser.GetViewModel; + } + + public UserViewModel? Update(UserBindingModel model) + { + using var context = new Database(); + using var transaction = context.Database.BeginTransaction(); + try + { + var user = context.User + .Include(record => record.Login) + .Include(record => record.RoleId) + .FirstOrDefault(record => record.Id.Equals(model.Id)); + if (user == null) + { + return null; + } + user.Update(model); + context.SaveChanges(); + transaction.Commit(); + return user.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +} diff --git a/UniversityDataBaseImplemet/Models/User.cs b/UniversityDataBaseImplemet/Models/User.cs new file mode 100644 index 0000000..65692ef --- /dev/null +++ b/UniversityDataBaseImplemet/Models/User.cs @@ -0,0 +1,46 @@ +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; + +namespace UniversityDataBaseImplemet.Models +{ + public class User + { + public int Id { get; private set; } + [Required] + public string Login { get; set; } = string.Empty; + [Required] + public string Password { get; set; } = string.Empty; + [Required] + public int RoleId { get; set; } + + public static User Create(UserBindingModel model) + { + return new User() + { + Id = model.Id, + Login = model.Login, + Password = model.Password, + RoleId = model.RoleId + }; + } + public void Update(UserBindingModel model) + { + Password = model.Password; + RoleId = model.RoleId; + } + public UserViewModel GetViewModel => new() + { + Id = Id, + Login = Login, + Password = Password, + RoleId = RoleId + }; + } +} diff --git a/UniversityModels/Models/IUserModel.cs b/UniversityModels/Models/IUserModel.cs new file mode 100644 index 0000000..09d49dd --- /dev/null +++ b/UniversityModels/Models/IUserModel.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace UniversityModels.Models +{ + public interface IUserModel + { + string Login { get; } + string Password { get; } + int RoleId { get; } + } + +} -- 2.25.1 From 5fcd7a7018f8fc2d44998479e745d731f72cae1e Mon Sep 17 00:00:00 2001 From: ker73rus Date: Fri, 7 Apr 2023 19:30:31 +0400 Subject: [PATCH 2/2] fix --- .../BusinessLogics/UserLogic.cs | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/UniversityBusinessLogic/BusinessLogics/UserLogic.cs b/UniversityBusinessLogic/BusinessLogics/UserLogic.cs index 866aa64..8b522ea 100644 --- a/UniversityBusinessLogic/BusinessLogics/UserLogic.cs +++ b/UniversityBusinessLogic/BusinessLogics/UserLogic.cs @@ -13,6 +13,85 @@ namespace UniversityBusinessLogic.BusinessLogics { public class UserLogic : IUserLogic { + private readonly IUserStorage _userStorage; + public UserLogic(IUserStorage userStorage) + { + _userStorage = userStorage; + } + public bool Create(UserBindingModel model) + { + CheckModel(model); + if (_userStorage.Insert(model) == null) + { + return false; + } + return true; + } + + public bool Delete(UserBindingModel model) + { + CheckModel(model, false); + if (_userStorage.Delete(model) == null) + { + return false; + } + return true; + } + + public UserViewModel? ReadElement(UserSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + var user = _userStorage.GetElement(model); + if (user == null) + { + return null; + } + return user; + } + + public List? ReadList(UserSearchModel? model) + { + var list = model == null ? _userStorage.GetFullList() : _userStorage.GetFilteredList(model); + if (list == null) + { + return null; + } + return list; + } + + public bool Update(UserBindingModel model) + { + CheckModel(model, false); + if (_userStorage.Update(model) == null) + { + return false; + } + return true; + } + private void CheckModel(UserBindingModel 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)); + } + + var user = _userStorage.GetElement(new UserSearchModel{Login = model.Login}); + if (user != null) + { + throw new InvalidOperationException("Пользователь с таким логином уже есть"); + } + } } } -- 2.25.1