From e852d86b6c8e1a1ba87ca0b7311fd88640a06fd8 Mon Sep 17 00:00:00 2001 From: urlilpolly Date: Mon, 17 Jun 2024 22:38:17 +0400 Subject: [PATCH] laba3hard --- .../FishFactoryDatabase.cs | 2 + .../Implements/ShopStorage.cs | 155 +++++++++++ .../20240617130014_lab3hard.Designer.cs | 246 ++++++++++++++++++ .../Migrations/20240617130014_lab3hard.cs | 78 ++++++ .../FishFactoryDatabaseModelSnapshot.cs | 77 ++++++ .../Models/Shop.cs | 109 ++++++++ .../Models/ShopCanned.cs | 27 ++ 7 files changed, 694 insertions(+) create mode 100644 FishFactory/FishFactoryDatabaseImplement/Implements/ShopStorage.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Migrations/20240617130014_lab3hard.Designer.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Migrations/20240617130014_lab3hard.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Models/Shop.cs create mode 100644 FishFactory/FishFactoryDatabaseImplement/Models/ShopCanned.cs diff --git a/FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabase.cs b/FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabase.cs index f306e61..0d222ef 100644 --- a/FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabase.cs +++ b/FishFactory/FishFactoryDatabaseImplement/FishFactoryDatabase.cs @@ -22,5 +22,7 @@ namespace FishFactoryDatabaseImplement public virtual DbSet Canneds { set; get; } public virtual DbSet CannedComponents { 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/Implements/ShopStorage.cs b/FishFactory/FishFactoryDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..bebb7d8 --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,155 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.SearchModels; +using FishFactoryContracts.StoragesContracts; +using FishFactoryContracts.ViewModels; +using FishFactoryDatabaseImplement.Models; +using FishFactoryDataModels.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +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/20240617130014_lab3hard.Designer.cs b/FishFactory/FishFactoryDatabaseImplement/Migrations/20240617130014_lab3hard.Designer.cs new file mode 100644 index 0000000..434516e --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Migrations/20240617130014_lab3hard.Designer.cs @@ -0,0 +1,246 @@ +// +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("20240617130014_lab3hard")] + partial class lab3hard + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.16") + .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("Canneds"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.CannedComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("CannedId") + .HasColumnType("int"); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("CannedId"); + + b.HasIndex("ComponentId"); + + b.ToTable("CannedComponents"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("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.CannedComponent", b => + { + b.HasOne("FishFactoryDatabaseImplement.Models.Canned", "Canned") + .WithMany("Components") + .HasForeignKey("CannedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FishFactoryDatabaseImplement.Models.Component", "Component") + .WithMany("CannedComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Canned"); + + b.Navigation("Component"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Order", b => + { + b.HasOne("FishFactoryDatabaseImplement.Models.Canned", null) + .WithMany("Orders") + .HasForeignKey("CannedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + 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("Components"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Component", b => + { + b.Navigation("CannedComponents"); + }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Shop", b => + { + b.Navigation("ListCannedFk"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/FishFactory/FishFactoryDatabaseImplement/Migrations/20240617130014_lab3hard.cs b/FishFactory/FishFactoryDatabaseImplement/Migrations/20240617130014_lab3hard.cs new file mode 100644 index 0000000..fa15ce0 --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Migrations/20240617130014_lab3hard.cs @@ -0,0 +1,78 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace FishFactoryDatabaseImplement.Migrations +{ + /// + public partial class lab3hard : 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), + 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: "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_Canneds_CannedId", + column: x => x.CannedId, + principalTable: "Canneds", + 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_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: "ShopCanned"); + + migrationBuilder.DropTable( + name: "Shops"); + } + } +} diff --git a/FishFactory/FishFactoryDatabaseImplement/Migrations/FishFactoryDatabaseModelSnapshot.cs b/FishFactory/FishFactoryDatabaseImplement/Migrations/FishFactoryDatabaseModelSnapshot.cs index a905fcc..ebc0388 100644 --- a/FishFactory/FishFactoryDatabaseImplement/Migrations/FishFactoryDatabaseModelSnapshot.cs +++ b/FishFactory/FishFactoryDatabaseImplement/Migrations/FishFactoryDatabaseModelSnapshot.cs @@ -121,6 +121,59 @@ namespace FishFactoryDatabaseImplement.Migrations 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.CannedComponent", b => { b.HasOne("FishFactoryDatabaseImplement.Models.Canned", "Canned") @@ -149,6 +202,25 @@ namespace FishFactoryDatabaseImplement.Migrations .IsRequired(); }); + 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("Components"); @@ -160,6 +232,11 @@ namespace FishFactoryDatabaseImplement.Migrations { b.Navigation("CannedComponents"); }); + + modelBuilder.Entity("FishFactoryDatabaseImplement.Models.Shop", b => + { + b.Navigation("ListCannedFk"); + }); #pragma warning restore 612, 618 } } diff --git a/FishFactory/FishFactoryDatabaseImplement/Models/Shop.cs b/FishFactory/FishFactoryDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..d16ddc6 --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Models/Shop.cs @@ -0,0 +1,109 @@ +using FishFactoryContracts.BindingModels; +using FishFactoryContracts.ViewModels; +using FishFactoryDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace 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.Canneds.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.Canneds.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..980e85f --- /dev/null +++ b/FishFactory/FishFactoryDatabaseImplement/Models/ShopCanned.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace 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(); + } +}