Готовая 3 лабораторная (Усложненная)
This commit is contained in:
parent
e4d314dd00
commit
a81651f025
@ -21,5 +21,9 @@ namespace IceCreamShopDatabaseImplement
|
||||
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; }
|
||||
}
|
||||
}
|
@ -0,0 +1,146 @@
|
||||
using IceCreamShopContracts.BindingModels;
|
||||
using IceCreamShopContracts.SearchModels;
|
||||
using IceCreamShopContracts.StoragesContracts;
|
||||
using IceCreamShopContracts.ViewModels;
|
||||
using IceCreamShopDatabaseImplement.Models;
|
||||
using IceCreamShopDataModels.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace IceCreamShopDatabaseImplement.Implements
|
||||
{
|
||||
public class ShopStorage : IShopStorage
|
||||
{
|
||||
public List<ShopViewModel> GetFullList()
|
||||
{
|
||||
using var context = new IceCreamShopDatabase();
|
||||
return context.Shops
|
||||
.Include(x => x.IceCreams)
|
||||
.ThenInclude(x => x.IceCream)
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.ShopName))
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new IceCreamShopDatabase();
|
||||
return context.Shops
|
||||
.Include(x => x.IceCreams)
|
||||
.ThenInclude(x => x.IceCream)
|
||||
.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 IceCreamShopDatabase();
|
||||
return context.Shops
|
||||
.Include(x => x.IceCreams)
|
||||
.ThenInclude(x => x.IceCream)
|
||||
.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 IceCreamShopDatabase();
|
||||
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 IceCreamShopDatabase();
|
||||
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.UpdateIceCreams(context, model);
|
||||
transaction.Commit();
|
||||
return shop.GetViewModel;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public ShopViewModel? Delete(ShopBindingModel model)
|
||||
{
|
||||
using var context = new IceCreamShopDatabase();
|
||||
var element = context.Shops
|
||||
.Include(x => x.IceCreams)
|
||||
.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.Shops.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool MakeSale(IIceCreamModel model, int count)
|
||||
{
|
||||
using var context = new IceCreamShopDatabase();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
foreach (var shop in context.Shops.Include(x => x.IceCreams).ThenInclude(x => x.IceCream)
|
||||
.Where(x => x.IceCreams.Any(x => x.IceCreamId == model.Id))
|
||||
.ToList())
|
||||
{
|
||||
var iceCream = shop.ShopIceCreams[model.Id];
|
||||
int min = Math.Min(iceCream.Item2, count);
|
||||
if (min == iceCream.Item2)
|
||||
{
|
||||
shop.ShopIceCreams.Remove(model.Id);
|
||||
}
|
||||
else
|
||||
{
|
||||
shop.ShopIceCreams[model.Id] = (iceCream.Item1, iceCream.Item2 - min);
|
||||
}
|
||||
shop.UpdateIceCreams(context, new() { Id = shop.Id, ShopIceCreams = shop.ShopIceCreams });
|
||||
count -= min;
|
||||
if (count == 0)
|
||||
{
|
||||
context.SaveChanges();
|
||||
transaction.Commit();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
transaction.Rollback();
|
||||
return false;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
250
IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240408110201_ShopAddition.Designer.cs
generated
Normal file
250
IceCreamShop/IceCreamShopDatabaseImplement/Migrations/20240408110201_ShopAddition.Designer.cs
generated
Normal file
@ -0,0 +1,250 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using IceCreamShopDatabaseImplement;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace IceCreamShopDatabaseImplement.Migrations
|
||||
{
|
||||
[DbContext(typeof(IceCreamShopDatabase))]
|
||||
[Migration("20240408110201_ShopAddition")]
|
||||
partial class ShopAddition
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "7.0.16")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 128);
|
||||
|
||||
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Component", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<string>("ComponentName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<double>("Cost")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Components");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCream", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<string>("IceCreamName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<double>("Price")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("IceCreams");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCreamComponent", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<int>("ComponentId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("IceCreamId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ComponentId");
|
||||
|
||||
b.HasIndex("IceCreamId");
|
||||
|
||||
b.ToTable("IceCreamComponents");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<DateTime>("DateCreate")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<DateTime?>("DateImplement")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<int>("IceCreamId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("Status")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<double>("Sum")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("IceCreamId");
|
||||
|
||||
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>("DateOpening")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<int>("IceCreamsMaximum")
|
||||
.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")
|
||||
.WithMany("IceCreamComponents")
|
||||
.HasForeignKey("ComponentId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream")
|
||||
.WithMany("Components")
|
||||
.HasForeignKey("IceCreamId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Component");
|
||||
|
||||
b.Navigation("IceCream");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Order", b =>
|
||||
{
|
||||
b.HasOne("IceCreamShopDatabaseImplement.Models.IceCream", "IceCream")
|
||||
.WithMany("Orders")
|
||||
.HasForeignKey("IceCreamId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
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");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.IceCream", b =>
|
||||
{
|
||||
b.Navigation("Components");
|
||||
|
||||
b.Navigation("Orders");
|
||||
|
||||
b.Navigation("ShopIceCreams");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("IceCreamShopDatabaseImplement.Models.Shop", b =>
|
||||
{
|
||||
b.Navigation("IceCreams");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace IceCreamShopDatabaseImplement.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class ShopAddition : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
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),
|
||||
DateOpening = table.Column<DateTime>(type: "datetime2", nullable: false),
|
||||
IceCreamsMaximum = table.Column<int>(type: "int", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Shops", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "ShopIceCreams",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(type: "int", nullable: false)
|
||||
.Annotation("SqlServer:Identity", "1, 1"),
|
||||
ShopId = table.Column<int>(type: "int", nullable: false),
|
||||
IceCreamId = 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_ShopIceCreams_IceCreamId",
|
||||
table: "ShopIceCreams",
|
||||
column: "IceCreamId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_ShopIceCreams_ShopId",
|
||||
table: "ShopIceCreams",
|
||||
column: "ShopId");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "ShopIceCreams");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Shops");
|
||||
}
|
||||
}
|
||||
}
|
@ -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>("DateOpening")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<int>("IceCreamsMaximum")
|
||||
.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
|
||||
}
|
||||
|
@ -38,6 +38,9 @@ namespace IceCreamShopDatabaseImplement.Models
|
||||
[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)
|
||||
{
|
||||
return new IceCream()
|
||||
|
110
IceCreamShop/IceCreamShopDatabaseImplement/Models/Shop.cs
Normal file
110
IceCreamShop/IceCreamShopDatabaseImplement/Models/Shop.cs
Normal file
@ -0,0 +1,110 @@
|
||||
using IceCreamShopContracts.BindingModels;
|
||||
using IceCreamShopContracts.ViewModels;
|
||||
using IceCreamShopDataModels.Models;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace IceCreamShopDatabaseImplement.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 IceCreamsMaximum { get; 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)
|
||||
{
|
||||
return new Shop()
|
||||
{
|
||||
Id = model.Id,
|
||||
ShopName = model.ShopName,
|
||||
Address = model.Address,
|
||||
DateOpening = model.DateOpening,
|
||||
IceCreamsMaximum = model.IceCreamsMaximum,
|
||||
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)
|
||||
{
|
||||
ShopName = model.ShopName;
|
||||
Address = model.Address;
|
||||
DateOpening = model.DateOpening;
|
||||
IceCreamsMaximum = model.IceCreamsMaximum;
|
||||
}
|
||||
|
||||
public ShopViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
ShopName = ShopName,
|
||||
Address = Address,
|
||||
DateOpening = DateOpening,
|
||||
IceCreamsMaximum = IceCreamsMaximum,
|
||||
ShopIceCreams = ShopIceCreams
|
||||
};
|
||||
|
||||
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.IceCreamId)));
|
||||
context.SaveChanges();
|
||||
foreach (var updateIceCream in shopIceCreams)
|
||||
{
|
||||
if (model.ShopIceCreams.ContainsKey(updateIceCream.IceCreamId))
|
||||
{
|
||||
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 ic in model.ShopIceCreams)
|
||||
{
|
||||
context.ShopIceCreams.Add(new ShopIceCream
|
||||
{
|
||||
Shop = shop,
|
||||
IceCream = context.IceCreams.First(x => x.Id == ic.Key),
|
||||
Count = ic.Value.Item2
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_shopIceCreams = null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace IceCreamShopDatabaseImplement.Models
|
||||
{
|
||||
public class ShopIceCream
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public int ShopId { get; set; }
|
||||
|
||||
[Required]
|
||||
public int IceCreamId { get; set; }
|
||||
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
|
||||
public virtual IceCream IceCream { get; set; } = new();
|
||||
|
||||
public virtual Shop Shop { get; set; } = new();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user