lab3 hard
This commit is contained in:
parent
4490bc9611
commit
ca2fcea02d
@ -116,7 +116,7 @@ namespace JewelryStore
|
||||
DataGridView.Rows.Clear();
|
||||
foreach (var elem in _listStores)
|
||||
{
|
||||
DataGridView.Rows.Add(new object[] { elem.Key, elem.Value.Item1.JewelName, elem.Value.Item2 });
|
||||
DataGridView.Rows.Add(new object[] { elem.Value.Item1.JewelName, elem.Value.Item1.Price, elem.Value.Item2 });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
168
JewelryStoreDatabaseImplement/Implements/StoreStorage.cs
Normal file
168
JewelryStoreDatabaseImplement/Implements/StoreStorage.cs
Normal file
@ -0,0 +1,168 @@
|
||||
using JewelryStoreContracts.BindingModels;
|
||||
using JewelryStoreContracts.SearchModels;
|
||||
using JewelryStoreContracts.StoragesContracts;
|
||||
using JewelryStoreContracts.ViewModels;
|
||||
using JewelryStoreDatabaseImplement.Models;
|
||||
using JewelryStoreDataModels.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace JewelryStoreDatabaseImplement.Implements
|
||||
{
|
||||
public class StoreStorage : IStoreStorage
|
||||
{
|
||||
public StoreViewModel? Delete(StoreBindingModel model)
|
||||
{
|
||||
using var context = new JewelryStoreDataBase();
|
||||
|
||||
var element = context.Stores
|
||||
.Include(x => x.Jewels)
|
||||
.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
|
||||
if (element != null)
|
||||
{
|
||||
context.Stores.Remove(element);
|
||||
context.SaveChanges();
|
||||
|
||||
return element.GetViewModel;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public StoreViewModel? GetElement(StoreSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.StoreName) && !model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
using var context = new JewelryStoreDataBase();
|
||||
|
||||
return context.Stores
|
||||
.Include(x => x.Jewels)
|
||||
.ThenInclude(x => x.Jewel)
|
||||
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.StoreName) && x.StoreName == model.StoreName) || (model.Id.HasValue && x.Id == model.Id))
|
||||
?.GetViewModel;
|
||||
}
|
||||
|
||||
public List<StoreViewModel> GetFilteredList(StoreSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.StoreName))
|
||||
{
|
||||
return new();
|
||||
}
|
||||
|
||||
using var context = new JewelryStoreDataBase();
|
||||
|
||||
return context.Stores
|
||||
.Include(x => x.Jewels)
|
||||
.ThenInclude(x => x.Jewel)
|
||||
.Where(x => x.StoreName.Contains(model.StoreName))
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<StoreViewModel> GetFullList()
|
||||
{
|
||||
using var context = new JewelryStoreDataBase();
|
||||
return context.Stores
|
||||
.Include(x => x.Jewels)
|
||||
.ThenInclude(x => x.Jewel)
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public StoreViewModel? Insert(StoreBindingModel model)
|
||||
{
|
||||
using var context = new JewelryStoreDataBase();
|
||||
var newStore = Store.Create(context, model);
|
||||
if (newStore == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
context.Stores.Add(newStore);
|
||||
context.SaveChanges();
|
||||
return newStore.GetViewModel;
|
||||
}
|
||||
|
||||
public bool SellJewel(IJewelModel model, int quantity)
|
||||
{
|
||||
using var context = new JewelryStoreDataBase();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
foreach (var store in context.Stores
|
||||
.Include(x => x.Jewels)
|
||||
.ThenInclude(x => x.Jewel)
|
||||
.ToList()
|
||||
.Where(x => x.StoreJewels.ContainsKey(model.Id)))
|
||||
{
|
||||
int countInCurrentStore = store.StoreJewels[model.Id].Item2;
|
||||
if (countInCurrentStore <= quantity)
|
||||
{
|
||||
var elem = context.StoreJewels
|
||||
.Where(x => x.JewelId == model.Id)
|
||||
.FirstOrDefault(x => x.StoreId == store.Id);
|
||||
context.StoreJewels.Remove(elem);
|
||||
store.StoreJewels.Remove(model.Id);
|
||||
quantity -= countInCurrentStore;
|
||||
}
|
||||
else
|
||||
{
|
||||
store.StoreJewels[model.Id] = (store.StoreJewels[model.Id].Item1, countInCurrentStore - quantity);
|
||||
quantity = 0;
|
||||
store.UpdateJewel(context, new()
|
||||
{
|
||||
Id = store.Id,
|
||||
StoreJewels = store.StoreJewels,
|
||||
});
|
||||
}
|
||||
if (quantity == 0)
|
||||
{
|
||||
context.SaveChanges();
|
||||
transaction.Commit();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
transaction.Rollback();
|
||||
return false;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public StoreViewModel? Update(StoreBindingModel model)
|
||||
{
|
||||
using var context = new JewelryStoreDataBase();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var store = context.Stores.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (store == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
store.Update(model);
|
||||
context.SaveChanges();
|
||||
store.UpdateJewel(context, model);
|
||||
transaction.Commit();
|
||||
return store.GetViewModel;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -5,7 +5,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using JewelryStoreDatabaseImplement.Models;
|
||||
|
||||
using JewelryStoreDatabaseImplement.Implements;
|
||||
|
||||
namespace JewelryStoreDatabaseImplement
|
||||
{
|
||||
@ -25,6 +25,8 @@ namespace JewelryStoreDatabaseImplement
|
||||
public virtual DbSet<Jewel> Jewels { set; get; }
|
||||
public virtual DbSet<JewelComponent> JewelComponents { set; get; }
|
||||
public virtual DbSet<Order> Orders { set; get; }
|
||||
}
|
||||
public virtual DbSet<Store> Stores { set; get; }
|
||||
public virtual DbSet<StoreJewel> StoreJewels { set; get; }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,175 +0,0 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using JewelryStoreDatabaseImplement;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace JewelryStoreDatabaseImplement.Migrations
|
||||
{
|
||||
[DbContext(typeof(JewelryStoreDataBase))]
|
||||
[Migration("20230305150722_InitialCreate_1")]
|
||||
partial class InitialCreate_1
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "7.0.3")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 128);
|
||||
|
||||
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("JewelryStoreDatabaseImplement.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("JewelryStoreDatabaseImplement.Models.Jewel", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<string>("JewelName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<double>("Price")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Jewels");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.JewelComponent", 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>("JewelId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ComponentId");
|
||||
|
||||
b.HasIndex("JewelId");
|
||||
|
||||
b.ToTable("JewelComponents");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("JewelryStoreDatabaseImplement.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>("JewelId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<string>("JewelName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<int>("Status")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<double>("Sum")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("JewelId");
|
||||
|
||||
b.ToTable("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.JewelComponent", b =>
|
||||
{
|
||||
b.HasOne("JewelryStoreDatabaseImplement.Models.Component", "Component")
|
||||
.WithMany("JewelComponents")
|
||||
.HasForeignKey("ComponentId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("JewelryStoreDatabaseImplement.Models.Jewel", "Jewel")
|
||||
.WithMany("Components")
|
||||
.HasForeignKey("JewelId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Component");
|
||||
|
||||
b.Navigation("Jewel");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Order", b =>
|
||||
{
|
||||
b.HasOne("JewelryStoreDatabaseImplement.Models.Jewel", "Jewel")
|
||||
.WithMany("Orders")
|
||||
.HasForeignKey("JewelId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Jewel");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Component", b =>
|
||||
{
|
||||
b.Navigation("JewelComponents");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Jewel", b =>
|
||||
{
|
||||
b.Navigation("Components");
|
||||
|
||||
b.Navigation("Orders");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace JewelryStoreDatabaseImplement.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class InitialCreate_1 : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@ -12,8 +12,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
namespace JewelryStoreDatabaseImplement.Migrations
|
||||
{
|
||||
[DbContext(typeof(JewelryStoreDataBase))]
|
||||
[Migration("20230305150151_InitialCreate")]
|
||||
partial class InitialCreate
|
||||
[Migration("20230616194257_init")]
|
||||
partial class init
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
@ -128,6 +128,59 @@ namespace JewelryStoreDatabaseImplement.Migrations
|
||||
b.ToTable("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Store", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<int>("JewelMaxCount")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<DateTime>("OpeningDate")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<string>("StoreAdress")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<string>("StoreName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Stores");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.StoreJewel", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("JewelId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("StoreId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("JewelId");
|
||||
|
||||
b.HasIndex("StoreId");
|
||||
|
||||
b.ToTable("StoreJewels");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.JewelComponent", b =>
|
||||
{
|
||||
b.HasOne("JewelryStoreDatabaseImplement.Models.Component", "Component")
|
||||
@ -158,6 +211,25 @@ namespace JewelryStoreDatabaseImplement.Migrations
|
||||
b.Navigation("Jewel");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.StoreJewel", b =>
|
||||
{
|
||||
b.HasOne("JewelryStoreDatabaseImplement.Models.Jewel", "Jewel")
|
||||
.WithMany()
|
||||
.HasForeignKey("JewelId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("JewelryStoreDatabaseImplement.Models.Store", "Store")
|
||||
.WithMany("Jewels")
|
||||
.HasForeignKey("StoreId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Jewel");
|
||||
|
||||
b.Navigation("Store");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Component", b =>
|
||||
{
|
||||
b.Navigation("JewelComponents");
|
||||
@ -169,6 +241,11 @@ namespace JewelryStoreDatabaseImplement.Migrations
|
||||
|
||||
b.Navigation("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Store", b =>
|
||||
{
|
||||
b.Navigation("Jewels");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
@ -6,7 +6,7 @@ using Microsoft.EntityFrameworkCore.Migrations;
|
||||
namespace JewelryStoreDatabaseImplement.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class InitialCreate : Migration
|
||||
public partial class init : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
@ -39,6 +39,22 @@ namespace JewelryStoreDatabaseImplement.Migrations
|
||||
table.PrimaryKey("PK_Jewels", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "Stores",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(type: "int", nullable: false)
|
||||
.Annotation("SqlServer:Identity", "1, 1"),
|
||||
StoreName = table.Column<string>(type: "nvarchar(max)", nullable: false),
|
||||
StoreAdress = table.Column<string>(type: "nvarchar(max)", nullable: false),
|
||||
OpeningDate = table.Column<DateTime>(type: "datetime2", nullable: false),
|
||||
JewelMaxCount = table.Column<int>(type: "int", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Stores", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "JewelComponents",
|
||||
columns: table => new
|
||||
@ -91,6 +107,33 @@ namespace JewelryStoreDatabaseImplement.Migrations
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "StoreJewels",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(type: "int", nullable: false)
|
||||
.Annotation("SqlServer:Identity", "1, 1"),
|
||||
JewelId = table.Column<int>(type: "int", nullable: false),
|
||||
StoreId = table.Column<int>(type: "int", nullable: false),
|
||||
Count = table.Column<int>(type: "int", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_StoreJewels", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_StoreJewels_Jewels_JewelId",
|
||||
column: x => x.JewelId,
|
||||
principalTable: "Jewels",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "FK_StoreJewels_Stores_StoreId",
|
||||
column: x => x.StoreId,
|
||||
principalTable: "Stores",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_JewelComponents_ComponentId",
|
||||
table: "JewelComponents",
|
||||
@ -105,6 +148,16 @@ namespace JewelryStoreDatabaseImplement.Migrations
|
||||
name: "IX_Orders_JewelId",
|
||||
table: "Orders",
|
||||
column: "JewelId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_StoreJewels_JewelId",
|
||||
table: "StoreJewels",
|
||||
column: "JewelId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_StoreJewels_StoreId",
|
||||
table: "StoreJewels",
|
||||
column: "StoreId");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@ -116,11 +169,17 @@ namespace JewelryStoreDatabaseImplement.Migrations
|
||||
migrationBuilder.DropTable(
|
||||
name: "Orders");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "StoreJewels");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Components");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Jewels");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Stores");
|
||||
}
|
||||
}
|
||||
}
|
@ -125,6 +125,59 @@ namespace JewelryStoreDatabaseImplement.Migrations
|
||||
b.ToTable("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Store", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<int>("JewelMaxCount")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<DateTime>("OpeningDate")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<string>("StoreAdress")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<string>("StoreName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Stores");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.StoreJewel", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("JewelId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("StoreId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("JewelId");
|
||||
|
||||
b.HasIndex("StoreId");
|
||||
|
||||
b.ToTable("StoreJewels");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.JewelComponent", b =>
|
||||
{
|
||||
b.HasOne("JewelryStoreDatabaseImplement.Models.Component", "Component")
|
||||
@ -155,6 +208,25 @@ namespace JewelryStoreDatabaseImplement.Migrations
|
||||
b.Navigation("Jewel");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.StoreJewel", b =>
|
||||
{
|
||||
b.HasOne("JewelryStoreDatabaseImplement.Models.Jewel", "Jewel")
|
||||
.WithMany()
|
||||
.HasForeignKey("JewelId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("JewelryStoreDatabaseImplement.Models.Store", "Store")
|
||||
.WithMany("Jewels")
|
||||
.HasForeignKey("StoreId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Jewel");
|
||||
|
||||
b.Navigation("Store");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Component", b =>
|
||||
{
|
||||
b.Navigation("JewelComponents");
|
||||
@ -166,6 +238,11 @@ namespace JewelryStoreDatabaseImplement.Migrations
|
||||
|
||||
b.Navigation("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("JewelryStoreDatabaseImplement.Models.Store", b =>
|
||||
{
|
||||
b.Navigation("Jewels");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
|
110
JewelryStoreDatabaseImplement/Models/Store.cs
Normal file
110
JewelryStoreDatabaseImplement/Models/Store.cs
Normal file
@ -0,0 +1,110 @@
|
||||
using JewelryStoreContracts.BindingModels;
|
||||
using JewelryStoreContracts.ViewModels;
|
||||
using JewelryStoreDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace JewelryStoreDatabaseImplement.Models
|
||||
{
|
||||
public class Store : IStoreModel
|
||||
{
|
||||
[Required]
|
||||
public string StoreName { get; private set; } = string.Empty;
|
||||
[Required]
|
||||
public string StoreAdress { get; private set; } = string.Empty;
|
||||
[Required]
|
||||
public DateTime OpeningDate { get; private set; }
|
||||
[Required]
|
||||
public int JewelMaxCount { get; private set; }
|
||||
|
||||
public int Id { get; private set; }
|
||||
|
||||
|
||||
private Dictionary<int, (IJewelModel, int)> _storeJewels = null;
|
||||
[NotMapped]
|
||||
public Dictionary<int, (IJewelModel, int)> StoreJewels
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_storeJewels == null)
|
||||
{
|
||||
_storeJewels = Jewels
|
||||
.ToDictionary(recPC => recPC.JewelId, recPC => (recPC.Jewel as IJewelModel, recPC.Count));
|
||||
}
|
||||
return _storeJewels;
|
||||
}
|
||||
}
|
||||
|
||||
[ForeignKey("StoreId")]
|
||||
public virtual List<StoreJewel> Jewels { get; set; } = new();
|
||||
|
||||
public static Store Create(JewelryStoreDataBase context, StoreBindingModel model)
|
||||
{
|
||||
return new Store()
|
||||
{
|
||||
Id = model.Id,
|
||||
StoreName = model.StoreName,
|
||||
StoreAdress = model.StoreAdress,
|
||||
OpeningDate = model.OpeningDate,
|
||||
JewelMaxCount = model.JewelMaxCount,
|
||||
Jewels = model.StoreJewels.Select(x => new StoreJewel
|
||||
{
|
||||
Jewel = context.Jewels.First(y => y.Id == x.Key),
|
||||
Count = x.Value.Item2
|
||||
}).ToList()
|
||||
};
|
||||
}
|
||||
|
||||
public void Update(StoreBindingModel model)
|
||||
{
|
||||
StoreName = model.StoreName;
|
||||
StoreAdress = model.StoreAdress;
|
||||
OpeningDate = model.OpeningDate;
|
||||
JewelMaxCount = model.JewelMaxCount;
|
||||
}
|
||||
|
||||
public StoreViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
StoreName = StoreName,
|
||||
StoreAdress = StoreAdress,
|
||||
OpeningDate = OpeningDate,
|
||||
JewelMaxCount = JewelMaxCount,
|
||||
StoreJewels = StoreJewels
|
||||
};
|
||||
|
||||
public void UpdateJewel(JewelryStoreDataBase context, StoreBindingModel model)
|
||||
{
|
||||
var storeJewels = context.StoreJewels.Where(rec => rec.StoreId == model.Id).ToList();
|
||||
if (storeJewels != null && storeJewels.Count > 0)
|
||||
{ // удалили те, которых нет в модели
|
||||
context.StoreJewels.RemoveRange(storeJewels.Where(rec => !model.StoreJewels.ContainsKey(rec.JewelId)));
|
||||
context.SaveChanges();
|
||||
// обновили количество у существующих записей
|
||||
foreach (var updateJewel in storeJewels)
|
||||
{
|
||||
updateJewel.Count = model.StoreJewels[updateJewel.JewelId].Item2;
|
||||
model.StoreJewels.Remove(updateJewel.JewelId);
|
||||
}
|
||||
context.SaveChanges();
|
||||
}
|
||||
var store = context.Stores.First(x => x.Id == Id);
|
||||
foreach (var sp in model.StoreJewels)
|
||||
{
|
||||
context.StoreJewels.Add(new StoreJewel
|
||||
{
|
||||
Store = store,
|
||||
Jewel = context.Jewels.First(x => x.Id == sp.Key),
|
||||
Count = sp.Value.Item2
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_storeJewels = null;
|
||||
}
|
||||
}
|
||||
}
|
27
JewelryStoreDatabaseImplement/Models/StoreJewel.cs
Normal file
27
JewelryStoreDatabaseImplement/Models/StoreJewel.cs
Normal file
@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace JewelryStoreDatabaseImplement.Models
|
||||
{
|
||||
public class StoreJewel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public int JewelId { get; set; }
|
||||
|
||||
[Required]
|
||||
public int StoreId { get; set; }
|
||||
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
|
||||
public virtual Store Store { get; set; } = new();
|
||||
|
||||
public virtual Jewel Jewel { get; set; } = new();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user