diff --git a/ComputersShop/ComputersShopDatabaseImplements/ComputersShopDatabase.cs b/ComputersShop/ComputersShopDatabaseImplements/ComputersShopDatabase.cs index 31db7ad..ba234c7 100644 --- a/ComputersShop/ComputersShopDatabaseImplements/ComputersShopDatabase.cs +++ b/ComputersShop/ComputersShopDatabaseImplements/ComputersShopDatabase.cs @@ -23,5 +23,7 @@ namespace ComputersShopDatabaseImplements public virtual DbSet Computers { set; get; } public virtual DbSet ComputerComponents { set; get; } public virtual DbSet Orders { set; get; } + public virtual DbSet Shops { get; set; } + public virtual DbSet ShopComputers { get; set; } } } diff --git a/ComputersShop/ComputersShopDatabaseImplements/Implements/ShopStorage.cs b/ComputersShop/ComputersShopDatabaseImplements/Implements/ShopStorage.cs new file mode 100644 index 0000000..e2b7593 --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplements/Implements/ShopStorage.cs @@ -0,0 +1,164 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using Microsoft.EntityFrameworkCore; +using ComputersShopDatabaseImplements.Models; +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopDatabaseImplements.Implements +{ + public class ShopStorage : IShopStorage + { + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new ComputersShopDatabase(); + var element = context.Shops.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + context.Shops.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + using var context = new ComputersShopDatabase(); + return context.Shops.Include(x => x.Computers).ThenInclude(x => x.Computer).FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; + } + public List GetFiltredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + using var context = new ComputersShopDatabase(); + return context.Shops + .Include(x => x.Computers) + .ThenInclude(x => x.Computer) + .Select(x => x.GetViewModel) + .Where(x => x.ShopName.Contains(model.Name ?? string.Empty)) + .ToList(); + } + public List GetFullList() + { + using var context = new ComputersShopDatabase(); + return context.Shops + .Include(x => x.Computers) + .ThenInclude(x => x.Computer) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + using var context = new ComputersShopDatabase(); + try + { + var newShop = Shop.Create(context, model); + if (newShop == null) + { + return null; + } + if (context.Shops.Any(x => x.ShopName == newShop.ShopName)) + { + throw new Exception("Не должно быть два магазина с одним названием"); + } + context.Shops.Add(newShop); + context.SaveChanges(); + return newShop.GetViewModel; + } + catch + { + throw; + } + } + + public ShopViewModel? Update(ShopBindingModel model) + { + using var context = new ComputersShopDatabase(); + var shop = context.Shops.FirstOrDefault(x => x.Id == model.Id); + if (shop == null) + { + return null; + } + try + { + if (context.Shops.Any(x => (x.ShopName == model.ShopName && x.Id != model.Id))) + { + throw new Exception("Не должно быть два магазина с одним названием"); + } + shop.Update(model); + shop.UpdateDresses(context, model); + context.SaveChanges(); + return shop.GetViewModel; + } + catch + { + throw; + } + } + public bool SellComputers(IComputerModel model, int count) + { + if (model == null) + { + return false; + } + + using var context = new ComputersShopDatabase(); + using var transaction = context.Database.BeginTransaction(); + + List lst = new List(); + + foreach (var el in context.ShopComputers.Where(x => x.ComputerId == model.Id)) + { + int dif = count; + + if (el.Count < dif) + { + dif = el.Count; + } + + el.Count -= dif; + count -= dif; + + if (el.Count == 0) + { + lst.Add(el); + } + + if (count == 0) + { + break; + } + } + + if (count > 0) + { + transaction.Rollback(); + return false; + } + + foreach (var el in lst) + { + context.ShopComputers.Remove(el); + } + + context.SaveChanges(); + transaction.Commit(); + + return true; + } + + } +} diff --git a/ComputersShop/ComputersShopDatabaseImplements/Migrations/20240308192454_InitialCreate.Designer.cs b/ComputersShop/ComputersShopDatabaseImplements/Migrations/20240517011907_iwantsleep.Designer.cs similarity index 67% rename from ComputersShop/ComputersShopDatabaseImplements/Migrations/20240308192454_InitialCreate.Designer.cs rename to ComputersShop/ComputersShopDatabaseImplements/Migrations/20240517011907_iwantsleep.Designer.cs index efdc78a..6e1d242 100644 --- a/ComputersShop/ComputersShopDatabaseImplements/Migrations/20240308192454_InitialCreate.Designer.cs +++ b/ComputersShop/ComputersShopDatabaseImplements/Migrations/20240517011907_iwantsleep.Designer.cs @@ -12,8 +12,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace ComputersShopDatabaseImplements.Migrations { [DbContext(typeof(ComputersShopDatabase))] - [Migration("20240308192454_InitialCreate")] - partial class InitialCreate + [Migration("20240517011907_iwantsleep")] + partial class iwantsleep { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -124,6 +124,59 @@ namespace ComputersShopDatabaseImplements.Migrations b.ToTable("Orders"); }); + modelBuilder.Entity("ComputersShopDatabaseImplements.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DateOpen") + .HasColumnType("datetime2"); + + b.Property("MaxCount") + .HasColumnType("int"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("ComputersShopDatabaseImplements.Models.ShopComputer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComputerId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComputerId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopComputers"); + }); + modelBuilder.Entity("ComputersShopDatabaseImplements.Models.ComputerComponent", b => { b.HasOne("ComputersShopDatabaseImplements.Models.Component", "Component") @@ -154,6 +207,25 @@ namespace ComputersShopDatabaseImplements.Migrations b.Navigation("Computer"); }); + modelBuilder.Entity("ComputersShopDatabaseImplements.Models.ShopComputer", b => + { + b.HasOne("ComputersShopDatabaseImplements.Models.Computer", "Computer") + .WithMany() + .HasForeignKey("ComputerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ComputersShopDatabaseImplements.Models.Shop", "Shop") + .WithMany("Computers") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Computer"); + + b.Navigation("Shop"); + }); + modelBuilder.Entity("ComputersShopDatabaseImplements.Models.Component", b => { b.Navigation("ComputerComponents"); @@ -165,6 +237,11 @@ namespace ComputersShopDatabaseImplements.Migrations b.Navigation("Orders"); }); + + modelBuilder.Entity("ComputersShopDatabaseImplements.Models.Shop", b => + { + b.Navigation("Computers"); + }); #pragma warning restore 612, 618 } } diff --git a/ComputersShop/ComputersShopDatabaseImplements/Migrations/20240308192454_InitialCreate.cs b/ComputersShop/ComputersShopDatabaseImplements/Migrations/20240517011907_iwantsleep.cs similarity index 65% rename from ComputersShop/ComputersShopDatabaseImplements/Migrations/20240308192454_InitialCreate.cs rename to ComputersShop/ComputersShopDatabaseImplements/Migrations/20240517011907_iwantsleep.cs index 89d93a0..74a4300 100644 --- a/ComputersShop/ComputersShopDatabaseImplements/Migrations/20240308192454_InitialCreate.cs +++ b/ComputersShop/ComputersShopDatabaseImplements/Migrations/20240517011907_iwantsleep.cs @@ -6,7 +6,7 @@ using Microsoft.EntityFrameworkCore.Migrations; namespace ComputersShopDatabaseImplements.Migrations { /// - public partial class InitialCreate : Migration + public partial class iwantsleep : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) @@ -39,6 +39,22 @@ namespace ComputersShopDatabaseImplements.Migrations table.PrimaryKey("PK_Computers", 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), + MaxCount = table.Column(type: "int", nullable: false), + DateOpen = table.Column(type: "datetime2", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + migrationBuilder.CreateTable( name: "ComputerComponents", columns: table => new @@ -90,6 +106,33 @@ namespace ComputersShopDatabaseImplements.Migrations onDelete: ReferentialAction.Cascade); }); + migrationBuilder.CreateTable( + name: "ShopComputers", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ComputerId = 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_ShopComputers", x => x.Id); + table.ForeignKey( + name: "FK_ShopComputers_Computers_ComputerId", + column: x => x.ComputerId, + principalTable: "Computers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopComputers_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + migrationBuilder.CreateIndex( name: "IX_ComputerComponents_ComponentId", table: "ComputerComponents", @@ -104,6 +147,16 @@ namespace ComputersShopDatabaseImplements.Migrations name: "IX_Orders_ComputerId", table: "Orders", column: "ComputerId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopComputers_ComputerId", + table: "ShopComputers", + column: "ComputerId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopComputers_ShopId", + table: "ShopComputers", + column: "ShopId"); } /// @@ -115,11 +168,17 @@ namespace ComputersShopDatabaseImplements.Migrations migrationBuilder.DropTable( name: "Orders"); + migrationBuilder.DropTable( + name: "ShopComputers"); + migrationBuilder.DropTable( name: "Components"); migrationBuilder.DropTable( name: "Computers"); + + migrationBuilder.DropTable( + name: "Shops"); } } } diff --git a/ComputersShop/ComputersShopDatabaseImplements/Migrations/ComputersShopDatabaseModelSnapshot.cs b/ComputersShop/ComputersShopDatabaseImplements/Migrations/ComputersShopDatabaseModelSnapshot.cs index 2a06cd7..8be8c7c 100644 --- a/ComputersShop/ComputersShopDatabaseImplements/Migrations/ComputersShopDatabaseModelSnapshot.cs +++ b/ComputersShop/ComputersShopDatabaseImplements/Migrations/ComputersShopDatabaseModelSnapshot.cs @@ -121,6 +121,59 @@ namespace ComputersShopDatabaseImplements.Migrations b.ToTable("Orders"); }); + modelBuilder.Entity("ComputersShopDatabaseImplements.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DateOpen") + .HasColumnType("datetime2"); + + b.Property("MaxCount") + .HasColumnType("int"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("ComputersShopDatabaseImplements.Models.ShopComputer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComputerId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComputerId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopComputers"); + }); + modelBuilder.Entity("ComputersShopDatabaseImplements.Models.ComputerComponent", b => { b.HasOne("ComputersShopDatabaseImplements.Models.Component", "Component") @@ -151,6 +204,25 @@ namespace ComputersShopDatabaseImplements.Migrations b.Navigation("Computer"); }); + modelBuilder.Entity("ComputersShopDatabaseImplements.Models.ShopComputer", b => + { + b.HasOne("ComputersShopDatabaseImplements.Models.Computer", "Computer") + .WithMany() + .HasForeignKey("ComputerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ComputersShopDatabaseImplements.Models.Shop", "Shop") + .WithMany("Computers") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Computer"); + + b.Navigation("Shop"); + }); + modelBuilder.Entity("ComputersShopDatabaseImplements.Models.Component", b => { b.Navigation("ComputerComponents"); @@ -162,6 +234,11 @@ namespace ComputersShopDatabaseImplements.Migrations b.Navigation("Orders"); }); + + modelBuilder.Entity("ComputersShopDatabaseImplements.Models.Shop", b => + { + b.Navigation("Computers"); + }); #pragma warning restore 612, 618 } } diff --git a/ComputersShop/ComputersShopDatabaseImplements/Models/Shop.cs b/ComputersShop/ComputersShopDatabaseImplements/Models/Shop.cs new file mode 100644 index 0000000..be013ea --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplements/Models/Shop.cs @@ -0,0 +1,101 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopDatabaseImplements.Models +{ + public class Shop: IShopModel + { + public int Id { get; set; } + public string ShopName { get; set;} = string.Empty; + public string Address { get; set;} = string.Empty; + public int MaxCount { get; set;} + public DateTime DateOpen { get; set; } + public Dictionary? _shopComputers = null; + + [NotMapped] + public Dictionary? ShopComputers + { + get + { + if (_shopComputers == null) + { + _shopComputers = Computers.ToDictionary(x => x.ComputerId, x => (x.Computer as IComputerModel, x.Count)); + } + return _shopComputers; + } + } + [ForeignKey("ShopId")] + public virtual List Computers { get; set; } = new(); + public static Shop? Create(ComputersShopDatabase context, ShopBindingModel model) + { + if (model == null) + return null; + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOpen = model.DateOpen, + MaxCount = model.MaxCount, + Computers = model.ShopComputers.Select(x => new ShopComputer + { + Computer = context.Computers.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(ShopBindingModel? model) + { + if (model == null) + return; + ShopName = model.ShopName; + Address = model.Address; + DateOpen = model.DateOpen; + MaxCount = model.MaxCount; + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpen = DateOpen, + MaxCount = MaxCount, + ShopComputers = ShopComputers + }; + + public void UpdateDresses(ComputersShopDatabase context, ShopBindingModel model) + { + var shopComputers = context.ShopComputers.Where(rec => rec.ShopId == model.Id).ToList(); + if (shopComputers != null && shopComputers.Count > 0) + { + context.ShopComputers.RemoveRange(shopComputers.Where(rec => !model.ShopComputers.ContainsKey(rec.ComputerId))); + context.SaveChanges(); + foreach (var uDress in shopComputers) + { + uDress.Count = model.ShopComputers[uDress.ComputerId].Item2; + model.ShopComputers.Remove(uDress.ComputerId); + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var pc in model.ShopComputers) + { + context.ShopComputers.Add(new ShopComputer + { + Shop = shop, + Computer = context.Computers.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _shopComputers = null; + } + } +} diff --git a/ComputersShop/ComputersShopDatabaseImplements/Models/ShopComputer.cs b/ComputersShop/ComputersShopDatabaseImplements/Models/ShopComputer.cs new file mode 100644 index 0000000..315ccae --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplements/Models/ShopComputer.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 ComputersShopDatabaseImplements.Models +{ + public class ShopComputer + { + public int Id { get; set; } + [Required] + public int ComputerId { get; set; } + [Required] + public int ShopId { get; set; } + [Required] + public int Count { get; set; } + public virtual Shop Shop { get; set; } = new(); + public virtual Computer Computer { get; set; } = new(); + } +}