LabWork03_Hard
This commit is contained in:
parent
12c34b4097
commit
855bcfd6ec
@ -45,5 +45,15 @@ namespace AircraftPlantDatabaseImplement
|
||||
/// Таблица заказов
|
||||
/// </summary>
|
||||
public virtual DbSet<Order> Orders { set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// Таблица магазинов
|
||||
/// </summary>
|
||||
public virtual DbSet<Shop> Shops { set; get; }
|
||||
|
||||
/// <summary>
|
||||
/// Связь между магазинами и изделиями
|
||||
/// </summary>
|
||||
public virtual DbSet<ShopPlane> ShopPlanes { set; get; }
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,218 @@
|
||||
using AircraftPlantContracts.BindingModels;
|
||||
using AircraftPlantContracts.SearchModels;
|
||||
using AircraftPlantContracts.StoragesContracts;
|
||||
using AircraftPlantContracts.ViewModels;
|
||||
using AircraftPlantDatabaseImplement.Models;
|
||||
using AircraftPlantDataModels.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AircraftPlantDatabaseImplement.Implements
|
||||
{
|
||||
/// <summary>
|
||||
/// Реализация интерфейса хранилища для магазинов
|
||||
/// </summary>
|
||||
public class ShopStorage : IShopStorage
|
||||
{
|
||||
/// <summary>
|
||||
/// Получение полного списка
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public List<ShopViewModel> GetFullList()
|
||||
{
|
||||
using var context = new AircraftPlantDatabase();
|
||||
return context.Shops
|
||||
.Include(x => x.Planes)
|
||||
.ThenInclude(x => x.Plane)
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение фильтрованного списка
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <returns></returns>
|
||||
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.ShopName))
|
||||
{
|
||||
return new();
|
||||
}
|
||||
|
||||
using var context = new AircraftPlantDatabase();
|
||||
return context.Shops
|
||||
.Include(x => x.Planes)
|
||||
.ThenInclude(x => x.Plane)
|
||||
.Where(x => x.ShopName.Contains(model.ShopName))
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение элемента
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <returns></returns>
|
||||
public ShopViewModel? GetElement(ShopSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
using var context = new AircraftPlantDatabase();
|
||||
return context.Shops
|
||||
.Include(x => x.Planes)
|
||||
.ThenInclude(x => x.Plane)
|
||||
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) &&
|
||||
x.ShopName == model.ShopName) ||
|
||||
(model.Id.HasValue && x.Id == model.Id))
|
||||
?.GetViewModel;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Добавление элемента
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <returns></returns>
|
||||
public ShopViewModel? Insert(ShopBindingModel model)
|
||||
{
|
||||
using var context = new AircraftPlantDatabase();
|
||||
var newShop = Shop.Create(context, model);
|
||||
if (newShop == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
context.Shops.Add(newShop);
|
||||
context.SaveChanges();
|
||||
return newShop.GetViewModel;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Редактирование элемента
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <returns></returns>
|
||||
public ShopViewModel? Update(ShopBindingModel model)
|
||||
{
|
||||
using var context = new AircraftPlantDatabase();
|
||||
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.UpdatePlanes(context, model);
|
||||
transaction.Commit();
|
||||
return shop.GetViewModel;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Удаление элемента
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <returns></returns>
|
||||
public ShopViewModel? Delete(ShopBindingModel model)
|
||||
{
|
||||
using var context = new AircraftPlantDatabase();
|
||||
var element = context.Shops
|
||||
.Include(x => x.Planes)
|
||||
.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.Shops.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Продажа изделий
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <param name="count"></param>
|
||||
/// <returns></returns>
|
||||
public bool SellPlanes(IPlaneModel model, int count)
|
||||
{
|
||||
using var context = new AircraftPlantDatabase();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var shops = context.Shops
|
||||
.Include(x => x.Planes)
|
||||
.ThenInclude(x => x.Plane)
|
||||
.ToList()
|
||||
.Where(x => x.ShopPlanes.ContainsKey(model.Id));
|
||||
|
||||
foreach (var shop in shops)
|
||||
{
|
||||
int countInCurrentShop = shop.ShopPlanes[model.Id].Item2;
|
||||
if (countInCurrentShop <= count)
|
||||
{
|
||||
var elem = context.ShopPlanes
|
||||
.Where(x => x.PlaneId == model.Id)
|
||||
.FirstOrDefault(x => x.ShopId == shop.Id);
|
||||
context.ShopPlanes.Remove(elem);
|
||||
shop.ShopPlanes.Remove(model.Id);
|
||||
count -= countInCurrentShop;
|
||||
}
|
||||
else
|
||||
{
|
||||
shop.ShopPlanes[model.Id] = (shop.ShopPlanes[model.Id].Item1, countInCurrentShop - count);
|
||||
count = 0;
|
||||
shop.Update(new ShopBindingModel
|
||||
{
|
||||
Id = shop.Id,
|
||||
ShopPlanes = shop.ShopPlanes
|
||||
});
|
||||
}
|
||||
if (count <= 0)
|
||||
{
|
||||
context.SaveChanges();
|
||||
transaction.Commit();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
transaction.Rollback();
|
||||
return false;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Проверка наличия в нужном количестве
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
/// <param name="count"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotImplementedException"></exception>
|
||||
public bool CheckCount(IPlaneModel model, int count)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
246
AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240324224237_HardCreate.Designer.cs
generated
Normal file
246
AircraftPlant/AircraftPlantDatabaseImplement/Migrations/20240324224237_HardCreate.Designer.cs
generated
Normal file
@ -0,0 +1,246 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using AircraftPlantDatabaseImplement;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace AircraftPlantDatabaseImplement.Migrations
|
||||
{
|
||||
[DbContext(typeof(AircraftPlantDatabase))]
|
||||
[Migration("20240324224237_HardCreate")]
|
||||
partial class HardCreate
|
||||
{
|
||||
/// <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("AircraftPlantDatabaseImplement.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("AircraftPlantDatabaseImplement.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>("PlaneId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("Status")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<double>("Sum")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("PlaneId");
|
||||
|
||||
b.ToTable("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Plane", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<string>("PlaneName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<double>("Price")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Planes");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.PlaneComponent", 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>("PlaneId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ComponentId");
|
||||
|
||||
b.HasIndex("PlaneId");
|
||||
|
||||
b.ToTable("PlaneComponents");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("AircraftPlantDatabaseImplement.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>("MaxPlanes")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<string>("ShopName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Shops");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.ShopPlane", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("PlaneId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("ShopId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("PlaneId");
|
||||
|
||||
b.HasIndex("ShopId");
|
||||
|
||||
b.ToTable("ShopPlanes");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Order", b =>
|
||||
{
|
||||
b.HasOne("AircraftPlantDatabaseImplement.Models.Plane", null)
|
||||
.WithMany("Orders")
|
||||
.HasForeignKey("PlaneId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
});
|
||||
|
||||
modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.PlaneComponent", b =>
|
||||
{
|
||||
b.HasOne("AircraftPlantDatabaseImplement.Models.Component", "Component")
|
||||
.WithMany("PlaneComponents")
|
||||
.HasForeignKey("ComponentId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("AircraftPlantDatabaseImplement.Models.Plane", "Plane")
|
||||
.WithMany("Components")
|
||||
.HasForeignKey("PlaneId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Component");
|
||||
|
||||
b.Navigation("Plane");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.ShopPlane", b =>
|
||||
{
|
||||
b.HasOne("AircraftPlantDatabaseImplement.Models.Plane", "Plane")
|
||||
.WithMany()
|
||||
.HasForeignKey("PlaneId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("AircraftPlantDatabaseImplement.Models.Shop", "Shop")
|
||||
.WithMany("Planes")
|
||||
.HasForeignKey("ShopId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Plane");
|
||||
|
||||
b.Navigation("Shop");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Component", b =>
|
||||
{
|
||||
b.Navigation("PlaneComponents");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Plane", b =>
|
||||
{
|
||||
b.Navigation("Components");
|
||||
|
||||
b.Navigation("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Shop", b =>
|
||||
{
|
||||
b.Navigation("Planes");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace AircraftPlantDatabaseImplement.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class HardCreate : 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),
|
||||
MaxPlanes = table.Column<int>(type: "int", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Shops", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "ShopPlanes",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(type: "int", nullable: false)
|
||||
.Annotation("SqlServer:Identity", "1, 1"),
|
||||
ShopId = table.Column<int>(type: "int", nullable: false),
|
||||
PlaneId = table.Column<int>(type: "int", nullable: false),
|
||||
Count = table.Column<int>(type: "int", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_ShopPlanes", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_ShopPlanes_Planes_PlaneId",
|
||||
column: x => x.PlaneId,
|
||||
principalTable: "Planes",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "FK_ShopPlanes_Shops_ShopId",
|
||||
column: x => x.ShopId,
|
||||
principalTable: "Shops",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_ShopPlanes_PlaneId",
|
||||
table: "ShopPlanes",
|
||||
column: "PlaneId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_ShopPlanes_ShopId",
|
||||
table: "ShopPlanes",
|
||||
column: "ShopId");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "ShopPlanes");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Shops");
|
||||
}
|
||||
}
|
||||
}
|
@ -121,6 +121,59 @@ namespace AircraftPlantDatabaseImplement.Migrations
|
||||
b.ToTable("PlaneComponents");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("AircraftPlantDatabaseImplement.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>("MaxPlanes")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<string>("ShopName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Shops");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.ShopPlane", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("PlaneId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("ShopId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("PlaneId");
|
||||
|
||||
b.HasIndex("ShopId");
|
||||
|
||||
b.ToTable("ShopPlanes");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Order", b =>
|
||||
{
|
||||
b.HasOne("AircraftPlantDatabaseImplement.Models.Plane", null)
|
||||
@ -149,6 +202,25 @@ namespace AircraftPlantDatabaseImplement.Migrations
|
||||
b.Navigation("Plane");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.ShopPlane", b =>
|
||||
{
|
||||
b.HasOne("AircraftPlantDatabaseImplement.Models.Plane", "Plane")
|
||||
.WithMany()
|
||||
.HasForeignKey("PlaneId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("AircraftPlantDatabaseImplement.Models.Shop", "Shop")
|
||||
.WithMany("Planes")
|
||||
.HasForeignKey("ShopId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Plane");
|
||||
|
||||
b.Navigation("Shop");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Component", b =>
|
||||
{
|
||||
b.Navigation("PlaneComponents");
|
||||
@ -160,6 +232,11 @@ namespace AircraftPlantDatabaseImplement.Migrations
|
||||
|
||||
b.Navigation("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("AircraftPlantDatabaseImplement.Models.Shop", b =>
|
||||
{
|
||||
b.Navigation("Planes");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
|
157
AircraftPlant/AircraftPlantDatabaseImplement/Models/Shop.cs
Normal file
157
AircraftPlant/AircraftPlantDatabaseImplement/Models/Shop.cs
Normal file
@ -0,0 +1,157 @@
|
||||
using AircraftPlantContracts.BindingModels;
|
||||
using AircraftPlantContracts.ViewModels;
|
||||
using AircraftPlantDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Linq;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AircraftPlantDatabaseImplement.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Сущность "Магазин"
|
||||
/// </summary>
|
||||
public class Shop : IShopModel
|
||||
{
|
||||
/// <summary>
|
||||
/// Идентификатор
|
||||
/// </summary>
|
||||
public int Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Название магазина
|
||||
/// </summary>
|
||||
[Required]
|
||||
public string ShopName { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Адрес магазина
|
||||
/// </summary>
|
||||
[Required]
|
||||
public string Address { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Дата открытия магазина
|
||||
/// </summary>
|
||||
[Required]
|
||||
public DateTime DateOpening { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Коллекция изделий в магазине
|
||||
/// </summary>
|
||||
private Dictionary<int, (IPlaneModel, int)>? _shopPlanes = null;
|
||||
|
||||
[NotMapped]
|
||||
public Dictionary<int, (IPlaneModel, int)> ShopPlanes
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_shopPlanes == null)
|
||||
{
|
||||
_shopPlanes = Planes
|
||||
.ToDictionary(recSP => recSP.PlaneId, recSP => (recSP.Plane as IPlaneModel, recSP.Count));
|
||||
}
|
||||
return _shopPlanes;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Максимальное количество изделий
|
||||
/// </summary>
|
||||
public int MaxPlanes { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Связь с классом связи магазина и изделий
|
||||
/// </summary>
|
||||
[ForeignKey("ShopId")]
|
||||
public List<ShopPlane> Planes { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Создание модели магазина
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <param name="model"></param>
|
||||
/// <returns></returns>
|
||||
public static Shop Create(AircraftPlantDatabase context, ShopBindingModel model)
|
||||
{
|
||||
return new Shop
|
||||
{
|
||||
Id = model.Id,
|
||||
ShopName = model.ShopName,
|
||||
Address = model.Address,
|
||||
DateOpening = model.DateOpening,
|
||||
MaxPlanes = model.MaxPlanes,
|
||||
Planes = model.ShopPlanes.Select(x => new ShopPlane
|
||||
{
|
||||
Plane = context.Planes.First(y => y.Id == x.Key),
|
||||
Count = x.Value.Item2
|
||||
}).ToList()
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Изменение модели магазина
|
||||
/// </summary>
|
||||
/// <param name="model"></param>
|
||||
public void Update(ShopBindingModel model)
|
||||
{
|
||||
ShopName = model.ShopName;
|
||||
Address = model.Address;
|
||||
DateOpening = model.DateOpening;
|
||||
MaxPlanes = model.MaxPlanes;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получение модели магазина
|
||||
/// </summary>
|
||||
public ShopViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
ShopName = ShopName,
|
||||
Address = Address,
|
||||
DateOpening = DateOpening,
|
||||
ShopPlanes = ShopPlanes,
|
||||
MaxPlanes = MaxPlanes
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Метод обновления списка связей
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
/// <param name="model"></param>
|
||||
public void UpdatePlanes(AircraftPlantDatabase context, ShopBindingModel model)
|
||||
{
|
||||
var shopPlanes = context.ShopPlanes.Where(rec => rec.ShopId == model.Id).ToList();
|
||||
if (shopPlanes != null && shopPlanes.Count > 0)
|
||||
{
|
||||
// Удаление изделий, которых нет в магазине
|
||||
context.ShopPlanes.RemoveRange(shopPlanes.Where(rec => !model.ShopPlanes.ContainsKey(rec.PlaneId)));
|
||||
context.SaveChanges();
|
||||
// Обновление количества у существующих записей
|
||||
foreach (var updatePlanes in shopPlanes)
|
||||
{
|
||||
updatePlanes.Count = model.ShopPlanes[updatePlanes.PlaneId].Item2;
|
||||
model.ShopPlanes.Remove(updatePlanes.PlaneId);
|
||||
}
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
var shop = context.Shops.First(x => x.Id == Id);
|
||||
foreach (var sp in model.ShopPlanes)
|
||||
{
|
||||
context.ShopPlanes.Add(new ShopPlane
|
||||
{
|
||||
Shop = shop,
|
||||
Plane = context.Planes.First(x => x.Id == sp.Key),
|
||||
Count = sp.Value.Item2
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_shopPlanes = null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace AircraftPlantDatabaseImplement.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Класс для связи магазина и изделия
|
||||
/// </summary>
|
||||
public class ShopPlane
|
||||
{
|
||||
/// <summary>
|
||||
/// Идентификатор
|
||||
/// </summary>
|
||||
public int Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Идентификатор магазина
|
||||
/// </summary>
|
||||
[Required]
|
||||
public int ShopId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Идентификатор изделия
|
||||
/// </summary>
|
||||
[Required]
|
||||
public int PlaneId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Количество изделий
|
||||
/// </summary>
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Магазин
|
||||
/// </summary>
|
||||
public virtual Shop Shop { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Изделие
|
||||
/// </summary>
|
||||
public virtual Plane Plane { get; set; } = new();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user