From 8540b924da616fc51559c50840a64e68b77f8139 Mon Sep 17 00:00:00 2001 From: MaxKarme <91691525+MaxKarme@users.noreply.github.com> Date: Sat, 8 Apr 2023 16:44:04 +0400 Subject: [PATCH 1/4] delete useless using --- University/UniversityDataModels/Enums/MarkType.cs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/University/UniversityDataModels/Enums/MarkType.cs b/University/UniversityDataModels/Enums/MarkType.cs index 6ad9982..a4180f8 100644 --- a/University/UniversityDataModels/Enums/MarkType.cs +++ b/University/UniversityDataModels/Enums/MarkType.cs @@ -1,10 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace UniversityDataModels.Enums +namespace UniversityDataModels.Enums { public enum MarkType { From a424c0708e0116d729c13020bb12686ad1d9111b Mon Sep 17 00:00:00 2001 From: MaxKarme <91691525+MaxKarme@users.noreply.github.com> Date: Sat, 8 Apr 2023 16:44:15 +0400 Subject: [PATCH 2/4] add user models --- .../BindingModels/UserBindingModel.cs | 14 ++++++++++++++ .../BuisnessLogicContracts/IUserLogic.cs | 15 +++++++++++++++ .../SearchModels/UserSearchModel.cs | 9 +++++++++ .../StoragesContracts/IUserStorage.cs | 16 ++++++++++++++++ .../ViewModels/UserViewModel.cs | 14 ++++++++++++++ .../UniversityDataModels/Models/IUserModel.cs | 18 ++++++++++++++++++ 6 files changed, 86 insertions(+) create mode 100644 University/UniversityContracts/BindingModels/UserBindingModel.cs create mode 100644 University/UniversityContracts/BuisnessLogicContracts/IUserLogic.cs create mode 100644 University/UniversityContracts/SearchModels/UserSearchModel.cs create mode 100644 University/UniversityContracts/StoragesContracts/IUserStorage.cs create mode 100644 University/UniversityContracts/ViewModels/UserViewModel.cs create mode 100644 University/UniversityDataModels/Models/IUserModel.cs diff --git a/University/UniversityContracts/BindingModels/UserBindingModel.cs b/University/UniversityContracts/BindingModels/UserBindingModel.cs new file mode 100644 index 0000000..9b144d7 --- /dev/null +++ b/University/UniversityContracts/BindingModels/UserBindingModel.cs @@ -0,0 +1,14 @@ +using UniversityDataModels.Models; +namespace UniversityContracts.BindingModels +{ + public class UserBindingModel : IUserModel + { + public int Id { get; set; } + public string Name { get; set; } = String.Empty; + public string Surname { get; set; } = String.Empty; + public string PhoneNumber { get; set; } = String.Empty; + public string Position { get; set; } = String.Empty; + public string Login { get; set; } = String.Empty; + public string Password { get; set; } = String.Empty; + } +} diff --git a/University/UniversityContracts/BuisnessLogicContracts/IUserLogic.cs b/University/UniversityContracts/BuisnessLogicContracts/IUserLogic.cs new file mode 100644 index 0000000..218c1df --- /dev/null +++ b/University/UniversityContracts/BuisnessLogicContracts/IUserLogic.cs @@ -0,0 +1,15 @@ +using UniversityContracts.BindingModels; +using UniversityContracts.SearchModels; +using UniversityContracts.ViewModels; + +namespace UniversityContracts.BuisnessLogicContracts +{ + public interface IUserLogic + { + List? ReadList(UserSearchModel? model); + UserViewModel? ReadElement(UserSearchModel model); + bool Create(UserBindingModel model); + bool Update(UserBindingModel model); + bool Delete(UserBindingModel model); + } +} diff --git a/University/UniversityContracts/SearchModels/UserSearchModel.cs b/University/UniversityContracts/SearchModels/UserSearchModel.cs new file mode 100644 index 0000000..83bd13e --- /dev/null +++ b/University/UniversityContracts/SearchModels/UserSearchModel.cs @@ -0,0 +1,9 @@ +namespace UniversityContracts.SearchModels +{ + public class UserSearchModel + { + public int? Id { get; set; } + public string? Login { get; set; } + public string? Password { get; set; } + } +} diff --git a/University/UniversityContracts/StoragesContracts/IUserStorage.cs b/University/UniversityContracts/StoragesContracts/IUserStorage.cs new file mode 100644 index 0000000..574c780 --- /dev/null +++ b/University/UniversityContracts/StoragesContracts/IUserStorage.cs @@ -0,0 +1,16 @@ +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/University/UniversityContracts/ViewModels/UserViewModel.cs b/University/UniversityContracts/ViewModels/UserViewModel.cs new file mode 100644 index 0000000..35afe78 --- /dev/null +++ b/University/UniversityContracts/ViewModels/UserViewModel.cs @@ -0,0 +1,14 @@ +using UniversityDataModels.Models; +namespace UniversityContracts.ViewModels +{ + public class UserViewModel : IUserModel + { + public int Id { get; set; } + public string Name { get; set; } = String.Empty; + public string Surname { get; set; } = String.Empty; + public string PhoneNumber { get; set; } = String.Empty; + public string Position { get; set; } = String.Empty; + public string Login { get; set; } = String.Empty; + public string Password { get; set; } = String.Empty; + } +} diff --git a/University/UniversityDataModels/Models/IUserModel.cs b/University/UniversityDataModels/Models/IUserModel.cs new file mode 100644 index 0000000..a4a74d0 --- /dev/null +++ b/University/UniversityDataModels/Models/IUserModel.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace UniversityDataModels.Models +{ + public interface IUserModel : IId + { + string Name { get; } + string Surname { get; } + string PhoneNumber { get; } + string Position { get; } + string Login { get; } + string Password { get; } + } +} From 7a7a1f2ed3e1f1767fe4f0e3f0de5af23dcaa2ae Mon Sep 17 00:00:00 2001 From: MaxKarme <91691525+MaxKarme@users.noreply.github.com> Date: Sat, 8 Apr 2023 17:38:26 +0400 Subject: [PATCH 3/4] fix --- University/DatabaseImplement/Implements/DisciplineStorage.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/University/DatabaseImplement/Implements/DisciplineStorage.cs b/University/DatabaseImplement/Implements/DisciplineStorage.cs index d5a39ff..de5a49f 100644 --- a/University/DatabaseImplement/Implements/DisciplineStorage.cs +++ b/University/DatabaseImplement/Implements/DisciplineStorage.cs @@ -66,7 +66,7 @@ namespace UniversityDatabaseImplement.Implements var element = context.Disciplines.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { - context.ExaminationResults.Remove(element); + context.Disciplines.Remove(element); context.SaveChanges(); return element.GetViewModel; } From 92cfe7ad61ffb27c55d4fe29e808cb500e2118ac Mon Sep 17 00:00:00 2001 From: MaxKarme <91691525+MaxKarme@users.noreply.github.com> Date: Sat, 8 Apr 2023 17:38:34 +0400 Subject: [PATCH 4/4] add user --- .../Implements/UserStorage.cs | 78 +++++++++++++++++++ University/DatabaseImplement/Models/User.cs | 62 +++++++++++++++ .../DatabaseImplement/UniversityDatabase.cs | 1 + 3 files changed, 141 insertions(+) create mode 100644 University/DatabaseImplement/Implements/UserStorage.cs create mode 100644 University/DatabaseImplement/Models/User.cs diff --git a/University/DatabaseImplement/Implements/UserStorage.cs b/University/DatabaseImplement/Implements/UserStorage.cs new file mode 100644 index 0000000..1e8b534 --- /dev/null +++ b/University/DatabaseImplement/Implements/UserStorage.cs @@ -0,0 +1,78 @@ +using UniversityContracts.BindingModels; +using UniversityContracts.SearchModels; +using UniversityContracts.StoragesContracts; +using UniversityContracts.ViewModels; +using UniversityDatabaseImplement.Models; + +namespace UniversityDatabaseImplement.Implements +{ + internal class UserStorage : IUserStorage + { + public List GetFullList() + { + using var context = new UniversityDatabase(); + return context.Users.Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(UserSearchModel model) + { + using var context = new UniversityDatabase(); + return context.Users + .Where(x => ( + (!model.Id.HasValue || x.Id == model.Id) && + (string.IsNullOrEmpty(model.Login) || x.Login.Contains(model.Login)) && + (string.IsNullOrEmpty(model.Password) || x.Password.Contains(model.Password)) + ) + ) + .Select(x => x.GetViewModel) + .ToList(); + } + public UserViewModel? GetElement(UserSearchModel model) + { + if (string.IsNullOrEmpty(model.Login) && string.IsNullOrEmpty(model.Password) && !model.Id.HasValue) + { + return null; + } + using var context = new UniversityDatabase(); + return context.Users + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Login) && x.Login == model.Login) || + (!string.IsNullOrEmpty(model.Password) && x.Password == model.Password) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public UserViewModel? Insert(UserBindingModel model) + { + var newUser = User.Create(model); + if (newUser == null) + { + return null; + } + using var context = new UniversityDatabase(); + context.Users.Add(newUser); + context.SaveChanges(); + return newUser.GetViewModel; + } + public UserViewModel? Update(UserBindingModel model) + { + using var context = new UniversityDatabase(); + var user = context.Users.FirstOrDefault(x => x.Id == model.Id); + if (user == null) + { + return null; + } + user.Update(model); + context.SaveChanges(); + return user.GetViewModel; + } + public UserViewModel? Delete(UserBindingModel model) + { + using var context = new UniversityDatabase(); + var element = context.Users.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Users.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/University/DatabaseImplement/Models/User.cs b/University/DatabaseImplement/Models/User.cs new file mode 100644 index 0000000..f6090e0 --- /dev/null +++ b/University/DatabaseImplement/Models/User.cs @@ -0,0 +1,62 @@ +using UniversityContracts.BindingModels; +using UniversityContracts.ViewModels; +using UniversityDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace UniversityDatabaseImplement.Models +{ + public class User : IUserModel + { + public int Id { get; set; } + [Required] + public string Name { get; set; } = string.Empty; + [Required] + public string Surname { get; set; } = string.Empty; + [Required] + public string PhoneNumber { get; set; } = string.Empty; + [Required] + public string Position { get; set; } = string.Empty; + [Required] + public string Login { get; set; } = string.Empty; + [Required] + public string Password { get; set; } = string.Empty; + public static User Create(UserBindingModel model) + { + return new User + { + Id = model.Id, + Name = model.Name, + Surname = model.Surname, + PhoneNumber = model.PhoneNumber, + Position = model.Position, + Login = model.Login, + Password = model.Password + }; + } + + public void Update(UserBindingModel model) + { + if (model == null) return; + + Id = model.Id; + Name = model.Name; + Surname = model.Surname; + PhoneNumber = model.PhoneNumber; + Position = model.Position; + Login = model.Login; + Password = model.Password; + } + + public UserViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Surname = Surname, + PhoneNumber = PhoneNumber, + Position = Position, + Login = Login, + Password = Password + }; + } +} diff --git a/University/DatabaseImplement/UniversityDatabase.cs b/University/DatabaseImplement/UniversityDatabase.cs index 3c78a0b..7561a59 100644 --- a/University/DatabaseImplement/UniversityDatabase.cs +++ b/University/DatabaseImplement/UniversityDatabase.cs @@ -23,5 +23,6 @@ namespace UniversityDatabaseImplement public virtual DbSet StatementStudents { set; get; } public virtual DbSet Students { set; get; } public virtual DbSet StudentExaminationResults { set; get; } + public virtual DbSet Users { set; get; } } } \ No newline at end of file