From 45205630a5825260c221f288d265158ab41afbed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B3=D0=BE=D1=80=D1=8C=20=D0=93=D0=BE=D1=80=D0=B4?= =?UTF-8?q?=D0=B5=D0=B5=D0=B2?= <89176335310x@gmail.com> Date: Mon, 29 Apr 2024 01:55:20 +0400 Subject: [PATCH] Last fix ProductList --- .../StorageContracts/IProductStorage.cs | 10 +-- .../DataBase.cs | 6 +- .../Implements/CategoryProductStorage.cs | 20 ++++- .../Implements/ProductStorage.cs | 84 +++++++++++++++++ .../Implements/RoleStorage.cs | 82 +++++++++++++++++ .../Implements/UserStorage.cs | 83 +++++++++++++++++ .../Models/CategoryProduct.cs | 3 +- .../Models/Order.cs | 88 ++++++++++++++++++ .../Models/Product.cs | 72 +++++++++++++++ .../Models/Role.cs | 55 ++++++++++++ .../Models/User.cs | 90 +++++++++++++++++++ 11 files changed, 581 insertions(+), 12 deletions(-) create mode 100644 ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/ProductStorage.cs create mode 100644 ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/RoleStorage.cs create mode 100644 ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/UserStorage.cs create mode 100644 ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Order.cs create mode 100644 ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Product.cs create mode 100644 ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Role.cs create mode 100644 ElectronicsShop/ElectronicsShopDataBaseImplement/Models/User.cs diff --git a/ElectronicsShop/ElectronicsShopContracts/StorageContracts/IProductStorage.cs b/ElectronicsShop/ElectronicsShopContracts/StorageContracts/IProductStorage.cs index 3616e74..a4c3d3b 100644 --- a/ElectronicsShop/ElectronicsShopContracts/StorageContracts/IProductStorage.cs +++ b/ElectronicsShop/ElectronicsShopContracts/StorageContracts/IProductStorage.cs @@ -12,10 +12,10 @@ namespace ElectronicsShopContracts.StorageContracts public interface IProductStorage { List GetFullList(); - List GetFilteredList(OrderSearchModel model); - ProductViewModel? GetElement(OrderSearchModel model); - ProductViewModel? Insert(OrderBindingModel model); - ProductViewModel? Update(OrderBindingModel model); - ProductViewModel? Delete(OrderBindingModel model); + List GetFilteredList(ProductSearchModel model); + ProductViewModel? GetElement(ProductSearchModel model); + ProductViewModel? Insert(ProductBindingModel model); + ProductViewModel? Update(ProductBindingModel model); + ProductViewModel? Delete(ProductBindingModel model); } } diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/DataBase.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/DataBase.cs index f018ca4..9bbb6b3 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/DataBase.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/DataBase.cs @@ -17,9 +17,9 @@ namespace ElectronicsShopDataBaseImplement } public virtual DbSet CategoryProducts { set; get; } public virtual DbSet Users { set; get; } - public virtual DbSet Routes { set; get; } - public virtual DbSet RoutePreserves { set; get; } - public virtual DbSet Preserves { set; get; } + public virtual DbSet Roles { set; get; } + public virtual DbSet Products { set; get; } + public virtual DbSet Orders { set; get; } } } diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/CategoryProductStorage.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/CategoryProductStorage.cs index ce97435..b33553a 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/CategoryProductStorage.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/CategoryProductStorage.cs @@ -15,7 +15,15 @@ namespace ElectronicsShopDataBaseImplement.Implements { public CategoryProductViewModel? Delete(CategoryProductBindingModel model) { - throw new NotImplementedException(); + using var context = new Database(); + var element = context.CategoryProducts.FirstOrDefault(rec => rec.ID == model.ID); + if (element != null) + { + context.CategoryProducts.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; } public CategoryProductViewModel? GetElement(CategoryProductSearchModel model) @@ -60,7 +68,15 @@ namespace ElectronicsShopDataBaseImplement.Implements public CategoryProductViewModel? Update(CategoryProductBindingModel model) { - throw new NotImplementedException(); + using var context = new Database(); + var component = context.CategoryProducts.FirstOrDefault(x => x.ID == model.ID); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; } } } diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/ProductStorage.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/ProductStorage.cs new file mode 100644 index 0000000..0b53295 --- /dev/null +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/ProductStorage.cs @@ -0,0 +1,84 @@ +using ElectronicsShopContracts.BindingModels; +using ElectronicsShopContracts.SearchModels; +using ElectronicsShopContracts.StorageContracts; +using ElectronicsShopContracts.ViewModels; +using ElectronicsShopDataBaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ElectronicsShopDataBaseImplement.Implements +{ + public class ProductStorage : IProductStorage + { + public ProductViewModel? Delete(ProductBindingModel model) + { + using var context = new Database(); + var element = context.Products.FirstOrDefault(rec => rec.ID == model.ID); + if (element != null) + { + context.Products.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public ProductViewModel? GetElement(ProductSearchModel model) + { + if (string.IsNullOrEmpty(model.ProductName) && !model.ID.HasValue) + { + return null; + } + using var context = new Database(); + return context.Products.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.ProductName) && (model.ID.HasValue && x.ID == model.ID)))?.GetViewModel; + } + + public List GetFilteredList(ProductSearchModel model) + { + if (string.IsNullOrEmpty(model.ProductName)) + { + return new(); + } + using var context = new Database(); + return context.Products.Where(x => x.ProductName.Contains(model.ProductName)).Select(x => x.GetViewModel).ToList(); + } + + + public List GetFullList() + { + using var context = new Database(); + return context.Products.Select(x => x.GetViewModel).ToList(); + } + + public ProductViewModel? Insert(ProductBindingModel model) + { + var newComponent = Product.Create(model); + if (newComponent == null) + { + return null; + } + using var context = new Database(); + context.Products.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + + + public ProductViewModel? Update(ProductBindingModel model) + { + using var context = new Database(); + var component = context.Products.FirstOrDefault(x => x.ID == model.ID); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + } +} diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/RoleStorage.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/RoleStorage.cs new file mode 100644 index 0000000..9c6c4ad --- /dev/null +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/RoleStorage.cs @@ -0,0 +1,82 @@ +using ElectronicsShopContracts.BindingModels; +using ElectronicsShopContracts.SearchModels; +using ElectronicsShopContracts.StorageContracts; +using ElectronicsShopContracts.ViewModels; +using ElectronicsShopDataBaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ElectronicsShopDataBaseImplement.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) + { + if (string.IsNullOrEmpty(model.Name) && !model.ID.HasValue) + { + return null; + } + using var context = new Database(); + return context.Roles.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name && (model.ID.HasValue && x.ID == model.ID)))?.GetViewModel; + } + + public List GetFilteredList(RoleSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + using var context = new Database(); + return context.Roles.Where(x => x.Name.Contains(model.Name)).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new Database(); + return context.Roles.Select(x => x.GetViewModel).ToList(); + } + + public RoleViewModel? Insert(RoleBindingModel model) + { + var newComponent = Role.Create(model); + if (newComponent == null) + { + return null; + } + using var context = new Database(); + context.Roles.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + + public RoleViewModel? Update(RoleBindingModel model) + { + using var context = new Database(); + var component = context.Roles.FirstOrDefault(x => x.ID == model.ID); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + } +} diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/UserStorage.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/UserStorage.cs new file mode 100644 index 0000000..3db3c96 --- /dev/null +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/UserStorage.cs @@ -0,0 +1,83 @@ +using ElectronicsShopContracts.BindingModels; +using ElectronicsShopContracts.SearchModels; +using ElectronicsShopContracts.StorageContracts; +using ElectronicsShopContracts.ViewModels; +using ElectronicsShopDataBaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ElectronicsShopDataBaseImplement.Implements +{ + public class UserStorage : IUserStorage + { + public UserViewModel? Delete(UserBindingModel model) + { + using var context = new Database(); + var element = context.Users.FirstOrDefault(rec => rec.ID == model.ID); + if (element != null) + { + context.Users.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public UserViewModel? GetElement(UserSearchModel model) + { + if (string.IsNullOrEmpty(model.Login) && !model.ID.HasValue) + { + return null; + } + using var context = new Database(); + return context.Users.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Login) && x.Login == model.Login || + (model.ID.HasValue && x.ID == model.ID)))?.GetViewModel; + } + + public List GetFilteredList(UserSearchModel model) + { + if (string.IsNullOrEmpty(model.Login)) + { + return new(); + } + using var context = new Database(); + return context.Users.Where(x => x.Login.Contains(model.Login)).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new Database(); + return context.Users.Select(x => x.GetViewModel).ToList(); + } + + public UserViewModel? Insert(UserBindingModel model) + { + var newComponent = User.Create(model); + if (newComponent == null) + { + return null; + } + using var context = new Database(); + context.Users.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + + public UserViewModel? Update(UserBindingModel model) + { + using var context = new Database(); + var component = context.Users.FirstOrDefault(x => x.ID == model.ID); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + } +} diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/CategoryProduct.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/CategoryProduct.cs index 89f13aa..3d4bb3a 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/CategoryProduct.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/CategoryProduct.cs @@ -44,8 +44,7 @@ namespace ElectronicsShopDataBaseImplement.Models { return; } - model.ID = ID; - model.Name = Name; + Name = model.Name; } public CategoryProductViewModel GetViewModel => new() diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Order.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Order.cs new file mode 100644 index 0000000..34381ad --- /dev/null +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Order.cs @@ -0,0 +1,88 @@ +using ElectronicsShopContracts.BindingModels; +using ElectronicsShopContracts.ViewModels; +using ElectronicsShopDataModels.Enums; +using ElectronicsShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ElectronicsShopDataBaseImplement.Models +{ + public class Order : IOrderModel + { + public int ID { get; set; } + [Required] + public double Sum { get; set; } + [Required] + public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; + [Required] + public PaymeantOption PaymeantOption { get; set; } = PaymeantOption.Неизвестно; + [Required] + public DateTime DateCreate { get; set; }= DateTime.Now; + [Required] + public DateTime? DateImplemet { get; set; } = null; + [Required] + public Dictionary ProductList { get; set; } = new(); + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + ID = model.ID, + Sum = model.Sum, + Status = model.Status, + PaymeantOption = model.PaymeantOption, + DateCreate = model.DateCreate, + //todo ProductList + }; + } + public static Order? Create(OrderViewModel model) + { + return new Order + { + ID = model.ID, + Sum = model.Sum, + Status = model.Status, + PaymeantOption = model.PaymeantOption, + DateCreate = model.DateCreate, + //todo ProductList + }; + } + public void Update(OrderBindingModel model) + { + if (model == null) + { + return; + } + ID = model.ID; + Sum = model.Sum; + PaymeantOption = model.PaymeantOption; + Status = model.Status; + DateCreate = model.DateCreate; + if (model.DateImplemet != null) + { + DateImplemet = model.DateImplemet; + } + //todo ProductList + } + + public OrderViewModel GetViewModel => new() + { + ID = ID, + Sum = Sum, + PaymeantOption = PaymeantOption, + Status = Status, + DateCreate = DateCreate, + DateImplemet=DateImplemet, + ProductList = ProductList + }; + } +} diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Product.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Product.cs new file mode 100644 index 0000000..b46ce43 --- /dev/null +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Product.cs @@ -0,0 +1,72 @@ +using ElectronicsShopContracts.BindingModels; +using ElectronicsShopContracts.ViewModels; +using ElectronicsShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ElectronicsShopDataBaseImplement.Models +{ + public class Product : IProductModel + { + public int ID { get; set; } + [Required] + public string ProductName { get; set; }=string.Empty; + [Required] + public double Price { get; set; } + [Required] + public int Count { get; set; } + [Required] + public int CategoryID { get; set; } + + public static Product? Create(ProductBindingModel? model) + { + if (model == null) + { + return null; + } + return new Product() + { + ID = model.ID, + ProductName = model.ProductName, + Price = model.Price, + Count = model.Count, + CategoryID = model.CategoryID + }; + } + public static Product? Create(ProductViewModel model) + { + return new Product + { + ID = model.ID, + ProductName = model.ProductName, + Price = model.Price, + Count = model.Count, + CategoryID = model.CategoryID + }; + } + public void Update(ProductBindingModel model) + { + if (model == null) + { + return; + } + ProductName = model.ProductName; + Price = model.Price; + Count = model.Count; + CategoryID = model.CategoryID; + } + + public ProductViewModel GetViewModel => new() + { + ID = ID, + ProductName = ProductName, + Price = Price, + Count = Count, + CategoryID = CategoryID + }; + } +} diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Role.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Role.cs new file mode 100644 index 0000000..6f2e925 --- /dev/null +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/Role.cs @@ -0,0 +1,55 @@ +using ElectronicsShopContracts.BindingModels; +using ElectronicsShopContracts.ViewModels; +using ElectronicsShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ElectronicsShopDataBaseImplement.Models +{ + public class Role : IRoleModel + { + public int ID { get; set; } + + [Required] + public string Name { get; set; } = string.Empty; + + 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/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/User.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/User.cs new file mode 100644 index 0000000..3dcdec3 --- /dev/null +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/User.cs @@ -0,0 +1,90 @@ +using ElectronicsShopContracts.BindingModels; +using ElectronicsShopContracts.ViewModels; +using ElectronicsShopDataModels; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ElectronicsShopDataBaseImplement.Models +{ + public class User : IUserModel + { + public int ID { get; set; } + [Required] + public string FirstName { get; set; }=string.Empty; + [Required] + public string LastName {get; set; }=string.Empty; + [Required] + public string Password { get; set; } = string.Empty; + [Required] + public string PhoneNumber { get; set; } = string.Empty; + [Required] + public string Login { get; set; } = string.Empty; + [Required] + public string Email { get; set; } = string.Empty; + [Required] + public int RoleID { get; set; } + + public static User? Create(UserBindingModel model) + { + if(model == null) + { + return null; + } + return new User() + { + ID = model.ID, + FirstName = model.FirstName, + LastName = model.LastName, + Password = model.Password, + PhoneNumber = model.PhoneNumber, + Login = model.Login, + Email = model.Email, + RoleID = model.RoleID + }; + } + public static User Create(UserViewModel model) + { + return new User + { + ID = model.ID, + FirstName = model.FirstName, + LastName = model.LastName, + Password = model.Password, + PhoneNumber = model.PhoneNumber, + Login = model.Login, + Email = model.Email, + RoleID = model.RoleID + }; + } + + public void Update(UserBindingModel model) + { + if (model == null) + { + return; + } + FirstName = model.FirstName; + LastName = model.LastName; + Password = model.Password; + PhoneNumber = model.PhoneNumber; + Login = model.Login; + Email = model.Email; + RoleID = model.RoleID; + } + public UserViewModel GetViewModel => new() + { + ID = ID, + FirstName = FirstName, + LastName = LastName, + Password = Password, + PhoneNumber = PhoneNumber, + Login = Login, + Email = Email, + RoleID = RoleID + }; + } +}