From c1f94359c19231f03b22a666fc61966ad4eba757 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B8=D1=88=D0=B5=D1=80=20=D0=91=D1=83=D1=81?= =?UTF-8?q?=D1=82=D0=BE=D0=BD=D0=BE=D0=B2?= Date: Thu, 20 Apr 2023 23:16:59 +0400 Subject: [PATCH] ThirdLaba Hard --- FishFactory/FishFactory.sln | 8 +- .../BusinessLogics/OrderLogic.cs | 2 +- .../FishFactoryBusinessLogic.csproj | 4 + .../BindingModels/OrderBindingModel.cs | 1 - .../FishFactoryContracts.csproj | 7 + .../FishFactoryDataModels.csproj | 7 + .../FishFactoryDatabase.cs | 29 ++ .../FishFactoryDatabaseImplement.csproj | 23 ++ .../Implements/CannedStorage.cs | 107 ++++++++ .../Implements/IngredientStorage.cs | 83 ++++++ .../Implements/OrderStorage.cs | 93 +++++++ .../Implements/ShopStorage.cs | 150 +++++++++++ .../20230409183751_InitialCreate.Designer.cs | 248 ++++++++++++++++++ .../20230409183751_InitialCreate.cs | 184 +++++++++++++ .../FishFactoryDatabaseModelSnapshot.cs | 245 +++++++++++++++++ .../Models/Canned.cs | 99 +++++++ .../Models/CannedIngredient.cs | 22 ++ .../Models/Ingredient.cs | 63 +++++ .../Models/Order.cs | 64 +++++ .../Models/Shop.cs | 105 ++++++++ .../Models/ShopCanned.cs | 22 ++ .../FishFactoryFileImplement.csproj | 7 + .../FishFactoryFileImplement/Models/Shop.cs | 3 +- .../FishFactoryListImplement.csproj | 4 + .../Implements/OrderStorage.cs | 27 +- .../FishFactoryListImplement/Models/Order.cs | 9 - .../FishFactoryView/FishFactoryView.csproj | 9 + .../FishFactoryView/FormCreateOrder.cs | 1 - .../FormListCanned.Designer.cs | 2 +- FishFactory/FishFactoryView/Program.cs | 2 +- 30 files changed, 1607 insertions(+), 23 deletions(-) create mode 100644 FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabase.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabaseImplement.csproj create mode 100644 FishFactory/FishFactoryDatabaseImplement/Implements/CannedStorage.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Implements/IngredientStorage.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Implements/OrderStorage.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Implements/ShopStorage.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Migrations/20230409183751_InitialCreate.Designer.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Migrations/20230409183751_InitialCreate.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Migrations/FishFactoryDatabaseModelSnapshot.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Models/Canned.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Models/CannedIngredient.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Models/Ingredient.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Models/Order.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Models/Shop.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Models/ShopCanned.cs diff --git a/FishFactory/FishFactory.sln b/FishFactory/FishFactory.sln index 9bfc6c0..8d71063 100644 --- a/FishFactory/FishFactory.sln +++ b/FishFactory/FishFactory.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FishFactoryBusinessLogic", EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FishFactoryListImplement", "FishFactoryListImplement\FishFactoryListImplement.csproj", "{726F9326-8BDA-4A88-BD87-AD5F88CE3779}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FishFactoryFileImplement", "FishFactoryFileImplement\FishFactoryFileImplement.csproj", "{DE30E611-C52D-4BDB-8CD1-2331EC5D24F8}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FishFactoryFileImplement", "FishFactoryFileImplement\FishFactoryFileImplement.csproj", "{DE30E611-C52D-4BDB-8CD1-2331EC5D24F8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FishFactoryDatabaseImplement", "FishFactoryDatabaseImplement\FishFactoryDatabaseImplement.csproj", "{F5D03645-1AAA-492E-87CD-9903C526FD7B}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {DE30E611-C52D-4BDB-8CD1-2331EC5D24F8}.Debug|Any CPU.Build.0 = Debug|Any CPU {DE30E611-C52D-4BDB-8CD1-2331EC5D24F8}.Release|Any CPU.ActiveCfg = Release|Any CPU {DE30E611-C52D-4BDB-8CD1-2331EC5D24F8}.Release|Any CPU.Build.0 = Release|Any CPU + {F5D03645-1AAA-492E-87CD-9903C526FD7B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F5D03645-1AAA-492E-87CD-9903C526FD7B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F5D03645-1AAA-492E-87CD-9903C526FD7B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F5D03645-1AAA-492E-87CD-9903C526FD7B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/FishFactory/FishFactoryBusinessLogic/BusinessLogics/OrderLogic.cs b/FishFactory/FishFactoryBusinessLogic/BusinessLogics/OrderLogic.cs index faac3ad..c2d5d80 100644 --- a/FishFactory/FishFactoryBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/FishFactory/FishFactoryBusinessLogic/BusinessLogics/OrderLogic.cs @@ -134,4 +134,4 @@ namespace FishFactoryBusinessLogic.BusinessLogics _logger.LogInformation("Order. OrderID:{Id}. Sum:{ Sum}. CannedId: { CannedId}", model.Id, model.Sum, model.CannedId); } } -} +} \ No newline at end of file diff --git a/FishFactory/FishFactoryBusinessLogic/FishFactoryBusinessLogic.csproj b/FishFactory/FishFactoryBusinessLogic/FishFactoryBusinessLogic.csproj index cbb2fe0..7448a7c 100644 --- a/FishFactory/FishFactoryBusinessLogic/FishFactoryBusinessLogic.csproj +++ b/FishFactory/FishFactoryBusinessLogic/FishFactoryBusinessLogic.csproj @@ -7,6 +7,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/FishFactory/FishFactoryContracts/BindingModels/OrderBindingModel.cs b/FishFactory/FishFactoryContracts/BindingModels/OrderBindingModel.cs index 40bb262..f5327a9 100644 --- a/FishFactory/FishFactoryContracts/BindingModels/OrderBindingModel.cs +++ b/FishFactory/FishFactoryContracts/BindingModels/OrderBindingModel.cs @@ -6,7 +6,6 @@ namespace FishFactoryContracts.BindingModels public class OrderBindingModel : IOrderModel { public int CannedId { get; set; } - public string CannedName { get; set; } = string.Empty; public int Count { get; set; } public double Sum { get; set; } public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; diff --git a/FishFactory/FishFactoryContracts/FishFactoryContracts.csproj b/FishFactory/FishFactoryContracts/FishFactoryContracts.csproj index 4a17316..5ed46d1 100644 --- a/FishFactory/FishFactoryContracts/FishFactoryContracts.csproj +++ b/FishFactory/FishFactoryContracts/FishFactoryContracts.csproj @@ -6,6 +6,13 @@ enable + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/FishFactory/FishFactoryDataModels/FishFactoryDataModels.csproj b/FishFactory/FishFactoryDataModels/FishFactoryDataModels.csproj index 132c02c..a8bf2a6 100644 --- a/FishFactory/FishFactoryDataModels/FishFactoryDataModels.csproj +++ b/FishFactory/FishFactoryDataModels/FishFactoryDataModels.csproj @@ -6,4 +6,11 @@ enable + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabase.cs b/FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabase.cs new file mode 100644 index 0000000..690ebeb --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabase.cs @@ -0,0 +1,29 @@ +using Microsoft.EntityFrameworkCore; +using FishFactoryDatabaseImplement.Models; + +namespace FishFactoryDatabaseImplement +{ + public class FishFactoryDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-OM38GNQ;Initial Catalog=FishFactoryDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + + public virtual DbSet Ingredients { set; get; } + + public virtual DbSet CannedList { set; get; } + + public virtual DbSet CannedIngredients { set; get; } + + public virtual DbSet Orders { set; get; } + + public virtual DbSet Shops { set; get; } + + public virtual DbSet ShopCanned { set; get; } + } +} diff --git a/FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabaseImplement.csproj b/FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabaseImplement.csproj new file mode 100644 index 0000000..d39908f --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/FishFactory/FishFactoryDatabaseImplement/Implements/CannedStorage.cs b/FishFactory/FishFactoryDatabaseImplement/Implements/CannedStorage.cs new file mode 100644 index 0000000..4bcd460 --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Implements/CannedStorage.cs @@ -0,0 +1,107 @@ +using Microsoft.EntityFrameworkCore; +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.StoragesContracts; +using FishFactoryContracts.ViewModels; +using FishFactoryDatabaseImplement.Models; + +namespace FishFactoryDatabaseImplement.Implements +{ + public class CannedStorage : ICannedStorage + { + public List GetFullList() + { + using var context = new FishFactoryDatabase(); + return context.CannedList + .Include(x => x.Ingredients) + .ThenInclude(x => x.Ingredient) + .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.CannedList + .Include(x => x.Ingredients) + .ThenInclude(x => x.Ingredient) + .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.CannedList + .Include(x => x.Ingredients) + .ThenInclude(x => x.Ingredient) + .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.CannedList.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 canned = context.CannedList.FirstOrDefault(rec => rec.Id == model.Id); + if (canned == null) + { + return null; + } + canned.Update(model); + context.SaveChanges(); + canned.UpdateIngredients(context, model); + transaction.Commit(); + return canned.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public CannedViewModel? Delete(CannedBindingModel model) + { + using var context = new FishFactoryDatabase(); + var element = context.CannedList + .Include(x => x.Ingredients) + .Include(x => x.Orders) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.CannedList.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/FishFactory/FishFactoryDatabaseImplement/Implements/IngredientStorage.cs b/FishFactory/FishFactoryDatabaseImplement/Implements/IngredientStorage.cs new file mode 100644 index 0000000..2e06928 --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Implements/IngredientStorage.cs @@ -0,0 +1,83 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.StoragesContracts; +using FishFactoryContracts.ViewModels; +using FishFactoryDatabaseImplement.Models; + +namespace FishFactoryDatabaseImplement.Implements +{ + public class IngredientStorage : IIngredientStorage + { + public List GetFullList() + { + using var context = new FishFactoryDatabase(); + return context.Ingredients + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(IngredientSearchModel model) + { + if (string.IsNullOrEmpty(model.IngredientName)) + { + return new(); + } + using var context = new FishFactoryDatabase(); + return context.Ingredients + .Where(x => x.IngredientName.Contains(model.IngredientName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public IngredientViewModel? GetElement(IngredientSearchModel model) + { + if (string.IsNullOrEmpty(model.IngredientName) && !model.Id.HasValue) + { + return null; + } + using var context = new FishFactoryDatabase(); + return context.Ingredients + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.IngredientName) && x.IngredientName == model.IngredientName) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public IngredientViewModel? Insert(IngredientBindingModel model) + { + var newIngredient = Ingredient.Create(model); + if (newIngredient == null) + { + return null; + } + using var context = new FishFactoryDatabase(); + context.Ingredients.Add(newIngredient); + context.SaveChanges(); + return newIngredient.GetViewModel; + } + + public IngredientViewModel? Update(IngredientBindingModel model) + { + using var context = new FishFactoryDatabase(); + var ingredient = context.Ingredients.FirstOrDefault(x => x.Id == model.Id); + if (ingredient == null) + { + return null; + } + ingredient.Update(model); + context.SaveChanges(); + return ingredient.GetViewModel; + } + + public IngredientViewModel? Delete(IngredientBindingModel model) + { + using var context = new FishFactoryDatabase(); + var element = context.Ingredients.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Ingredients.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/FishFactory/FishFactoryDatabaseImplement/Implements/OrderStorage.cs b/FishFactory/FishFactoryDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..08ab4ca --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,93 @@ +using Microsoft.EntityFrameworkCore; +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.StoragesContracts; +using FishFactoryContracts.ViewModels; +using FishFactoryDatabaseImplement.Models; + +namespace FishFactoryDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new FishFactoryDatabase(); + var element = context.Orders + .Include(x => x.Canned) + .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 FishFactoryDatabase(); + return context.Orders + .Include(x => x.Canned) + .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 FishFactoryDatabase(); + return context.Orders + .Include(x => x.Canned) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFullList() + { + using var context = new FishFactoryDatabase(); + return context.Orders + .Include(x => x.Canned) + .Select(x => x.GetViewModel) + .ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new FishFactoryDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return context.Orders + .Include(x => x.Canned) + .FirstOrDefault(x => x.Id == newOrder.Id) + ?.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new FishFactoryDatabase(); + var order = context.Orders + .Include(x => x.Canned) + .FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return order.GetViewModel; + } + } +} diff --git a/FishFactory/FishFactoryDatabaseImplement/Implements/ShopStorage.cs b/FishFactory/FishFactoryDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..4e324e7 --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,150 @@ +using Microsoft.EntityFrameworkCore; +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.StoragesContracts; +using FishFactoryContracts.ViewModels; +using FishFactoryDatabaseImplement.Models; +using FishFactoryDataModels.Models; + +namespace FishFactoryDatabaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + public List GetFullList() + { + using var context = new FishFactoryDatabase(); + return context.Shops + .Include(x => x.ListCannedFk) + .ThenInclude(x => x.Canned) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + using var context = new FishFactoryDatabase(); + return context.Shops + .Include(x => x.ListCannedFk) + .ThenInclude(x => x.Canned) + .Where(x => x.ShopName.Contains(model.ShopName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return null; + } + using var context = new FishFactoryDatabase(); + return context.Shops + .Include(x => x.ListCannedFk) + .ThenInclude(x => x.Canned) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + public ShopViewModel? Insert(ShopBindingModel model) + { + using var context = new FishFactoryDatabase(); + var newShop = Shop.Create(context, model); + if (newShop == null) + { + return null; + } + context.Shops.Add(newShop); + context.SaveChanges(); + return newShop.GetViewModel; + } + public ShopViewModel? Update(ShopBindingModel model) + { + using var context = new FishFactoryDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var shop = context.Shops.FirstOrDefault(rec => rec.Id == model.Id); + if (shop == null) + { + return null; + } + shop.Update(model); + context.SaveChanges(); + shop.UpdateCanned(context, model); + transaction.Commit(); + return shop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new FishFactoryDatabase(); + var element = context.Shops + .Include(x => x.ListCannedFk) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Shops.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + public bool SellCanned(ICannedModel model, int count) + { + using var context = new FishFactoryDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + foreach (var shop in context.Shops + .Include(x => x.ListCannedFk) + .ThenInclude(x => x.Canned) + .ToList() + .Where(x => x.ListCanned.ContainsKey(model.Id))) + { + int countInCurrentShop = shop.ListCanned[model.Id].Item2; + if (countInCurrentShop <= count) + { + var elem = context.ShopCanned + .Where(x => x.CannedId == model.Id) + .FirstOrDefault(x => x.ShopId == shop.Id); + context.ShopCanned.Remove(elem); + shop.ListCanned.Remove(model.Id); + count -= countInCurrentShop; + } + else + { + shop.ListCanned[model.Id] = (shop.ListCanned[model.Id].Item1, countInCurrentShop - count); + count = 0; + shop.UpdateCanned(context, new() + { + Id = shop.Id, + ListCanned = shop.ListCanned, + }); + } + if (count == 0) + { + context.SaveChanges(); + transaction.Commit(); + return true; + } + } + transaction.Rollback(); + return false; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +} diff --git a/FishFactory/FishFactoryDatabaseImplement/Migrations/20230409183751_InitialCreate.Designer.cs b/FishFactory/FishFactoryDatabaseImplement/Migrations/20230409183751_InitialCreate.Designer.cs new file mode 100644 index 0000000..1ebc9df --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Migrations/20230409183751_InitialCreate.Designer.cs @@ -0,0 +1,248 @@ +// +using System; +using FishFactoryDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace FishFactoryDatabaseImplement.Migrations +{ + [DbContext(typeof(FishFactoryDatabase))] + [Migration("20230409183751_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("FishFactoryDatabaseImplement.Models.Canned", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CannedName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("CannedList"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.CannedIngredient", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CannedId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("IngredientId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("CannedId"); + + b.HasIndex("IngredientId"); + + b.ToTable("CannedIngredients"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Ingredient", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Cost") + .HasColumnType("float"); + + b.Property("IngredientName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Ingredients"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CannedId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("CannedId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DateOpening") + .HasColumnType("datetime2"); + + b.Property("MaxCountCanned") + .HasColumnType("int"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.ShopCanned", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CannedId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("CannedId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopCanned"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.CannedIngredient", b => + { + b.HasOne("FishFactoryDatabaseImplement.Models.Canned", "Canned") + .WithMany("Ingredients") + .HasForeignKey("CannedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FishFactoryDatabaseImplement.Models.Ingredient", "Ingredient") + .WithMany("CannedIngredients") + .HasForeignKey("IngredientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Canned"); + + b.Navigation("Ingredient"); + }); + + 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.ShopCanned", b => + { + b.HasOne("FishFactoryDatabaseImplement.Models.Canned", "Canned") + .WithMany() + .HasForeignKey("CannedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FishFactoryDatabaseImplement.Models.Shop", "Shop") + .WithMany("ListCannedFk") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Canned"); + + b.Navigation("Shop"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Canned", b => + { + b.Navigation("Ingredients"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Ingredient", b => + { + b.Navigation("CannedIngredients"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Shop", b => + { + b.Navigation("ListCannedFk"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/FishFactory/FishFactoryDatabaseImplement/Migrations/20230409183751_InitialCreate.cs b/FishFactory/FishFactoryDatabaseImplement/Migrations/20230409183751_InitialCreate.cs new file mode 100644 index 0000000..cf67ca4 --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Migrations/20230409183751_InitialCreate.cs @@ -0,0 +1,184 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace FishFactoryDatabaseImplement.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "CannedList", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + CannedName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_CannedList", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Ingredients", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + IngredientName = table.Column(type: "nvarchar(max)", nullable: false), + Cost = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Ingredients", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Shops", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ShopName = table.Column(type: "nvarchar(max)", nullable: false), + Address = table.Column(type: "nvarchar(max)", nullable: false), + DateOpening = table.Column(type: "datetime2", nullable: false), + MaxCountCanned = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + CannedId = table.Column(type: "int", 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_CannedList_CannedId", + column: x => x.CannedId, + principalTable: "CannedList", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "CannedIngredients", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + CannedId = table.Column(type: "int", nullable: false), + IngredientId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_CannedIngredients", x => x.Id); + table.ForeignKey( + name: "FK_CannedIngredients_CannedList_CannedId", + column: x => x.CannedId, + principalTable: "CannedList", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_CannedIngredients_Ingredients_IngredientId", + column: x => x.IngredientId, + principalTable: "Ingredients", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "ShopCanned", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + CannedId = table.Column(type: "int", nullable: false), + ShopId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ShopCanned", x => x.Id); + table.ForeignKey( + name: "FK_ShopCanned_CannedList_CannedId", + column: x => x.CannedId, + principalTable: "CannedList", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopCanned_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_CannedIngredients_CannedId", + table: "CannedIngredients", + column: "CannedId"); + + migrationBuilder.CreateIndex( + name: "IX_CannedIngredients_IngredientId", + table: "CannedIngredients", + column: "IngredientId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_CannedId", + table: "Orders", + column: "CannedId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopCanned_CannedId", + table: "ShopCanned", + column: "CannedId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopCanned_ShopId", + table: "ShopCanned", + column: "ShopId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "CannedIngredients"); + + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "ShopCanned"); + + migrationBuilder.DropTable( + name: "Ingredients"); + + migrationBuilder.DropTable( + name: "CannedList"); + + migrationBuilder.DropTable( + name: "Shops"); + } + } +} diff --git a/FishFactory/FishFactoryDatabaseImplement/Migrations/FishFactoryDatabaseModelSnapshot.cs b/FishFactory/FishFactoryDatabaseImplement/Migrations/FishFactoryDatabaseModelSnapshot.cs new file mode 100644 index 0000000..a47f1dc --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Migrations/FishFactoryDatabaseModelSnapshot.cs @@ -0,0 +1,245 @@ +// +using System; +using FishFactoryDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#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.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Canned", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CannedName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("CannedList"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.CannedIngredient", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CannedId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("IngredientId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("CannedId"); + + b.HasIndex("IngredientId"); + + b.ToTable("CannedIngredients"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Ingredient", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Cost") + .HasColumnType("float"); + + b.Property("IngredientName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Ingredients"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CannedId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("CannedId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DateOpening") + .HasColumnType("datetime2"); + + b.Property("MaxCountCanned") + .HasColumnType("int"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.ShopCanned", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CannedId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("CannedId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopCanned"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.CannedIngredient", b => + { + b.HasOne("FishFactoryDatabaseImplement.Models.Canned", "Canned") + .WithMany("Ingredients") + .HasForeignKey("CannedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FishFactoryDatabaseImplement.Models.Ingredient", "Ingredient") + .WithMany("CannedIngredients") + .HasForeignKey("IngredientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Canned"); + + b.Navigation("Ingredient"); + }); + + 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.ShopCanned", b => + { + b.HasOne("FishFactoryDatabaseImplement.Models.Canned", "Canned") + .WithMany() + .HasForeignKey("CannedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FishFactoryDatabaseImplement.Models.Shop", "Shop") + .WithMany("ListCannedFk") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Canned"); + + b.Navigation("Shop"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Canned", b => + { + b.Navigation("Ingredients"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Ingredient", b => + { + b.Navigation("CannedIngredients"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Shop", b => + { + b.Navigation("ListCannedFk"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/FishFactory/FishFactoryDatabaseImplement/Models/Canned.cs b/FishFactory/FishFactoryDatabaseImplement/Models/Canned.cs new file mode 100644 index 0000000..9bbc293 --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Models/Canned.cs @@ -0,0 +1,99 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.ViewModels; +using FishFactoryDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace FishFactoryDatabaseImplement.Models +{ + public class Canned : ICannedModel + { + public int Id { get; set; } + + [Required] + public string CannedName { get; set; } = string.Empty; + + [Required] + public double Price { get; set; } + + private Dictionary? _cannedIngredients = null; + + [NotMapped] + public Dictionary CannedIngredients + { + get + { + if (_cannedIngredients == null) + { + _cannedIngredients = Ingredients + .ToDictionary(recPC => recPC.IngredientId, recPC => (recPC.Ingredient as IIngredientModel, recPC.Count)); + } + return _cannedIngredients; + } + } + + [ForeignKey("CannedId")] + public virtual List Ingredients { 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, + Ingredients = model.CannedIngredients.Select(x => new CannedIngredient + { + Ingredient = context.Ingredients.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, + CannedIngredients = CannedIngredients + }; + + public void UpdateIngredients(FishFactoryDatabase context, CannedBindingModel model) + { + var cannedIngredients = context.CannedIngredients.Where(rec => rec.CannedId == model.Id).ToList(); + if (cannedIngredients != null && cannedIngredients.Count > 0) + { // удалили те, которых нет в модели + context.CannedIngredients.RemoveRange(cannedIngredients.Where(rec => !model.CannedIngredients.ContainsKey(rec.IngredientId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateIngredient in cannedIngredients) + { + updateIngredient.Count = model.CannedIngredients[updateIngredient.IngredientId].Item2; + model.CannedIngredients.Remove(updateIngredient.IngredientId); + } + context.SaveChanges(); + } + var canned = context.CannedList.First(x => x.Id == Id); + foreach (var cd in model.CannedIngredients) + { + context.CannedIngredients.Add(new CannedIngredient + { + Canned = canned, + Ingredient = context.Ingredients.First(x => x.Id == cd.Key), + Count = cd.Value.Item2 + }); + context.SaveChanges(); + } + _cannedIngredients = null; + } + } +} \ No newline at end of file diff --git a/FishFactory/FishFactoryDatabaseImplement/Models/CannedIngredient.cs b/FishFactory/FishFactoryDatabaseImplement/Models/CannedIngredient.cs new file mode 100644 index 0000000..0bd7a42 --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Models/CannedIngredient.cs @@ -0,0 +1,22 @@ +using System.ComponentModel.DataAnnotations; + +namespace FishFactoryDatabaseImplement.Models +{ + public class CannedIngredient + { + public int Id { get; set; } + + [Required] + public int CannedId { get; set; } + + [Required] + public int IngredientId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Ingredient Ingredient { get; set; } = new(); + + public virtual Canned Canned { get; set; } = new(); + } +} \ No newline at end of file diff --git a/FishFactory/FishFactoryDatabaseImplement/Models/Ingredient.cs b/FishFactory/FishFactoryDatabaseImplement/Models/Ingredient.cs new file mode 100644 index 0000000..41602f0 --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Models/Ingredient.cs @@ -0,0 +1,63 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.ViewModels; +using FishFactoryDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace FishFactoryDatabaseImplement.Models +{ + public class Ingredient : IIngredientModel + { + public int Id { get; private set; } + + [Required] + public string IngredientName { get; private set; } = string.Empty; + + [Required] + public double Cost { get; set; } + + [ForeignKey("IngredientId")] + public virtual List CannedIngredients { get; set; } = new(); + + public static Ingredient? Create(IngredientBindingModel model) + { + if (model == null) + { + return null; + } + return new Ingredient() + { + Id = model.Id, + IngredientName = model.IngredientName, + Cost = model.Cost + }; + } + + public static Ingredient Create(IngredientViewModel model) + { + return new Ingredient + { + Id = model.Id, + IngredientName = model.IngredientName, + Cost = model.Cost + }; + } + + public void Update(IngredientBindingModel model) + { + if (model == null) + { + return; + } + IngredientName = model.IngredientName; + Cost = model.Cost; + } + + public IngredientViewModel GetViewModel => new() + { + Id = Id, + IngredientName = IngredientName, + Cost = Cost + }; + } +} diff --git a/FishFactory/FishFactoryDatabaseImplement/Models/Order.cs b/FishFactory/FishFactoryDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..29a0ed8 --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Models/Order.cs @@ -0,0 +1,64 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.ViewModels; +using FishFactoryDataModels.Models; +using FishFactoryDataModels.Enums; +using System.ComponentModel.DataAnnotations; + +namespace FishFactoryDatabaseImplement.Models +{ + public class Order : IOrderModel + { + [Required] + public int CannedId { get; set; } + [Required] + public int Count { get; set; } + [Required] + public double Sum { get; set; } + [Required] + public OrderStatus Status { get; set; } + [Required] + public DateTime DateCreate { get; set; } + public DateTime? DateImplement { get; set; } + public int Id { get; set; } + public Canned Canned { get; set; } + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + CannedId = model.CannedId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + CannedId = CannedId, + Count = Count, + Sum = Sum, + DateCreate = DateCreate, + DateImplement = DateImplement, + Id = Id, + Status = Status, + CannedName = Canned.CannedName, + }; + } +} diff --git a/FishFactory/FishFactoryDatabaseImplement/Models/Shop.cs b/FishFactory/FishFactoryDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..f6c8bf0 --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Models/Shop.cs @@ -0,0 +1,105 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.ViewModels; +using FishFactoryDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Xml.Linq; + +namespace FishFactoryDatabaseImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + [Required] + public string ShopName { get; private set; } = string.Empty; + [Required] + public string Address { get; private set; } = string.Empty; + [Required] + public DateTime DateOpening { get; private set; } + [Required] + public int MaxCountCanned { get; private set; } + + private Dictionary? _shopCanned = null; + + [NotMapped] + public Dictionary ListCanned + { + get + { + if (_shopCanned == null) + { + _shopCanned = ListCannedFk + .ToDictionary(recPC => recPC.CannedId, recPC => (recPC.Canned as ICannedModel, recPC.Count)); + } + return _shopCanned; + } + } + + [ForeignKey("ShopId")] + public virtual List ListCannedFk { get; set; } = new(); + + public static Shop Create(FishFactoryDatabase context, ShopBindingModel model) + { + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOpening = model.DateOpening, + MaxCountCanned = model.MaxCountCanned, + ListCannedFk = model.ListCanned.Select(x => new ShopCanned + { + Canned = context.CannedList.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(ShopBindingModel model) + { + ShopName = model.ShopName; + Address = model.Address; + DateOpening = model.DateOpening; + MaxCountCanned = model.MaxCountCanned; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpening = DateOpening, + MaxCountCanned = MaxCountCanned, + ListCanned = ListCanned + }; + + public void UpdateCanned(FishFactoryDatabase context, ShopBindingModel model) + { + var shopCanned = context.ShopCanned.Where(rec => rec.ShopId == model.Id).ToList(); + if (shopCanned != null && shopCanned.Count > 0) + { // удалили те, которых нет в модели + context.ShopCanned.RemoveRange(shopCanned.Where(rec => !model.ListCanned.ContainsKey(rec.CannedId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateCanned in shopCanned) + { + updateCanned.Count = model.ListCanned[updateCanned.CannedId].Item2; + model.ListCanned.Remove(updateCanned.CannedId); + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var ss in model.ListCanned) + { + context.ShopCanned.Add(new ShopCanned + { + Shop = shop, + Canned = context.CannedList.First(x => x.Id == ss.Key), + Count = ss.Value.Item2 + }); + context.SaveChanges(); + } + _shopCanned = null; + } + } +} diff --git a/FishFactory/FishFactoryDatabaseImplement/Models/ShopCanned.cs b/FishFactory/FishFactoryDatabaseImplement/Models/ShopCanned.cs new file mode 100644 index 0000000..a310188 --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Models/ShopCanned.cs @@ -0,0 +1,22 @@ +using System.ComponentModel.DataAnnotations; + +namespace FishFactoryDatabaseImplement.Models +{ + public class ShopCanned + { + public int Id { get; set; } + + [Required] + public int CannedId { get; set; } + + [Required] + public int ShopId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Shop Shop { get; set; } = new(); + + public virtual Canned Canned { get; set; } = new(); + } +} diff --git a/FishFactory/FishFactoryFileImplement/FishFactoryFileImplement.csproj b/FishFactory/FishFactoryFileImplement/FishFactoryFileImplement.csproj index b2c107f..26c6f57 100644 --- a/FishFactory/FishFactoryFileImplement/FishFactoryFileImplement.csproj +++ b/FishFactory/FishFactoryFileImplement/FishFactoryFileImplement.csproj @@ -6,6 +6,13 @@ enable + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/FishFactory/FishFactoryFileImplement/Models/Shop.cs b/FishFactory/FishFactoryFileImplement/Models/Shop.cs index 3a8f460..b598393 100644 --- a/FishFactory/FishFactoryFileImplement/Models/Shop.cs +++ b/FishFactory/FishFactoryFileImplement/Models/Shop.cs @@ -1,5 +1,4 @@ - -using FishFactoryContracts.BindingModels; +using FishFactoryContracts.BindingModels; using FishFactoryContracts.ViewModels; using FishFactoryDataModels.Models; using System.Xml.Linq; diff --git a/FishFactory/FishFactoryListImplement/FishFactoryListImplement.csproj b/FishFactory/FishFactoryListImplement/FishFactoryListImplement.csproj index 6d7e23b..b525cd1 100644 --- a/FishFactory/FishFactoryListImplement/FishFactoryListImplement.csproj +++ b/FishFactory/FishFactoryListImplement/FishFactoryListImplement.csproj @@ -7,6 +7,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/FishFactory/FishFactoryListImplement/Implements/OrderStorage.cs b/FishFactory/FishFactoryListImplement/Implements/OrderStorage.cs index f122fdc..cc1a5bf 100644 --- a/FishFactory/FishFactoryListImplement/Implements/OrderStorage.cs +++ b/FishFactory/FishFactoryListImplement/Implements/OrderStorage.cs @@ -18,7 +18,7 @@ namespace FishFactoryListImplement.Implements var result = new List(); foreach (var order in _source.Orders) { - result.Add(order.GetViewModel); + result.Add(GetViewModel(order)); } return result; } @@ -34,7 +34,7 @@ namespace FishFactoryListImplement.Implements { if (order.Id == model.Id) { - result.Add(order.GetViewModel); + result.Add(GetViewModel(order)); } } return result; @@ -50,12 +50,27 @@ namespace FishFactoryListImplement.Implements { if (model.Id.HasValue && order.Id == model.Id) { - return order.GetViewModel; + return GetViewModel(order); } } return null; } + + private OrderViewModel GetViewModel(Order order) + { + var viewModel = order.GetViewModel; + foreach (var canned in _source.ListCanned) + { + if (canned.Id == order.CannedId) + { + viewModel.CannedName = canned.CannedName; + break; + } + } + return viewModel; + } + public OrderViewModel? Insert(OrderBindingModel model) { model.Id = 1; @@ -72,7 +87,7 @@ namespace FishFactoryListImplement.Implements return null; } _source.Orders.Add(newOrder); - return newOrder.GetViewModel; + return GetViewModel(newOrder); } public OrderViewModel? Update(OrderBindingModel model) @@ -82,7 +97,7 @@ namespace FishFactoryListImplement.Implements if (order.Id == model.Id) { order.Update(model); - return order.GetViewModel; + return GetViewModel(order); } } return null; @@ -96,7 +111,7 @@ namespace FishFactoryListImplement.Implements { var element = _source.Orders[i]; _source.Orders.RemoveAt(i); - return element.GetViewModel; + return GetViewModel(element); } } return null; diff --git a/FishFactory/FishFactoryListImplement/Models/Order.cs b/FishFactory/FishFactoryListImplement/Models/Order.cs index 9886ecd..e1b75c5 100644 --- a/FishFactory/FishFactoryListImplement/Models/Order.cs +++ b/FishFactory/FishFactoryListImplement/Models/Order.cs @@ -8,7 +8,6 @@ namespace FishFactoryListImplement.Models public class Order : IOrderModel { public int CannedId { get; private set; } - public string CannedName { get; private set; } = string.Empty; public int Count { get; private set; } public double Sum { get; private set; } public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; @@ -25,7 +24,6 @@ namespace FishFactoryListImplement.Models return new Order { CannedId = model.CannedId, - CannedName = model.CannedName, Count = model.Count, Sum = model.Sum, Status = model.Status, @@ -41,20 +39,13 @@ namespace FishFactoryListImplement.Models { return; } - CannedId = model.CannedId; - CannedName = model.CannedName; - Count = model.Count; - Sum = model.Sum; Status = model.Status; - DateCreate = model.DateCreate; DateImplement = model.DateImplement; - Id = model.Id; } public OrderViewModel GetViewModel => new() { CannedId = CannedId, - CannedName = CannedName, Count = Count, Sum = Sum, DateCreate = DateCreate, diff --git a/FishFactory/FishFactoryView/FishFactoryView.csproj b/FishFactory/FishFactoryView/FishFactoryView.csproj index 70b52c5..b98d4d7 100644 --- a/FishFactory/FishFactoryView/FishFactoryView.csproj +++ b/FishFactory/FishFactoryView/FishFactoryView.csproj @@ -9,6 +9,14 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -16,6 +24,7 @@ + diff --git a/FishFactory/FishFactoryView/FormCreateOrder.cs b/FishFactory/FishFactoryView/FormCreateOrder.cs index 8a42249..e8e70f2 100644 --- a/FishFactory/FishFactoryView/FormCreateOrder.cs +++ b/FishFactory/FishFactoryView/FormCreateOrder.cs @@ -88,7 +88,6 @@ namespace FishFactoryView var operationResult = _logicO.CreateOrder(new OrderBindingModel { CannedId = Convert.ToInt32(comboBoxCanned.SelectedValue), - CannedName = comboBoxCanned.Text, Count = Convert.ToInt32(textBoxCount.Text), Sum = Convert.ToDouble(textBoxSum.Text) }); diff --git a/FishFactory/FishFactoryView/FormListCanned.Designer.cs b/FishFactory/FishFactoryView/FormListCanned.Designer.cs index 300edda..ffbe7e4 100644 --- a/FishFactory/FishFactoryView/FormListCanned.Designer.cs +++ b/FishFactory/FishFactoryView/FormListCanned.Designer.cs @@ -104,7 +104,7 @@ this.Controls.Add(this.buttonEdit); this.Controls.Add(this.buttonAdd); this.Name = "FormListCanned"; - this.Text = "Список консервы"; + this.Text = "Список консерв"; this.Load += new System.EventHandler(this.FormDocuments_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); this.ResumeLayout(false); diff --git a/FishFactory/FishFactoryView/Program.cs b/FishFactory/FishFactoryView/Program.cs index ea94a8e..93f5659 100644 --- a/FishFactory/FishFactoryView/Program.cs +++ b/FishFactory/FishFactoryView/Program.cs @@ -4,7 +4,7 @@ using NLog.Extensions.Logging; using FishFactoryBusinessLogic.BusinessLogics; using FishFactoryContracts.BusinessLogicsContracts; using FishFactoryContracts.StoragesContracts; -using FishFactoryFileImplement.Implements; +using FishFactoryDatabaseImplement.Implements; using CannedBusinessLogic; namespace FishFactoryView -- 2.25.1