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; } + } + +}