From c7bcc084197a122fbd6de9ee08e45f1a8edb50f7 Mon Sep 17 00:00:00 2001 From: VictoriaPresnyakova Date: Sun, 5 Mar 2023 18:39:34 +0400 Subject: [PATCH 1/5] db models --- JewelryStore/JewelryStore.csproj | 4 + JewelryStore/JewelryStore.sln | 8 +- .../JewelryStoreBusinessLogic.csproj | 4 + .../JewelryStoreDataBase.cs | 12 ++ .../JewelryStoreDatabaseImplement.csproj | 27 +++++ .../Models/Component.cs | 61 ++++++++++ JewelryStoreDatabaseImplement/Models/Jewel.cs | 106 ++++++++++++++++++ .../Models/JewelComponent.cs | 26 +++++ JewelryStoreDatabaseImplement/Models/Order.cs | 87 ++++++++++++++ 9 files changed, 334 insertions(+), 1 deletion(-) create mode 100644 JewelryStoreDatabaseImplement/JewelryStoreDataBase.cs create mode 100644 JewelryStoreDatabaseImplement/JewelryStoreDatabaseImplement.csproj create mode 100644 JewelryStoreDatabaseImplement/Models/Component.cs create mode 100644 JewelryStoreDatabaseImplement/Models/Jewel.cs create mode 100644 JewelryStoreDatabaseImplement/Models/JewelComponent.cs create mode 100644 JewelryStoreDatabaseImplement/Models/Order.cs diff --git a/JewelryStore/JewelryStore.csproj b/JewelryStore/JewelryStore.csproj index 8a1c665..67eeabd 100644 --- a/JewelryStore/JewelryStore.csproj +++ b/JewelryStore/JewelryStore.csproj @@ -14,6 +14,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/JewelryStore/JewelryStore.sln b/JewelryStore/JewelryStore.sln index 478a62b..6ca5868 100644 --- a/JewelryStore/JewelryStore.sln +++ b/JewelryStore/JewelryStore.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewelryStoreBusinessLogic", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewelryStoreListImplement", "..\JewelryStoreListImplement\JewelryStoreListImplement.csproj", "{B8C8AA30-FC16-4331-B456-CD16C3C97B25}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JewelryStoreFileImplement", "..\JewelryStoreFileImplement\JewelryStoreFileImplement.csproj", "{2906CD1C-4D78-4329-A2E9-8B67E624DD92}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JewelryStoreFileImplement", "..\JewelryStoreFileImplement\JewelryStoreFileImplement.csproj", "{2906CD1C-4D78-4329-A2E9-8B67E624DD92}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JewelryStoreDatabaseImplement", "..\JewelryStoreDatabaseImplement\JewelryStoreDatabaseImplement.csproj", "{672057E7-DCAE-4B6B-A3EF-39690E577C9D}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {2906CD1C-4D78-4329-A2E9-8B67E624DD92}.Debug|Any CPU.Build.0 = Debug|Any CPU {2906CD1C-4D78-4329-A2E9-8B67E624DD92}.Release|Any CPU.ActiveCfg = Release|Any CPU {2906CD1C-4D78-4329-A2E9-8B67E624DD92}.Release|Any CPU.Build.0 = Release|Any CPU + {672057E7-DCAE-4B6B-A3EF-39690E577C9D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {672057E7-DCAE-4B6B-A3EF-39690E577C9D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {672057E7-DCAE-4B6B-A3EF-39690E577C9D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {672057E7-DCAE-4B6B-A3EF-39690E577C9D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/JewelryStoreBusinessLogic/JewelryStoreBusinessLogic.csproj b/JewelryStoreBusinessLogic/JewelryStoreBusinessLogic.csproj index d6db721..2dcdca3 100644 --- a/JewelryStoreBusinessLogic/JewelryStoreBusinessLogic.csproj +++ b/JewelryStoreBusinessLogic/JewelryStoreBusinessLogic.csproj @@ -10,6 +10,10 @@ + + + + diff --git a/JewelryStoreDatabaseImplement/JewelryStoreDataBase.cs b/JewelryStoreDatabaseImplement/JewelryStoreDataBase.cs new file mode 100644 index 0000000..f21bf9c --- /dev/null +++ b/JewelryStoreDatabaseImplement/JewelryStoreDataBase.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreDatabaseImplement +{ + public class JewelryStoreDataBase + { + } +} diff --git a/JewelryStoreDatabaseImplement/JewelryStoreDatabaseImplement.csproj b/JewelryStoreDatabaseImplement/JewelryStoreDatabaseImplement.csproj new file mode 100644 index 0000000..2cb019c --- /dev/null +++ b/JewelryStoreDatabaseImplement/JewelryStoreDatabaseImplement.csproj @@ -0,0 +1,27 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + diff --git a/JewelryStoreDatabaseImplement/Models/Component.cs b/JewelryStoreDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..3e9d653 --- /dev/null +++ b/JewelryStoreDatabaseImplement/Models/Component.cs @@ -0,0 +1,61 @@ +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.ViewModels; +using JewelryStoreDatabaseImplement.Models; +using JewelryStoreDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace JewelryStoreDatabaseImplement.Models +{ + public class Component : IComponentModel + { + public int Id { get; private set; } + + [Required] + public string ComponentName { get; private set; } = string.Empty; + + [Required] + public double Cost { get; set; } + + [ForeignKey("ComponentId")] + public virtual List JewelComponents { get; set; } = new(); + + public static Component? Create(ComponentBindingModel model) + { + if (model == null) + { + return null; + } + return new Component() + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public static Component Create(ComponentViewModel model) + { + return new Component + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public void Update(ComponentBindingModel model) + { + if (model == null) + { + return; + } + ComponentName = model.ComponentName; + Cost = model.Cost; + } + public ComponentViewModel GetViewModel => new() + { + Id = Id, + ComponentName = ComponentName, + Cost = Cost + }; + } +} \ No newline at end of file diff --git a/JewelryStoreDatabaseImplement/Models/Jewel.cs b/JewelryStoreDatabaseImplement/Models/Jewel.cs new file mode 100644 index 0000000..0d01025 --- /dev/null +++ b/JewelryStoreDatabaseImplement/Models/Jewel.cs @@ -0,0 +1,106 @@ +using JewelryStoreDataModels.Models; +using JewelryStoreContracts.ViewModels; +using JewelryStoreContracts.BindingModels; +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; + +namespace JewelryStoreDatabaseImplement.Models +{ + public class Jewel : IJewelModel + { + public int Id { get; set; } + + [Required] + public string JewelName { get; set; } = string.Empty; + + [Required] + public double Price { get; set; } + + private Dictionary? _jewelComponents = null; + + [NotMapped] + public Dictionary JewelComponents + { + get + { + if (_jewelComponents == null) + { + _jewelComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count)); + } + return _jewelComponents; + } + } + + [ForeignKey("JewelId")] + public virtual List Components { get; set; } = new(); + + [ForeignKey("JewelId")] + public virtual List Orders { get; set; } = new(); + + public static Jewel Create(JewelryStoreDataBase context, JewelBindingModel model) + { + return new Jewel() + { + Id = model.Id, + JewelName = model.JewelName, + Price = model.Price, + Components = model.JewelComponents.Select(x => new JewelComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(JewelBindingModel model) + { + JewelName = model.JewelName; + Price = model.Price; + } + + public JewelViewModel GetViewModel => new() + { + Id = Id, + JewelName = JewelName, + Price = Price, + JewelComponents = JewelComponents + }; + + public void UpdateComponents(JewelryStoreDataBase context, JewelBindingModel model) + { + var jewelComponents = context.JewelComponents.Where(rec => rec.JewelId == model.Id).ToList(); + + if (jewelComponents != null && jewelComponents.Count > 0) + { // удалили те, которых нет в модели + context.JewelComponents.RemoveRange(jewelComponents.Where(rec => !model.JewelComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in jewelComponents) + { + updateComponent.Count = model.JewelComponents[updateComponent.ComponentId].Item2; + model.JewelComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + + var jewel = context.Jewels.First(x => x.Id == Id); + + foreach (var pc in model.JewelComponents) + { + context.JewelComponents.Add(new JewelComponent + { + Jewel = jewel, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _jewelComponents = null; + } + } +} diff --git a/JewelryStoreDatabaseImplement/Models/JewelComponent.cs b/JewelryStoreDatabaseImplement/Models/JewelComponent.cs new file mode 100644 index 0000000..79fa697 --- /dev/null +++ b/JewelryStoreDatabaseImplement/Models/JewelComponent.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreDatabaseImplement.Models +{ + public class JewelComponent + { + public int Id { get; set; } + + [Required] + public int JewelId { get; set; } + + [Required] + public int ComponentId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Component Component { get; set; } = new(); + public virtual Jewel Jewel { get; set; } = new(); + } +} diff --git a/JewelryStoreDatabaseImplement/Models/Order.cs b/JewelryStoreDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..0800ca7 --- /dev/null +++ b/JewelryStoreDatabaseImplement/Models/Order.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using JewelryStoreContracts.ViewModels; +using JewelryStoreContracts.BindingModels; +using JewelryStoreDataModels.Models; +using JewelryStoreDataModels.Enums; +using System.ComponentModel.DataAnnotations; + + +namespace JewelryStoreDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + + public int JewelId { get; private set; } + + public string JewelName { get; private set; } = string.Empty; + + [Required] + public int Count { get; private set; } + + [Required] + public double Sum { get; private set; } + + [Required] + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + + [Required] + public DateTime DateCreate { get; private set; } = DateTime.Now; + + public DateTime? DateImplement { get; private set; } + + public virtual Jewel Jewel { get; set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + + return new Order() + { + Id = model.Id, + JewelId = model.JewelId, + JewelName = model.JewelName, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + + JewelId = model.JewelId; + JewelName = model.JewelName; + Count = model.Count; + Sum = model.Sum; + Status = model.Status; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + JewelId = JewelId, + JewelName = JewelName, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + } +} -- 2.25.1 From a24cc9623690bf89160607f69dd5a79fca49b27f Mon Sep 17 00:00:00 2001 From: VictoriaPresnyakova Date: Sun, 5 Mar 2023 19:31:05 +0400 Subject: [PATCH 2/5] Lab_3 --- JewelryStore/JewelryStore.csproj | 1 + JewelryStore/Program.cs | 2 +- .../Implements/ComponentStorage.cs | 98 ++++++++++ .../Implements/JewelStorage.cs | 112 +++++++++++ .../Implements/OrderStorage.cs | 98 ++++++++++ .../JewelryStoreDataBase.cs | 24 ++- .../JewelryStoreDatabaseImplement.csproj | 6 +- .../20230305150151_InitialCreate.Designer.cs | 175 ++++++++++++++++++ .../20230305150151_InitialCreate.cs | 126 +++++++++++++ ...20230305150722_InitialCreate_1.Designer.cs | 175 ++++++++++++++++++ .../20230305150722_InitialCreate_1.cs | 22 +++ .../JewelryStoreDataBaseModelSnapshot.cs | 172 +++++++++++++++++ 12 files changed, 1002 insertions(+), 9 deletions(-) create mode 100644 JewelryStoreDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 JewelryStoreDatabaseImplement/Implements/JewelStorage.cs create mode 100644 JewelryStoreDatabaseImplement/Implements/OrderStorage.cs create mode 100644 JewelryStoreDatabaseImplement/Migrations/20230305150151_InitialCreate.Designer.cs create mode 100644 JewelryStoreDatabaseImplement/Migrations/20230305150151_InitialCreate.cs create mode 100644 JewelryStoreDatabaseImplement/Migrations/20230305150722_InitialCreate_1.Designer.cs create mode 100644 JewelryStoreDatabaseImplement/Migrations/20230305150722_InitialCreate_1.cs create mode 100644 JewelryStoreDatabaseImplement/Migrations/JewelryStoreDataBaseModelSnapshot.cs diff --git a/JewelryStore/JewelryStore.csproj b/JewelryStore/JewelryStore.csproj index 67eeabd..bb468e4 100644 --- a/JewelryStore/JewelryStore.csproj +++ b/JewelryStore/JewelryStore.csproj @@ -25,6 +25,7 @@ + diff --git a/JewelryStore/Program.cs b/JewelryStore/Program.cs index df72a59..152f4e3 100644 --- a/JewelryStore/Program.cs +++ b/JewelryStore/Program.cs @@ -1,7 +1,7 @@ using JewelryStoreContracts.BusinessLogicsContracts; using JewelryStoreContracts.StoragesContracts; using JewelryStoreBusinessLogic.BusinessLogics; -using JewelryStoreFileImplement.Implements; +using JewelryStoreDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using System.Drawing; diff --git a/JewelryStoreDatabaseImplement/Implements/ComponentStorage.cs b/JewelryStoreDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..179fe09 --- /dev/null +++ b/JewelryStoreDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,98 @@ +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.SearchModels; +using JewelryStoreContracts.StoragesContracts; +using JewelryStoreContracts.ViewModels; +using JewelryStoreDatabaseImplement.Models; + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public ComponentViewModel? Delete(ComponentBindingModel model) + { + using var context = new JewelryStoreDataBase(); + + var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + + return null; + } + + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + + using var context = new JewelryStoreDataBase(); + + return context.Components.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + + using var context = new JewelryStoreDataBase(); + + return context.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new JewelryStoreDataBase(); + + return context.Components.Select(x => x.GetViewModel).ToList(); + } + + public ComponentViewModel? Insert(ComponentBindingModel model) + { + var newComponent = Component.Create(model); + + if (newComponent == null) + { + return null; + } + + using var context = new JewelryStoreDataBase(); + + context.Components.Add(newComponent); + context.SaveChanges(); + + return newComponent.GetViewModel; + } + + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new JewelryStoreDataBase(); + + var component = context.Components.FirstOrDefault(x => x.Id == model.Id); + + if (component == null) + { + return null; + } + + component.Update(model); + context.SaveChanges(); + + return component.GetViewModel; + } + } +} diff --git a/JewelryStoreDatabaseImplement/Implements/JewelStorage.cs b/JewelryStoreDatabaseImplement/Implements/JewelStorage.cs new file mode 100644 index 0000000..c153e37 --- /dev/null +++ b/JewelryStoreDatabaseImplement/Implements/JewelStorage.cs @@ -0,0 +1,112 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.SearchModels; +using JewelryStoreContracts.StoragesContracts; +using JewelryStoreContracts.ViewModels; +using JewelryStoreDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + + +namespace JewelryStoreDatabaseImplement.Implements +{ + public class JewelStorage : IJewelStorage + { + public JewelViewModel? Delete(JewelBindingModel model) + { + using var context = new JewelryStoreDataBase(); + + var element = context.Jewels.Include(x => x.Components).FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Jewels.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + + return null; + } + + public JewelViewModel? GetElement(JewelSearchModel model) + { + if (string.IsNullOrEmpty(model.JewelName) && !model.Id.HasValue) + { + return null; + } + + using var context = new JewelryStoreDataBase(); + + return context.Jewels.Include(x => x.Components).ThenInclude(x => x.Component).FirstOrDefault(x => (!string.IsNullOrEmpty(model.JewelName) && x.JewelName == model.JewelName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(JewelSearchModel model) + { + if (string.IsNullOrEmpty(model.JewelName)) + { + return new(); + } + + using var context = new JewelryStoreDataBase(); + + return context.Jewels.Include(x => x.Components).ThenInclude(x => x.Component).Where(x => x.JewelName.Contains(model.JewelName)).ToList().Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new JewelryStoreDataBase(); + + return context.Jewels.Include(x => x.Components).ThenInclude(x => x.Component).ToList().Select(x => x.GetViewModel).ToList(); + } + + public JewelViewModel? Insert(JewelBindingModel model) + { + using var context = new JewelryStoreDataBase(); + + var newJewel = Jewel.Create(context, model); + + if (newJewel == null) + { + return null; + } + + context.Jewels.Add(newJewel); + context.SaveChanges(); + + return newJewel.GetViewModel; + } + + public JewelViewModel? Update(JewelBindingModel model) + { + using var context = new JewelryStoreDataBase(); + + using var transaction = context.Database.BeginTransaction(); + + try + { + var jewel = context.Jewels.FirstOrDefault(rec => rec.Id == model.Id); + + if (jewel == null) + { + return null; + } + + jewel.Update(model); + context.SaveChanges(); + jewel.UpdateComponents(context, model); + transaction.Commit(); + + return jewel.GetViewModel; + } + catch + { + transaction.Rollback(); + + throw; + } + } + } +} diff --git a/JewelryStoreDatabaseImplement/Implements/OrderStorage.cs b/JewelryStoreDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..a030f11 --- /dev/null +++ b/JewelryStoreDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,98 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.SearchModels; +using JewelryStoreContracts.StoragesContracts; +using JewelryStoreContracts.ViewModels; +using JewelryStoreDatabaseImplement.Models; + +namespace JewelryStoreDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new JewelryStoreDataBase(); + + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + + using var context = new JewelryStoreDataBase(); + + return context.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + + using var context = new JewelryStoreDataBase(); + + return context.Orders.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); + } + + public List GetFullList() + { + using var context = new JewelryStoreDataBase(); + + return context.Orders.Select(x => x.GetViewModel).ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + + if (newOrder == null) + { + return null; + } + + using var context = new JewelryStoreDataBase(); + + context.Orders.Add(newOrder); + context.SaveChanges(); + + return newOrder.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new JewelryStoreDataBase(); + + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + + if (order == null) + { + return null; + } + + order.Update(model); + context.SaveChanges(); + + return order.GetViewModel; + } + } +} diff --git a/JewelryStoreDatabaseImplement/JewelryStoreDataBase.cs b/JewelryStoreDatabaseImplement/JewelryStoreDataBase.cs index f21bf9c..b921251 100644 --- a/JewelryStoreDatabaseImplement/JewelryStoreDataBase.cs +++ b/JewelryStoreDatabaseImplement/JewelryStoreDataBase.cs @@ -1,12 +1,30 @@ -using System; +using Microsoft.EntityFrameworkCore; +using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using JewelryStoreDatabaseImplement.Models; + namespace JewelryStoreDatabaseImplement { - public class JewelryStoreDataBase + public class JewelryStoreDataBase : DbContext { + + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-H890EHR\SQLEXPRESS;Initial Catalog=JewelryStoreDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Jewels { set; get; } + public virtual DbSet JewelComponents { set; get; } + public virtual DbSet Orders { set; get; } + } } -} + diff --git a/JewelryStoreDatabaseImplement/JewelryStoreDatabaseImplement.csproj b/JewelryStoreDatabaseImplement/JewelryStoreDatabaseImplement.csproj index 2cb019c..1359fa1 100644 --- a/JewelryStoreDatabaseImplement/JewelryStoreDatabaseImplement.csproj +++ b/JewelryStoreDatabaseImplement/JewelryStoreDatabaseImplement.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -11,10 +11,6 @@ - - - - diff --git a/JewelryStoreDatabaseImplement/Migrations/20230305150151_InitialCreate.Designer.cs b/JewelryStoreDatabaseImplement/Migrations/20230305150151_InitialCreate.Designer.cs new file mode 100644 index 0000000..efa2be2 --- /dev/null +++ b/JewelryStoreDatabaseImplement/Migrations/20230305150151_InitialCreate.Designer.cs @@ -0,0 +1,175 @@ +// +using System; +using JewelryStoreDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace JewelryStoreDatabaseImplement.Migrations +{ + [DbContext(typeof(JewelryStoreDataBase))] + [Migration("20230305150151_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Jewel", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("JewelName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Jewels"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.JewelComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("JewelId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("JewelId"); + + b.ToTable("JewelComponents"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("JewelId") + .HasColumnType("int"); + + b.Property("JewelName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("JewelId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.JewelComponent", b => + { + b.HasOne("JewelryStoreDatabaseImplement.Models.Component", "Component") + .WithMany("JewelComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("JewelryStoreDatabaseImplement.Models.Jewel", "Jewel") + .WithMany("Components") + .HasForeignKey("JewelId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Jewel"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Order", b => + { + b.HasOne("JewelryStoreDatabaseImplement.Models.Jewel", "Jewel") + .WithMany("Orders") + .HasForeignKey("JewelId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Jewel"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Component", b => + { + b.Navigation("JewelComponents"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Jewel", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/JewelryStoreDatabaseImplement/Migrations/20230305150151_InitialCreate.cs b/JewelryStoreDatabaseImplement/Migrations/20230305150151_InitialCreate.cs new file mode 100644 index 0000000..92dcf01 --- /dev/null +++ b/JewelryStoreDatabaseImplement/Migrations/20230305150151_InitialCreate.cs @@ -0,0 +1,126 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace JewelryStoreDatabaseImplement.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Components", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ComponentName = table.Column(type: "nvarchar(max)", nullable: false), + Cost = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Components", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Jewels", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + JewelName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Jewels", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "JewelComponents", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + JewelId = table.Column(type: "int", nullable: false), + ComponentId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_JewelComponents", x => x.Id); + table.ForeignKey( + name: "FK_JewelComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_JewelComponents_Jewels_JewelId", + column: x => x.JewelId, + principalTable: "Jewels", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + JewelId = table.Column(type: "int", nullable: false), + JewelName = table.Column(type: "nvarchar(max)", nullable: false), + Count = table.Column(type: "int", nullable: false), + Sum = table.Column(type: "float", nullable: false), + Status = table.Column(type: "int", nullable: false), + DateCreate = table.Column(type: "datetime2", nullable: false), + DateImplement = table.Column(type: "datetime2", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Jewels_JewelId", + column: x => x.JewelId, + principalTable: "Jewels", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_JewelComponents_ComponentId", + table: "JewelComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_JewelComponents_JewelId", + table: "JewelComponents", + column: "JewelId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_JewelId", + table: "Orders", + column: "JewelId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "JewelComponents"); + + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Jewels"); + } + } +} diff --git a/JewelryStoreDatabaseImplement/Migrations/20230305150722_InitialCreate_1.Designer.cs b/JewelryStoreDatabaseImplement/Migrations/20230305150722_InitialCreate_1.Designer.cs new file mode 100644 index 0000000..ea9b43e --- /dev/null +++ b/JewelryStoreDatabaseImplement/Migrations/20230305150722_InitialCreate_1.Designer.cs @@ -0,0 +1,175 @@ +// +using System; +using JewelryStoreDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace JewelryStoreDatabaseImplement.Migrations +{ + [DbContext(typeof(JewelryStoreDataBase))] + [Migration("20230305150722_InitialCreate_1")] + partial class InitialCreate_1 + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Jewel", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("JewelName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Jewels"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.JewelComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("JewelId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("JewelId"); + + b.ToTable("JewelComponents"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("JewelId") + .HasColumnType("int"); + + b.Property("JewelName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("JewelId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.JewelComponent", b => + { + b.HasOne("JewelryStoreDatabaseImplement.Models.Component", "Component") + .WithMany("JewelComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("JewelryStoreDatabaseImplement.Models.Jewel", "Jewel") + .WithMany("Components") + .HasForeignKey("JewelId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Jewel"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Order", b => + { + b.HasOne("JewelryStoreDatabaseImplement.Models.Jewel", "Jewel") + .WithMany("Orders") + .HasForeignKey("JewelId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Jewel"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Component", b => + { + b.Navigation("JewelComponents"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Jewel", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/JewelryStoreDatabaseImplement/Migrations/20230305150722_InitialCreate_1.cs b/JewelryStoreDatabaseImplement/Migrations/20230305150722_InitialCreate_1.cs new file mode 100644 index 0000000..d2ede86 --- /dev/null +++ b/JewelryStoreDatabaseImplement/Migrations/20230305150722_InitialCreate_1.cs @@ -0,0 +1,22 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace JewelryStoreDatabaseImplement.Migrations +{ + /// + public partial class InitialCreate_1 : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + + } + } +} diff --git a/JewelryStoreDatabaseImplement/Migrations/JewelryStoreDataBaseModelSnapshot.cs b/JewelryStoreDatabaseImplement/Migrations/JewelryStoreDataBaseModelSnapshot.cs new file mode 100644 index 0000000..08db833 --- /dev/null +++ b/JewelryStoreDatabaseImplement/Migrations/JewelryStoreDataBaseModelSnapshot.cs @@ -0,0 +1,172 @@ +// +using System; +using JewelryStoreDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace JewelryStoreDatabaseImplement.Migrations +{ + [DbContext(typeof(JewelryStoreDataBase))] + partial class JewelryStoreDataBaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Jewel", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("JewelName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Jewels"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.JewelComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("JewelId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("JewelId"); + + b.ToTable("JewelComponents"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("JewelId") + .HasColumnType("int"); + + b.Property("JewelName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("JewelId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.JewelComponent", b => + { + b.HasOne("JewelryStoreDatabaseImplement.Models.Component", "Component") + .WithMany("JewelComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("JewelryStoreDatabaseImplement.Models.Jewel", "Jewel") + .WithMany("Components") + .HasForeignKey("JewelId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Jewel"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Order", b => + { + b.HasOne("JewelryStoreDatabaseImplement.Models.Jewel", "Jewel") + .WithMany("Orders") + .HasForeignKey("JewelId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Jewel"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Component", b => + { + b.Navigation("JewelComponents"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Jewel", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} -- 2.25.1 From 37ab6a8ae128599e99322d42a35ff686c3ec89c8 Mon Sep 17 00:00:00 2001 From: VictoriaPresnyakova Date: Tue, 7 Mar 2023 12:55:01 +0400 Subject: [PATCH 3/5] =?UTF-8?q?=D1=81=D0=B4=D0=B0=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JewelryStore/JewelryStore.csproj | 1 - 1 file changed, 1 deletion(-) diff --git a/JewelryStore/JewelryStore.csproj b/JewelryStore/JewelryStore.csproj index bb468e4..b133b52 100644 --- a/JewelryStore/JewelryStore.csproj +++ b/JewelryStore/JewelryStore.csproj @@ -27,7 +27,6 @@ - \ No newline at end of file -- 2.25.1 From ca2fcea02d24a540ac151b8994c6162b9277f55d Mon Sep 17 00:00:00 2001 From: VictoriaPresnyakova Date: Fri, 16 Jun 2023 23:49:21 +0400 Subject: [PATCH 4/5] lab3 hard --- JewelryStore/FormStore.cs | 2 +- .../Implements/StoreStorage.cs | 168 +++++++++++++++++ .../JewelryStoreDataBase.cs | 6 +- ...20230305150722_InitialCreate_1.Designer.cs | 175 ------------------ .../20230305150722_InitialCreate_1.cs | 22 --- ...ner.cs => 20230616194257_init.Designer.cs} | 81 +++++++- ...nitialCreate.cs => 20230616194257_init.cs} | 61 +++++- .../JewelryStoreDataBaseModelSnapshot.cs | 77 ++++++++ JewelryStoreDatabaseImplement/Models/Store.cs | 110 +++++++++++ .../Models/StoreJewel.cs | 27 +++ 10 files changed, 526 insertions(+), 203 deletions(-) create mode 100644 JewelryStoreDatabaseImplement/Implements/StoreStorage.cs delete mode 100644 JewelryStoreDatabaseImplement/Migrations/20230305150722_InitialCreate_1.Designer.cs delete mode 100644 JewelryStoreDatabaseImplement/Migrations/20230305150722_InitialCreate_1.cs rename JewelryStoreDatabaseImplement/Migrations/{20230305150151_InitialCreate.Designer.cs => 20230616194257_init.Designer.cs} (68%) rename JewelryStoreDatabaseImplement/Migrations/{20230305150151_InitialCreate.cs => 20230616194257_init.cs} (66%) create mode 100644 JewelryStoreDatabaseImplement/Models/Store.cs create mode 100644 JewelryStoreDatabaseImplement/Models/StoreJewel.cs diff --git a/JewelryStore/FormStore.cs b/JewelryStore/FormStore.cs index 317952a..47e9e49 100644 --- a/JewelryStore/FormStore.cs +++ b/JewelryStore/FormStore.cs @@ -116,7 +116,7 @@ namespace JewelryStore DataGridView.Rows.Clear(); foreach (var elem in _listStores) { - DataGridView.Rows.Add(new object[] { elem.Key, elem.Value.Item1.JewelName, elem.Value.Item2 }); + DataGridView.Rows.Add(new object[] { elem.Value.Item1.JewelName, elem.Value.Item1.Price, elem.Value.Item2 }); } } } diff --git a/JewelryStoreDatabaseImplement/Implements/StoreStorage.cs b/JewelryStoreDatabaseImplement/Implements/StoreStorage.cs new file mode 100644 index 0000000..6bf2b16 --- /dev/null +++ b/JewelryStoreDatabaseImplement/Implements/StoreStorage.cs @@ -0,0 +1,168 @@ +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.SearchModels; +using JewelryStoreContracts.StoragesContracts; +using JewelryStoreContracts.ViewModels; +using JewelryStoreDatabaseImplement.Models; +using JewelryStoreDataModels.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreDatabaseImplement.Implements +{ + public class StoreStorage : IStoreStorage + { + public StoreViewModel? Delete(StoreBindingModel model) + { + using var context = new JewelryStoreDataBase(); + + var element = context.Stores + .Include(x => x.Jewels) + .FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Stores.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + + public StoreViewModel? GetElement(StoreSearchModel model) + { + if (string.IsNullOrEmpty(model.StoreName) && !model.Id.HasValue) + { + return null; + } + + using var context = new JewelryStoreDataBase(); + + return context.Stores + .Include(x => x.Jewels) + .ThenInclude(x => x.Jewel) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.StoreName) && x.StoreName == model.StoreName) || (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(StoreSearchModel model) + { + if (string.IsNullOrEmpty(model.StoreName)) + { + return new(); + } + + using var context = new JewelryStoreDataBase(); + + return context.Stores + .Include(x => x.Jewels) + .ThenInclude(x => x.Jewel) + .Where(x => x.StoreName.Contains(model.StoreName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new JewelryStoreDataBase(); + return context.Stores + .Include(x => x.Jewels) + .ThenInclude(x => x.Jewel) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public StoreViewModel? Insert(StoreBindingModel model) + { + using var context = new JewelryStoreDataBase(); + var newStore = Store.Create(context, model); + if (newStore == null) + { + return null; + } + context.Stores.Add(newStore); + context.SaveChanges(); + return newStore.GetViewModel; + } + + public bool SellJewel(IJewelModel model, int quantity) + { + using var context = new JewelryStoreDataBase(); + using var transaction = context.Database.BeginTransaction(); + try + { + foreach (var store in context.Stores + .Include(x => x.Jewels) + .ThenInclude(x => x.Jewel) + .ToList() + .Where(x => x.StoreJewels.ContainsKey(model.Id))) + { + int countInCurrentStore = store.StoreJewels[model.Id].Item2; + if (countInCurrentStore <= quantity) + { + var elem = context.StoreJewels + .Where(x => x.JewelId == model.Id) + .FirstOrDefault(x => x.StoreId == store.Id); + context.StoreJewels.Remove(elem); + store.StoreJewels.Remove(model.Id); + quantity -= countInCurrentStore; + } + else + { + store.StoreJewels[model.Id] = (store.StoreJewels[model.Id].Item1, countInCurrentStore - quantity); + quantity = 0; + store.UpdateJewel(context, new() + { + Id = store.Id, + StoreJewels = store.StoreJewels, + }); + } + if (quantity == 0) + { + context.SaveChanges(); + transaction.Commit(); + return true; + } + } + transaction.Rollback(); + return false; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public StoreViewModel? Update(StoreBindingModel model) + { + using var context = new JewelryStoreDataBase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var store = context.Stores.FirstOrDefault(rec => rec.Id == model.Id); + if (store == null) + { + return null; + } + store.Update(model); + context.SaveChanges(); + store.UpdateJewel(context, model); + transaction.Commit(); + return store.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +} diff --git a/JewelryStoreDatabaseImplement/JewelryStoreDataBase.cs b/JewelryStoreDatabaseImplement/JewelryStoreDataBase.cs index b921251..93a713d 100644 --- a/JewelryStoreDatabaseImplement/JewelryStoreDataBase.cs +++ b/JewelryStoreDatabaseImplement/JewelryStoreDataBase.cs @@ -5,7 +5,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using JewelryStoreDatabaseImplement.Models; - +using JewelryStoreDatabaseImplement.Implements; namespace JewelryStoreDatabaseImplement { @@ -25,6 +25,8 @@ namespace JewelryStoreDatabaseImplement public virtual DbSet Jewels { set; get; } public virtual DbSet JewelComponents { set; get; } public virtual DbSet Orders { set; get; } - } + public virtual DbSet Stores { set; get; } + public virtual DbSet StoreJewels { set; get; } + } } diff --git a/JewelryStoreDatabaseImplement/Migrations/20230305150722_InitialCreate_1.Designer.cs b/JewelryStoreDatabaseImplement/Migrations/20230305150722_InitialCreate_1.Designer.cs deleted file mode 100644 index ea9b43e..0000000 --- a/JewelryStoreDatabaseImplement/Migrations/20230305150722_InitialCreate_1.Designer.cs +++ /dev/null @@ -1,175 +0,0 @@ -// -using System; -using JewelryStoreDatabaseImplement; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; -using Microsoft.EntityFrameworkCore.Metadata; -using Microsoft.EntityFrameworkCore.Migrations; -using Microsoft.EntityFrameworkCore.Storage.ValueConversion; - -#nullable disable - -namespace JewelryStoreDatabaseImplement.Migrations -{ - [DbContext(typeof(JewelryStoreDataBase))] - [Migration("20230305150722_InitialCreate_1")] - partial class InitialCreate_1 - { - /// - protected override void BuildTargetModel(ModelBuilder modelBuilder) - { -#pragma warning disable 612, 618 - modelBuilder - .HasAnnotation("ProductVersion", "7.0.3") - .HasAnnotation("Relational:MaxIdentifierLength", 128); - - SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); - - modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Component", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("ComponentName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("Cost") - .HasColumnType("float"); - - b.HasKey("Id"); - - b.ToTable("Components"); - }); - - modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Jewel", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("JewelName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("Price") - .HasColumnType("float"); - - b.HasKey("Id"); - - b.ToTable("Jewels"); - }); - - modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.JewelComponent", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("ComponentId") - .HasColumnType("int"); - - b.Property("Count") - .HasColumnType("int"); - - b.Property("JewelId") - .HasColumnType("int"); - - b.HasKey("Id"); - - b.HasIndex("ComponentId"); - - b.HasIndex("JewelId"); - - b.ToTable("JewelComponents"); - }); - - modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Order", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("Count") - .HasColumnType("int"); - - b.Property("DateCreate") - .HasColumnType("datetime2"); - - b.Property("DateImplement") - .HasColumnType("datetime2"); - - b.Property("JewelId") - .HasColumnType("int"); - - b.Property("JewelName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("Status") - .HasColumnType("int"); - - b.Property("Sum") - .HasColumnType("float"); - - b.HasKey("Id"); - - b.HasIndex("JewelId"); - - b.ToTable("Orders"); - }); - - modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.JewelComponent", b => - { - b.HasOne("JewelryStoreDatabaseImplement.Models.Component", "Component") - .WithMany("JewelComponents") - .HasForeignKey("ComponentId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.HasOne("JewelryStoreDatabaseImplement.Models.Jewel", "Jewel") - .WithMany("Components") - .HasForeignKey("JewelId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Component"); - - b.Navigation("Jewel"); - }); - - modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Order", b => - { - b.HasOne("JewelryStoreDatabaseImplement.Models.Jewel", "Jewel") - .WithMany("Orders") - .HasForeignKey("JewelId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); - - b.Navigation("Jewel"); - }); - - modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Component", b => - { - b.Navigation("JewelComponents"); - }); - - modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Jewel", b => - { - b.Navigation("Components"); - - b.Navigation("Orders"); - }); -#pragma warning restore 612, 618 - } - } -} diff --git a/JewelryStoreDatabaseImplement/Migrations/20230305150722_InitialCreate_1.cs b/JewelryStoreDatabaseImplement/Migrations/20230305150722_InitialCreate_1.cs deleted file mode 100644 index d2ede86..0000000 --- a/JewelryStoreDatabaseImplement/Migrations/20230305150722_InitialCreate_1.cs +++ /dev/null @@ -1,22 +0,0 @@ -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace JewelryStoreDatabaseImplement.Migrations -{ - /// - public partial class InitialCreate_1 : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - - } - } -} diff --git a/JewelryStoreDatabaseImplement/Migrations/20230305150151_InitialCreate.Designer.cs b/JewelryStoreDatabaseImplement/Migrations/20230616194257_init.Designer.cs similarity index 68% rename from JewelryStoreDatabaseImplement/Migrations/20230305150151_InitialCreate.Designer.cs rename to JewelryStoreDatabaseImplement/Migrations/20230616194257_init.Designer.cs index efa2be2..095daf5 100644 --- a/JewelryStoreDatabaseImplement/Migrations/20230305150151_InitialCreate.Designer.cs +++ b/JewelryStoreDatabaseImplement/Migrations/20230616194257_init.Designer.cs @@ -12,8 +12,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace JewelryStoreDatabaseImplement.Migrations { [DbContext(typeof(JewelryStoreDataBase))] - [Migration("20230305150151_InitialCreate")] - partial class InitialCreate + [Migration("20230616194257_init")] + partial class init { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -128,6 +128,59 @@ namespace JewelryStoreDatabaseImplement.Migrations b.ToTable("Orders"); }); + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Store", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("JewelMaxCount") + .HasColumnType("int"); + + b.Property("OpeningDate") + .HasColumnType("datetime2"); + + b.Property("StoreAdress") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("StoreName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Stores"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.StoreJewel", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("JewelId") + .HasColumnType("int"); + + b.Property("StoreId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("JewelId"); + + b.HasIndex("StoreId"); + + b.ToTable("StoreJewels"); + }); + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.JewelComponent", b => { b.HasOne("JewelryStoreDatabaseImplement.Models.Component", "Component") @@ -158,6 +211,25 @@ namespace JewelryStoreDatabaseImplement.Migrations b.Navigation("Jewel"); }); + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.StoreJewel", b => + { + b.HasOne("JewelryStoreDatabaseImplement.Models.Jewel", "Jewel") + .WithMany() + .HasForeignKey("JewelId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("JewelryStoreDatabaseImplement.Models.Store", "Store") + .WithMany("Jewels") + .HasForeignKey("StoreId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Jewel"); + + b.Navigation("Store"); + }); + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Component", b => { b.Navigation("JewelComponents"); @@ -169,6 +241,11 @@ namespace JewelryStoreDatabaseImplement.Migrations b.Navigation("Orders"); }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Store", b => + { + b.Navigation("Jewels"); + }); #pragma warning restore 612, 618 } } diff --git a/JewelryStoreDatabaseImplement/Migrations/20230305150151_InitialCreate.cs b/JewelryStoreDatabaseImplement/Migrations/20230616194257_init.cs similarity index 66% rename from JewelryStoreDatabaseImplement/Migrations/20230305150151_InitialCreate.cs rename to JewelryStoreDatabaseImplement/Migrations/20230616194257_init.cs index 92dcf01..11942ec 100644 --- a/JewelryStoreDatabaseImplement/Migrations/20230305150151_InitialCreate.cs +++ b/JewelryStoreDatabaseImplement/Migrations/20230616194257_init.cs @@ -6,7 +6,7 @@ using Microsoft.EntityFrameworkCore.Migrations; namespace JewelryStoreDatabaseImplement.Migrations { /// - public partial class InitialCreate : Migration + public partial class init : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) @@ -39,6 +39,22 @@ namespace JewelryStoreDatabaseImplement.Migrations table.PrimaryKey("PK_Jewels", x => x.Id); }); + migrationBuilder.CreateTable( + name: "Stores", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + StoreName = table.Column(type: "nvarchar(max)", nullable: false), + StoreAdress = table.Column(type: "nvarchar(max)", nullable: false), + OpeningDate = table.Column(type: "datetime2", nullable: false), + JewelMaxCount = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Stores", x => x.Id); + }); + migrationBuilder.CreateTable( name: "JewelComponents", columns: table => new @@ -91,6 +107,33 @@ namespace JewelryStoreDatabaseImplement.Migrations onDelete: ReferentialAction.Cascade); }); + migrationBuilder.CreateTable( + name: "StoreJewels", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + JewelId = table.Column(type: "int", nullable: false), + StoreId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_StoreJewels", x => x.Id); + table.ForeignKey( + name: "FK_StoreJewels_Jewels_JewelId", + column: x => x.JewelId, + principalTable: "Jewels", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_StoreJewels_Stores_StoreId", + column: x => x.StoreId, + principalTable: "Stores", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + migrationBuilder.CreateIndex( name: "IX_JewelComponents_ComponentId", table: "JewelComponents", @@ -105,6 +148,16 @@ namespace JewelryStoreDatabaseImplement.Migrations name: "IX_Orders_JewelId", table: "Orders", column: "JewelId"); + + migrationBuilder.CreateIndex( + name: "IX_StoreJewels_JewelId", + table: "StoreJewels", + column: "JewelId"); + + migrationBuilder.CreateIndex( + name: "IX_StoreJewels_StoreId", + table: "StoreJewels", + column: "StoreId"); } /// @@ -116,11 +169,17 @@ namespace JewelryStoreDatabaseImplement.Migrations migrationBuilder.DropTable( name: "Orders"); + migrationBuilder.DropTable( + name: "StoreJewels"); + migrationBuilder.DropTable( name: "Components"); migrationBuilder.DropTable( name: "Jewels"); + + migrationBuilder.DropTable( + name: "Stores"); } } } diff --git a/JewelryStoreDatabaseImplement/Migrations/JewelryStoreDataBaseModelSnapshot.cs b/JewelryStoreDatabaseImplement/Migrations/JewelryStoreDataBaseModelSnapshot.cs index 08db833..10dc2c0 100644 --- a/JewelryStoreDatabaseImplement/Migrations/JewelryStoreDataBaseModelSnapshot.cs +++ b/JewelryStoreDatabaseImplement/Migrations/JewelryStoreDataBaseModelSnapshot.cs @@ -125,6 +125,59 @@ namespace JewelryStoreDatabaseImplement.Migrations b.ToTable("Orders"); }); + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Store", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("JewelMaxCount") + .HasColumnType("int"); + + b.Property("OpeningDate") + .HasColumnType("datetime2"); + + b.Property("StoreAdress") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("StoreName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Stores"); + }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.StoreJewel", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("JewelId") + .HasColumnType("int"); + + b.Property("StoreId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("JewelId"); + + b.HasIndex("StoreId"); + + b.ToTable("StoreJewels"); + }); + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.JewelComponent", b => { b.HasOne("JewelryStoreDatabaseImplement.Models.Component", "Component") @@ -155,6 +208,25 @@ namespace JewelryStoreDatabaseImplement.Migrations b.Navigation("Jewel"); }); + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.StoreJewel", b => + { + b.HasOne("JewelryStoreDatabaseImplement.Models.Jewel", "Jewel") + .WithMany() + .HasForeignKey("JewelId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("JewelryStoreDatabaseImplement.Models.Store", "Store") + .WithMany("Jewels") + .HasForeignKey("StoreId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Jewel"); + + b.Navigation("Store"); + }); + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Component", b => { b.Navigation("JewelComponents"); @@ -166,6 +238,11 @@ namespace JewelryStoreDatabaseImplement.Migrations b.Navigation("Orders"); }); + + modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Store", b => + { + b.Navigation("Jewels"); + }); #pragma warning restore 612, 618 } } diff --git a/JewelryStoreDatabaseImplement/Models/Store.cs b/JewelryStoreDatabaseImplement/Models/Store.cs new file mode 100644 index 0000000..cfaf073 --- /dev/null +++ b/JewelryStoreDatabaseImplement/Models/Store.cs @@ -0,0 +1,110 @@ +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.ViewModels; +using JewelryStoreDataModels.Models; +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; + +namespace JewelryStoreDatabaseImplement.Models +{ + public class Store : IStoreModel + { + [Required] + public string StoreName { get; private set; } = string.Empty; + [Required] + public string StoreAdress { get; private set; } = string.Empty; + [Required] + public DateTime OpeningDate { get; private set; } + [Required] + public int JewelMaxCount { get; private set; } + + public int Id { get; private set; } + + + private Dictionary _storeJewels = null; + [NotMapped] + public Dictionary StoreJewels + { + get + { + if (_storeJewels == null) + { + _storeJewels = Jewels + .ToDictionary(recPC => recPC.JewelId, recPC => (recPC.Jewel as IJewelModel, recPC.Count)); + } + return _storeJewels; + } + } + + [ForeignKey("StoreId")] + public virtual List Jewels { get; set; } = new(); + + public static Store Create(JewelryStoreDataBase context, StoreBindingModel model) + { + return new Store() + { + Id = model.Id, + StoreName = model.StoreName, + StoreAdress = model.StoreAdress, + OpeningDate = model.OpeningDate, + JewelMaxCount = model.JewelMaxCount, + Jewels = model.StoreJewels.Select(x => new StoreJewel + { + Jewel = context.Jewels.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(StoreBindingModel model) + { + StoreName = model.StoreName; + StoreAdress = model.StoreAdress; + OpeningDate = model.OpeningDate; + JewelMaxCount = model.JewelMaxCount; + } + + public StoreViewModel GetViewModel => new() + { + Id = Id, + StoreName = StoreName, + StoreAdress = StoreAdress, + OpeningDate = OpeningDate, + JewelMaxCount = JewelMaxCount, + StoreJewels = StoreJewels + }; + + public void UpdateJewel(JewelryStoreDataBase context, StoreBindingModel model) + { + var storeJewels = context.StoreJewels.Where(rec => rec.StoreId == model.Id).ToList(); + if (storeJewels != null && storeJewels.Count > 0) + { // удалили те, которых нет в модели + context.StoreJewels.RemoveRange(storeJewels.Where(rec => !model.StoreJewels.ContainsKey(rec.JewelId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateJewel in storeJewels) + { + updateJewel.Count = model.StoreJewels[updateJewel.JewelId].Item2; + model.StoreJewels.Remove(updateJewel.JewelId); + } + context.SaveChanges(); + } + var store = context.Stores.First(x => x.Id == Id); + foreach (var sp in model.StoreJewels) + { + context.StoreJewels.Add(new StoreJewel + { + Store = store, + Jewel = context.Jewels.First(x => x.Id == sp.Key), + Count = sp.Value.Item2 + }); + context.SaveChanges(); + } + _storeJewels = null; + } + } +} diff --git a/JewelryStoreDatabaseImplement/Models/StoreJewel.cs b/JewelryStoreDatabaseImplement/Models/StoreJewel.cs new file mode 100644 index 0000000..e99f463 --- /dev/null +++ b/JewelryStoreDatabaseImplement/Models/StoreJewel.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreDatabaseImplement.Models +{ + public class StoreJewel + { + public int Id { get; set; } + + [Required] + public int JewelId { get; set; } + + [Required] + public int StoreId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Store Store { get; set; } = new(); + + public virtual Jewel Jewel { get; set; } = new(); + } +} -- 2.25.1 From 402f60036d07286e88f5ab1dae7ceb20322b3cfe Mon Sep 17 00:00:00 2001 From: VictoriaPresnyakova Date: Sat, 17 Jun 2023 15:16:46 +0400 Subject: [PATCH 5/5] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=81=D0=B2=D1=8F=D0=B7=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ....Designer.cs => 20230617111616_init.Designer.cs} | 13 +++++++++---- ...0230616194257_init.cs => 20230617111616_init.cs} | 11 ++++++----- .../Migrations/JewelryStoreDataBaseModelSnapshot.cs | 11 ++++++++--- JewelryStoreDatabaseImplement/Models/Jewel.cs | 3 +++ 4 files changed, 26 insertions(+), 12 deletions(-) rename JewelryStoreDatabaseImplement/Migrations/{20230616194257_init.Designer.cs => 20230617111616_init.Designer.cs} (96%) rename JewelryStoreDatabaseImplement/Migrations/{20230616194257_init.cs => 20230617111616_init.cs} (95%) diff --git a/JewelryStoreDatabaseImplement/Migrations/20230616194257_init.Designer.cs b/JewelryStoreDatabaseImplement/Migrations/20230617111616_init.Designer.cs similarity index 96% rename from JewelryStoreDatabaseImplement/Migrations/20230616194257_init.Designer.cs rename to JewelryStoreDatabaseImplement/Migrations/20230617111616_init.Designer.cs index 095daf5..2a62019 100644 --- a/JewelryStoreDatabaseImplement/Migrations/20230616194257_init.Designer.cs +++ b/JewelryStoreDatabaseImplement/Migrations/20230617111616_init.Designer.cs @@ -12,7 +12,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace JewelryStoreDatabaseImplement.Migrations { [DbContext(typeof(JewelryStoreDataBase))] - [Migration("20230616194257_init")] + [Migration("20230617111616_init")] partial class init { /// @@ -169,12 +169,15 @@ namespace JewelryStoreDatabaseImplement.Migrations b.Property("JewelId") .HasColumnType("int"); + b.Property("ManufactureId") + .HasColumnType("int"); + b.Property("StoreId") .HasColumnType("int"); b.HasKey("Id"); - b.HasIndex("JewelId"); + b.HasIndex("ManufactureId"); b.HasIndex("StoreId"); @@ -214,8 +217,8 @@ namespace JewelryStoreDatabaseImplement.Migrations modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.StoreJewel", b => { b.HasOne("JewelryStoreDatabaseImplement.Models.Jewel", "Jewel") - .WithMany() - .HasForeignKey("JewelId") + .WithMany("Shops") + .HasForeignKey("ManufactureId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); @@ -240,6 +243,8 @@ namespace JewelryStoreDatabaseImplement.Migrations b.Navigation("Components"); b.Navigation("Orders"); + + b.Navigation("Shops"); }); modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Store", b => diff --git a/JewelryStoreDatabaseImplement/Migrations/20230616194257_init.cs b/JewelryStoreDatabaseImplement/Migrations/20230617111616_init.cs similarity index 95% rename from JewelryStoreDatabaseImplement/Migrations/20230616194257_init.cs rename to JewelryStoreDatabaseImplement/Migrations/20230617111616_init.cs index 11942ec..b31b60e 100644 --- a/JewelryStoreDatabaseImplement/Migrations/20230616194257_init.cs +++ b/JewelryStoreDatabaseImplement/Migrations/20230617111616_init.cs @@ -115,14 +115,15 @@ namespace JewelryStoreDatabaseImplement.Migrations .Annotation("SqlServer:Identity", "1, 1"), JewelId = table.Column(type: "int", nullable: false), StoreId = table.Column(type: "int", nullable: false), - Count = table.Column(type: "int", nullable: false) + Count = table.Column(type: "int", nullable: false), + ManufactureId = table.Column(type: "int", nullable: false) }, constraints: table => { table.PrimaryKey("PK_StoreJewels", x => x.Id); table.ForeignKey( - name: "FK_StoreJewels_Jewels_JewelId", - column: x => x.JewelId, + name: "FK_StoreJewels_Jewels_ManufactureId", + column: x => x.ManufactureId, principalTable: "Jewels", principalColumn: "Id", onDelete: ReferentialAction.Cascade); @@ -150,9 +151,9 @@ namespace JewelryStoreDatabaseImplement.Migrations column: "JewelId"); migrationBuilder.CreateIndex( - name: "IX_StoreJewels_JewelId", + name: "IX_StoreJewels_ManufactureId", table: "StoreJewels", - column: "JewelId"); + column: "ManufactureId"); migrationBuilder.CreateIndex( name: "IX_StoreJewels_StoreId", diff --git a/JewelryStoreDatabaseImplement/Migrations/JewelryStoreDataBaseModelSnapshot.cs b/JewelryStoreDatabaseImplement/Migrations/JewelryStoreDataBaseModelSnapshot.cs index 10dc2c0..7a76299 100644 --- a/JewelryStoreDatabaseImplement/Migrations/JewelryStoreDataBaseModelSnapshot.cs +++ b/JewelryStoreDatabaseImplement/Migrations/JewelryStoreDataBaseModelSnapshot.cs @@ -166,12 +166,15 @@ namespace JewelryStoreDatabaseImplement.Migrations b.Property("JewelId") .HasColumnType("int"); + b.Property("ManufactureId") + .HasColumnType("int"); + b.Property("StoreId") .HasColumnType("int"); b.HasKey("Id"); - b.HasIndex("JewelId"); + b.HasIndex("ManufactureId"); b.HasIndex("StoreId"); @@ -211,8 +214,8 @@ namespace JewelryStoreDatabaseImplement.Migrations modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.StoreJewel", b => { b.HasOne("JewelryStoreDatabaseImplement.Models.Jewel", "Jewel") - .WithMany() - .HasForeignKey("JewelId") + .WithMany("Shops") + .HasForeignKey("ManufactureId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); @@ -237,6 +240,8 @@ namespace JewelryStoreDatabaseImplement.Migrations b.Navigation("Components"); b.Navigation("Orders"); + + b.Navigation("Shops"); }); modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Store", b => diff --git a/JewelryStoreDatabaseImplement/Models/Jewel.cs b/JewelryStoreDatabaseImplement/Models/Jewel.cs index 0d01025..1d49ab5 100644 --- a/JewelryStoreDatabaseImplement/Models/Jewel.cs +++ b/JewelryStoreDatabaseImplement/Models/Jewel.cs @@ -42,6 +42,9 @@ namespace JewelryStoreDatabaseImplement.Models [ForeignKey("JewelId")] public virtual List Orders { get; set; } = new(); + [ForeignKey("ManufactureId")] + public virtual List Shops { get; set; } = new(); + public static Jewel Create(JewelryStoreDataBase context, JewelBindingModel model) { return new Jewel() -- 2.25.1