diff --git a/FlowerShopDatabaseImplement/Flower.cs b/FlowerShopDatabaseImplement/Flower.cs index 1c50d43..729c70d 100644 --- a/FlowerShopDatabaseImplement/Flower.cs +++ b/FlowerShopDatabaseImplement/Flower.cs @@ -39,6 +39,8 @@ namespace FlowerShopDatabaseImplement.Models public virtual List Components { get; set; } = new(); [ForeignKey("FlowerId")] public virtual List Orders { get; set; } = new(); + [ForeignKey("FlowerId")] + public virtual List ShopFlowers { get; set; } = new(); public static Flower Create(FlowerShopDataBase context, FlowerBindingModel model) { return new Flower() @@ -68,7 +70,7 @@ namespace FlowerShopDatabaseImplement.Models public void UpdateComponents(FlowerShopDataBase context, FlowerBindingModel model) { var flowerComponents = context.FlowerComponents.Where(rec =>rec.FlowerId == model.Id).ToList(); - if (flowerComponents != null && FlowerComponents.Count > 0) + if (flowerComponents != null && flowerComponents.Count > 0) { context.FlowerComponents.RemoveRange(flowerComponents.Where(rec => !model.FlowerComponents.ContainsKey(rec.ComponentId))); context.SaveChanges(); diff --git a/FlowerShopDatabaseImplement/FlowerShopDataBase.cs b/FlowerShopDatabaseImplement/FlowerShopDataBase.cs index 2dcffdc..91013e5 100644 --- a/FlowerShopDatabaseImplement/FlowerShopDataBase.cs +++ b/FlowerShopDatabaseImplement/FlowerShopDataBase.cs @@ -14,7 +14,7 @@ namespace FlowerShopDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=FlowerShopDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=FlowerShopHardDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } @@ -22,5 +22,7 @@ namespace FlowerShopDatabaseImplement public virtual DbSet Flowers { set; get; } public virtual DbSet FlowerComponents { set; get; } public virtual DbSet Orders { set; get; } + public virtual DbSet Shops { set; get; } + public virtual DbSet ShopFlowers { set; get; } } } diff --git a/FlowerShopDatabaseImplement/Migrations/20240312171524_InitialCreate.Designer.cs b/FlowerShopDatabaseImplement/Migrations/20240324171319_InitialCreate.Designer.cs similarity index 65% rename from FlowerShopDatabaseImplement/Migrations/20240312171524_InitialCreate.Designer.cs rename to FlowerShopDatabaseImplement/Migrations/20240324171319_InitialCreate.Designer.cs index 4be73f1..657cead 100644 --- a/FlowerShopDatabaseImplement/Migrations/20240312171524_InitialCreate.Designer.cs +++ b/FlowerShopDatabaseImplement/Migrations/20240324171319_InitialCreate.Designer.cs @@ -12,7 +12,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace FlowerShopDatabaseImplement.Migrations { [DbContext(typeof(FlowerShopDataBase))] - [Migration("20240312171524_InitialCreate")] + [Migration("20240324171319_InitialCreate")] partial class InitialCreate { protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -90,7 +90,7 @@ namespace FlowerShopDatabaseImplement.Migrations b.ToTable("FlowerComponents"); }); - modelBuilder.Entity("FlowerShopDatabaseImplement.Order", b => + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Order", b => { b.Property("Id") .ValueGeneratedOnAdd() @@ -123,6 +123,59 @@ namespace FlowerShopDatabaseImplement.Migrations b.ToTable("Orders"); }); + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DateOpen") + .HasColumnType("datetime2"); + + b.Property("MaxCapacity") + .HasColumnType("int"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.ShopFlower", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("FlowerId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("FlowerId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopFlowers"); + }); + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.FlowerComponent", b => { b.HasOne("FlowerShopDatabaseImplement.Models.Component", "Component") @@ -142,13 +195,34 @@ namespace FlowerShopDatabaseImplement.Migrations b.Navigation("Flower"); }); - modelBuilder.Entity("FlowerShopDatabaseImplement.Order", b => + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Order", b => { - b.HasOne("FlowerShopDatabaseImplement.Models.Flower", null) + b.HasOne("FlowerShopDatabaseImplement.Models.Flower", "Flower") .WithMany("Orders") .HasForeignKey("FlowerId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + + b.Navigation("Flower"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.ShopFlower", b => + { + b.HasOne("FlowerShopDatabaseImplement.Models.Flower", "Flower") + .WithMany("ShopFlowers") + .HasForeignKey("FlowerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FlowerShopDatabaseImplement.Models.Shop", "Shop") + .WithMany("Flowers") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Flower"); + + b.Navigation("Shop"); }); modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Component", b => @@ -161,6 +235,13 @@ namespace FlowerShopDatabaseImplement.Migrations b.Navigation("Components"); b.Navigation("Orders"); + + b.Navigation("ShopFlowers"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Shop", b => + { + b.Navigation("Flowers"); }); #pragma warning restore 612, 618 } diff --git a/FlowerShopDatabaseImplement/Migrations/20240312171524_InitialCreate.cs b/FlowerShopDatabaseImplement/Migrations/20240324171319_InitialCreate.cs similarity index 66% rename from FlowerShopDatabaseImplement/Migrations/20240312171524_InitialCreate.cs rename to FlowerShopDatabaseImplement/Migrations/20240324171319_InitialCreate.cs index f2647dd..92d6a96 100644 --- a/FlowerShopDatabaseImplement/Migrations/20240312171524_InitialCreate.cs +++ b/FlowerShopDatabaseImplement/Migrations/20240324171319_InitialCreate.cs @@ -37,6 +37,22 @@ namespace FlowerShopDatabaseImplement.Migrations table.PrimaryKey("PK_Flowers", 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), + DateOpen = table.Column(type: "datetime2", nullable: false), + MaxCapacity = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + migrationBuilder.CreateTable( name: "FlowerComponents", columns: table => new @@ -88,6 +104,33 @@ namespace FlowerShopDatabaseImplement.Migrations onDelete: ReferentialAction.Cascade); }); + migrationBuilder.CreateTable( + name: "ShopFlowers", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + FlowerId = 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_ShopFlowers", x => x.Id); + table.ForeignKey( + name: "FK_ShopFlowers_Flowers_FlowerId", + column: x => x.FlowerId, + principalTable: "Flowers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopFlowers_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + migrationBuilder.CreateIndex( name: "IX_FlowerComponents_ComponentId", table: "FlowerComponents", @@ -102,6 +145,16 @@ namespace FlowerShopDatabaseImplement.Migrations name: "IX_Orders_FlowerId", table: "Orders", column: "FlowerId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopFlowers_FlowerId", + table: "ShopFlowers", + column: "FlowerId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopFlowers_ShopId", + table: "ShopFlowers", + column: "ShopId"); } protected override void Down(MigrationBuilder migrationBuilder) @@ -112,11 +165,17 @@ namespace FlowerShopDatabaseImplement.Migrations migrationBuilder.DropTable( name: "Orders"); + migrationBuilder.DropTable( + name: "ShopFlowers"); + migrationBuilder.DropTable( name: "Components"); migrationBuilder.DropTable( name: "Flowers"); + + migrationBuilder.DropTable( + name: "Shops"); } } } diff --git a/FlowerShopDatabaseImplement/Migrations/FlowerShopDataBaseModelSnapshot.cs b/FlowerShopDatabaseImplement/Migrations/FlowerShopDataBaseModelSnapshot.cs index 75b6ac8..3effe9c 100644 --- a/FlowerShopDatabaseImplement/Migrations/FlowerShopDataBaseModelSnapshot.cs +++ b/FlowerShopDatabaseImplement/Migrations/FlowerShopDataBaseModelSnapshot.cs @@ -88,7 +88,7 @@ namespace FlowerShopDatabaseImplement.Migrations b.ToTable("FlowerComponents"); }); - modelBuilder.Entity("FlowerShopDatabaseImplement.Order", b => + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Order", b => { b.Property("Id") .ValueGeneratedOnAdd() @@ -121,6 +121,59 @@ namespace FlowerShopDatabaseImplement.Migrations b.ToTable("Orders"); }); + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DateOpen") + .HasColumnType("datetime2"); + + b.Property("MaxCapacity") + .HasColumnType("int"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.ShopFlower", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("FlowerId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("FlowerId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopFlowers"); + }); + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.FlowerComponent", b => { b.HasOne("FlowerShopDatabaseImplement.Models.Component", "Component") @@ -140,13 +193,34 @@ namespace FlowerShopDatabaseImplement.Migrations b.Navigation("Flower"); }); - modelBuilder.Entity("FlowerShopDatabaseImplement.Order", b => + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Order", b => { - b.HasOne("FlowerShopDatabaseImplement.Models.Flower", null) + b.HasOne("FlowerShopDatabaseImplement.Models.Flower", "Flower") .WithMany("Orders") .HasForeignKey("FlowerId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + + b.Navigation("Flower"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.ShopFlower", b => + { + b.HasOne("FlowerShopDatabaseImplement.Models.Flower", "Flower") + .WithMany("ShopFlowers") + .HasForeignKey("FlowerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("FlowerShopDatabaseImplement.Models.Shop", "Shop") + .WithMany("Flowers") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Flower"); + + b.Navigation("Shop"); }); modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Component", b => @@ -159,6 +233,13 @@ namespace FlowerShopDatabaseImplement.Migrations b.Navigation("Components"); b.Navigation("Orders"); + + b.Navigation("ShopFlowers"); + }); + + modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Shop", b => + { + b.Navigation("Flowers"); }); #pragma warning restore 612, 618 } diff --git a/FlowerShopDatabaseImplement/OrderStorage.cs b/FlowerShopDatabaseImplement/OrderStorage.cs index 6e7996b..e76132b 100644 --- a/FlowerShopDatabaseImplement/OrderStorage.cs +++ b/FlowerShopDatabaseImplement/OrderStorage.cs @@ -18,7 +18,7 @@ namespace FlowerShopDatabaseImplement.Implements public List GetFullList() { using var context = new FlowerShopDataBase(); - return context.Orders + return context.Orders.Include(x => x.Flower) .Select(x => AccessFlowerStorage(x.GetViewModel)) .ToList(); } @@ -29,7 +29,7 @@ namespace FlowerShopDatabaseImplement.Implements return new(); } using var context = new FlowerShopDataBase(); - return context.Orders + return context.Orders.Include(x => x.Flower) .Where(x => x.Id == model.Id) .Select(x => AccessFlowerStorage(x.GetViewModel)) .ToList(); @@ -41,16 +41,16 @@ namespace FlowerShopDatabaseImplement.Implements return null; } using var context = new FlowerShopDataBase(); - return AccessFlowerStorage(context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel); + return AccessFlowerStorage(context.Orders.Include(x => x.Flower).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel); } public OrderViewModel? Insert(OrderBindingModel model) { - var newOrder = Order.Create(model); + using var context = new FlowerShopDataBase(); + var newOrder = Order.Create(context, model); if (newOrder == null) { return null; - } - using var context = new FlowerShopDataBase(); + } context.Orders.Add(newOrder); context.SaveChanges(); return AccessFlowerStorage(newOrder.GetViewModel); @@ -58,8 +58,7 @@ namespace FlowerShopDatabaseImplement.Implements public OrderViewModel? Update(OrderBindingModel model) { using var context = new FlowerShopDataBase(); - var order = context.Orders.FirstOrDefault(x => x.Id == - model.Id); + var order = context.Orders.Include(x => x.Flower).FirstOrDefault(x => x.Id == model.Id); if (order == null) { return null; @@ -71,7 +70,7 @@ namespace FlowerShopDatabaseImplement.Implements public OrderViewModel? Delete(OrderBindingModel model) { using var context = new FlowerShopDataBase(); - var element = context.Orders.FirstOrDefault(rec => rec.Id == + var element = context.Orders.Include(x => x.Flower).FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { diff --git a/FlowerShopDatabaseImplement/Shop.cs b/FlowerShopDatabaseImplement/Shop.cs new file mode 100644 index 0000000..cf7ff97 --- /dev/null +++ b/FlowerShopDatabaseImplement/Shop.cs @@ -0,0 +1,116 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.ViewModels; +using FlowerShopDataModels.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 FlowerShopDatabaseImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + [Required] + public string ShopName { get; private set; } + [Required] + public string Address { get; private set; } + [Required] + public DateTime DateOpen { get; private set; } + [Required] + public int MaxCapacity { get; private set; } + private Dictionary? _shopFlowers = + null; + [NotMapped] + public Dictionary ShopFlowers + { + get + { + if (_shopFlowers == null) + { + _shopFlowers = Flowers + .ToDictionary(x => x.FlowerId, x => + (x.Flower as IFlowerModel, x.Count)); + } + return _shopFlowers; + } + } + [ForeignKey("ShopId")] + public virtual List Flowers { get; set; } = new(); + public static Shop? Create(FlowerShopDataBase context, ShopBindingModel model) + { + if (model == null) + return null; + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOpen = model.DateOpen, + MaxCapacity = model.MaxCapacity, + Flowers = model.ShopFlowers.Select(x => new + ShopFlower + { + Flower = context.Flowers.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; + MaxCapacity = model.MaxCapacity; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpen = DateOpen, + ShopFlowers = ShopFlowers, + MaxCapacity = MaxCapacity + }; + + public void UpdateFlowers(FlowerShopDataBase context, ShopBindingModel model) + { + var shopFlowers = context.ShopFlowers.Where(rec => + rec.ShopId == model.Id).ToList(); + if (shopFlowers != null && shopFlowers.Count > 0) + { + context.ShopFlowers.RemoveRange(shopFlowers.Where(rec => !model.ShopFlowers.ContainsKey(rec.ShopId))); + + context.SaveChanges(); + foreach (var updateFlower in shopFlowers) + { + updateFlower.Count = + model.ShopFlowers[updateFlower.FlowerId].Item2; + model.ShopFlowers.Remove(updateFlower.FlowerId); + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var pc in model.ShopFlowers) + { + context.ShopFlowers.Add(new ShopFlower + { + Shop = shop, + Flower = context.Flowers.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _shopFlowers = null; + } + } +} diff --git a/FlowerShopDatabaseImplement/ShopFlower.cs b/FlowerShopDatabaseImplement/ShopFlower.cs new file mode 100644 index 0000000..ce533a3 --- /dev/null +++ b/FlowerShopDatabaseImplement/ShopFlower.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 FlowerShopDatabaseImplement.Models +{ + public class ShopFlower + { + public int Id { get; set; } + [Required] + public int FlowerId { get; set; } + [Required] + public int ShopId { get; set; } + [Required] + public int Count { get; set; } + public virtual Shop Shop { get; set; } = new(); + public virtual Flower Flower { get; set; } = new(); + } +} diff --git a/FlowerShopDatabaseImplement/ShopStorage.cs b/FlowerShopDatabaseImplement/ShopStorage.cs new file mode 100644 index 0000000..5084393 --- /dev/null +++ b/FlowerShopDatabaseImplement/ShopStorage.cs @@ -0,0 +1,153 @@ +using FlowerShopContracts.BindingModels; +using FlowerShopContracts.SearchModels; +using FlowerShopContracts.StoragesContracts; +using FlowerShopContracts.ViewModels; +using FlowerShopDatabaseImplement.Models; +using FlowerShopDataModels.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FlowerShopDatabaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new FlowerShopDataBase(); + 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 FlowerShopDataBase(); + return context.Shops + .Include(x => x.Flowers) + .ThenInclude(x => x.Flower) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + using var context = new FlowerShopDataBase(); + return context.Shops + .Include(x => x.Flowers) + .ThenInclude(x => x.Flower) + .Select(x => x.GetViewModel) + .Where(x => x.ShopName.Contains(model.Name ?? string.Empty)) + .ToList(); + } + + public List GetFullList() + { + using var context = new FlowerShopDataBase(); + return context.Shops + .Include(x => x.Flowers) + .ThenInclude(x => x.Flower) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + using var context = new FlowerShopDataBase(); + 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 FlowerShopDataBase(); + 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.UpdateFlowers(context, model); + context.SaveChanges(); + return shop.GetViewModel; + } + catch + { + throw; + } + } + public bool SellFlowers(IFlowerModel model, int count) + { + if (model == null) + return false; + using var context = new FlowerShopDataBase(); + using var transaction = context.Database.BeginTransaction(); + List lst = new List(); + foreach (var el in context.ShopFlowers.Where(x => x.FlowerId == 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.ShopFlowers.Remove(el); + } + context.SaveChanges(); + transaction.Commit(); + return true; + } + } +}