ПИбд-23 Салин Олег Алексеевич Лабораторная работа №4 (Усложненная) #18

Closed
Oleja123 wants to merge 30 commits from Lab4_Hard into Lab4
9 changed files with 520 additions and 6 deletions
Showing only changes of commit 7949f9dbd6 - Show all commits

View File

@ -24,6 +24,8 @@ optionsBuilder)
public virtual DbSet<IceCream> IceCreams { set; get; }
public virtual DbSet<IceCreamComponent> IceCreamComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Shop> Shops { set; get; }
public virtual DbSet<ShopIceCream> ShopIceCreams { set; get; }
}
}

View File

@ -28,7 +28,7 @@ namespace IceCreamShopDatabaseImplement.Implements
return new();
}
using var context = new IceCreamShopDataBase();
return context.Orders
return context.Orders.Include(x => x.IceCream)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
@ -40,7 +40,7 @@ namespace IceCreamShopDatabaseImplement.Implements
return null;
}
using var context = new IceCreamShopDataBase();
return context.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
return context.Orders.Include(x => x.IceCream).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
public OrderViewModel? Insert(OrderBindingModel model)
{
@ -57,7 +57,7 @@ namespace IceCreamShopDatabaseImplement.Implements
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new IceCreamShopDataBase();
var order = context.Orders.FirstOrDefault(x => x.Id ==
var order = context.Orders.Include(x => x.IceCream).FirstOrDefault(x => x.Id ==
model.Id);
if (order == null)
{
@ -70,7 +70,7 @@ namespace IceCreamShopDatabaseImplement.Implements
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new IceCreamShopDataBase();
var element = context.Orders.FirstOrDefault(rec => rec.Id ==
var element = context.Orders.Include(x => x.IceCream).FirstOrDefault(rec => rec.Id ==
model.Id);
if (element != null)
{

View File

@ -0,0 +1,153 @@
using IceCreamShopContracts.BindingModels;
using IceCreamShopContracts.SearchModels;
using IceCreamShopContracts.StoragesContracts;
using IceCreamShopContracts.ViewModels;
using IceCreamShopDatabaseImplement.Models;
using IceCreamShopDataModels.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IceCreamShopDatabaseImplement.Implements
{
public class ShopStorage : IShopStorage
{
public ShopViewModel? Delete(ShopBindingModel model)
{
using var context = new IceCreamShopDataBase();
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 IceCreamShopDataBase();
return context.Shops
.Include(x => x.IceCreams)
.ThenInclude(x => x.IceCream)
.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 IceCreamShopDataBase();
return context.Shops
.Include(x => x.IceCreams)
.ThenInclude(x => x.IceCream)
.Select(x => x.GetViewModel)
.Where(x => x.ShopName.Contains(model.Name ?? string.Empty))
.ToList();
}
public List<ShopViewModel> GetFullList()
{
using var context = new IceCreamShopDataBase();
return context.Shops
.Include(x => x.IceCreams)
.ThenInclude(x => x.IceCream)
.Select(x => x.GetViewModel)
.ToList();
}
public ShopViewModel? Insert(ShopBindingModel model)
{
using var context = new IceCreamShopDataBase();
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 IceCreamShopDataBase();
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.UpdateIceCreams(context, model);
context.SaveChanges();
return shop.GetViewModel;
}
catch
{
throw;
}
}
public bool SellIceCreams(IIceCreamModel model, int count)
{
if (model == null)
return false;
using var context = new IceCreamShopDataBase();
using var transaction = context.Database.BeginTransaction();
List<ShopIceCream> lst = new List<ShopIceCream>();
foreach(var el in context.ShopIceCreams.Where(x => x.IceCreamId == 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.ShopIceCreams.Remove(el);
}
context.SaveChanges();
transaction.Commit();
return true;
}
}
}

View File

@ -12,7 +12,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace IceCreamShopDatabaseImplement.Migrations
{
[DbContext(typeof(IceCreamShopDataBase))]
[Migration("20240318125446_InitialCreate")]
[Migration("20240318151414_InitialCreate")]
partial class InitialCreate
{
/// <inheritdoc />
@ -124,6 +124,59 @@ namespace IceCreamShopDatabaseImplement.Migrations
b.ToTable("Orders");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
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("IceCreamShopDatabaseImplement.Models.ShopIceCream", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("IceCreamId")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("IceCreamId");
b.HasIndex("ShopId");
b.ToTable("ShopIceCreams");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b =>
{
b.HasOne("IceCreamShopDatabaseImplement.Models.Component", "Component")
@ -154,6 +207,25 @@ namespace IceCreamShopDatabaseImplement.Migrations
b.Navigation("IceCream");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.ShopIceCream", b =>
{
b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream")
.WithMany("ShopIceCreams")
.HasForeignKey("IceCreamId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("IceCreamShopDatabaseImplement.Models.Shop", "Shop")
.WithMany("IceCreams")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("IceCream");
b.Navigation("Shop");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b =>
{
b.Navigation("IceCreamComponents");
@ -164,6 +236,13 @@ namespace IceCreamShopDatabaseImplement.Migrations
b.Navigation("Components");
b.Navigation("Orders");
b.Navigation("ShopIceCreams");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Shop", b =>
{
b.Navigation("IceCreams");
});
#pragma warning restore 612, 618
}

View File

@ -39,6 +39,22 @@ namespace IceCreamShopDatabaseImplement.Migrations
table.PrimaryKey("PK_IceCreams", 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: "IceCreamComponents",
columns: table => new
@ -90,6 +106,33 @@ namespace IceCreamShopDatabaseImplement.Migrations
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "ShopIceCreams",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
IceCreamId = 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_ShopIceCreams", x => x.Id);
table.ForeignKey(
name: "FK_ShopIceCreams_IceCreams_IceCreamId",
column: x => x.IceCreamId,
principalTable: "IceCreams",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ShopIceCreams_Shops_ShopId",
column: x => x.ShopId,
principalTable: "Shops",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_IceCreamComponents_ComponentId",
table: "IceCreamComponents",
@ -104,6 +147,16 @@ namespace IceCreamShopDatabaseImplement.Migrations
name: "IX_Orders_IceCreamId",
table: "Orders",
column: "IceCreamId");
migrationBuilder.CreateIndex(
name: "IX_ShopIceCreams_IceCreamId",
table: "ShopIceCreams",
column: "IceCreamId");
migrationBuilder.CreateIndex(
name: "IX_ShopIceCreams_ShopId",
table: "ShopIceCreams",
column: "ShopId");
}
/// <inheritdoc />
@ -115,11 +168,17 @@ namespace IceCreamShopDatabaseImplement.Migrations
migrationBuilder.DropTable(
name: "Orders");
migrationBuilder.DropTable(
name: "ShopIceCreams");
migrationBuilder.DropTable(
name: "Components");
migrationBuilder.DropTable(
name: "IceCreams");
migrationBuilder.DropTable(
name: "Shops");
}
}
}

View File

@ -121,6 +121,59 @@ namespace IceCreamShopDatabaseImplement.Migrations
b.ToTable("Orders");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
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("IceCreamShopDatabaseImplement.Models.ShopIceCream", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("IceCreamId")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("IceCreamId");
b.HasIndex("ShopId");
b.ToTable("ShopIceCreams");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b =>
{
b.HasOne("IceCreamShopDatabaseImplement.Models.Component", "Component")
@ -151,6 +204,25 @@ namespace IceCreamShopDatabaseImplement.Migrations
b.Navigation("IceCream");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.ShopIceCream", b =>
{
b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream")
.WithMany("ShopIceCreams")
.HasForeignKey("IceCreamId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("IceCreamShopDatabaseImplement.Models.Shop", "Shop")
.WithMany("IceCreams")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("IceCream");
b.Navigation("Shop");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b =>
{
b.Navigation("IceCreamComponents");
@ -161,6 +233,13 @@ namespace IceCreamShopDatabaseImplement.Migrations
b.Navigation("Components");
b.Navigation("Orders");
b.Navigation("ShopIceCreams");
});
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Shop", b =>
{
b.Navigation("IceCreams");
});
#pragma warning restore 612, 618
}

View File

@ -38,6 +38,8 @@ namespace IceCreamShopDatabaseImplement.Models
public virtual List<IceCreamComponent> Components { get; set; } = new();
[ForeignKey("IceCreamId")]
public virtual List<Order> Orders { get; set; } = new();
[ForeignKey("IceCreamId")]
public virtual List<ShopIceCream> ShopIceCreams { get; set; } = new();
public static IceCream Create(IceCreamShopDataBase context,
IceCreamBindingModel model)
{
@ -71,7 +73,7 @@ namespace IceCreamShopDatabaseImplement.Models
{
var iceCreamComponents = context.IceCreamComponents.Where(rec =>
rec.IceCreamId == model.Id).ToList();
if (iceCreamComponents != null && IceCreamComponents.Count > 0)
if (iceCreamComponents != null && iceCreamComponents.Count > 0)
{
context.IceCreamComponents.RemoveRange(iceCreamComponents.Where(rec
=> !model.IceCreamComponents.ContainsKey(rec.ComponentId)));

View File

@ -0,0 +1,118 @@
using IceCreamShopContracts.BindingModels;
using IceCreamShopContracts.ViewModels;
using IceCreamShopDataModels.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 IceCreamShopDatabaseImplement.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, (IIceCreamModel, int)>? _shopIceCreams =
null;
[NotMapped]
public Dictionary<int, (IIceCreamModel, int)> ShopIceCreams
{
get
{
if (_shopIceCreams == null)
{
_shopIceCreams = IceCreams
.ToDictionary(x => x.IceCreamId, x =>
(x.IceCream as IIceCreamModel, x.Count));
}
return _shopIceCreams;
}
}
[ForeignKey("ShopId")]
public virtual List<ShopIceCream> IceCreams { get; set; } = new();
public static Shop? Create(IceCreamShopDataBase 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,
IceCreams = model.ShopIceCreams.Select(x => new
ShopIceCream
{
IceCream = context.IceCreams.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,
ShopIceCreams = ShopIceCreams,
MaxCapacity = MaxCapacity
};
public void UpdateIceCreams(IceCreamShopDataBase context,
ShopBindingModel model)
{
var shopIceCreams = context.ShopIceCreams.Where(rec =>
rec.ShopId == model.Id).ToList();
if (shopIceCreams != null && shopIceCreams.Count > 0)
{
context.ShopIceCreams.RemoveRange(shopIceCreams.Where(rec
=> !model.ShopIceCreams.ContainsKey(rec.ShopId)));
context.SaveChanges();
foreach (var updateIceCream in shopIceCreams)
{
updateIceCream.Count =
model.ShopIceCreams[updateIceCream.IceCreamId].Item2;
model.ShopIceCreams.Remove(updateIceCream.IceCreamId);
}
context.SaveChanges();
}
var shop = context.Shops.First(x => x.Id == Id);
foreach (var pc in model.ShopIceCreams)
{
context.ShopIceCreams.Add(new ShopIceCream
{
Shop = shop,
IceCream = context.IceCreams.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_shopIceCreams = 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 IceCreamShopDatabaseImplement.Models
{
public class ShopIceCream
{
public int Id { get; set; }
[Required]
public int IceCreamId { get; set; }
[Required]
public int ShopId { get; set; }
[Required]
public int Count { get; set; }
public virtual Shop Shop { get; set; } = new();
public virtual IceCream IceCream { get; set; } = new();
}
}