diff --git a/.gitignore b/.gitignore index ca1c7a3..ecb2ca5 100644 --- a/.gitignore +++ b/.gitignore @@ -398,3 +398,4 @@ FodyWeavers.xsd # JetBrains Rider *.sln.iml +/Confectionery/ImplementationExtensions \ No newline at end of file diff --git a/Confectionery/Confectionery.sln b/Confectionery/Confectionery.sln index dcd8aca..5660fff 100644 --- a/Confectionery/Confectionery.sln +++ b/Confectionery/Confectionery.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryBusinessLogic" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryListImplement", "ConfectioneryListImplement\ConfectioneryListImplement.csproj", "{85992074-656D-4F79-AB23-60DE4F9ABF30}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryFileImplement", "ConfectioneryFileImplements\ConfectioneryFileImplement.csproj", "{48A8592F-EF58-4FC4-9C9E-B401836FF21E}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConfectioneryFileImplement", "ConfectioneryFileImplements\ConfectioneryFileImplement.csproj", "{48A8592F-EF58-4FC4-9C9E-B401836FF21E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConfectioneryDataBaseImplement", "ConfectioneryDataBaseImplement\ConfectioneryDataBaseImplement.csproj", "{29FD1CED-C24D-40AD-9734-889C0B192A6A}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {48A8592F-EF58-4FC4-9C9E-B401836FF21E}.Debug|Any CPU.Build.0 = Debug|Any CPU {48A8592F-EF58-4FC4-9C9E-B401836FF21E}.Release|Any CPU.ActiveCfg = Release|Any CPU {48A8592F-EF58-4FC4-9C9E-B401836FF21E}.Release|Any CPU.Build.0 = Release|Any CPU + {29FD1CED-C24D-40AD-9734-889C0B192A6A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {29FD1CED-C24D-40AD-9734-889C0B192A6A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {29FD1CED-C24D-40AD-9734-889C0B192A6A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {29FD1CED-C24D-40AD-9734-889C0B192A6A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Confectionery/Confectionery/Confectionery.csproj b/Confectionery/Confectionery/Confectionery.csproj index 09d809e..b60ee41 100644 --- a/Confectionery/Confectionery/Confectionery.csproj +++ b/Confectionery/Confectionery/Confectionery.csproj @@ -19,6 +19,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -30,6 +34,7 @@ + diff --git a/Confectionery/Confectionery/Program.cs b/Confectionery/Confectionery/Program.cs index 9bd00dc..ece7dd1 100644 --- a/Confectionery/Confectionery/Program.cs +++ b/Confectionery/Confectionery/Program.cs @@ -1,7 +1,7 @@ using ConfectioneryBusinessLogic.BusinessLogics; using ConfectioneryContracts.BusinessLogicsContracts; using ConfectioneryContracts.StoragesContracts; -using ConfectioneryFileImplement.Implements; +using ConfectioneryDataBaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/Confectionery/ConfectioneryDataBaseImplement/ConfectioneryDataBaseImplement.csproj b/Confectionery/ConfectioneryDataBaseImplement/ConfectioneryDataBaseImplement.csproj new file mode 100644 index 0000000..6c6f63a --- /dev/null +++ b/Confectionery/ConfectioneryDataBaseImplement/ConfectioneryDataBaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/Confectionery/ConfectioneryDataBaseImplement/ConfectioneryDatabase.cs b/Confectionery/ConfectioneryDataBaseImplement/ConfectioneryDatabase.cs new file mode 100644 index 0000000..8e3d69e --- /dev/null +++ b/Confectionery/ConfectioneryDataBaseImplement/ConfectioneryDatabase.cs @@ -0,0 +1,29 @@ +using ConfectioneryDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDataBaseImplement +{ + public class ConfectioneryDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=ConfectioneryDatabaseHardLab;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Ingredients { set; get; } + public virtual DbSet Pastrys { set; get; } + public virtual DbSet PastryIngredients { set; get; } + public virtual DbSet Orders { set; get; } + public virtual DbSet Shops { set; get; } + public virtual DbSet ShopPastries { set; get; } + } +} diff --git a/Confectionery/ConfectioneryDataBaseImplement/Implements/IngredientStorage.cs b/Confectionery/ConfectioneryDataBaseImplement/Implements/IngredientStorage.cs new file mode 100644 index 0000000..3cf1a53 --- /dev/null +++ b/Confectionery/ConfectioneryDataBaseImplement/Implements/IngredientStorage.cs @@ -0,0 +1,85 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataBaseImplement.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDataBaseImplement.Implements +{ + public class IngredientStorage : IIngredientStorage + { + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.Ingredients + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(IngredientSearchModel model) + { + if (string.IsNullOrEmpty(model.IngredientName)) + { + return new(); + } + using var context = new ConfectioneryDatabase(); + 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 ConfectioneryDatabase(); + 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 ConfectioneryDatabase(); + context.Ingredients.Add(newIngredient); + context.SaveChanges(); + return newIngredient.GetViewModel; + } + public IngredientViewModel? Update(IngredientBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var component = context.Ingredients.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + public IngredientViewModel? Delete(IngredientBindingModel model) + { + using var context = new ConfectioneryDatabase(); + 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/Confectionery/ConfectioneryDataBaseImplement/Implements/OrderStorage.cs b/Confectionery/ConfectioneryDataBaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..7e5d22f --- /dev/null +++ b/Confectionery/ConfectioneryDataBaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,90 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataBaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDataBaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.Orders + .Select(x => AccessPastryStorage(x.GetViewModel, context)) + .ToList(); + } + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new ConfectioneryDatabase(); + return context.Orders + .Where(x => x.Id == model.Id) + .Select(x => AccessPastryStorage(x.GetViewModel, context)) + .ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new ConfectioneryDatabase(); + return AccessPastryStorage(context.Orders + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel, context); + } + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new ConfectioneryDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return AccessPastryStorage(newOrder.GetViewModel, context); + } + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return AccessPastryStorage(order.GetViewModel, context); + } + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return AccessPastryStorage(element.GetViewModel, context); + } + return null; + } + static OrderViewModel AccessPastryStorage(OrderViewModel model, ConfectioneryDatabase context) + { + if (model == null) return model; + string? pastryName = context.Pastrys.FirstOrDefault(x => x.Id == model.PastryId)?.PastryName; + if (pastryName != null) model.PastryName = pastryName; + return model; + } + } +} diff --git a/Confectionery/ConfectioneryDataBaseImplement/Implements/PastryStorage.cs b/Confectionery/ConfectioneryDataBaseImplement/Implements/PastryStorage.cs new file mode 100644 index 0000000..7b6b55c --- /dev/null +++ b/Confectionery/ConfectioneryDataBaseImplement/Implements/PastryStorage.cs @@ -0,0 +1,106 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDataBaseImplement.Implements +{ + public class PastryStorage : IPastryStorage + { + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.Pastrys + .Include(x => x.Ingredients) + .ThenInclude(x => x.Ingredient) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(PastrySearchModel model) + { + if (string.IsNullOrEmpty(model.PastryName)) + { + return new(); + } + using var context = new ConfectioneryDatabase(); + return context.Pastrys + .Include(x => x.Ingredients) + .ThenInclude(x => x.Ingredient) + .Where(x => x.PastryName.Contains(model.PastryName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public PastryViewModel? GetElement(PastrySearchModel model) + { + if (string.IsNullOrEmpty(model.PastryName) && !model.Id.HasValue) + { + return null; + } + using var context = new ConfectioneryDatabase(); + return context.Pastrys + .Include(x => x.Ingredients) + .ThenInclude(x => x.Ingredient) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.PastryName) && x.PastryName == model.PastryName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + public PastryViewModel? Insert(PastryBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var newPastry = Pastry.Create(context, model); + if (newPastry == null) + { + return null; + } + context.Pastrys.Add(newPastry); + context.SaveChanges(); + return newPastry.GetViewModel; + } + public PastryViewModel? Update(PastryBindingModel model) + { + using var context = new ConfectioneryDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var product = context.Pastrys.FirstOrDefault(rec => rec.Id == model.Id); + if (product == null) + { + return null; + } + product.Update(model); + context.SaveChanges(); + product.UpdateIngredients(context, model); + transaction.Commit(); + return product.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public PastryViewModel? Delete(PastryBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var element = context.Pastrys + .Include(x => x.Ingredients) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Pastrys.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/Confectionery/ConfectioneryDataBaseImplement/Implements/ShopStorage.cs b/Confectionery/ConfectioneryDataBaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..1e811f7 --- /dev/null +++ b/Confectionery/ConfectioneryDataBaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,190 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System.Transactions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDataBaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.Shops + .Include(x => x.Pastries) + .ThenInclude(x => x.Pastry) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + using var context = new ConfectioneryDatabase(); + return context.Shops + .Include(x => x.Pastries) + .ThenInclude(x => x.Pastry) + .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 ConfectioneryDatabase(); + return context.Shops + .Include(x => x.Pastries) + .ThenInclude(x => x.Pastry) + .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 ConfectioneryDatabase(); + var newShop = Shop.Create(model, context); + if (newShop == null) + { + return null; + } + context.Shops.Add(newShop); + context.SaveChanges(); + return newShop.GetViewModel; + } + public ShopViewModel? Update(ShopBindingModel model) + { + using var context = new ConfectioneryDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var product = context.Shops.FirstOrDefault(rec => rec.Id == model.Id); + if (product == null) + { + return null; + } + product.Update(model); + context.SaveChanges(); + product.UpdatePastries(context, model); + transaction.Commit(); + return product.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var element = context.Shops + .Include(x => x.Pastries) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Shops.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public bool Supply(int pastryId, int count) + { + using ConfectioneryDatabase context = new ConfectioneryDatabase(); + var transaction = context.Database.BeginTransaction(); + foreach (Shop shop in context.Shops.Include(x => x.Pastries).ThenInclude(x => x.Pastry)) + { + int freeShopSpace = shop.PastryCapacity - shop.ShopPastries.Select(y => y.Value.Item2).Sum(); + if (freeShopSpace > 0) + { + if (freeShopSpace >= count) + { + if (shop.ShopPastries.ContainsKey(pastryId)) + { + var shopPastry = shop.ShopPastries[pastryId]; + shopPastry.Item2 += count; + shop.ShopPastries[pastryId] = shopPastry; + } + else + { + Pastry pastry = context.Pastrys.First(x => x.Id == pastryId); + shop.ShopPastries.Add(pastryId, (pastry, count)); + } + shop.RemapPastries(context); + count = 0; + } + else + { + int dif = count - freeShopSpace; + count -= freeShopSpace; + if (shop.ShopPastries.TryGetValue(pastryId, out var pastryCount)) + { + var shopPastry = shop.ShopPastries[pastryId]; + shopPastry.Item2 = pastryCount.Item2 + freeShopSpace; + shop.ShopPastries[pastryId] = shopPastry; + } + else + { + Pastry pastry = context.Pastrys.First(x => x.Id == pastryId); + shop.ShopPastries.Add(pastryId, (pastry, freeShopSpace)); + } + shop.RemapPastries(context); + } + } + } + if (count == 0) + { + transaction.Commit(); + return true; + } + transaction.Rollback(); + return false; + } + + public bool Sell(int pastryId, int count) + { + using ConfectioneryDatabase context = new ConfectioneryDatabase(); + var transaction = context.Database.BeginTransaction(); + foreach (Shop shop in context.Shops.Include(x => x.Pastries).ThenInclude(x => x.Pastry)) + { + int shopPastryCount = shop.ShopPastries.Select(x => x.Value).Where(x => x.Item1.Id == pastryId).Sum(x => x.Item2); + if (count - shopPastryCount >= 0) + { + count -= shopPastryCount; + shop.ShopPastries.Remove(pastryId); + } + else + { + var shopPastry = shop.ShopPastries[pastryId]; + shopPastry.Item2 -= count; + count = 0; + shop.ShopPastries[pastryId] = shopPastry; + } + shop.RemapPastries(context); + } + if (count == 0) + { + transaction.Commit(); + return true; + } + transaction.Rollback(); + return false; + } + } +} diff --git a/Confectionery/ConfectioneryDataBaseImplement/Migrations/20230306053539_InitMigration.Designer.cs b/Confectionery/ConfectioneryDataBaseImplement/Migrations/20230306053539_InitMigration.Designer.cs new file mode 100644 index 0000000..c8212d5 --- /dev/null +++ b/Confectionery/ConfectioneryDataBaseImplement/Migrations/20230306053539_InitMigration.Designer.cs @@ -0,0 +1,169 @@ +// +using System; +using ConfectioneryDataBaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace ConfectioneryDataBaseImplement.Migrations +{ + [DbContext(typeof(ConfectioneryDatabase))] + [Migration("20230306053539_InitMigration")] + partial class InitMigration + { + /// + 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("ConfectioneryDataBaseImplement.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("ConfectioneryDataBaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.Pastry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("PastryName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Pastrys"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.PastryIngredient", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("IngredientId") + .HasColumnType("int"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("IngredientId"); + + b.HasIndex("PastryId"); + + b.ToTable("PastryIngredients"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.Order", b => + { + b.HasOne("ConfectioneryDataBaseImplement.Models.Pastry", null) + .WithMany("Orders") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.PastryIngredient", b => + { + b.HasOne("ConfectioneryDataBaseImplement.Models.Ingredient", "Ingredient") + .WithMany("PastryIgredients") + .HasForeignKey("IngredientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ConfectioneryDataBaseImplement.Models.Pastry", "Pastry") + .WithMany("Ingredients") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Ingredient"); + + b.Navigation("Pastry"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.Ingredient", b => + { + b.Navigation("PastryIgredients"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.Pastry", b => + { + b.Navigation("Ingredients"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Confectionery/ConfectioneryDataBaseImplement/Migrations/20230306053539_InitMigration.cs b/Confectionery/ConfectioneryDataBaseImplement/Migrations/20230306053539_InitMigration.cs new file mode 100644 index 0000000..913ef98 --- /dev/null +++ b/Confectionery/ConfectioneryDataBaseImplement/Migrations/20230306053539_InitMigration.cs @@ -0,0 +1,125 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace ConfectioneryDataBaseImplement.Migrations +{ + /// + public partial class InitMigration : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + 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: "Pastrys", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PastryName = table.Column(type: "nvarchar(max)", nullable: false), + Price = table.Column(type: "float", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Pastrys", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PastryId = 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_Pastrys_PastryId", + column: x => x.PastryId, + principalTable: "Pastrys", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "PastryIngredients", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PastryId = 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_PastryIngredients", x => x.Id); + table.ForeignKey( + name: "FK_PastryIngredients_Ingredients_IngredientId", + column: x => x.IngredientId, + principalTable: "Ingredients", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_PastryIngredients_Pastrys_PastryId", + column: x => x.PastryId, + principalTable: "Pastrys", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_Orders_PastryId", + table: "Orders", + column: "PastryId"); + + migrationBuilder.CreateIndex( + name: "IX_PastryIngredients_IngredientId", + table: "PastryIngredients", + column: "IngredientId"); + + migrationBuilder.CreateIndex( + name: "IX_PastryIngredients_PastryId", + table: "PastryIngredients", + column: "PastryId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "PastryIngredients"); + + migrationBuilder.DropTable( + name: "Ingredients"); + + migrationBuilder.DropTable( + name: "Pastrys"); + } + } +} diff --git a/Confectionery/ConfectioneryDataBaseImplement/Migrations/20230515043657_HardInitialCreate.Designer.cs b/Confectionery/ConfectioneryDataBaseImplement/Migrations/20230515043657_HardInitialCreate.Designer.cs new file mode 100644 index 0000000..39afb7d --- /dev/null +++ b/Confectionery/ConfectioneryDataBaseImplement/Migrations/20230515043657_HardInitialCreate.Designer.cs @@ -0,0 +1,246 @@ +// +using System; +using ConfectioneryDataBaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace ConfectioneryDataBaseImplement.Migrations +{ + [DbContext(typeof(ConfectioneryDatabase))] + [Migration("20230515043657_HardInitialCreate")] + partial class HardInitialCreate + { + /// + 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("ConfectioneryDataBaseImplement.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("ConfectioneryDataBaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.Pastry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("PastryName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Pastrys"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.PastryIngredient", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("IngredientId") + .HasColumnType("int"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("IngredientId"); + + b.HasIndex("PastryId"); + + b.ToTable("PastryIngredients"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("OpeningDate") + .HasColumnType("datetime2"); + + b.Property("PastryCapacity") + .HasColumnType("int"); + + b.Property("ShopAdress") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.ShopPastry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopPastries"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.Order", b => + { + b.HasOne("ConfectioneryDataBaseImplement.Models.Pastry", null) + .WithMany("Orders") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.PastryIngredient", b => + { + b.HasOne("ConfectioneryDataBaseImplement.Models.Ingredient", "Ingredient") + .WithMany("PastryIgredients") + .HasForeignKey("IngredientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ConfectioneryDataBaseImplement.Models.Pastry", "Pastry") + .WithMany("Ingredients") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Ingredient"); + + b.Navigation("Pastry"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.ShopPastry", b => + { + b.HasOne("ConfectioneryDataBaseImplement.Models.Pastry", "Pastry") + .WithMany() + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ConfectioneryDataBaseImplement.Models.Shop", "Shop") + .WithMany("Pastries") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pastry"); + + b.Navigation("Shop"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.Ingredient", b => + { + b.Navigation("PastryIgredients"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.Pastry", b => + { + b.Navigation("Ingredients"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.Shop", b => + { + b.Navigation("Pastries"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Confectionery/ConfectioneryDataBaseImplement/Migrations/20230515043657_HardInitialCreate.cs b/Confectionery/ConfectioneryDataBaseImplement/Migrations/20230515043657_HardInitialCreate.cs new file mode 100644 index 0000000..6104ca7 --- /dev/null +++ b/Confectionery/ConfectioneryDataBaseImplement/Migrations/20230515043657_HardInitialCreate.cs @@ -0,0 +1,78 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace ConfectioneryDataBaseImplement.Migrations +{ + /// + public partial class HardInitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + 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), + ShopAdress = table.Column(type: "nvarchar(max)", nullable: false), + OpeningDate = table.Column(type: "datetime2", nullable: false), + PastryCapacity = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "ShopPastries", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ShopId = table.Column(type: "int", nullable: false), + PastryId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ShopPastries", x => x.Id); + table.ForeignKey( + name: "FK_ShopPastries_Pastrys_PastryId", + column: x => x.PastryId, + principalTable: "Pastrys", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopPastries_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_ShopPastries_PastryId", + table: "ShopPastries", + column: "PastryId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopPastries_ShopId", + table: "ShopPastries", + column: "ShopId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ShopPastries"); + + migrationBuilder.DropTable( + name: "Shops"); + } + } +} diff --git a/Confectionery/ConfectioneryDataBaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs b/Confectionery/ConfectioneryDataBaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs new file mode 100644 index 0000000..0d31cfc --- /dev/null +++ b/Confectionery/ConfectioneryDataBaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs @@ -0,0 +1,243 @@ +// +using System; +using ConfectioneryDataBaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace ConfectioneryDataBaseImplement.Migrations +{ + [DbContext(typeof(ConfectioneryDatabase))] + partial class ConfectioneryDatabaseModelSnapshot : 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("ConfectioneryDataBaseImplement.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("ConfectioneryDataBaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.Pastry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("PastryName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Pastrys"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.PastryIngredient", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("IngredientId") + .HasColumnType("int"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("IngredientId"); + + b.HasIndex("PastryId"); + + b.ToTable("PastryIngredients"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("OpeningDate") + .HasColumnType("datetime2"); + + b.Property("PastryCapacity") + .HasColumnType("int"); + + b.Property("ShopAdress") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.ShopPastry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopPastries"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.Order", b => + { + b.HasOne("ConfectioneryDataBaseImplement.Models.Pastry", null) + .WithMany("Orders") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.PastryIngredient", b => + { + b.HasOne("ConfectioneryDataBaseImplement.Models.Ingredient", "Ingredient") + .WithMany("PastryIgredients") + .HasForeignKey("IngredientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ConfectioneryDataBaseImplement.Models.Pastry", "Pastry") + .WithMany("Ingredients") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Ingredient"); + + b.Navigation("Pastry"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.ShopPastry", b => + { + b.HasOne("ConfectioneryDataBaseImplement.Models.Pastry", "Pastry") + .WithMany() + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ConfectioneryDataBaseImplement.Models.Shop", "Shop") + .WithMany("Pastries") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pastry"); + + b.Navigation("Shop"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.Ingredient", b => + { + b.Navigation("PastryIgredients"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.Pastry", b => + { + b.Navigation("Ingredients"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("ConfectioneryDataBaseImplement.Models.Shop", b => + { + b.Navigation("Pastries"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Confectionery/ConfectioneryDataBaseImplement/Models/Ingredient.cs b/Confectionery/ConfectioneryDataBaseImplement/Models/Ingredient.cs new file mode 100644 index 0000000..2f1d6a8 --- /dev/null +++ b/Confectionery/ConfectioneryDataBaseImplement/Models/Ingredient.cs @@ -0,0 +1,61 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDataBaseImplement.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 PastryIgredients { 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/Confectionery/ConfectioneryDataBaseImplement/Models/Order.cs b/Confectionery/ConfectioneryDataBaseImplement/Models/Order.cs new file mode 100644 index 0000000..e9f3c9d --- /dev/null +++ b/Confectionery/ConfectioneryDataBaseImplement/Models/Order.cs @@ -0,0 +1,65 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Enums; +using ConfectioneryDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDataBaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + [Required] + public int PastryId { get; private set; } + [Required] + public int Count { get; private set; } + [Required] + public double Sum { get; private set; } + [Required] + public OrderStatus Status { get; private set; } + [Required] + public DateTime DateCreate { get; private set; } + public DateTime? DateImplement { get; private set; } + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + PastryId = model.PastryId, + 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; + if (model.DateImplement.HasValue) DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + PastryId = PastryId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + } +} diff --git a/Confectionery/ConfectioneryDataBaseImplement/Models/Pastry.cs b/Confectionery/ConfectioneryDataBaseImplement/Models/Pastry.cs new file mode 100644 index 0000000..c7a3c44 --- /dev/null +++ b/Confectionery/ConfectioneryDataBaseImplement/Models/Pastry.cs @@ -0,0 +1,94 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDataBaseImplement.Models +{ + public class Pastry : IPastryModel + { + public int Id { get; set; } + [Required] + public string PastryName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _pastryIngredients = null; + [NotMapped] + public Dictionary PastryIngredients + { + get + { + if (_pastryIngredients == null) + { + _pastryIngredients = Ingredients + .ToDictionary(recPI => recPI.IngredientId, recPI => (recPI.Ingredient as IIngredientModel, recPI.Count)); + } + return _pastryIngredients; + } + } + [ForeignKey("PastryId")] + public virtual List Ingredients { get; set; } = new(); + [ForeignKey("PastryId")] + public virtual List Orders { get; set; } = new(); + public static Pastry Create(ConfectioneryDatabase context, PastryBindingModel model) + { + return new Pastry() + { + Id = model.Id, + PastryName = model.PastryName, + Price = model.Price, + Ingredients = model.PastryIngredients.Select(x => new PastryIngredient + { + Ingredient = context.Ingredients.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(PastryBindingModel model) + { + PastryName = model.PastryName; + Price = model.Price; + } + public PastryViewModel GetViewModel => new() + { + Id = Id, + PastryName = PastryName, + Price = Price, + PastryIngredients = PastryIngredients + }; + public void UpdateIngredients(ConfectioneryDatabase context, PastryBindingModel model) + { + var pastryIngredients = context.PastryIngredients.Where(rec => rec.PastryId == model.Id).ToList(); + if (pastryIngredients != null && pastryIngredients.Count > 0) + { + // удалили те, которых нет в модели + context.PastryIngredients.RemoveRange(pastryIngredients.Where(rec => !model.PastryIngredients.ContainsKey(rec.IngredientId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateIngredient in pastryIngredients) + { + updateIngredient.Count = model.PastryIngredients[updateIngredient.IngredientId].Item2; + model.PastryIngredients.Remove(updateIngredient.IngredientId); + } + var pastry = context.Pastrys.First(x => x.Id == Id); + foreach (var pc in model.PastryIngredients) + { + context.PastryIngredients.Add(new PastryIngredient + { + Pastry = pastry, + Ingredient = context.Ingredients.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _pastryIngredients = null; + } + } + } +} diff --git a/Confectionery/ConfectioneryDataBaseImplement/Models/PastryIngredient.cs b/Confectionery/ConfectioneryDataBaseImplement/Models/PastryIngredient.cs new file mode 100644 index 0000000..3d21566 --- /dev/null +++ b/Confectionery/ConfectioneryDataBaseImplement/Models/PastryIngredient.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDataBaseImplement.Models +{ + public class PastryIngredient + { + public int Id { get; set; } + [Required] + public int PastryId { get; set; } + [Required] + public int IngredientId { get; set; } + [Required] + public int Count { get; set; } + public virtual Ingredient Ingredient { get; set; } = new(); + public virtual Pastry Pastry { get; set; } = new(); + } +} diff --git a/Confectionery/ConfectioneryDataBaseImplement/Models/Shop.cs b/Confectionery/ConfectioneryDataBaseImplement/Models/Shop.cs new file mode 100644 index 0000000..89252d8 --- /dev/null +++ b/Confectionery/ConfectioneryDataBaseImplement/Models/Shop.cs @@ -0,0 +1,116 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDataBaseImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + [Required] + public string ShopName { get; private set; } = string.Empty; + [Required] + public string ShopAdress { get; private set; } = string.Empty; + [Required] + public DateTime OpeningDate { get; private set; } + [Required] + public int PastryCapacity { get; private set; } + private Dictionary? _shopPastries = null; + [NotMapped] + public Dictionary ShopPastries + { + get + { + if (_shopPastries == null) + { + _shopPastries = Pastries + .ToDictionary(recSP => recSP.PastryId, recPI => (recPI.Pastry as IPastryModel, recPI.Count)); + } + return _shopPastries; + } + } + [ForeignKey("ShopId")] + public virtual List Pastries { get; set; } = new(); + + public static Shop Create(ShopBindingModel model, ConfectioneryDatabase context) + { + return new Shop() + { + Id = model.Id, + ShopAdress = model.ShopAdress, + ShopName = model.ShopName, + OpeningDate = model.OpeningDate, + PastryCapacity = model.PastryCapacity, + Pastries = model.ShopPastries.Select(x => new ShopPastry { + Pastry = context.Pastrys.First(y => y.Id == x.Key), Count = x.Value.Item2 }).ToList() + }; + } + + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + ShopName = model.ShopName; + ShopAdress = model.ShopAdress; + OpeningDate = model.OpeningDate; + PastryCapacity = model.PastryCapacity; + } + + public void UpdatePastries(ConfectioneryDatabase context, ShopBindingModel model) + { + var list = context.ShopPastries.ToList(); + var shopPastries = context.ShopPastries.Where(rec => rec.ShopId == model.Id).ToList(); + if (shopPastries != null && shopPastries.Count > 0) + { + // удалили те, которых нет в модели + context.ShopPastries.RemoveRange(shopPastries.Where((ShopPastry rec) => !model.ShopPastries.ContainsKey(rec.PastryId))); + context.SaveChanges(); + + shopPastries = context.ShopPastries.Where((ShopPastry rec) => rec.ShopId == model.Id).ToList(); + // обновили количество у существующих записей + foreach (var updateIngredient in shopPastries) + { + updateIngredient.Count = model.ShopPastries[updateIngredient.PastryId].Item2; + model.ShopPastries.Remove(updateIngredient.PastryId); + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var sp in model.ShopPastries) + { + context.ShopPastries.Add(new ShopPastry + { + Shop = shop, + Pastry = context.Pastrys.First(x => x.Id == sp.Key), + Count = sp.Value.Item2 + }); + context.SaveChanges(); + } + _shopPastries = null; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + ShopAdress = ShopAdress, + ShopPastries = ShopPastries, + OpeningDate = OpeningDate, + PastryCapacity = PastryCapacity, + }; + + public void RemapPastries(ConfectioneryDatabase context) + { + UpdatePastries(context, new ShopBindingModel { Id = Id, ShopPastries = ShopPastries }); + } + } +} diff --git a/Confectionery/ConfectioneryDataBaseImplement/Models/ShopPastry.cs b/Confectionery/ConfectioneryDataBaseImplement/Models/ShopPastry.cs new file mode 100644 index 0000000..57e2830 --- /dev/null +++ b/Confectionery/ConfectioneryDataBaseImplement/Models/ShopPastry.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDataBaseImplement.Models +{ + public class ShopPastry + { + public int Id { get; set; } + [Required] + public int ShopId { get; set; } + [Required] + public int PastryId { get; set; } + [Required] + public int Count { get; set; } + public virtual Shop Shop { get; set; } = new(); + public virtual Pastry Pastry { get; set; } = new(); + } +}