всё
This commit is contained in:
parent
8de0355107
commit
380740c36a
@ -39,6 +39,8 @@ namespace FlowerShopDatabaseImplement.Models
|
||||
public virtual List<FlowerComponent> Components { get; set; } = new();
|
||||
[ForeignKey("FlowerId")]
|
||||
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)
|
||||
{
|
||||
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();
|
||||
|
@ -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<Flower> Flowers { set; get; }
|
||||
public virtual DbSet<FlowerComponent> FlowerComponents { set; get; }
|
||||
public virtual DbSet<Order> Orders { set; get; }
|
||||
public virtual DbSet<Shop> Shops { set; get; }
|
||||
public virtual DbSet<ShopFlower> ShopFlowers { set; get; }
|
||||
}
|
||||
}
|
||||
|
@ -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<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
@ -123,6 +123,59 @@ namespace FlowerShopDatabaseImplement.Migrations
|
||||
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 =>
|
||||
{
|
||||
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
|
||||
}
|
@ -37,6 +37,22 @@ namespace FlowerShopDatabaseImplement.Migrations
|
||||
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(
|
||||
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<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(
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
@ -88,7 +88,7 @@ namespace FlowerShopDatabaseImplement.Migrations
|
||||
b.ToTable("FlowerComponents");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("FlowerShopDatabaseImplement.Order", b =>
|
||||
modelBuilder.Entity("FlowerShopDatabaseImplement.Models.Order", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
@ -121,6 +121,59 @@ namespace FlowerShopDatabaseImplement.Migrations
|
||||
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 =>
|
||||
{
|
||||
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
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ namespace FlowerShopDatabaseImplement.Implements
|
||||
public List<OrderViewModel> 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)
|
||||
{
|
||||
|
116
FlowerShopDatabaseImplement/Shop.cs
Normal file
116
FlowerShopDatabaseImplement/Shop.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
22
FlowerShopDatabaseImplement/ShopFlower.cs
Normal file
22
FlowerShopDatabaseImplement/ShopFlower.cs
Normal 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();
|
||||
}
|
||||
}
|
153
FlowerShopDatabaseImplement/ShopStorage.cs
Normal file
153
FlowerShopDatabaseImplement/ShopStorage.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user