This commit is contained in:
antoc0der 2024-03-24 21:16:51 +04:00
parent 8de0355107
commit 380740c36a
9 changed files with 533 additions and 18 deletions

View File

@ -39,6 +39,8 @@ namespace FlowerShopDatabaseImplement.Models
public virtual List<FlowerComponent> Components { get; set; } = new(); public virtual List<FlowerComponent> Components { get; set; } = new();
[ForeignKey("FlowerId")] [ForeignKey("FlowerId")]
public virtual List<Order> Orders { get; set; } = new(); public virtual List<Order> Orders { get; set; } = new();
[ForeignKey("FlowerId")]
public virtual List<ShopFlower> ShopFlowers { get; set; } = new();
public static Flower Create(FlowerShopDataBase context, FlowerBindingModel model) public static Flower Create(FlowerShopDataBase context, FlowerBindingModel model)
{ {
return new Flower() return new Flower()
@ -68,7 +70,7 @@ namespace FlowerShopDatabaseImplement.Models
public void UpdateComponents(FlowerShopDataBase context, FlowerBindingModel model) public void UpdateComponents(FlowerShopDataBase context, FlowerBindingModel model)
{ {
var flowerComponents = context.FlowerComponents.Where(rec =>rec.FlowerId == model.Id).ToList(); 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.FlowerComponents.RemoveRange(flowerComponents.Where(rec => !model.FlowerComponents.ContainsKey(rec.ComponentId)));
context.SaveChanges(); context.SaveChanges();

View File

@ -14,7 +14,7 @@ namespace FlowerShopDatabaseImplement
{ {
if (optionsBuilder.IsConfigured == false) 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); base.OnConfiguring(optionsBuilder);
} }
@ -22,5 +22,7 @@ namespace FlowerShopDatabaseImplement
public virtual DbSet<Flower> Flowers { set; get; } public virtual DbSet<Flower> Flowers { set; get; }
public virtual DbSet<FlowerComponent> FlowerComponents { set; get; } public virtual DbSet<FlowerComponent> FlowerComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; } public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Shop> Shops { set; get; }
public virtual DbSet<ShopFlower> ShopFlowers { set; get; }
} }
} }

View File

@ -12,7 +12,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace FlowerShopDatabaseImplement.Migrations namespace FlowerShopDatabaseImplement.Migrations
{ {
[DbContext(typeof(FlowerShopDataBase))] [DbContext(typeof(FlowerShopDataBase))]
[Migration("20240312171524_InitialCreate")] [Migration("20240324171319_InitialCreate")]
partial class InitialCreate partial class InitialCreate
{ {
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -90,7 +90,7 @@ namespace FlowerShopDatabaseImplement.Migrations
b.ToTable("FlowerComponents"); b.ToTable("FlowerComponents");
}); });
modelBuilder.Entity("FlowerShopDatabaseImplement.Order", b => modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Order", b =>
{ {
b.Property<int>("Id") b.Property<int>("Id")
.ValueGeneratedOnAdd() .ValueGeneratedOnAdd()
@ -123,6 +123,59 @@ namespace FlowerShopDatabaseImplement.Migrations
b.ToTable("Orders"); b.ToTable("Orders");
}); });
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<string>("Address")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("DateOpen")
.HasColumnType("datetime2");
b.Property<int>("MaxCapacity")
.HasColumnType("int");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.ShopFlower", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("FlowerId")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("FlowerId");
b.HasIndex("ShopId");
b.ToTable("ShopFlowers");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.FlowerComponent", b => modelBuilder.Entity("FlowerShopDatabaseImplement.Models.FlowerComponent", b =>
{ {
b.HasOne("FlowerShopDatabaseImplement.Models.Component", "Component") b.HasOne("FlowerShopDatabaseImplement.Models.Component", "Component")
@ -142,13 +195,34 @@ namespace FlowerShopDatabaseImplement.Migrations
b.Navigation("Flower"); 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") .WithMany("Orders")
.HasForeignKey("FlowerId") .HasForeignKey("FlowerId")
.OnDelete(DeleteBehavior.Cascade) .OnDelete(DeleteBehavior.Cascade)
.IsRequired(); .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 => modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Component", b =>
@ -161,6 +235,13 @@ namespace FlowerShopDatabaseImplement.Migrations
b.Navigation("Components"); b.Navigation("Components");
b.Navigation("Orders"); b.Navigation("Orders");
b.Navigation("ShopFlowers");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Shop", b =>
{
b.Navigation("Flowers");
}); });
#pragma warning restore 612, 618 #pragma warning restore 612, 618
} }

View File

@ -37,6 +37,22 @@ namespace FlowerShopDatabaseImplement.Migrations
table.PrimaryKey("PK_Flowers", x => x.Id); table.PrimaryKey("PK_Flowers", x => x.Id);
}); });
migrationBuilder.CreateTable(
name: "Shops",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ShopName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Address = table.Column<string>(type: "nvarchar(max)", nullable: false),
DateOpen = table.Column<DateTime>(type: "datetime2", nullable: false),
MaxCapacity = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Shops", x => x.Id);
});
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "FlowerComponents", name: "FlowerComponents",
columns: table => new columns: table => new
@ -88,6 +104,33 @@ namespace FlowerShopDatabaseImplement.Migrations
onDelete: ReferentialAction.Cascade); onDelete: ReferentialAction.Cascade);
}); });
migrationBuilder.CreateTable(
name: "ShopFlowers",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
FlowerId = table.Column<int>(type: "int", nullable: false),
ShopId = table.Column<int>(type: "int", nullable: false),
Count = table.Column<int>(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( migrationBuilder.CreateIndex(
name: "IX_FlowerComponents_ComponentId", name: "IX_FlowerComponents_ComponentId",
table: "FlowerComponents", table: "FlowerComponents",
@ -102,6 +145,16 @@ namespace FlowerShopDatabaseImplement.Migrations
name: "IX_Orders_FlowerId", name: "IX_Orders_FlowerId",
table: "Orders", table: "Orders",
column: "FlowerId"); 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) protected override void Down(MigrationBuilder migrationBuilder)
@ -112,11 +165,17 @@ namespace FlowerShopDatabaseImplement.Migrations
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "Orders"); name: "Orders");
migrationBuilder.DropTable(
name: "ShopFlowers");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "Components"); name: "Components");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "Flowers"); name: "Flowers");
migrationBuilder.DropTable(
name: "Shops");
} }
} }
} }

View File

@ -88,7 +88,7 @@ namespace FlowerShopDatabaseImplement.Migrations
b.ToTable("FlowerComponents"); b.ToTable("FlowerComponents");
}); });
modelBuilder.Entity("FlowerShopDatabaseImplement.Order", b => modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Order", b =>
{ {
b.Property<int>("Id") b.Property<int>("Id")
.ValueGeneratedOnAdd() .ValueGeneratedOnAdd()
@ -121,6 +121,59 @@ namespace FlowerShopDatabaseImplement.Migrations
b.ToTable("Orders"); b.ToTable("Orders");
}); });
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<string>("Address")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("DateOpen")
.HasColumnType("datetime2");
b.Property<int>("MaxCapacity")
.HasColumnType("int");
b.Property<string>("ShopName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.ShopFlower", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("FlowerId")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("FlowerId");
b.HasIndex("ShopId");
b.ToTable("ShopFlowers");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.FlowerComponent", b => modelBuilder.Entity("FlowerShopDatabaseImplement.Models.FlowerComponent", b =>
{ {
b.HasOne("FlowerShopDatabaseImplement.Models.Component", "Component") b.HasOne("FlowerShopDatabaseImplement.Models.Component", "Component")
@ -140,13 +193,34 @@ namespace FlowerShopDatabaseImplement.Migrations
b.Navigation("Flower"); 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") .WithMany("Orders")
.HasForeignKey("FlowerId") .HasForeignKey("FlowerId")
.OnDelete(DeleteBehavior.Cascade) .OnDelete(DeleteBehavior.Cascade)
.IsRequired(); .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 => modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Component", b =>
@ -159,6 +233,13 @@ namespace FlowerShopDatabaseImplement.Migrations
b.Navigation("Components"); b.Navigation("Components");
b.Navigation("Orders"); b.Navigation("Orders");
b.Navigation("ShopFlowers");
});
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Shop", b =>
{
b.Navigation("Flowers");
}); });
#pragma warning restore 612, 618 #pragma warning restore 612, 618
} }

View File

@ -18,7 +18,7 @@ namespace FlowerShopDatabaseImplement.Implements
public List<OrderViewModel> GetFullList() public List<OrderViewModel> GetFullList()
{ {
using var context = new FlowerShopDataBase(); using var context = new FlowerShopDataBase();
return context.Orders return context.Orders.Include(x => x.Flower)
.Select(x => AccessFlowerStorage(x.GetViewModel)) .Select(x => AccessFlowerStorage(x.GetViewModel))
.ToList(); .ToList();
} }
@ -29,7 +29,7 @@ namespace FlowerShopDatabaseImplement.Implements
return new(); return new();
} }
using var context = new FlowerShopDataBase(); using var context = new FlowerShopDataBase();
return context.Orders return context.Orders.Include(x => x.Flower)
.Where(x => x.Id == model.Id) .Where(x => x.Id == model.Id)
.Select(x => AccessFlowerStorage(x.GetViewModel)) .Select(x => AccessFlowerStorage(x.GetViewModel))
.ToList(); .ToList();
@ -41,16 +41,16 @@ namespace FlowerShopDatabaseImplement.Implements
return null; return null;
} }
using var context = new FlowerShopDataBase(); 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) public OrderViewModel? Insert(OrderBindingModel model)
{ {
var newOrder = Order.Create(model); using var context = new FlowerShopDataBase();
var newOrder = Order.Create(context, model);
if (newOrder == null) if (newOrder == null)
{ {
return null; return null;
} }
using var context = new FlowerShopDataBase();
context.Orders.Add(newOrder); context.Orders.Add(newOrder);
context.SaveChanges(); context.SaveChanges();
return AccessFlowerStorage(newOrder.GetViewModel); return AccessFlowerStorage(newOrder.GetViewModel);
@ -58,8 +58,7 @@ namespace FlowerShopDatabaseImplement.Implements
public OrderViewModel? Update(OrderBindingModel model) public OrderViewModel? Update(OrderBindingModel model)
{ {
using var context = new FlowerShopDataBase(); using var context = new FlowerShopDataBase();
var order = context.Orders.FirstOrDefault(x => x.Id == var order = context.Orders.Include(x => x.Flower).FirstOrDefault(x => x.Id == model.Id);
model.Id);
if (order == null) if (order == null)
{ {
return null; return null;
@ -71,7 +70,7 @@ namespace FlowerShopDatabaseImplement.Implements
public OrderViewModel? Delete(OrderBindingModel model) public OrderViewModel? Delete(OrderBindingModel model)
{ {
using var context = new FlowerShopDataBase(); 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); model.Id);
if (element != null) if (element != null)
{ {

View File

@ -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<int, (IFlowerModel, int)>? _shopFlowers =
null;
[NotMapped]
public Dictionary<int, (IFlowerModel, int)> 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<ShopFlower> 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;
}
}
}

View File

@ -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();
}
}

View File

@ -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<ShopViewModel> 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<ShopViewModel> 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<ShopFlower> lst = new List<ShopFlower>();
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;
}
}
}