From 00eee48168a9e7a7f7d90ccd6ac220b509870cfc Mon Sep 17 00:00:00 2001 From: dex_moth Date: Sun, 17 Mar 2024 20:53:20 +0400 Subject: [PATCH 1/5] =?UTF-8?q?=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FishFactory/FishFactory.csproj | 7 ++ FishFactory/FishFactory.sln | 10 ++ FishFactory/Program.cs | 2 +- .../StoragesContracts/IOrderStorage.cs | 5 - .../FishFactoryDatabase.cs | 21 ++++ .../FishFactoryDatabaseImplement.csproj | 23 ++++ .../Implements/CannedStorage.cs | 101 ++++++++++++++++++ .../Implements/ComponentStorage.cs | 76 +++++++++++++ .../Implements/OrderStorage.cs | 84 +++++++++++++++ FishFactoryDatabaseImplement/Models/Canned.cs | 88 +++++++++++++++ .../Models/CannedComponent.cs | 17 +++ .../Models/Component.cs | 56 ++++++++++ FishFactoryDatabaseImplement/Models/Order.cs | 55 ++++++++++ 13 files changed, 539 insertions(+), 6 deletions(-) create mode 100644 FishFactoryDatabaseImplement/FishFactoryDatabase.cs create mode 100644 FishFactoryDatabaseImplement/FishFactoryDatabaseImplement.csproj create mode 100644 FishFactoryDatabaseImplement/Implements/CannedStorage.cs create mode 100644 FishFactoryDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 FishFactoryDatabaseImplement/Implements/OrderStorage.cs create mode 100644 FishFactoryDatabaseImplement/Models/Canned.cs create mode 100644 FishFactoryDatabaseImplement/Models/CannedComponent.cs create mode 100644 FishFactoryDatabaseImplement/Models/Component.cs create mode 100644 FishFactoryDatabaseImplement/Models/Order.cs diff --git a/FishFactory/FishFactory.csproj b/FishFactory/FishFactory.csproj index 95274fe..13ee669 100644 --- a/FishFactory/FishFactory.csproj +++ b/FishFactory/FishFactory.csproj @@ -9,6 +9,12 @@ + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + @@ -16,6 +22,7 @@ + diff --git a/FishFactory/FishFactory.sln b/FishFactory/FishFactory.sln index c761982..a986a5c 100644 --- a/FishFactory/FishFactory.sln +++ b/FishFactory/FishFactory.sln @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FishFactoryListImplement", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FishFactoryFileImplement", "..\FishFactoryFileImplement\FishFactoryFileImplement.csproj", "{4EB943AB-67A8-4386-8874-FA0E3E98159C}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FishFactoryDatabaseImplement", "..\FishFactoryDatabaseImplement\FishFactoryDatabaseImplement.csproj", "{5744376D-B7D8-4FFD-B6D9-F53C169B056C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -71,6 +73,14 @@ Global {4EB943AB-67A8-4386-8874-FA0E3E98159C}.Release|Any CPU.Build.0 = Release|Any CPU {4EB943AB-67A8-4386-8874-FA0E3E98159C}.Release|x86.ActiveCfg = Release|Any CPU {4EB943AB-67A8-4386-8874-FA0E3E98159C}.Release|x86.Build.0 = Release|Any CPU + {5744376D-B7D8-4FFD-B6D9-F53C169B056C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5744376D-B7D8-4FFD-B6D9-F53C169B056C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5744376D-B7D8-4FFD-B6D9-F53C169B056C}.Debug|x86.ActiveCfg = Debug|Any CPU + {5744376D-B7D8-4FFD-B6D9-F53C169B056C}.Debug|x86.Build.0 = Debug|Any CPU + {5744376D-B7D8-4FFD-B6D9-F53C169B056C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5744376D-B7D8-4FFD-B6D9-F53C169B056C}.Release|Any CPU.Build.0 = Release|Any CPU + {5744376D-B7D8-4FFD-B6D9-F53C169B056C}.Release|x86.ActiveCfg = Release|Any CPU + {5744376D-B7D8-4FFD-B6D9-F53C169B056C}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/FishFactory/Program.cs b/FishFactory/Program.cs index 1aa0d38..ba4bc0a 100644 --- a/FishFactory/Program.cs +++ b/FishFactory/Program.cs @@ -2,7 +2,7 @@ using FishFactory.Forms; using FishFactoryContracts.BusinessLogicsContracts; using FishFactoryBusinessLogic.BusinessLogic; using FishFactoryContracts.StoragesContracts; -using FishFactoryFileImplement.Implements; +using FishFactoryDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/FishFactoryContracts/StoragesContracts/IOrderStorage.cs b/FishFactoryContracts/StoragesContracts/IOrderStorage.cs index 1f385c5..a81b868 100644 --- a/FishFactoryContracts/StoragesContracts/IOrderStorage.cs +++ b/FishFactoryContracts/StoragesContracts/IOrderStorage.cs @@ -1,11 +1,6 @@ using FishFactoryContracts.BindingModels; using FishFactoryContracts.SearchModels; using FishFactoryContracts.ViewModels; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace FishFactoryContracts.StoragesContracts { diff --git a/FishFactoryDatabaseImplement/FishFactoryDatabase.cs b/FishFactoryDatabaseImplement/FishFactoryDatabase.cs new file mode 100644 index 0000000..89c09ee --- /dev/null +++ b/FishFactoryDatabaseImplement/FishFactoryDatabase.cs @@ -0,0 +1,21 @@ +using FishFactoryDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace FishFactoryDatabaseImplement +{ + public class FishFactoryDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=CHESHIR\SQLEXPRESS;Initial Catalog=FishFactoryDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Canneds { set; get; } + public virtual DbSet CannedComponents { set; get; } + public virtual DbSet Orders { set; get; } + } +} diff --git a/FishFactoryDatabaseImplement/FishFactoryDatabaseImplement.csproj b/FishFactoryDatabaseImplement/FishFactoryDatabaseImplement.csproj new file mode 100644 index 0000000..338b17d --- /dev/null +++ b/FishFactoryDatabaseImplement/FishFactoryDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + diff --git a/FishFactoryDatabaseImplement/Implements/CannedStorage.cs b/FishFactoryDatabaseImplement/Implements/CannedStorage.cs new file mode 100644 index 0000000..526a8a5 --- /dev/null +++ b/FishFactoryDatabaseImplement/Implements/CannedStorage.cs @@ -0,0 +1,101 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.StoragesContracts; +using FishFactoryContracts.ViewModels; +using FishFactoryDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace FishFactoryDatabaseImplement.Implements +{ + public class CannedStorage : ICannedStorage + { + public List GetFullList() + { + using var context = new FishFactoryDatabase(); + return context.Canneds + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(CannedSearchModel model) + { + if (string.IsNullOrEmpty(model.CannedName)) + { + return new(); + } + using var context = new FishFactoryDatabase(); + return context.Canneds + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.CannedName.Contains(model.CannedName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public CannedViewModel? GetElement(CannedSearchModel model) + { + if (string.IsNullOrEmpty(model.CannedName) && !model.Id.HasValue) + { + return null; + } + using var context = new FishFactoryDatabase(); + return context.Canneds + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.CannedName) && x.CannedName == model.CannedName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + public CannedViewModel? Insert(CannedBindingModel model) + { + using var context = new FishFactoryDatabase(); + var newCanned = Canned.Create(context, model); + if (newCanned == null) + { + return null; + } + context.Canneds.Add(newCanned); + context.SaveChanges(); + return newCanned.GetViewModel; + } + public CannedViewModel? Update(CannedBindingModel model) + { + using var context = new FishFactoryDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var product = context.Canneds.FirstOrDefault(rec => rec.Id == model.Id); + if (product == null) + { + return null; + } + product.Update(model); + context.SaveChanges(); + product.UpdateComponents(context, model); + transaction.Commit(); + return product.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public CannedViewModel? Delete(CannedBindingModel model) + { + using var context = new FishFactoryDatabase(); + var element = context.Canneds + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Canneds.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/FishFactoryDatabaseImplement/Implements/ComponentStorage.cs b/FishFactoryDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..2021947 --- /dev/null +++ b/FishFactoryDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,76 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.StoragesContracts; +using FishFactoryContracts.ViewModels; +using FishFactoryDatabaseImplement.Models; + +namespace FishFactoryDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new FishFactoryDatabase(); + return context.Components + .Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new FishFactoryDatabase(); + return context.Components + .Where(x => x.ComponentName.Contains(model.ComponentName)) + .Select(x => x.GetViewModel) + .ToList(); + } + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + using var context = new FishFactoryDatabase(); + return context.Components + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public ComponentViewModel? Insert(ComponentBindingModel model) + { + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + using var context = new FishFactoryDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new FishFactoryDatabase(); + var component = context.Components.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + public ComponentViewModel? Delete(ComponentBindingModel model) + { + using var context = new FishFactoryDatabase(); + var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/FishFactoryDatabaseImplement/Implements/OrderStorage.cs b/FishFactoryDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..3c38dca --- /dev/null +++ b/FishFactoryDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,84 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.StoragesContracts; +using FishFactoryContracts.ViewModels; +using FishFactoryDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace FishFactoryDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new FishFactoryDatabase(); + return context.Orders + .Include(x => x.Canned) + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new FishFactoryDatabase(); + return context.Orders + .Include(x => x.Canned) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new FishFactoryDatabase(); + return context.Orders + .Include(x => x.Canned) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + public OrderViewModel? Insert(OrderBindingModel model) + { + using var context = new FishFactoryDatabase(); + if (model == null) + return null; + var newOrder = Order.Create(context, model); + if (newOrder == null) + { + return null; + } + context.Orders.Add(newOrder); + context.SaveChanges(); + return newOrder.GetViewModel; + } + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new FishFactoryDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return order.GetViewModel; + } + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new FishFactoryDatabase(); + var order = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (order != null) + { + context.Orders.Remove(order); + context.SaveChanges(); + return order.GetViewModel; + } + return null; + } + } +} diff --git a/FishFactoryDatabaseImplement/Models/Canned.cs b/FishFactoryDatabaseImplement/Models/Canned.cs new file mode 100644 index 0000000..9202805 --- /dev/null +++ b/FishFactoryDatabaseImplement/Models/Canned.cs @@ -0,0 +1,88 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.ViewModels; +using FishFactoryDataModel.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; + +namespace FishFactoryDatabaseImplement.Models +{ + public class Canned + { + public int Id { get; set; } + [Required] + public string CannedName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _cannedComponents = null; + [NotMapped] + public Dictionary CannedComponents + { + get + { + if (_cannedComponents == null) + { + _cannedComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count)); + } + return _cannedComponents; + } + } + [ForeignKey("CannedId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("CannedId")] + public virtual List Orders { get; set; } = new(); + public static Canned Create(FishFactoryDatabase context, CannedBindingModel model) + { + return new Canned() + { + Id = model.Id, + CannedName = model.CannedName, + Price = model.Price, + Components = model.CannedComponents.Select(x => new CannedComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(CannedBindingModel model) + { + CannedName = model.CannedName; + Price = model.Price; + } + public CannedViewModel GetViewModel => new() + { + Id = Id, + CannedName = CannedName, + Price = Price, + CannedComponents = CannedComponents + }; + public void UpdateComponents(FishFactoryDatabase context, CannedBindingModel model) + { + var cannedComponents = context.CannedComponents.Where(rec => rec.CannedId == model.Id).ToList(); + if (cannedComponents != null && cannedComponents.Count > 0) + { // удалили те, которых нет в модели + context.CannedComponents.RemoveRange(cannedComponents.Where(rec => !model.CannedComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in cannedComponents) + { + updateComponent.Count = model.CannedComponents[updateComponent.ComponentId].Item2; + model.CannedComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var canned = context.Canneds.First(x => x.Id == Id); + foreach (var pc in model.CannedComponents) + { + context.CannedComponents.Add(new CannedComponent + { + Canned = canned, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _cannedComponents = null; + } + } +} diff --git a/FishFactoryDatabaseImplement/Models/CannedComponent.cs b/FishFactoryDatabaseImplement/Models/CannedComponent.cs new file mode 100644 index 0000000..785a669 --- /dev/null +++ b/FishFactoryDatabaseImplement/Models/CannedComponent.cs @@ -0,0 +1,17 @@ +using System.ComponentModel.DataAnnotations; + +namespace FishFactoryDatabaseImplement.Models +{ + public class CannedComponent + { + public int Id { get; set; } + [Required] + public int CannedId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Canned Canned { get; set; } = new(); + } +} \ No newline at end of file diff --git a/FishFactoryDatabaseImplement/Models/Component.cs b/FishFactoryDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..d811164 --- /dev/null +++ b/FishFactoryDatabaseImplement/Models/Component.cs @@ -0,0 +1,56 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.ViewModels; +using FishFactoryDataModel.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace FishFactoryDatabaseImplement.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 CannedComponents { 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/FishFactoryDatabaseImplement/Models/Order.cs b/FishFactoryDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..4b24b0e --- /dev/null +++ b/FishFactoryDatabaseImplement/Models/Order.cs @@ -0,0 +1,55 @@ +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using FishFactoryDataModel.Enums; +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.ViewModels; + +namespace FishFactoryDatabaseImplement.Models +{ + public class Order + { + public int Id { get; set; } + [Required] + public int CannedId { get; set; } + public virtual Canned Canned { get; set; } = new(); + [Required] + public int Count { get; set; } + [Required] + public double Sum { get; set; } + [Required] + public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; + [Required] + public DateTime DateCreate { get; set; } = DateTime.Now; + public DateTime? DateImplement { get; set; } + public static Order Create (FishFactoryDatabase context, OrderBindingModel model) + { + return new Order() + { + Id = model.Id, + CannedId = model.CannedId, + Canned = context.Canneds.First(x => x.Id == model.CannedId), + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + }; + } + public void Update(OrderBindingModel model) + { + Status = model.Status; + DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + CannedId = CannedId, + CannedName = Canned.CannedName, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + }; + } +} -- 2.25.1 From cb531ff862bb9650f309c0af28c92f01804185ca Mon Sep 17 00:00:00 2001 From: dex_moth Date: Sun, 17 Mar 2024 20:57:13 +0400 Subject: [PATCH 2/5] =?UTF-8?q?Revert=20"=D0=B3=D0=BE=D1=82=D0=BE=D0=B2?= =?UTF-8?q?=D0=BE"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 00eee48168a9e7a7f7d90ccd6ac220b509870cfc. --- FishFactory/FishFactory.csproj | 7 -- FishFactory/FishFactory.sln | 10 -- FishFactory/Program.cs | 2 +- .../StoragesContracts/IOrderStorage.cs | 5 + .../FishFactoryDatabase.cs | 21 ---- .../FishFactoryDatabaseImplement.csproj | 23 ---- .../Implements/CannedStorage.cs | 101 ------------------ .../Implements/ComponentStorage.cs | 76 ------------- .../Implements/OrderStorage.cs | 84 --------------- FishFactoryDatabaseImplement/Models/Canned.cs | 88 --------------- .../Models/CannedComponent.cs | 17 --- .../Models/Component.cs | 56 ---------- FishFactoryDatabaseImplement/Models/Order.cs | 55 ---------- 13 files changed, 6 insertions(+), 539 deletions(-) delete mode 100644 FishFactoryDatabaseImplement/FishFactoryDatabase.cs delete mode 100644 FishFactoryDatabaseImplement/FishFactoryDatabaseImplement.csproj delete mode 100644 FishFactoryDatabaseImplement/Implements/CannedStorage.cs delete mode 100644 FishFactoryDatabaseImplement/Implements/ComponentStorage.cs delete mode 100644 FishFactoryDatabaseImplement/Implements/OrderStorage.cs delete mode 100644 FishFactoryDatabaseImplement/Models/Canned.cs delete mode 100644 FishFactoryDatabaseImplement/Models/CannedComponent.cs delete mode 100644 FishFactoryDatabaseImplement/Models/Component.cs delete mode 100644 FishFactoryDatabaseImplement/Models/Order.cs diff --git a/FishFactory/FishFactory.csproj b/FishFactory/FishFactory.csproj index 13ee669..95274fe 100644 --- a/FishFactory/FishFactory.csproj +++ b/FishFactory/FishFactory.csproj @@ -9,12 +9,6 @@ - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - @@ -22,7 +16,6 @@ - diff --git a/FishFactory/FishFactory.sln b/FishFactory/FishFactory.sln index a986a5c..c761982 100644 --- a/FishFactory/FishFactory.sln +++ b/FishFactory/FishFactory.sln @@ -15,8 +15,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FishFactoryListImplement", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FishFactoryFileImplement", "..\FishFactoryFileImplement\FishFactoryFileImplement.csproj", "{4EB943AB-67A8-4386-8874-FA0E3E98159C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FishFactoryDatabaseImplement", "..\FishFactoryDatabaseImplement\FishFactoryDatabaseImplement.csproj", "{5744376D-B7D8-4FFD-B6D9-F53C169B056C}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -73,14 +71,6 @@ Global {4EB943AB-67A8-4386-8874-FA0E3E98159C}.Release|Any CPU.Build.0 = Release|Any CPU {4EB943AB-67A8-4386-8874-FA0E3E98159C}.Release|x86.ActiveCfg = Release|Any CPU {4EB943AB-67A8-4386-8874-FA0E3E98159C}.Release|x86.Build.0 = Release|Any CPU - {5744376D-B7D8-4FFD-B6D9-F53C169B056C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {5744376D-B7D8-4FFD-B6D9-F53C169B056C}.Debug|Any CPU.Build.0 = Debug|Any CPU - {5744376D-B7D8-4FFD-B6D9-F53C169B056C}.Debug|x86.ActiveCfg = Debug|Any CPU - {5744376D-B7D8-4FFD-B6D9-F53C169B056C}.Debug|x86.Build.0 = Debug|Any CPU - {5744376D-B7D8-4FFD-B6D9-F53C169B056C}.Release|Any CPU.ActiveCfg = Release|Any CPU - {5744376D-B7D8-4FFD-B6D9-F53C169B056C}.Release|Any CPU.Build.0 = Release|Any CPU - {5744376D-B7D8-4FFD-B6D9-F53C169B056C}.Release|x86.ActiveCfg = Release|Any CPU - {5744376D-B7D8-4FFD-B6D9-F53C169B056C}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/FishFactory/Program.cs b/FishFactory/Program.cs index ba4bc0a..1aa0d38 100644 --- a/FishFactory/Program.cs +++ b/FishFactory/Program.cs @@ -2,7 +2,7 @@ using FishFactory.Forms; using FishFactoryContracts.BusinessLogicsContracts; using FishFactoryBusinessLogic.BusinessLogic; using FishFactoryContracts.StoragesContracts; -using FishFactoryDatabaseImplement.Implements; +using FishFactoryFileImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/FishFactoryContracts/StoragesContracts/IOrderStorage.cs b/FishFactoryContracts/StoragesContracts/IOrderStorage.cs index a81b868..1f385c5 100644 --- a/FishFactoryContracts/StoragesContracts/IOrderStorage.cs +++ b/FishFactoryContracts/StoragesContracts/IOrderStorage.cs @@ -1,6 +1,11 @@ using FishFactoryContracts.BindingModels; using FishFactoryContracts.SearchModels; using FishFactoryContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; namespace FishFactoryContracts.StoragesContracts { diff --git a/FishFactoryDatabaseImplement/FishFactoryDatabase.cs b/FishFactoryDatabaseImplement/FishFactoryDatabase.cs deleted file mode 100644 index 89c09ee..0000000 --- a/FishFactoryDatabaseImplement/FishFactoryDatabase.cs +++ /dev/null @@ -1,21 +0,0 @@ -using FishFactoryDatabaseImplement.Models; -using Microsoft.EntityFrameworkCore; - -namespace FishFactoryDatabaseImplement -{ - public class FishFactoryDatabase : DbContext - { - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - { - if (optionsBuilder.IsConfigured == false) - { - optionsBuilder.UseSqlServer(@"Data Source=CHESHIR\SQLEXPRESS;Initial Catalog=FishFactoryDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); - } - base.OnConfiguring(optionsBuilder); - } - public virtual DbSet Components { set; get; } - public virtual DbSet Canneds { set; get; } - public virtual DbSet CannedComponents { set; get; } - public virtual DbSet Orders { set; get; } - } -} diff --git a/FishFactoryDatabaseImplement/FishFactoryDatabaseImplement.csproj b/FishFactoryDatabaseImplement/FishFactoryDatabaseImplement.csproj deleted file mode 100644 index 338b17d..0000000 --- a/FishFactoryDatabaseImplement/FishFactoryDatabaseImplement.csproj +++ /dev/null @@ -1,23 +0,0 @@ - - - - net6.0 - enable - enable - - - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - - - - - - diff --git a/FishFactoryDatabaseImplement/Implements/CannedStorage.cs b/FishFactoryDatabaseImplement/Implements/CannedStorage.cs deleted file mode 100644 index 526a8a5..0000000 --- a/FishFactoryDatabaseImplement/Implements/CannedStorage.cs +++ /dev/null @@ -1,101 +0,0 @@ -using FishFactoryContracts.BindingModels; -using FishFactoryContracts.SearchModels; -using FishFactoryContracts.StoragesContracts; -using FishFactoryContracts.ViewModels; -using FishFactoryDatabaseImplement.Models; -using Microsoft.EntityFrameworkCore; - -namespace FishFactoryDatabaseImplement.Implements -{ - public class CannedStorage : ICannedStorage - { - public List GetFullList() - { - using var context = new FishFactoryDatabase(); - return context.Canneds - .Include(x => x.Components) - .ThenInclude(x => x.Component) - .ToList() - .Select(x => x.GetViewModel) - .ToList(); - } - public List GetFilteredList(CannedSearchModel model) - { - if (string.IsNullOrEmpty(model.CannedName)) - { - return new(); - } - using var context = new FishFactoryDatabase(); - return context.Canneds - .Include(x => x.Components) - .ThenInclude(x => x.Component) - .Where(x => x.CannedName.Contains(model.CannedName)) - .ToList() - .Select(x => x.GetViewModel) - .ToList(); - } - public CannedViewModel? GetElement(CannedSearchModel model) - { - if (string.IsNullOrEmpty(model.CannedName) && !model.Id.HasValue) - { - return null; - } - using var context = new FishFactoryDatabase(); - return context.Canneds - .Include(x => x.Components) - .ThenInclude(x => x.Component) - .FirstOrDefault(x => (!string.IsNullOrEmpty(model.CannedName) && x.CannedName == model.CannedName) || - (model.Id.HasValue && x.Id == model.Id)) - ?.GetViewModel; - } - public CannedViewModel? Insert(CannedBindingModel model) - { - using var context = new FishFactoryDatabase(); - var newCanned = Canned.Create(context, model); - if (newCanned == null) - { - return null; - } - context.Canneds.Add(newCanned); - context.SaveChanges(); - return newCanned.GetViewModel; - } - public CannedViewModel? Update(CannedBindingModel model) - { - using var context = new FishFactoryDatabase(); - using var transaction = context.Database.BeginTransaction(); - try - { - var product = context.Canneds.FirstOrDefault(rec => rec.Id == model.Id); - if (product == null) - { - return null; - } - product.Update(model); - context.SaveChanges(); - product.UpdateComponents(context, model); - transaction.Commit(); - return product.GetViewModel; - } - catch - { - transaction.Rollback(); - throw; - } - } - public CannedViewModel? Delete(CannedBindingModel model) - { - using var context = new FishFactoryDatabase(); - var element = context.Canneds - .Include(x => x.Components) - .FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) - { - context.Canneds.Remove(element); - context.SaveChanges(); - return element.GetViewModel; - } - return null; - } - } -} diff --git a/FishFactoryDatabaseImplement/Implements/ComponentStorage.cs b/FishFactoryDatabaseImplement/Implements/ComponentStorage.cs deleted file mode 100644 index 2021947..0000000 --- a/FishFactoryDatabaseImplement/Implements/ComponentStorage.cs +++ /dev/null @@ -1,76 +0,0 @@ -using FishFactoryContracts.BindingModels; -using FishFactoryContracts.SearchModels; -using FishFactoryContracts.StoragesContracts; -using FishFactoryContracts.ViewModels; -using FishFactoryDatabaseImplement.Models; - -namespace FishFactoryDatabaseImplement.Implements -{ - public class ComponentStorage : IComponentStorage - { - public List GetFullList() - { - using var context = new FishFactoryDatabase(); - return context.Components - .Select(x => x.GetViewModel).ToList(); - } - public List GetFilteredList(ComponentSearchModel model) - { - if (string.IsNullOrEmpty(model.ComponentName)) - { - return new(); - } - using var context = new FishFactoryDatabase(); - return context.Components - .Where(x => x.ComponentName.Contains(model.ComponentName)) - .Select(x => x.GetViewModel) - .ToList(); - } - public ComponentViewModel? GetElement(ComponentSearchModel model) - { - if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) - { - return null; - } - using var context = new FishFactoryDatabase(); - return context.Components - .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; - } - public ComponentViewModel? Insert(ComponentBindingModel model) - { - var newComponent = Component.Create(model); - if (newComponent == null) - { - return null; - } - using var context = new FishFactoryDatabase(); - context.Components.Add(newComponent); - context.SaveChanges(); - return newComponent.GetViewModel; - } - public ComponentViewModel? Update(ComponentBindingModel model) - { - using var context = new FishFactoryDatabase(); - var component = context.Components.FirstOrDefault(x => x.Id == model.Id); - if (component == null) - { - return null; - } - component.Update(model); - context.SaveChanges(); - return component.GetViewModel; - } - public ComponentViewModel? Delete(ComponentBindingModel model) - { - using var context = new FishFactoryDatabase(); - var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) - { - context.Components.Remove(element); - context.SaveChanges(); - return element.GetViewModel; - } - return null; - } - } -} diff --git a/FishFactoryDatabaseImplement/Implements/OrderStorage.cs b/FishFactoryDatabaseImplement/Implements/OrderStorage.cs deleted file mode 100644 index 3c38dca..0000000 --- a/FishFactoryDatabaseImplement/Implements/OrderStorage.cs +++ /dev/null @@ -1,84 +0,0 @@ -using FishFactoryContracts.BindingModels; -using FishFactoryContracts.SearchModels; -using FishFactoryContracts.StoragesContracts; -using FishFactoryContracts.ViewModels; -using FishFactoryDatabaseImplement.Models; -using Microsoft.EntityFrameworkCore; - -namespace FishFactoryDatabaseImplement.Implements -{ - public class OrderStorage : IOrderStorage - { - public List GetFullList() - { - using var context = new FishFactoryDatabase(); - return context.Orders - .Include(x => x.Canned) - .Select(x => x.GetViewModel) - .ToList(); - } - public List GetFilteredList(OrderSearchModel model) - { - if (!model.Id.HasValue) - { - return new(); - } - using var context = new FishFactoryDatabase(); - return context.Orders - .Include(x => x.Canned) - .Where(x => x.Id == model.Id) - .Select(x => x.GetViewModel) - .ToList(); - } - public OrderViewModel? GetElement(OrderSearchModel model) - { - if (!model.Id.HasValue) - { - return new(); - } - using var context = new FishFactoryDatabase(); - return context.Orders - .Include(x => x.Canned) - .FirstOrDefault(x => x.Id == model.Id) - ?.GetViewModel; - } - public OrderViewModel? Insert(OrderBindingModel model) - { - using var context = new FishFactoryDatabase(); - if (model == null) - return null; - var newOrder = Order.Create(context, model); - if (newOrder == null) - { - return null; - } - context.Orders.Add(newOrder); - context.SaveChanges(); - return newOrder.GetViewModel; - } - public OrderViewModel? Update(OrderBindingModel model) - { - using var context = new FishFactoryDatabase(); - var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); - if (order == null) - { - return null; - } - order.Update(model); - context.SaveChanges(); - return order.GetViewModel; - } - public OrderViewModel? Delete(OrderBindingModel model) - { - using var context = new FishFactoryDatabase(); - var order = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); - if (order != null) - { - context.Orders.Remove(order); - context.SaveChanges(); - return order.GetViewModel; - } - return null; - } - } -} diff --git a/FishFactoryDatabaseImplement/Models/Canned.cs b/FishFactoryDatabaseImplement/Models/Canned.cs deleted file mode 100644 index 9202805..0000000 --- a/FishFactoryDatabaseImplement/Models/Canned.cs +++ /dev/null @@ -1,88 +0,0 @@ -using FishFactoryContracts.BindingModels; -using FishFactoryContracts.ViewModels; -using FishFactoryDataModel.Models; -using System.ComponentModel.DataAnnotations.Schema; -using System.ComponentModel.DataAnnotations; - -namespace FishFactoryDatabaseImplement.Models -{ - public class Canned - { - public int Id { get; set; } - [Required] - public string CannedName { get; set; } = string.Empty; - [Required] - public double Price { get; set; } - private Dictionary? _cannedComponents = null; - [NotMapped] - public Dictionary CannedComponents - { - get - { - if (_cannedComponents == null) - { - _cannedComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count)); - } - return _cannedComponents; - } - } - [ForeignKey("CannedId")] - public virtual List Components { get; set; } = new(); - [ForeignKey("CannedId")] - public virtual List Orders { get; set; } = new(); - public static Canned Create(FishFactoryDatabase context, CannedBindingModel model) - { - return new Canned() - { - Id = model.Id, - CannedName = model.CannedName, - Price = model.Price, - Components = model.CannedComponents.Select(x => new CannedComponent - { - Component = context.Components.First(y => y.Id == x.Key), - Count = x.Value.Item2 - }).ToList() - }; - } - public void Update(CannedBindingModel model) - { - CannedName = model.CannedName; - Price = model.Price; - } - public CannedViewModel GetViewModel => new() - { - Id = Id, - CannedName = CannedName, - Price = Price, - CannedComponents = CannedComponents - }; - public void UpdateComponents(FishFactoryDatabase context, CannedBindingModel model) - { - var cannedComponents = context.CannedComponents.Where(rec => rec.CannedId == model.Id).ToList(); - if (cannedComponents != null && cannedComponents.Count > 0) - { // удалили те, которых нет в модели - context.CannedComponents.RemoveRange(cannedComponents.Where(rec => !model.CannedComponents.ContainsKey(rec.ComponentId))); - context.SaveChanges(); - // обновили количество у существующих записей - foreach (var updateComponent in cannedComponents) - { - updateComponent.Count = model.CannedComponents[updateComponent.ComponentId].Item2; - model.CannedComponents.Remove(updateComponent.ComponentId); - } - context.SaveChanges(); - } - var canned = context.Canneds.First(x => x.Id == Id); - foreach (var pc in model.CannedComponents) - { - context.CannedComponents.Add(new CannedComponent - { - Canned = canned, - Component = context.Components.First(x => x.Id == pc.Key), - Count = pc.Value.Item2 - }); - context.SaveChanges(); - } - _cannedComponents = null; - } - } -} diff --git a/FishFactoryDatabaseImplement/Models/CannedComponent.cs b/FishFactoryDatabaseImplement/Models/CannedComponent.cs deleted file mode 100644 index 785a669..0000000 --- a/FishFactoryDatabaseImplement/Models/CannedComponent.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System.ComponentModel.DataAnnotations; - -namespace FishFactoryDatabaseImplement.Models -{ - public class CannedComponent - { - public int Id { get; set; } - [Required] - public int CannedId { get; set; } - [Required] - public int ComponentId { get; set; } - [Required] - public int Count { get; set; } - public virtual Component Component { get; set; } = new(); - public virtual Canned Canned { get; set; } = new(); - } -} \ No newline at end of file diff --git a/FishFactoryDatabaseImplement/Models/Component.cs b/FishFactoryDatabaseImplement/Models/Component.cs deleted file mode 100644 index d811164..0000000 --- a/FishFactoryDatabaseImplement/Models/Component.cs +++ /dev/null @@ -1,56 +0,0 @@ -using FishFactoryContracts.BindingModels; -using FishFactoryContracts.ViewModels; -using FishFactoryDataModel.Models; -using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; - -namespace FishFactoryDatabaseImplement.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 CannedComponents { 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/FishFactoryDatabaseImplement/Models/Order.cs b/FishFactoryDatabaseImplement/Models/Order.cs deleted file mode 100644 index 4b24b0e..0000000 --- a/FishFactoryDatabaseImplement/Models/Order.cs +++ /dev/null @@ -1,55 +0,0 @@ -using System.ComponentModel.DataAnnotations.Schema; -using System.ComponentModel.DataAnnotations; -using FishFactoryDataModel.Enums; -using FishFactoryContracts.BindingModels; -using FishFactoryContracts.ViewModels; - -namespace FishFactoryDatabaseImplement.Models -{ - public class Order - { - public int Id { get; set; } - [Required] - public int CannedId { get; set; } - public virtual Canned Canned { get; set; } = new(); - [Required] - public int Count { get; set; } - [Required] - public double Sum { get; set; } - [Required] - public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; - [Required] - public DateTime DateCreate { get; set; } = DateTime.Now; - public DateTime? DateImplement { get; set; } - public static Order Create (FishFactoryDatabase context, OrderBindingModel model) - { - return new Order() - { - Id = model.Id, - CannedId = model.CannedId, - Canned = context.Canneds.First(x => x.Id == model.CannedId), - Count = model.Count, - Sum = model.Sum, - Status = model.Status, - DateCreate = model.DateCreate, - DateImplement = model.DateImplement, - }; - } - public void Update(OrderBindingModel model) - { - Status = model.Status; - DateImplement = model.DateImplement; - } - public OrderViewModel GetViewModel => new() - { - Id = Id, - CannedId = CannedId, - CannedName = Canned.CannedName, - Count = Count, - Sum = Sum, - Status = Status, - DateCreate = DateCreate, - DateImplement = DateImplement, - }; - } -} -- 2.25.1 From 657f9469887fa323b260eb6c5bc5dfca1904d33e Mon Sep 17 00:00:00 2001 From: dex_moth Date: Sun, 17 Mar 2024 21:00:26 +0400 Subject: [PATCH 3/5] =?UTF-8?q?=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FishFactory/FishFactory.csproj | 7 ++ FishFactory/FishFactory.sln | 10 ++ FishFactory/Program.cs | 2 +- .../StoragesContracts/IOrderStorage.cs | 5 - .../FishFactoryDatabase.cs | 21 ++++ .../FishFactoryDatabaseImplement.csproj | 23 ++++ .../Implements/CannedStorage.cs | 101 ++++++++++++++++++ .../Implements/ComponentStorage.cs | 76 +++++++++++++ .../Implements/OrderStorage.cs | 84 +++++++++++++++ FishFactoryDatabaseImplement/Models/Canned.cs | 88 +++++++++++++++ .../Models/CannedComponent.cs | 17 +++ .../Models/Component.cs | 56 ++++++++++ FishFactoryDatabaseImplement/Models/Order.cs | 55 ++++++++++ 13 files changed, 539 insertions(+), 6 deletions(-) create mode 100644 FishFactoryDatabaseImplement/FishFactoryDatabase.cs create mode 100644 FishFactoryDatabaseImplement/FishFactoryDatabaseImplement.csproj create mode 100644 FishFactoryDatabaseImplement/Implements/CannedStorage.cs create mode 100644 FishFactoryDatabaseImplement/Implements/ComponentStorage.cs create mode 100644 FishFactoryDatabaseImplement/Implements/OrderStorage.cs create mode 100644 FishFactoryDatabaseImplement/Models/Canned.cs create mode 100644 FishFactoryDatabaseImplement/Models/CannedComponent.cs create mode 100644 FishFactoryDatabaseImplement/Models/Component.cs create mode 100644 FishFactoryDatabaseImplement/Models/Order.cs diff --git a/FishFactory/FishFactory.csproj b/FishFactory/FishFactory.csproj index 95274fe..13ee669 100644 --- a/FishFactory/FishFactory.csproj +++ b/FishFactory/FishFactory.csproj @@ -9,6 +9,12 @@ + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + @@ -16,6 +22,7 @@ + diff --git a/FishFactory/FishFactory.sln b/FishFactory/FishFactory.sln index c761982..a986a5c 100644 --- a/FishFactory/FishFactory.sln +++ b/FishFactory/FishFactory.sln @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FishFactoryListImplement", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FishFactoryFileImplement", "..\FishFactoryFileImplement\FishFactoryFileImplement.csproj", "{4EB943AB-67A8-4386-8874-FA0E3E98159C}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FishFactoryDatabaseImplement", "..\FishFactoryDatabaseImplement\FishFactoryDatabaseImplement.csproj", "{5744376D-B7D8-4FFD-B6D9-F53C169B056C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -71,6 +73,14 @@ Global {4EB943AB-67A8-4386-8874-FA0E3E98159C}.Release|Any CPU.Build.0 = Release|Any CPU {4EB943AB-67A8-4386-8874-FA0E3E98159C}.Release|x86.ActiveCfg = Release|Any CPU {4EB943AB-67A8-4386-8874-FA0E3E98159C}.Release|x86.Build.0 = Release|Any CPU + {5744376D-B7D8-4FFD-B6D9-F53C169B056C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5744376D-B7D8-4FFD-B6D9-F53C169B056C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5744376D-B7D8-4FFD-B6D9-F53C169B056C}.Debug|x86.ActiveCfg = Debug|Any CPU + {5744376D-B7D8-4FFD-B6D9-F53C169B056C}.Debug|x86.Build.0 = Debug|Any CPU + {5744376D-B7D8-4FFD-B6D9-F53C169B056C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5744376D-B7D8-4FFD-B6D9-F53C169B056C}.Release|Any CPU.Build.0 = Release|Any CPU + {5744376D-B7D8-4FFD-B6D9-F53C169B056C}.Release|x86.ActiveCfg = Release|Any CPU + {5744376D-B7D8-4FFD-B6D9-F53C169B056C}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/FishFactory/Program.cs b/FishFactory/Program.cs index 1aa0d38..ba4bc0a 100644 --- a/FishFactory/Program.cs +++ b/FishFactory/Program.cs @@ -2,7 +2,7 @@ using FishFactory.Forms; using FishFactoryContracts.BusinessLogicsContracts; using FishFactoryBusinessLogic.BusinessLogic; using FishFactoryContracts.StoragesContracts; -using FishFactoryFileImplement.Implements; +using FishFactoryDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/FishFactoryContracts/StoragesContracts/IOrderStorage.cs b/FishFactoryContracts/StoragesContracts/IOrderStorage.cs index 1f385c5..a81b868 100644 --- a/FishFactoryContracts/StoragesContracts/IOrderStorage.cs +++ b/FishFactoryContracts/StoragesContracts/IOrderStorage.cs @@ -1,11 +1,6 @@ using FishFactoryContracts.BindingModels; using FishFactoryContracts.SearchModels; using FishFactoryContracts.ViewModels; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace FishFactoryContracts.StoragesContracts { diff --git a/FishFactoryDatabaseImplement/FishFactoryDatabase.cs b/FishFactoryDatabaseImplement/FishFactoryDatabase.cs new file mode 100644 index 0000000..89c09ee --- /dev/null +++ b/FishFactoryDatabaseImplement/FishFactoryDatabase.cs @@ -0,0 +1,21 @@ +using FishFactoryDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace FishFactoryDatabaseImplement +{ + public class FishFactoryDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=CHESHIR\SQLEXPRESS;Initial Catalog=FishFactoryDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Canneds { set; get; } + public virtual DbSet CannedComponents { set; get; } + public virtual DbSet Orders { set; get; } + } +} diff --git a/FishFactoryDatabaseImplement/FishFactoryDatabaseImplement.csproj b/FishFactoryDatabaseImplement/FishFactoryDatabaseImplement.csproj new file mode 100644 index 0000000..338b17d --- /dev/null +++ b/FishFactoryDatabaseImplement/FishFactoryDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + diff --git a/FishFactoryDatabaseImplement/Implements/CannedStorage.cs b/FishFactoryDatabaseImplement/Implements/CannedStorage.cs new file mode 100644 index 0000000..526a8a5 --- /dev/null +++ b/FishFactoryDatabaseImplement/Implements/CannedStorage.cs @@ -0,0 +1,101 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.StoragesContracts; +using FishFactoryContracts.ViewModels; +using FishFactoryDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace FishFactoryDatabaseImplement.Implements +{ + public class CannedStorage : ICannedStorage + { + public List GetFullList() + { + using var context = new FishFactoryDatabase(); + return context.Canneds + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(CannedSearchModel model) + { + if (string.IsNullOrEmpty(model.CannedName)) + { + return new(); + } + using var context = new FishFactoryDatabase(); + return context.Canneds + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.CannedName.Contains(model.CannedName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public CannedViewModel? GetElement(CannedSearchModel model) + { + if (string.IsNullOrEmpty(model.CannedName) && !model.Id.HasValue) + { + return null; + } + using var context = new FishFactoryDatabase(); + return context.Canneds + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.CannedName) && x.CannedName == model.CannedName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + public CannedViewModel? Insert(CannedBindingModel model) + { + using var context = new FishFactoryDatabase(); + var newCanned = Canned.Create(context, model); + if (newCanned == null) + { + return null; + } + context.Canneds.Add(newCanned); + context.SaveChanges(); + return newCanned.GetViewModel; + } + public CannedViewModel? Update(CannedBindingModel model) + { + using var context = new FishFactoryDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var product = context.Canneds.FirstOrDefault(rec => rec.Id == model.Id); + if (product == null) + { + return null; + } + product.Update(model); + context.SaveChanges(); + product.UpdateComponents(context, model); + transaction.Commit(); + return product.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public CannedViewModel? Delete(CannedBindingModel model) + { + using var context = new FishFactoryDatabase(); + var element = context.Canneds + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Canneds.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/FishFactoryDatabaseImplement/Implements/ComponentStorage.cs b/FishFactoryDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..2021947 --- /dev/null +++ b/FishFactoryDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,76 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.StoragesContracts; +using FishFactoryContracts.ViewModels; +using FishFactoryDatabaseImplement.Models; + +namespace FishFactoryDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new FishFactoryDatabase(); + return context.Components + .Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new FishFactoryDatabase(); + return context.Components + .Where(x => x.ComponentName.Contains(model.ComponentName)) + .Select(x => x.GetViewModel) + .ToList(); + } + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + using var context = new FishFactoryDatabase(); + return context.Components + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public ComponentViewModel? Insert(ComponentBindingModel model) + { + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + using var context = new FishFactoryDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new FishFactoryDatabase(); + var component = context.Components.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + public ComponentViewModel? Delete(ComponentBindingModel model) + { + using var context = new FishFactoryDatabase(); + var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/FishFactoryDatabaseImplement/Implements/OrderStorage.cs b/FishFactoryDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..3c38dca --- /dev/null +++ b/FishFactoryDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,84 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.StoragesContracts; +using FishFactoryContracts.ViewModels; +using FishFactoryDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace FishFactoryDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new FishFactoryDatabase(); + return context.Orders + .Include(x => x.Canned) + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new FishFactoryDatabase(); + return context.Orders + .Include(x => x.Canned) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new FishFactoryDatabase(); + return context.Orders + .Include(x => x.Canned) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + public OrderViewModel? Insert(OrderBindingModel model) + { + using var context = new FishFactoryDatabase(); + if (model == null) + return null; + var newOrder = Order.Create(context, model); + if (newOrder == null) + { + return null; + } + context.Orders.Add(newOrder); + context.SaveChanges(); + return newOrder.GetViewModel; + } + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new FishFactoryDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return order.GetViewModel; + } + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new FishFactoryDatabase(); + var order = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (order != null) + { + context.Orders.Remove(order); + context.SaveChanges(); + return order.GetViewModel; + } + return null; + } + } +} diff --git a/FishFactoryDatabaseImplement/Models/Canned.cs b/FishFactoryDatabaseImplement/Models/Canned.cs new file mode 100644 index 0000000..9202805 --- /dev/null +++ b/FishFactoryDatabaseImplement/Models/Canned.cs @@ -0,0 +1,88 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.ViewModels; +using FishFactoryDataModel.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; + +namespace FishFactoryDatabaseImplement.Models +{ + public class Canned + { + public int Id { get; set; } + [Required] + public string CannedName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _cannedComponents = null; + [NotMapped] + public Dictionary CannedComponents + { + get + { + if (_cannedComponents == null) + { + _cannedComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count)); + } + return _cannedComponents; + } + } + [ForeignKey("CannedId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("CannedId")] + public virtual List Orders { get; set; } = new(); + public static Canned Create(FishFactoryDatabase context, CannedBindingModel model) + { + return new Canned() + { + Id = model.Id, + CannedName = model.CannedName, + Price = model.Price, + Components = model.CannedComponents.Select(x => new CannedComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(CannedBindingModel model) + { + CannedName = model.CannedName; + Price = model.Price; + } + public CannedViewModel GetViewModel => new() + { + Id = Id, + CannedName = CannedName, + Price = Price, + CannedComponents = CannedComponents + }; + public void UpdateComponents(FishFactoryDatabase context, CannedBindingModel model) + { + var cannedComponents = context.CannedComponents.Where(rec => rec.CannedId == model.Id).ToList(); + if (cannedComponents != null && cannedComponents.Count > 0) + { // удалили те, которых нет в модели + context.CannedComponents.RemoveRange(cannedComponents.Where(rec => !model.CannedComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in cannedComponents) + { + updateComponent.Count = model.CannedComponents[updateComponent.ComponentId].Item2; + model.CannedComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var canned = context.Canneds.First(x => x.Id == Id); + foreach (var pc in model.CannedComponents) + { + context.CannedComponents.Add(new CannedComponent + { + Canned = canned, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _cannedComponents = null; + } + } +} diff --git a/FishFactoryDatabaseImplement/Models/CannedComponent.cs b/FishFactoryDatabaseImplement/Models/CannedComponent.cs new file mode 100644 index 0000000..785a669 --- /dev/null +++ b/FishFactoryDatabaseImplement/Models/CannedComponent.cs @@ -0,0 +1,17 @@ +using System.ComponentModel.DataAnnotations; + +namespace FishFactoryDatabaseImplement.Models +{ + public class CannedComponent + { + public int Id { get; set; } + [Required] + public int CannedId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Canned Canned { get; set; } = new(); + } +} \ No newline at end of file diff --git a/FishFactoryDatabaseImplement/Models/Component.cs b/FishFactoryDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..d811164 --- /dev/null +++ b/FishFactoryDatabaseImplement/Models/Component.cs @@ -0,0 +1,56 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.ViewModels; +using FishFactoryDataModel.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace FishFactoryDatabaseImplement.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 CannedComponents { 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/FishFactoryDatabaseImplement/Models/Order.cs b/FishFactoryDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..4b24b0e --- /dev/null +++ b/FishFactoryDatabaseImplement/Models/Order.cs @@ -0,0 +1,55 @@ +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using FishFactoryDataModel.Enums; +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.ViewModels; + +namespace FishFactoryDatabaseImplement.Models +{ + public class Order + { + public int Id { get; set; } + [Required] + public int CannedId { get; set; } + public virtual Canned Canned { get; set; } = new(); + [Required] + public int Count { get; set; } + [Required] + public double Sum { get; set; } + [Required] + public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; + [Required] + public DateTime DateCreate { get; set; } = DateTime.Now; + public DateTime? DateImplement { get; set; } + public static Order Create (FishFactoryDatabase context, OrderBindingModel model) + { + return new Order() + { + Id = model.Id, + CannedId = model.CannedId, + Canned = context.Canneds.First(x => x.Id == model.CannedId), + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + }; + } + public void Update(OrderBindingModel model) + { + Status = model.Status; + DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + CannedId = CannedId, + CannedName = Canned.CannedName, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + }; + } +} -- 2.25.1 From 1ad47faea0148b36e8aea17fe017a15eb442ab5d Mon Sep 17 00:00:00 2001 From: dex_moth Date: Tue, 19 Mar 2024 20:55:45 +0400 Subject: [PATCH 4/5] =?UTF-8?q?=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=BE=20?= =?UTF-8?q?=D1=82=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20=D1=82=D0=BE=D1=87=D0=BD?= =?UTF-8?q?=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FishFactory/FishFactory.csproj | 6 +- .../FishFactoryDatabase.cs | 4 +- .../FishFactoryDatabaseImplement.csproj | 7 +- .../20240319164852_InitialCreate.Designer.cs | 171 ++++++++++++++++++ .../20240319164852_InitialCreate.cs | 126 +++++++++++++ .../FishFactoryDatabaseModelSnapshot.cs | 168 +++++++++++++++++ FishFactoryDatabaseImplement/Models/Order.cs | 23 ++- 7 files changed, 489 insertions(+), 16 deletions(-) create mode 100644 FishFactoryDatabaseImplement/Migrations/20240319164852_InitialCreate.Designer.cs create mode 100644 FishFactoryDatabaseImplement/Migrations/20240319164852_InitialCreate.cs create mode 100644 FishFactoryDatabaseImplement/Migrations/FishFactoryDatabaseModelSnapshot.cs diff --git a/FishFactory/FishFactory.csproj b/FishFactory/FishFactory.csproj index 13ee669..07508e2 100644 --- a/FishFactory/FishFactory.csproj +++ b/FishFactory/FishFactory.csproj @@ -9,12 +9,10 @@ - - - runtime; build; native; contentfiles; analyzers; buildtransitive + all + runtime; build; native; contentfiles; analyzers; buildtransitive - diff --git a/FishFactoryDatabaseImplement/FishFactoryDatabase.cs b/FishFactoryDatabaseImplement/FishFactoryDatabase.cs index 89c09ee..d93d11d 100644 --- a/FishFactoryDatabaseImplement/FishFactoryDatabase.cs +++ b/FishFactoryDatabaseImplement/FishFactoryDatabase.cs @@ -9,9 +9,11 @@ namespace FishFactoryDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Data Source=CHESHIR\SQLEXPRESS;Initial Catalog=FishFactoryDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseNpgsql(@"Host=localhost;Database=FishFactory;Username=postgres;Password=postgres"); } base.OnConfiguring(optionsBuilder); + AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); + AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true); } public virtual DbSet Components { set; get; } public virtual DbSet Canneds { set; get; } diff --git a/FishFactoryDatabaseImplement/FishFactoryDatabaseImplement.csproj b/FishFactoryDatabaseImplement/FishFactoryDatabaseImplement.csproj index 338b17d..dbab214 100644 --- a/FishFactoryDatabaseImplement/FishFactoryDatabaseImplement.csproj +++ b/FishFactoryDatabaseImplement/FishFactoryDatabaseImplement.csproj @@ -7,12 +7,13 @@ - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive - + + diff --git a/FishFactoryDatabaseImplement/Migrations/20240319164852_InitialCreate.Designer.cs b/FishFactoryDatabaseImplement/Migrations/20240319164852_InitialCreate.Designer.cs new file mode 100644 index 0000000..a9f77ff --- /dev/null +++ b/FishFactoryDatabaseImplement/Migrations/20240319164852_InitialCreate.Designer.cs @@ -0,0 +1,171 @@ +// +using System; +using FishFactoryDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace FishFactoryDatabaseImplement.Migrations +{ + [DbContext(typeof(FishFactoryDatabase))] + [Migration("20240319164852_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.16") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Canned", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CannedName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Canneds"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.CannedComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CannedId") + .HasColumnType("integer"); + + b.Property("ComponentId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("CannedId"); + + b.HasIndex("ComponentId"); + + b.ToTable("CannedComponents"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CannedId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreate") + .HasColumnType("timestamp without time zone"); + + b.Property("DateImplement") + .HasColumnType("timestamp without time zone"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.HasIndex("CannedId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.CannedComponent", b => + { + b.HasOne("FishFactoryDatabaseImplement.Models.Canned", "Canned") + .WithMany("Components") + .HasForeignKey("CannedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FishFactoryDatabaseImplement.Models.Component", "Component") + .WithMany("CannedComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Canned"); + + b.Navigation("Component"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Order", b => + { + b.HasOne("FishFactoryDatabaseImplement.Models.Canned", "Canned") + .WithMany("Orders") + .HasForeignKey("CannedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Canned"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Canned", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Component", b => + { + b.Navigation("CannedComponents"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/FishFactoryDatabaseImplement/Migrations/20240319164852_InitialCreate.cs b/FishFactoryDatabaseImplement/Migrations/20240319164852_InitialCreate.cs new file mode 100644 index 0000000..e05d406 --- /dev/null +++ b/FishFactoryDatabaseImplement/Migrations/20240319164852_InitialCreate.cs @@ -0,0 +1,126 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace FishFactoryDatabaseImplement.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Canneds", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + CannedName = table.Column(type: "text", nullable: false), + Price = table.Column(type: "double precision", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Canneds", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Components", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + ComponentName = table.Column(type: "text", nullable: false), + Cost = table.Column(type: "double precision", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Components", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + CannedId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false), + Sum = table.Column(type: "double precision", nullable: false), + Status = table.Column(type: "integer", nullable: false), + DateCreate = table.Column(type: "timestamp without time zone", nullable: false), + DateImplement = table.Column(type: "timestamp without time zone", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Canneds_CannedId", + column: x => x.CannedId, + principalTable: "Canneds", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "CannedComponents", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + CannedId = table.Column(type: "integer", nullable: false), + ComponentId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_CannedComponents", x => x.Id); + table.ForeignKey( + name: "FK_CannedComponents_Canneds_CannedId", + column: x => x.CannedId, + principalTable: "Canneds", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_CannedComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_CannedComponents_CannedId", + table: "CannedComponents", + column: "CannedId"); + + migrationBuilder.CreateIndex( + name: "IX_CannedComponents_ComponentId", + table: "CannedComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_CannedId", + table: "Orders", + column: "CannedId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "CannedComponents"); + + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Canneds"); + } + } +} diff --git a/FishFactoryDatabaseImplement/Migrations/FishFactoryDatabaseModelSnapshot.cs b/FishFactoryDatabaseImplement/Migrations/FishFactoryDatabaseModelSnapshot.cs new file mode 100644 index 0000000..523f92c --- /dev/null +++ b/FishFactoryDatabaseImplement/Migrations/FishFactoryDatabaseModelSnapshot.cs @@ -0,0 +1,168 @@ +// +using System; +using FishFactoryDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace FishFactoryDatabaseImplement.Migrations +{ + [DbContext(typeof(FishFactoryDatabase))] + partial class FishFactoryDatabaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.16") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Canned", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CannedName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Canneds"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.CannedComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CannedId") + .HasColumnType("integer"); + + b.Property("ComponentId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("CannedId"); + + b.HasIndex("ComponentId"); + + b.ToTable("CannedComponents"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("CannedId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreate") + .HasColumnType("timestamp without time zone"); + + b.Property("DateImplement") + .HasColumnType("timestamp without time zone"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.HasIndex("CannedId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.CannedComponent", b => + { + b.HasOne("FishFactoryDatabaseImplement.Models.Canned", "Canned") + .WithMany("Components") + .HasForeignKey("CannedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FishFactoryDatabaseImplement.Models.Component", "Component") + .WithMany("CannedComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Canned"); + + b.Navigation("Component"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Order", b => + { + b.HasOne("FishFactoryDatabaseImplement.Models.Canned", "Canned") + .WithMany("Orders") + .HasForeignKey("CannedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Canned"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Canned", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Component", b => + { + b.Navigation("CannedComponents"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/FishFactoryDatabaseImplement/Models/Order.cs b/FishFactoryDatabaseImplement/Models/Order.cs index 4b24b0e..7b2096a 100644 --- a/FishFactoryDatabaseImplement/Models/Order.cs +++ b/FishFactoryDatabaseImplement/Models/Order.cs @@ -8,26 +8,25 @@ namespace FishFactoryDatabaseImplement.Models { public class Order { - public int Id { get; set; } + public int Id { get; private set; } [Required] - public int CannedId { get; set; } + public int CannedId { get; private set; } public virtual Canned Canned { get; set; } = new(); [Required] - public int Count { get; set; } + public int Count { get; private set; } [Required] - public double Sum { get; set; } + public double Sum { get; private set; } [Required] - public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; [Required] - public DateTime DateCreate { get; set; } = DateTime.Now; - public DateTime? DateImplement { get; set; } + public DateTime DateCreate { get; private set; } = DateTime.Now; + public DateTime? DateImplement { get; private set; } public static Order Create (FishFactoryDatabase context, OrderBindingModel model) { return new Order() { Id = model.Id, CannedId = model.CannedId, - Canned = context.Canneds.First(x => x.Id == model.CannedId), Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -37,7 +36,15 @@ namespace FishFactoryDatabaseImplement.Models } public void Update(OrderBindingModel model) { + if (model == null) + { + return; + } + Id = model.Id; + CannedId = model.CannedId; + Sum = model.Sum; Status = model.Status; + DateCreate = model.DateCreate; DateImplement = model.DateImplement; } public OrderViewModel GetViewModel => new() -- 2.25.1 From a003aff777048bf5dac2f81ff944e067aba82ce9 Mon Sep 17 00:00:00 2001 From: Yourdax Date: Wed, 20 Mar 2024 10:57:23 +0400 Subject: [PATCH 5/5] =?UTF-8?q?=D1=82=D0=BE=D1=87=D0=BD=D0=BE-=D1=82=D0=BE?= =?UTF-8?q?=D1=87=D0=BD=D0=BE=20=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FishFactory/FormCanned.cs | 9 --------- FishFactoryDatabaseImplement/FishFactoryDatabase.cs | 2 +- ...igner.cs => 20240320065127_InitialCreate.Designer.cs} | 2 +- ..._InitialCreate.cs => 20240320065127_InitialCreate.cs} | 0 FishFactoryDatabaseImplement/Models/Canned.cs | 2 +- FishFactoryDatabaseImplement/Models/Order.cs | 4 +++- 6 files changed, 6 insertions(+), 13 deletions(-) rename FishFactoryDatabaseImplement/Migrations/{20240319164852_InitialCreate.Designer.cs => 20240320065127_InitialCreate.Designer.cs} (99%) rename FishFactoryDatabaseImplement/Migrations/{20240319164852_InitialCreate.cs => 20240320065127_InitialCreate.cs} (100%) diff --git a/FishFactory/FormCanned.cs b/FishFactory/FormCanned.cs index ffe2997..8b515ea 100644 --- a/FishFactory/FormCanned.cs +++ b/FishFactory/FormCanned.cs @@ -1,14 +1,5 @@ using FishFactoryDataModel.Models; using Microsoft.Extensions.Logging; -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; using FishFactoryContracts.BusinessLogicsContracts; using FishFactoryContracts.SearchModels; using FishFactoryContracts.BindingModels; diff --git a/FishFactoryDatabaseImplement/FishFactoryDatabase.cs b/FishFactoryDatabaseImplement/FishFactoryDatabase.cs index d93d11d..16b5a6e 100644 --- a/FishFactoryDatabaseImplement/FishFactoryDatabase.cs +++ b/FishFactoryDatabaseImplement/FishFactoryDatabase.cs @@ -9,7 +9,7 @@ namespace FishFactoryDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseNpgsql(@"Host=localhost;Database=FishFactory;Username=postgres;Password=postgres"); + optionsBuilder.UseNpgsql(@"Host=localhost;Database=FishFactory2;Username=postgres;Password=postgres"); } base.OnConfiguring(optionsBuilder); AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); diff --git a/FishFactoryDatabaseImplement/Migrations/20240319164852_InitialCreate.Designer.cs b/FishFactoryDatabaseImplement/Migrations/20240320065127_InitialCreate.Designer.cs similarity index 99% rename from FishFactoryDatabaseImplement/Migrations/20240319164852_InitialCreate.Designer.cs rename to FishFactoryDatabaseImplement/Migrations/20240320065127_InitialCreate.Designer.cs index a9f77ff..cd51fe7 100644 --- a/FishFactoryDatabaseImplement/Migrations/20240319164852_InitialCreate.Designer.cs +++ b/FishFactoryDatabaseImplement/Migrations/20240320065127_InitialCreate.Designer.cs @@ -12,7 +12,7 @@ using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; namespace FishFactoryDatabaseImplement.Migrations { [DbContext(typeof(FishFactoryDatabase))] - [Migration("20240319164852_InitialCreate")] + [Migration("20240320065127_InitialCreate")] partial class InitialCreate { /// diff --git a/FishFactoryDatabaseImplement/Migrations/20240319164852_InitialCreate.cs b/FishFactoryDatabaseImplement/Migrations/20240320065127_InitialCreate.cs similarity index 100% rename from FishFactoryDatabaseImplement/Migrations/20240319164852_InitialCreate.cs rename to FishFactoryDatabaseImplement/Migrations/20240320065127_InitialCreate.cs diff --git a/FishFactoryDatabaseImplement/Models/Canned.cs b/FishFactoryDatabaseImplement/Models/Canned.cs index 9202805..9bbb908 100644 --- a/FishFactoryDatabaseImplement/Models/Canned.cs +++ b/FishFactoryDatabaseImplement/Models/Canned.cs @@ -6,7 +6,7 @@ using System.ComponentModel.DataAnnotations; namespace FishFactoryDatabaseImplement.Models { - public class Canned + public class Canned : ICannedModel { public int Id { get; set; } [Required] diff --git a/FishFactoryDatabaseImplement/Models/Order.cs b/FishFactoryDatabaseImplement/Models/Order.cs index 7b2096a..62ce8dc 100644 --- a/FishFactoryDatabaseImplement/Models/Order.cs +++ b/FishFactoryDatabaseImplement/Models/Order.cs @@ -3,10 +3,11 @@ using System.ComponentModel.DataAnnotations; using FishFactoryDataModel.Enums; using FishFactoryContracts.BindingModels; using FishFactoryContracts.ViewModels; +using FishFactoryDataModel.Models; namespace FishFactoryDatabaseImplement.Models { - public class Order + public class Order : IOrderModel { public int Id { get; private set; } [Required] @@ -27,6 +28,7 @@ namespace FishFactoryDatabaseImplement.Models { Id = model.Id, CannedId = model.CannedId, + Canned = context.Canneds.First(x => x.Id == model.CannedId), Count = model.Count, Sum = model.Sum, Status = model.Status, -- 2.25.1