lab3_hard
This commit is contained in:
parent
25158912fa
commit
d6232e904f
@ -13,9 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarRepairShopDataModels", "
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarRepairShopListImplement", "CarRepairShopListImplement\CarRepairShopListImplement.csproj", "{5EF355A9-2708-47C4-B7B1-6D1CB89CDC02}"
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarRepairShopFileImplement", "CarRepairShopFileImplement\CarRepairShopFileImplement.csproj", "{FD920623-E8C5-45DE-9D7F-A6C643102F9D}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarRepairShopFileImplement", "CarRepairShopFileImplement\CarRepairShopFileImplement.csproj", "{4966A8B7-5985-48DF-834A-3E41D2E0D01D}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarRepairShopDatabaseImplement", "CarRepairShopDatabaseImplement\CarRepairShopDatabaseImplement.csproj", "{1AC87AFF-B786-44E5-AAAC-A87461E936FC}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarRepairShopDatabaseImplement", "CarRepairShopDatabaseImplement\CarRepairShopDatabaseImplement.csproj", "{C40664B4-5073-45D2-9E3A-562EDE32CC51}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
@ -43,14 +43,14 @@ Global
|
||||
{5EF355A9-2708-47C4-B7B1-6D1CB89CDC02}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{5EF355A9-2708-47C4-B7B1-6D1CB89CDC02}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{5EF355A9-2708-47C4-B7B1-6D1CB89CDC02}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{FD920623-E8C5-45DE-9D7F-A6C643102F9D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{FD920623-E8C5-45DE-9D7F-A6C643102F9D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{FD920623-E8C5-45DE-9D7F-A6C643102F9D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{FD920623-E8C5-45DE-9D7F-A6C643102F9D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{1AC87AFF-B786-44E5-AAAC-A87461E936FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{1AC87AFF-B786-44E5-AAAC-A87461E936FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{1AC87AFF-B786-44E5-AAAC-A87461E936FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{1AC87AFF-B786-44E5-AAAC-A87461E936FC}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{4966A8B7-5985-48DF-834A-3E41D2E0D01D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{4966A8B7-5985-48DF-834A-3E41D2E0D01D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{4966A8B7-5985-48DF-834A-3E41D2E0D01D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{4966A8B7-5985-48DF-834A-3E41D2E0D01D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{C40664B4-5073-45D2-9E3A-562EDE32CC51}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{C40664B4-5073-45D2-9E3A-562EDE32CC51}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{C40664B4-5073-45D2-9E3A-562EDE32CC51}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{C40664B4-5073-45D2-9E3A-562EDE32CC51}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
@ -21,5 +21,8 @@ namespace CarRepairShopDatabaseImplement
|
||||
public virtual DbSet<RepairComponent> RepairComponents { set; get; }
|
||||
|
||||
public virtual DbSet<Order> Orders { set; get; }
|
||||
public virtual DbSet<Shop> Shops { set; get; }
|
||||
|
||||
public virtual DbSet<ShopRepair> ShopRepairs { set; get; }
|
||||
}
|
||||
}
|
@ -0,0 +1,146 @@
|
||||
using CarRepairShopContracts.BindingModels;
|
||||
using CarRepairShopContracts.SearchModels;
|
||||
using CarRepairShopContracts.StoragesContracts;
|
||||
using CarRepairShopContracts.ViewModels;
|
||||
using CarRepairShopDatabaseImplement.Models;
|
||||
using CarRepairShopDataModels.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace CarRepairShopDatabaseImplement.Implements
|
||||
{
|
||||
public class ShopStorage : IShopStorage
|
||||
{
|
||||
public List<ShopViewModel> GetFullList()
|
||||
{
|
||||
using var context = new CarRepairShopDatabase();
|
||||
return context.Shops
|
||||
.Include(x => x.Repairs)
|
||||
.ThenInclude(x => x.Repair)
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.ShopName))
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new CarRepairShopDatabase();
|
||||
return context.Shops
|
||||
.Include(x => x.Repairs)
|
||||
.ThenInclude(x => x.Repair)
|
||||
.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 CarRepairShopDatabase();
|
||||
return context.Shops
|
||||
.Include(x => x.Repairs)
|
||||
.ThenInclude(x => x.Repair)
|
||||
.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 CarRepairShopDatabase();
|
||||
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 CarRepairShopDatabase();
|
||||
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.UpdateRepairs(context, model);
|
||||
transaction.Commit();
|
||||
return shop.GetViewModel;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public ShopViewModel? Delete(ShopBindingModel model)
|
||||
{
|
||||
using var context = new CarRepairShopDatabase();
|
||||
var element = context.Shops
|
||||
.Include(x => x.Repairs)
|
||||
.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.Shops.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool MakeSale(IRepairModel model, int count)
|
||||
{
|
||||
using var context = new CarRepairShopDatabase();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
foreach (var shop in context.Shops.Include(x => x.Repairs).ThenInclude(x => x.Repair)
|
||||
.Where(x => x.Repairs.Any(x => x.RepairId == model.Id))
|
||||
.ToList())
|
||||
{
|
||||
var repair = shop.ShopRepairs[model.Id];
|
||||
int min = Math.Min(repair.Item2, count);
|
||||
if (min == repair.Item2)
|
||||
{
|
||||
shop.ShopRepairs.Remove(model.Id);
|
||||
}
|
||||
else
|
||||
{
|
||||
shop.ShopRepairs[model.Id] = (repair.Item1, repair.Item2 - min);
|
||||
}
|
||||
shop.UpdateRepairs(context, new() { Id = shop.Id, ShopRepairs = shop.ShopRepairs });
|
||||
count -= min;
|
||||
if (count == 0)
|
||||
{
|
||||
context.SaveChanges();
|
||||
transaction.Commit();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
transaction.Rollback();
|
||||
return false;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
247
CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240418133633_ShopAddition.Designer.cs
generated
Normal file
247
CarRepairShop/CarRepairShopDatabaseImplement/Migrations/20240418133633_ShopAddition.Designer.cs
generated
Normal file
@ -0,0 +1,247 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using CarRepairShopDatabaseImplement;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace CarRepairShopDatabaseImplement.Migrations
|
||||
{
|
||||
[DbContext(typeof(CarRepairShopDatabase))]
|
||||
[Migration("20240418133633_ShopAddition")]
|
||||
partial class ShopAddition
|
||||
{
|
||||
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||
{
|
||||
#pragma warning disable 612, 618
|
||||
modelBuilder
|
||||
.HasAnnotation("ProductVersion", "6.0.27")
|
||||
.HasAnnotation("Relational:MaxIdentifierLength", 128);
|
||||
|
||||
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1);
|
||||
|
||||
modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Component", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
|
||||
|
||||
b.Property<string>("ComponentName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<double>("Cost")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Components");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Order", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<DateTime>("DateCreate")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<DateTime?>("DateImplement")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<int>("RepairId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("Status")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<double>("Sum")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("RepairId");
|
||||
|
||||
b.ToTable("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Repair", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
|
||||
|
||||
b.Property<double>("Price")
|
||||
.HasColumnType("float");
|
||||
|
||||
b.Property<string>("RepairName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Repairs");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.RepairComponent", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
|
||||
|
||||
b.Property<int>("ComponentId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("RepairId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("ComponentId");
|
||||
|
||||
b.HasIndex("RepairId");
|
||||
|
||||
b.ToTable("RepairComponents");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Shop", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
|
||||
|
||||
b.Property<string>("Address")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<DateTime>("DateOpening")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<int>("RepairMaxAmount")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<string>("ShopName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Shops");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.ShopRepair", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("RepairId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("ShopId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("RepairId");
|
||||
|
||||
b.HasIndex("ShopId");
|
||||
|
||||
b.ToTable("ShopRepairs");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Order", b =>
|
||||
{
|
||||
b.HasOne("CarRepairShopDatabaseImplement.Models.Repair", "Repair")
|
||||
.WithMany("Orders")
|
||||
.HasForeignKey("RepairId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Repair");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.RepairComponent", b =>
|
||||
{
|
||||
b.HasOne("CarRepairShopDatabaseImplement.Models.Component", "Component")
|
||||
.WithMany("RepairComponents")
|
||||
.HasForeignKey("ComponentId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("CarRepairShopDatabaseImplement.Models.Repair", "Repair")
|
||||
.WithMany("Components")
|
||||
.HasForeignKey("RepairId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Component");
|
||||
|
||||
b.Navigation("Repair");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.ShopRepair", b =>
|
||||
{
|
||||
b.HasOne("CarRepairShopDatabaseImplement.Models.Repair", "Repair")
|
||||
.WithMany()
|
||||
.HasForeignKey("RepairId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("CarRepairShopDatabaseImplement.Models.Shop", "Shop")
|
||||
.WithMany("Repairs")
|
||||
.HasForeignKey("ShopId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Repair");
|
||||
|
||||
b.Navigation("Shop");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Component", b =>
|
||||
{
|
||||
b.Navigation("RepairComponents");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Repair", b =>
|
||||
{
|
||||
b.Navigation("Components");
|
||||
|
||||
b.Navigation("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Shop", b =>
|
||||
{
|
||||
b.Navigation("Repairs");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace CarRepairShopDatabaseImplement.Migrations
|
||||
{
|
||||
public partial class ShopAddition : Migration
|
||||
{
|
||||
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),
|
||||
RepairMaxAmount = table.Column<int>(type: "int", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_Shops", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "ShopRepairs",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(type: "int", nullable: false)
|
||||
.Annotation("SqlServer:Identity", "1, 1"),
|
||||
ShopId = table.Column<int>(type: "int", nullable: false),
|
||||
RepairId = table.Column<int>(type: "int", nullable: false),
|
||||
Count = table.Column<int>(type: "int", nullable: false)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_ShopRepairs", x => x.Id);
|
||||
table.ForeignKey(
|
||||
name: "FK_ShopRepairs_Repairs_RepairId",
|
||||
column: x => x.RepairId,
|
||||
principalTable: "Repairs",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
table.ForeignKey(
|
||||
name: "FK_ShopRepairs_Shops_ShopId",
|
||||
column: x => x.ShopId,
|
||||
principalTable: "Shops",
|
||||
principalColumn: "Id",
|
||||
onDelete: ReferentialAction.Cascade);
|
||||
});
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_ShopRepairs_RepairId",
|
||||
table: "ShopRepairs",
|
||||
column: "RepairId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_ShopRepairs_ShopId",
|
||||
table: "ShopRepairs",
|
||||
column: "ShopId");
|
||||
}
|
||||
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropTable(
|
||||
name: "ShopRepairs");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "Shops");
|
||||
}
|
||||
}
|
||||
}
|
@ -121,6 +121,59 @@ namespace CarRepairShopDatabaseImplement.Migrations
|
||||
b.ToTable("RepairComponents");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Shop", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
|
||||
|
||||
b.Property<string>("Address")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<DateTime>("DateOpening")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<int>("RepairMaxAmount")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<string>("ShopName")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("Shops");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.ShopRepair", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
|
||||
|
||||
b.Property<int>("Count")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("RepairId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<int>("ShopId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.HasIndex("RepairId");
|
||||
|
||||
b.HasIndex("ShopId");
|
||||
|
||||
b.ToTable("ShopRepairs");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Order", b =>
|
||||
{
|
||||
b.HasOne("CarRepairShopDatabaseImplement.Models.Repair", "Repair")
|
||||
@ -151,6 +204,25 @@ namespace CarRepairShopDatabaseImplement.Migrations
|
||||
b.Navigation("Repair");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.ShopRepair", b =>
|
||||
{
|
||||
b.HasOne("CarRepairShopDatabaseImplement.Models.Repair", "Repair")
|
||||
.WithMany()
|
||||
.HasForeignKey("RepairId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.HasOne("CarRepairShopDatabaseImplement.Models.Shop", "Shop")
|
||||
.WithMany("Repairs")
|
||||
.HasForeignKey("ShopId")
|
||||
.OnDelete(DeleteBehavior.Cascade)
|
||||
.IsRequired();
|
||||
|
||||
b.Navigation("Repair");
|
||||
|
||||
b.Navigation("Shop");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Component", b =>
|
||||
{
|
||||
b.Navigation("RepairComponents");
|
||||
@ -162,6 +234,11 @@ namespace CarRepairShopDatabaseImplement.Migrations
|
||||
|
||||
b.Navigation("Orders");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("CarRepairShopDatabaseImplement.Models.Shop", b =>
|
||||
{
|
||||
b.Navigation("Repairs");
|
||||
});
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
}
|
||||
|
@ -16,19 +16,19 @@ namespace CarRepairShopDatabaseImplement.Models
|
||||
[Required]
|
||||
public double Price { get; set; }
|
||||
|
||||
private Dictionary<int, (IComponentModel, int)>? _repairComponents = null;
|
||||
private Dictionary<int, (IComponentModel, int)>? _productComponents = null;
|
||||
|
||||
[NotMapped]
|
||||
public Dictionary<int, (IComponentModel, int)> RepairComponents
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_repairComponents == null)
|
||||
if (_productComponents == null)
|
||||
{
|
||||
_repairComponents = Components
|
||||
_productComponents = Components
|
||||
.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count));
|
||||
}
|
||||
return _repairComponents;
|
||||
return _productComponents;
|
||||
}
|
||||
}
|
||||
|
||||
@ -69,31 +69,31 @@ namespace CarRepairShopDatabaseImplement.Models
|
||||
|
||||
public void UpdateComponents(CarRepairShopDatabase context, RepairBindingModel model)
|
||||
{
|
||||
var repairComponents = context.RepairComponents.Where(rec => rec.RepairId == model.Id).ToList();
|
||||
if (repairComponents != null && repairComponents.Count > 0)
|
||||
var productComponents = context.RepairComponents.Where(rec => rec.RepairId == model.Id).ToList();
|
||||
if (productComponents != null && productComponents.Count > 0)
|
||||
{ // удалили те, которых нет в модели
|
||||
context.RepairComponents.RemoveRange(repairComponents.Where(rec => !model.RepairComponents.ContainsKey(rec.ComponentId)));
|
||||
context.RepairComponents.RemoveRange(productComponents.Where(rec => !model.RepairComponents.ContainsKey(rec.ComponentId)));
|
||||
context.SaveChanges();
|
||||
// обновили количество у существующих записей
|
||||
foreach (var updateComponent in repairComponents)
|
||||
foreach (var updateComponent in productComponents)
|
||||
{
|
||||
updateComponent.Count = model.RepairComponents[updateComponent.ComponentId].Item2;
|
||||
model.RepairComponents.Remove(updateComponent.ComponentId);
|
||||
}
|
||||
context.SaveChanges();
|
||||
}
|
||||
var repair = context.Repairs.First(x => x.Id == Id);
|
||||
var product = context.Repairs.First(x => x.Id == Id);
|
||||
foreach (var pc in model.RepairComponents)
|
||||
{
|
||||
context.RepairComponents.Add(new RepairComponent
|
||||
{
|
||||
Repair = repair,
|
||||
Repair = product,
|
||||
Component = context.Components.First(x => x.Id == pc.Key),
|
||||
Count = pc.Value.Item2
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_repairComponents = null;
|
||||
_productComponents = null;
|
||||
}
|
||||
}
|
||||
}
|
110
CarRepairShop/CarRepairShopDatabaseImplement/Models/Shop.cs
Normal file
110
CarRepairShop/CarRepairShopDatabaseImplement/Models/Shop.cs
Normal file
@ -0,0 +1,110 @@
|
||||
using CarRepairShopContracts.BindingModels;
|
||||
using CarRepairShopContracts.ViewModels;
|
||||
using CarRepairShopDataModels.Models;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace CarRepairShopDatabaseImplement.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 RepairMaxAmount { get; set; }
|
||||
|
||||
private Dictionary<int, (IRepairModel, int)>? _shopRepairs = null;
|
||||
|
||||
[NotMapped]
|
||||
public Dictionary<int, (IRepairModel, int)> ShopRepairs
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_shopRepairs == null)
|
||||
{
|
||||
_shopRepairs = Repairs
|
||||
.ToDictionary(x => x.RepairId, x => (x.Repair as IRepairModel, x.Count));
|
||||
}
|
||||
return _shopRepairs;
|
||||
}
|
||||
}
|
||||
|
||||
[ForeignKey("ShopId")]
|
||||
public virtual List<ShopRepair> Repairs { get; set; } = new();
|
||||
|
||||
public static Shop Create(CarRepairShopDatabase context, ShopBindingModel model)
|
||||
{
|
||||
return new Shop()
|
||||
{
|
||||
Id = model.Id,
|
||||
ShopName = model.ShopName,
|
||||
Address = model.Address,
|
||||
DateOpening = model.DateOpening,
|
||||
RepairMaxAmount = model.RepairMaxAmount,
|
||||
Repairs = model.ShopRepairs.Select(x => new ShopRepair
|
||||
{
|
||||
Repair = context.Repairs.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;
|
||||
RepairMaxAmount = model.RepairMaxAmount;
|
||||
}
|
||||
|
||||
public ShopViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
ShopName = ShopName,
|
||||
Address = Address,
|
||||
DateOpening = DateOpening,
|
||||
RepairMaxAmount = RepairMaxAmount,
|
||||
ShopRepairs = ShopRepairs
|
||||
};
|
||||
|
||||
public void UpdateRepairs(CarRepairShopDatabase context, ShopBindingModel model)
|
||||
{
|
||||
var shopRepairs = context.ShopRepairs.Where(rec => rec.ShopId == model.Id).ToList();
|
||||
if (shopRepairs != null && shopRepairs.Count > 0)
|
||||
{
|
||||
context.ShopRepairs.RemoveRange(shopRepairs.Where(rec => !model.ShopRepairs.ContainsKey(rec.RepairId)));
|
||||
context.SaveChanges();
|
||||
foreach (var updateRepair in shopRepairs)
|
||||
{
|
||||
if (model.ShopRepairs.ContainsKey(updateRepair.RepairId))
|
||||
{
|
||||
updateRepair.Count = model.ShopRepairs[updateRepair.RepairId].Item2;
|
||||
model.ShopRepairs.Remove(updateRepair.RepairId);
|
||||
}
|
||||
}
|
||||
context.SaveChanges();
|
||||
}
|
||||
var shop = context.Shops.First(x => x.Id == Id);
|
||||
foreach (var ic in model.ShopRepairs)
|
||||
{
|
||||
context.ShopRepairs.Add(new ShopRepair
|
||||
{
|
||||
Shop = shop,
|
||||
Repair = context.Repairs.First(x => x.Id == ic.Key),
|
||||
Count = ic.Value.Item2
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_shopRepairs = null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace CarRepairShopDatabaseImplement.Models
|
||||
{
|
||||
public class ShopRepair
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public int ShopId { get; set; }
|
||||
|
||||
[Required]
|
||||
public int RepairId { get; set; }
|
||||
|
||||
[Required]
|
||||
public int Count { get; set; }
|
||||
|
||||
public virtual Repair Repair { get; set; } = new();
|
||||
|
||||
public virtual Shop Shop { get; set; } = new();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user