diff --git a/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs index 9682e7f..aedd294 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs @@ -14,7 +14,7 @@ namespace ConfectioneryDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-0CI5KVE\SQLEXPRESS;Initial Catalog=ConfectioneryDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-0CI5KVE\SQLEXPRESS;Initial Catalog=ConfectioneryDatabase3hard;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } @@ -22,5 +22,7 @@ namespace ConfectioneryDatabaseImplement public virtual DbSet Pastries { set; get; } public virtual DbSet PastryComponents { 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/ShopStorage.cs b/Confectionery/ConfectioneryDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..71363af --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,151 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDatabaseImplement.Models; +using ConfectioneryDataModels.Models; +using Microsoft.EntityFrameworkCore; + +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(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 ConfectioneryDatabase(); + 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.UpdatePastries(context, model); + transaction.Commit(); + return shop.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 Sale(IPastryModel model, int count) + { + using var context = new ConfectioneryDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + foreach (var shop in context.Shops.Include(x => x.Pastries).ThenInclude(x => x.Pastry) + .Where(x => x.Pastries.Any(x => x.PastryId == model.Id)) + .ToList()) + { + var pastry = shop.ShopPastries[model.Id]; + int min = Math.Min(pastry.Item2, count); + if (min == pastry.Item2) + { + shop.ShopPastries.Remove(model.Id); + } + else + { + shop.ShopPastries[model.Id] = (pastry.Item1, pastry.Item2 - min); + } + shop.UpdatePastries(context, new() { Id = shop.Id, ShopPastries = shop.ShopPastries }); + count -= min; + if (count == 0) + { + context.SaveChanges(); + transaction.Commit(); + return true; + } + } + transaction.Rollback(); + return false; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240325181336_InitialCreate.Designer.cs b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240621080649_InitialCreate.Designer.cs similarity index 68% rename from Confectionery/ConfectioneryDatabaseImplement/Migrations/20240325181336_InitialCreate.Designer.cs rename to Confectionery/ConfectioneryDatabaseImplement/Migrations/20240621080649_InitialCreate.Designer.cs index 82ef147..668be46 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240325181336_InitialCreate.Designer.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240621080649_InitialCreate.Designer.cs @@ -12,7 +12,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace ConfectioneryDatabaseImplement.Migrations { [DbContext(typeof(ConfectioneryDatabase))] - [Migration("20240325181336_InitialCreate")] + [Migration("20240621080649_InitialCreate")] partial class InitialCreate { /// @@ -124,6 +124,59 @@ namespace ConfectioneryDatabaseImplement.Migrations b.ToTable("PastryComponents"); }); + modelBuilder.Entity("ConfectioneryDatabaseImplement.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("PastriesMaximum") + .HasColumnType("int"); + + 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", "Pastry") @@ -154,6 +207,25 @@ namespace ConfectioneryDatabaseImplement.Migrations 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.Component", b => { b.Navigation("ProductComponents"); @@ -165,6 +237,11 @@ namespace ConfectioneryDatabaseImplement.Migrations b.Navigation("Orders"); }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b => + { + b.Navigation("Pastries"); + }); #pragma warning restore 612, 618 } } diff --git a/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240325181336_InitialCreate.cs b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240621080649_InitialCreate.cs similarity index 66% rename from Confectionery/ConfectioneryDatabaseImplement/Migrations/20240325181336_InitialCreate.cs rename to Confectionery/ConfectioneryDatabaseImplement/Migrations/20240621080649_InitialCreate.cs index d633f68..df49625 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240325181336_InitialCreate.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240621080649_InitialCreate.cs @@ -39,6 +39,22 @@ namespace ConfectioneryDatabaseImplement.Migrations table.PrimaryKey("PK_Pastries", 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), + PastriesMaximum = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + migrationBuilder.CreateTable( name: "Orders", columns: table => new @@ -90,6 +106,33 @@ namespace ConfectioneryDatabaseImplement.Migrations onDelete: ReferentialAction.Cascade); }); + 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_Pastries_PastryId", + column: x => x.PastryId, + principalTable: "Pastries", + 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_Orders_PastryId", table: "Orders", @@ -104,6 +147,16 @@ namespace ConfectioneryDatabaseImplement.Migrations name: "IX_PastryComponents_PastryId", table: "PastryComponents", column: "PastryId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopPastries_PastryId", + table: "ShopPastries", + column: "PastryId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopPastries_ShopId", + table: "ShopPastries", + column: "ShopId"); } /// @@ -115,11 +168,17 @@ namespace ConfectioneryDatabaseImplement.Migrations migrationBuilder.DropTable( name: "PastryComponents"); + migrationBuilder.DropTable( + name: "ShopPastries"); + migrationBuilder.DropTable( name: "Components"); migrationBuilder.DropTable( name: "Pastries"); + + migrationBuilder.DropTable( + name: "Shops"); } } } diff --git a/Confectionery/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs b/Confectionery/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs index e7c13d9..08ba109 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs @@ -121,6 +121,59 @@ namespace ConfectioneryDatabaseImplement.Migrations b.ToTable("PastryComponents"); }); + modelBuilder.Entity("ConfectioneryDatabaseImplement.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("PastriesMaximum") + .HasColumnType("int"); + + 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", "Pastry") @@ -151,6 +204,25 @@ namespace ConfectioneryDatabaseImplement.Migrations 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.Component", b => { b.Navigation("ProductComponents"); @@ -162,6 +234,11 @@ namespace ConfectioneryDatabaseImplement.Migrations b.Navigation("Orders"); }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b => + { + b.Navigation("Pastries"); + }); #pragma warning restore 612, 618 } } diff --git a/Confectionery/ConfectioneryDatabaseImplement/Models/Shop.cs b/Confectionery/ConfectioneryDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..04f35fc --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Models/Shop.cs @@ -0,0 +1,115 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.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 ConfectioneryDatabaseImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; set; } + + [Required] + public string ShopName { get; set; } = string.Empty; + + [Required] + public string Address { get; set; } = string.Empty; + + [Required] + public DateTime DateOpening { get; set; } + + [Required] + public int PastriesMaximum { get; set; } + + private Dictionary? _shopPastries = null; + + [NotMapped] + public Dictionary ShopPastries + { + get + { + if (_shopPastries == null) + { + _shopPastries = Pastries + .ToDictionary(x => x.PastryId, x => (x.Pastry as IPastryModel, x.Count)); + } + return _shopPastries; + } + } + + [ForeignKey("ShopId")] + public virtual List Pastries { get; set; } = new(); + + public static Shop Create(ConfectioneryDatabase context, ShopBindingModel model) + { + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOpening = model.DateOpening, + PastriesMaximum = model.PastriesMaximum, + Pastries = model.ShopPastries.Select(x => new ShopPastry + { + Pastry = context.Pastries.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; + PastriesMaximum = model.PastriesMaximum; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpening = DateOpening, + PastriesMaximum = PastriesMaximum, + ShopPastries = ShopPastries + }; + + public void UpdatePastries(ConfectioneryDatabase context, ShopBindingModel model) + { + var shopPastries = context.ShopPastries.Where(rec => rec.ShopId == model.Id).ToList(); + if (shopPastries != null && shopPastries.Count > 0) + { + context.ShopPastries.RemoveRange(shopPastries.Where(rec => !model.ShopPastries.ContainsKey(rec.PastryId))); + context.SaveChanges(); + foreach (var updatePastry in shopPastries) + { + if (model.ShopPastries.ContainsKey(updatePastry.PastryId)) + { + updatePastry.Count = model.ShopPastries[updatePastry.PastryId].Item2; + model.ShopPastries.Remove(updatePastry.PastryId); + } + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var ic in model.ShopPastries) + { + context.ShopPastries.Add(new ShopPastry + { + Shop = shop, + Pastry = context.Pastries.First(x => x.Id == ic.Key), + Count = ic.Value.Item2 + }); + context.SaveChanges(); + } + _shopPastries = null; + } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/Models/ShopPastry.cs b/Confectionery/ConfectioneryDatabaseImplement/Models/ShopPastry.cs new file mode 100644 index 0000000..fda1998 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Models/ShopPastry.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 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 Pastry Pastry { get; set; } = new(); + + public virtual Shop Shop { get; set; } = new(); + } +}