ISEbd-21 Melnikov I. O. Lab Work 03 Advanced #27
@ -44,15 +44,6 @@ namespace BlacksmithWorkShopBusinessLogic.BusinessLogics
|
||||
model.Status = vm?.Status ?? OrderStatus.Неизвестен;
|
||||
if ((int)model.Status == (int)newstatus - 1)
|
||||
{
|
||||
model.Status = newstatus;
|
||||
if (newstatus == OrderStatus.Готов)
|
||||
{
|
||||
model.DateImplement = DateTime.Now;
|
||||
}
|
||||
if (_orderStorage.Update(model) != null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (newstatus == OrderStatus.Выдан)
|
||||
{
|
||||
if (vm == null)
|
||||
@ -68,6 +59,15 @@ namespace BlacksmithWorkShopBusinessLogic.BusinessLogics
|
||||
throw new Exception("Не удалось заполнить магазины");
|
||||
}
|
||||
}
|
||||
model.Status = newstatus;
|
||||
if (newstatus == OrderStatus.Готов)
|
||||
{
|
||||
model.DateImplement = DateTime.Now;
|
||||
}
|
||||
if (_orderStorage.Update(model) != null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
_logger.LogWarning($"Changing order status of order {model.Id} to {newstatus} failed");
|
||||
return false;
|
||||
|
@ -86,6 +86,7 @@ namespace BlacksmithWorkshopBusinessLogic.BusinessLogics
|
||||
}
|
||||
_storeStorage.Update(new StoreBindingModel()
|
||||
{
|
||||
Id = model.Id,
|
||||
StoreName = model.StoreName,
|
||||
Address = model.Address,
|
||||
OpeningDate = model.OpeningDate,
|
||||
|
@ -12,7 +12,7 @@ namespace BlacksmithWorkshopDatabaseImplement
|
||||
optionsBuilder.UseSqlServer
|
||||
(
|
||||
@"Data Source=IGORS_2011;
|
||||
Initial Catalog=BlacksmithWorkshopDatabaseFull;
|
||||
Initial Catalog=BlacksmithWorkshopDatabaseFullAdv;
|
||||
Integrated Security=True;
|
||||
MultipleActiveResultSets=True;
|
||||
TrustServerCertificate=True"
|
||||
@ -24,5 +24,7 @@ namespace BlacksmithWorkshopDatabaseImplement
|
||||
public virtual DbSet<Manufacture> Manufactures { set; get; }
|
||||
public virtual DbSet<ManufactureComponent> ManufactureComponents { set; get; }
|
||||
public virtual DbSet<Order> Orders { set; get; }
|
||||
}
|
||||
public virtual DbSet<Store> Stores { set; get; }
|
||||
public virtual DbSet<ManufactureInStore> ManufacturesInStore { set; get; }
|
||||
}
|
||||
}
|
@ -0,0 +1,153 @@
|
||||
using BlacksmithWorkshopContracts.BindingModels;
|
||||
using BlacksmithWorkshopContracts.SearchModels;
|
||||
using BlacksmithWorkshopContracts.StoragesContracts;
|
||||
using BlacksmithWorkshopContracts.ViewModels;
|
||||
using BlacksmithWorkshopDatabaseImplement;
|
||||
using BlacksmithWorkshopDatabaseImplement.Models;
|
||||
using BlacksmithWorkshopDataModels.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace BlacksmithWorkshopDatabaseImplement.Implements
|
||||
{
|
||||
public class StoreStorage : IStoreStorage
|
||||
{
|
||||
public StoreViewModel? Delete(StoreBindingModel model)
|
||||
{
|
||||
using var context = new BlacksmithWorkshopDatabase();
|
||||
var element = context.Stores.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.Stores.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public StoreViewModel? GetElement(StoreSearchModel model)
|
||||
{
|
||||
if (!model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new BlacksmithWorkshopDatabase();
|
||||
return context.Stores
|
||||
.Include(x => x.ManufacturesInStore)
|
||||
.ThenInclude(x => x.Manufacture)
|
||||
.FirstOrDefault(x => 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 BlacksmithWorkshopDatabase();
|
||||
return context.Stores
|
||||
.Include(x => x.ManufacturesInStore)
|
||||
.ThenInclude(x => x.Manufacture)
|
||||
.Select(x => x.GetViewModel)
|
||||
.Where(x => x.StoreName.Contains(model.StoreName ?? string.Empty))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<StoreViewModel> GetFullList()
|
||||
{
|
||||
using var context = new BlacksmithWorkshopDatabase();
|
||||
return context.Stores
|
||||
.Include(x => x.ManufacturesInStore)
|
||||
.ThenInclude(x => x.Manufacture)
|
||||
.Select(shop => shop.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public StoreViewModel? Insert(StoreBindingModel model)
|
||||
{
|
||||
using var context = new BlacksmithWorkshopDatabase();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var newShop = Store.Create(context, model);
|
||||
if (newShop == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (context.Stores.Any(x => x.StoreName == newShop.StoreName))
|
||||
{
|
||||
throw new Exception("Не должно быть два магазина с одним названием");
|
||||
}
|
||||
context.Stores.Add(newShop);
|
||||
context.SaveChanges();
|
||||
transaction.Commit();
|
||||
return newShop.GetViewModel;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public StoreViewModel? Update(StoreBindingModel model)
|
||||
{
|
||||
using var context = new BlacksmithWorkshopDatabase();
|
||||
var store = context.Stores.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (store == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
store.Update(model);
|
||||
store.UpdateManufactures(context, model);
|
||||
context.SaveChanges();
|
||||
return store.GetViewModel;
|
||||
}
|
||||
|
||||
public bool HasManufactures(IManufactureModel manufacture, int count)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public bool SellManufacture(IManufactureModel manufacture, int count)
|
||||
{
|
||||
using var context = new BlacksmithWorkshopDatabase();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
foreach (var MIS in context.ManufacturesInStore.Where(x => x.ManufactureId == manufacture.Id))
|
||||
{
|
||||
var res = Math.Min(count, MIS.Count);
|
||||
MIS.Count -= res;
|
||||
count -= res;
|
||||
if (MIS.Count == 0)
|
||||
{
|
||||
context.ManufacturesInStore.Remove(MIS);
|
||||
}
|
||||
if (count == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (count == 0)
|
||||
{
|
||||
context.SaveChanges();
|
||||
transaction.Commit();
|
||||
}
|
||||
else
|
||||
{
|
||||
transaction.Rollback();
|
||||
}
|
||||
return count == 0;
|
||||
}
|
||||
|
||||
public int GetPlaceLeft(IStoreModel store)
|
||||
{
|
||||
using var context = new BlacksmithWorkshopDatabase();
|
||||
var selectedStore = context.Stores.FirstOrDefault(x => x.Id == store.Id);
|
||||
if (selectedStore == null)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
return selectedStore.MaxManufactures - selectedStore.Manufactures.Select(x => x.Value.Item2).Sum();
|
||||
}
|
||||
}
|
||||
}
|
@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace BlacksmithWorkshopDatebaseImplement.Migrations
|
||||
namespace BlacksmithWorkshopDatabaseImplement.Migrations
|
||||
{
|
||||
[DbContext(typeof(BlacksmithWorkshopDatabase))]
|
||||
[Migration("20230326142752_InitMigration")]
|
||||
|
@ -3,7 +3,7 @@ using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace BlacksmithWorkshopDatebaseImplement.Migrations
|
||||
namespace BlacksmithWorkshopDatabaseImplement.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class InitMigration : Migration
|
||||
|
@ -0,0 +1,246 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using BlacksmithWorkshopDatabaseImplement;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace BlacksmithWorkshopDatabaseImplement.Migrations
|
||||
{
|
||||
[DbContext(typeof(BlacksmithWorkshopDatabase))]
|
||||
[Migration("20230509130456_Lab3AdvMigration")]
|
||||
partial class Lab3AdvMigration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "7.0.4")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 128);
|
||||
|
||||
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
|
||||
|
||||
modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.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("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<string>("ManufactureName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<double>("Price")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Manufactures");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.ManufactureComponent", 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>("ManufactureId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ComponentId");
|
||||
|
||||
b.HasIndex("ManufactureId");
|
||||
|
||||
b.ToTable("ManufactureComponents");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.ManufactureInStore", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("ManufactureId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("StoreId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ManufactureId");
|
||||
|
||||
b.HasIndex("StoreId");
|
||||
|
||||
b.ToTable("ManufacturesInStore");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.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>("ManufactureId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("Status")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<double>("Sum")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ManufactureId");
|
||||
|
||||
b.ToTable("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Store", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<string>("Address")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<int>("MaxManufactures")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<DateTime>("OpeningDate")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<string>("StoreName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Stores");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.ManufactureComponent", b =>
|
||||
{
|
||||
b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Component", "Component")
|
||||
.WithMany("ManufactureComponents")
|
||||
.HasForeignKey("ComponentId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", "Manufacture")
|
||||
.WithMany("Components")
|
||||
.HasForeignKey("ManufactureId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Component");
|
||||
|
||||
b.Navigation("Manufacture");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.ManufactureInStore", b =>
|
||||
{
|
||||
b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", "Manufacture")
|
||||
.WithMany()
|
||||
.HasForeignKey("ManufactureId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Store", "Store")
|
||||
.WithMany("ManufacturesInStore")
|
||||
.HasForeignKey("StoreId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Manufacture");
|
||||
|
||||
b.Navigation("Store");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Order", b =>
|
||||
{
|
||||
b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", null)
|
||||
.WithMany("Orders")
|
||||
.HasForeignKey("ManufactureId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
});
|
||||
|
||||
modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Component", b =>
|
||||
{
|
||||
b.Navigation("ManufactureComponents");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", b =>
|
||||
{
|
||||
b.Navigation("Components");
|
||||
|
||||
b.Navigation("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Store", b =>
|
||||
{
|
||||
b.Navigation("ManufacturesInStore");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace BlacksmithWorkshopDatabaseImplement.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class Lab3AdvMigration : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
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),
|
||||
Address = table.Column<string>(type: "nvarchar(max)", nullable: false),
|
||||
OpeningDate = table.Column<DateTime>(type: "datetime2", nullable: false),
|
||||
MaxManufactures = table.Column<int>(type: "int", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Stores", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "ManufacturesInStore",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(type: "int", nullable: false)
|
||||
.Annotation("SqlServer:Identity", "1, 1"),
|
||||
ManufactureId = 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_ManufacturesInStore", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_ManufacturesInStore_Manufactures_ManufactureId",
|
||||
column: x => x.ManufactureId,
|
||||
principalTable: "Manufactures",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "FK_ManufacturesInStore_Stores_StoreId",
|
||||
column: x => x.StoreId,
|
||||
principalTable: "Stores",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_ManufacturesInStore_ManufactureId",
|
||||
table: "ManufacturesInStore",
|
||||
column: "ManufactureId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_ManufacturesInStore_StoreId",
|
||||
table: "ManufacturesInStore",
|
||||
column: "StoreId");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "ManufacturesInStore");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Stores");
|
||||
}
|
||||
}
|
||||
}
|
@ -8,7 +8,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace BlacksmithWorkshopDatebaseImplement.Migrations
|
||||
namespace BlacksmithWorkshopDatabaseImplement.Migrations
|
||||
{
|
||||
[DbContext(typeof(BlacksmithWorkshopDatabase))]
|
||||
partial class BlacksmithWorkshopDatabaseModelSnapshot : ModelSnapshot
|
||||
@ -88,6 +88,32 @@ namespace BlacksmithWorkshopDatebaseImplement.Migrations
|
||||
b.ToTable("ManufactureComponents");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.ManufactureInStore", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("ManufactureId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("StoreId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ManufactureId");
|
||||
|
||||
b.HasIndex("StoreId");
|
||||
|
||||
b.ToTable("ManufacturesInStore");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Order", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
@ -121,6 +147,33 @@ namespace BlacksmithWorkshopDatebaseImplement.Migrations
|
||||
b.ToTable("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Store", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<string>("Address")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<int>("MaxManufactures")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<DateTime>("OpeningDate")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<string>("StoreName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Stores");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.ManufactureComponent", b =>
|
||||
{
|
||||
b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Component", "Component")
|
||||
@ -140,6 +193,25 @@ namespace BlacksmithWorkshopDatebaseImplement.Migrations
|
||||
b.Navigation("Manufacture");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.ManufactureInStore", b =>
|
||||
{
|
||||
b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", "Manufacture")
|
||||
.WithMany()
|
||||
|
||||
.HasForeignKey("ManufactureId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Store", "Store")
|
||||
.WithMany("ManufacturesInStore")
|
||||
.HasForeignKey("StoreId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Manufacture");
|
||||
|
||||
b.Navigation("Store");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Order", b =>
|
||||
{
|
||||
b.HasOne("BlacksmithWorkshopDatabaseImplement.Models.Manufacture", null)
|
||||
@ -160,6 +232,11 @@ namespace BlacksmithWorkshopDatebaseImplement.Migrations
|
||||
|
||||
b.Navigation("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("BlacksmithWorkshopDatabaseImplement.Models.Store", b =>
|
||||
{
|
||||
b.Navigation("ManufacturesInStore");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,17 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace BlacksmithWorkshopDatabaseImplement.Models
|
||||
{
|
||||
public class ManufactureInStore
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
public int ManufactureId { get; set; }
|
||||
[Required]
|
||||
public int StoreId { get; set; }
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
public virtual Store Store { get; set; } = new();
|
||||
public virtual Manufacture Manufacture { get; set; } = new();
|
||||
}
|
||||
}
|
@ -0,0 +1,108 @@
|
||||
using BlacksmithWorkshopDataModels.Models;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using BlacksmithWorkshopContracts.BindingModels;
|
||||
using BlacksmithWorkshopContracts.ViewModels;
|
||||
|
||||
namespace BlacksmithWorkshopDatabaseImplement.Models
|
||||
{
|
||||
public class Store : IStoreModel
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
[Required]
|
||||
public string StoreName { get; private set; } = string.Empty;
|
||||
[Required]
|
||||
public string Address { get; private set; } = string.Empty;
|
||||
[Required]
|
||||
public DateTime OpeningDate { get; private set; }
|
||||
[Required]
|
||||
public int MaxManufactures { get; private set; }
|
||||
private Dictionary<int, (IManufactureModel, int)>? _manufactures = null;
|
||||
[NotMapped]
|
||||
public Dictionary<int, (IManufactureModel, int)> Manufactures
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_manufactures == null)
|
||||
{
|
||||
using var context = new BlacksmithWorkshopDatabase();
|
||||
_manufactures = ManufacturesInStore
|
||||
.ToDictionary(x => x.ManufactureId, x => (context.Manufactures
|
||||
.FirstOrDefault(y => y.Id == x.ManufactureId)! as IManufactureModel, x.Count));
|
||||
}
|
||||
return _manufactures;
|
||||
}
|
||||
}
|
||||
[ForeignKey("StoreId")]
|
||||
public virtual List<ManufactureInStore> ManufacturesInStore { get; set; } = new();
|
||||
|
||||
public static Store? Create(BlacksmithWorkshopDatabase context, StoreBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Store()
|
||||
{
|
||||
Id = model.Id,
|
||||
StoreName = model.StoreName,
|
||||
Address = model.Address,
|
||||
OpeningDate = model.OpeningDate,
|
||||
MaxManufactures = model.MaxManufactures,
|
||||
ManufacturesInStore = model.Manufactures.Select(x => new ManufactureInStore
|
||||
{
|
||||
Manufacture = context.Manufactures.FirstOrDefault(y => y.Id == x.Key)!,
|
||||
Count = x.Value.Item2,
|
||||
}).ToList()
|
||||
};
|
||||
}
|
||||
public void Update(StoreBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
StoreName = model.StoreName;
|
||||
Address = model.Address;
|
||||
OpeningDate = model.OpeningDate;
|
||||
}
|
||||
public StoreViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
StoreName = StoreName,
|
||||
Address = Address,
|
||||
Manufactures = Manufactures,
|
||||
OpeningDate = OpeningDate,
|
||||
MaxManufactures = MaxManufactures,
|
||||
};
|
||||
|
||||
public void UpdateManufactures(BlacksmithWorkshopDatabase context, StoreBindingModel model)
|
||||
{
|
||||
var manufacturesInStore = context.ManufacturesInStore
|
||||
.Where(rec => rec.StoreId == model.Id)
|
||||
.ToList();
|
||||
// удалили те, которых нет в модели
|
||||
if (manufacturesInStore != null && manufacturesInStore.Count > 0)
|
||||
{
|
||||
context.ManufacturesInStore
|
||||
.RemoveRange(manufacturesInStore
|
||||
.Where(rec => !model.Manufactures
|
||||
.ContainsKey(rec.ManufactureId)));
|
||||
// обновили количество у существующих записей
|
||||
foreach (var updateManufacture in manufacturesInStore.Where(x => model.Manufactures.ContainsKey(x.ManufactureId)))
|
||||
{
|
||||
updateManufacture.Count = model.Manufactures[updateManufacture.ManufactureId].Item2;
|
||||
model.Manufactures.Remove(updateManufacture.ManufactureId);
|
||||
}
|
||||
}
|
||||
var store = context.Stores.First(x => x.Id == model.Id);
|
||||
store.ManufacturesInStore.AddRange(model.Manufactures.Select(x => new ManufactureInStore
|
||||
{
|
||||
Manufacture = context.Manufactures.First(y => y.Id == x.Key),
|
||||
Count = x.Value.Item2,
|
||||
}).Except(manufacturesInStore ?? new()));
|
||||
context.SaveChanges();
|
||||
_manufactures = null;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user
Связь настроена не до конца