From 02d78da38faad97d92a6f4d661adf13da85c54df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F=20=D0=A4=D0=B5=D0=B4=D0=BE=D1=82?= =?UTF-8?q?=D0=BE=D0=B2?= Date: Mon, 29 Apr 2024 00:44:49 +0400 Subject: [PATCH] fixed 0.5 --- .../DataBase.cs | 30 ++++++------ .../Implements/CategoryProductStorage.cs | 46 ++++++++++--------- .../Models/CategoryProduct.cs | 10 ++-- 3 files changed, 44 insertions(+), 42 deletions(-) diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/DataBase.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/DataBase.cs index 7e3bb96..f018ca4 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/DataBase.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/DataBase.cs @@ -1,27 +1,25 @@ -using Microsoft.EntityFrameworkCore; +using ElectronicsShopDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; using System.Collections.Generic; namespace ElectronicsShopDataBaseImplement { - public class DataBase + public class Database : DbContext { - public class Database : DbContext + protected override void OnConfiguring(DbContextOptionsBuilder + optionsBuilder) { - protected override void OnConfiguring(DbContextOptionsBuilder - optionsBuilder) + if (optionsBuilder.IsConfigured == false) { - if (optionsBuilder.IsConfigured == false) - { - optionsBuilder.UseSqlServer(@"Data Source=.\SQLEXPRESS;Initial Catalog=ZooDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); - } - base.OnConfiguring(optionsBuilder); + optionsBuilder.UseSqlServer(@"Data Source=.\SQLEXPRESS;Initial Catalog=ZooDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } - 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; } - + base.OnConfiguring(optionsBuilder); } + 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; } + } } diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/CategoryProductStorage.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/CategoryProductStorage.cs index c8604d4..ce97435 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/CategoryProductStorage.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Implements/CategoryProductStorage.cs @@ -8,27 +8,17 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; -using static ElectronicsShopDataBaseImplement.DataBase; namespace ElectronicsShopDataBaseImplement.Implements { public class CategoryProductStorage : ICategoryProductStorage { - List ICategoryProductStorage.GetFullList() + public CategoryProductViewModel? Delete(CategoryProductBindingModel model) { - using var context = new Database(); - return context.CategoryProducts.Select(x => x.GetViewModel).ToList(); + throw new NotImplementedException(); } - List ICategoryProductStorage.GetFilteredList(CategoryProductSearchModel model) - { - if (string.IsNullOrEmpty(model.Name)) - { - return new(); - } - using var context = new Database(); - return context.CategoryProducts.Where(x => x.CategoryProducts.Contains(model.Name)).Select(x => x.GetViewModel).ToList(); - } - CategoryProductViewModel? ICategoryProductStorage.GetElement(CategoryProductSearchModel model) + + public CategoryProductViewModel? GetElement(CategoryProductSearchModel model) { if (string.IsNullOrEmpty(model.Name) && !model.ID.HasValue) { @@ -36,9 +26,26 @@ namespace ElectronicsShopDataBaseImplement.Implements } using var context = new Database(); return context.CategoryProducts.FirstOrDefault(x => - (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name && (model.ID.HasValue && x.Id == model.ID)) ) ?.GetViewModel; + (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name && (model.ID.HasValue && x.ID == model.ID)))?.GetViewModel; } - CategoryProductViewModel? ICategoryProductStorage.Insert(CategoryProductBindingModel model) + + public List GetFilteredList(CategoryProductSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + using var context = new Database(); + return context.CategoryProducts.Where(x => x.Name.Contains(model.Name)).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new Database(); + return context.CategoryProducts.Select(x => x.GetViewModel).ToList(); + } + + public CategoryProductViewModel? Insert(CategoryProductBindingModel model) { var newComponent = CategoryProduct.Create(model); if (newComponent == null) @@ -51,13 +58,10 @@ namespace ElectronicsShopDataBaseImplement.Implements return newComponent.GetViewModel; } - CategoryProductViewModel? ICategoryProductStorage.Update(CategoryProductBindingModel model) - { - throw new NotImplementedException(); - } - CategoryProductViewModel? ICategoryProductStorage.Delete(CategoryProductBindingModel model) + public CategoryProductViewModel? Update(CategoryProductBindingModel model) { throw new NotImplementedException(); } } } + diff --git a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/CategoryProduct.cs b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/CategoryProduct.cs index 66be802..89f13aa 100644 --- a/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/CategoryProduct.cs +++ b/ElectronicsShop/ElectronicsShopDataBaseImplement/Models/CategoryProduct.cs @@ -13,12 +13,12 @@ namespace ElectronicsShopDataBaseImplement.Models { public class CategoryProduct : ICategoryProductModel { - public int ID { get; private set; } + public int ID { get; set; } [Required] - public string Name { get; set; }=string.Empty; + public string Name { get; set; } = string.Empty; - public static CategoryProduct? Create(CategoryProductBindingModel model) + public static CategoryProduct? Create(CategoryProductBindingModel? model) { if(model == null) { @@ -30,7 +30,7 @@ namespace ElectronicsShopDataBaseImplement.Models Name = model.Name }; } - public static CategoryProduct Create(CategoryProductViewModel model) + public static CategoryProduct? Create(CategoryProductViewModel model) { return new CategoryProduct { @@ -48,7 +48,7 @@ namespace ElectronicsShopDataBaseImplement.Models model.Name = Name; } - public CategoryProduct GetViewModel => new() + public CategoryProductViewModel GetViewModel => new() { ID = ID, Name = Name