Проблемы с созданием миграций

This commit is contained in:
Кашин Максим 2023-04-09 18:26:39 +04:00
parent e78c13eb51
commit ff8c9169cb
15 changed files with 262 additions and 587 deletions

View File

@ -72,7 +72,7 @@ namespace ConfectioneryView
var model = new ShopBindingModel
{
Id = _id ?? 0,
Name = comboBoxShop.Text,
ShopName = comboBoxShop.Text,
Address = textBoxAddress.Text,
DateOpening = textBoxDateOpening.Value.Date,
ReinforcedMaxCount = (int)VolumeNumericUpDown.Value
@ -112,11 +112,11 @@ namespace ConfectioneryView
});
if (view != null)
{
comboBoxShop.Text = view.Name;
comboBoxShop.Text = view.ShopName;
textBoxAddress.Text = view.Address;
textBoxDateOpening.Text = view.DateOpening.ToString();
VolumeNumericUpDown.Value = view.ReinforcedMaxCount;
_listShops = view.Reinforcedies ?? new Dictionary<int, (IReinforcedModel, int)>();
_listShops = view.ShopReinforcedies ?? new Dictionary<int, (IReinforcedModel, int)>();
LoadData();
}
}

View File

@ -26,7 +26,7 @@ namespace PrecastConcretePlantBusinessLogic.BusinessLogic
public List<ShopViewModel>? ReadList(ShopSearchModel? model)
{
_logger.LogInformation("ReadList. Name:{Name}.Id:{ Id} ",
model?.Name, model?.Id);
model?.ShopName, model?.Id);
var list = (model == null) ? _shopStorage.GetFullList() :
_shopStorage.GetFilteredList(model);
if (list == null)
@ -44,7 +44,7 @@ namespace PrecastConcretePlantBusinessLogic.BusinessLogic
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Name:{Name}.Id:{ Id}",
model.Name, model.Id);
model.ShopName, model.Id);
var element = _shopStorage.GetElement(model);
if (element == null)
{
@ -57,7 +57,7 @@ namespace PrecastConcretePlantBusinessLogic.BusinessLogic
public bool Create(ShopBindingModel model)
{
CheckModel(model);
model.Reinforcedies = new();
model.ShopReinforcedies = new();
if (_shopStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
@ -72,10 +72,10 @@ namespace PrecastConcretePlantBusinessLogic.BusinessLogic
public bool Update(ShopBindingModel model)
{
CheckModel(model, false);
if (string.IsNullOrEmpty(model.Name))
if (string.IsNullOrEmpty(model.ShopName))
{
throw new ArgumentNullException("Нет названия магазина",
nameof(model.Name));
nameof(model.ShopName));
}
if (_shopStorage.Update(model) == null)
@ -106,22 +106,22 @@ namespace PrecastConcretePlantBusinessLogic.BusinessLogic
{
return;
}
if (string.IsNullOrEmpty(model.Name))
if (string.IsNullOrEmpty(model.ShopName))
{
throw new ArgumentNullException("Нет названия магазина",
nameof(model.Name));
nameof(model.ShopName));
}
if (model.ReinforcedMaxCount < 0)
{
throw new ArgumentException("Максимальное количество изделий в магазине не может быть меньше нуля", nameof(model.ReinforcedMaxCount));
}
_logger.LogInformation("Shop. Name:{0}.Address:{1}. Id: {2}",
model.Name, model.Address, model.Id);
model.ShopName, model.Address, model.Id);
var element = _shopStorage.GetElement(new ShopSearchModel
{
Name = model.Name
ShopName = model.ShopName
});
if (element != null && element.Id != model.Id && element.Name == model.Name)
if (element != null && element.Id != model.Id && element.ShopName == model.ShopName)
{
throw new InvalidOperationException("Магазин с таким названием уже есть");
}
@ -139,7 +139,7 @@ namespace PrecastConcretePlantBusinessLogic.BusinessLogic
throw new ArgumentException("Количество изделий должно быть больше 0", nameof(count));
}
_logger.LogInformation("AddReinforced. Name:{Name}.Id:{ Id}", model.Name, model.Id);
_logger.LogInformation("AddReinforced. Name:{Name}.Id:{ Id}", model.ShopName, model.Id);
var element = _shopStorage.GetElement(model);
if (element == null)
@ -148,32 +148,32 @@ namespace PrecastConcretePlantBusinessLogic.BusinessLogic
return false;
}
if (element.ReinforcedMaxCount - element.Reinforcedies.Select(x => x.Value.Item2).Sum() < count)
if (element.ReinforcedMaxCount - element.ShopReinforcedies.Select(x => x.Value.Item2).Sum() < count)
{
throw new ArgumentNullException("Магазин переполнен", nameof(count));
}
_logger.LogInformation("AddReinforced find. Id:{Id}", element.Id);
if (element.Reinforcedies.TryGetValue(reinforced.Id, out var pair))
if (element.ShopReinforcedies.TryGetValue(reinforced.Id, out var pair))
{
element.Reinforcedies[reinforced.Id] = (reinforced, count + pair.Item2);
_logger.LogInformation("AddReinforced. Added {count} {reinforced} to '{Name}' shop", count, reinforced.ReinforcedName, element.Name);
element.ShopReinforcedies[reinforced.Id] = (reinforced, count + pair.Item2);
_logger.LogInformation("AddReinforced. Added {count} {reinforced} to '{Name}' shop", count, reinforced.ReinforcedName, element.ShopName);
}
else
{
element.Reinforcedies[reinforced.Id] = (reinforced, count);
_logger.LogInformation("AddReinforced. Added {count} new reinforced {reinforced} to '{Name}' shop", count, reinforced.ReinforcedName, element.Name);
element.ShopReinforcedies[reinforced.Id] = (reinforced, count);
_logger.LogInformation("AddReinforced. Added {count} new reinforced {reinforced} to '{Name}' shop", count, reinforced.ReinforcedName, element.ShopName);
}
_shopStorage.Update(new()
{
Id = element.Id,
Address = element.Address,
Name = element.Name,
ShopName = element.ShopName,
DateOpening = element.DateOpening,
ReinforcedMaxCount = element.ReinforcedMaxCount,
Reinforcedies = element.Reinforcedies,
ShopReinforcedies = element.ShopReinforcedies,
});
return true;
@ -191,7 +191,7 @@ namespace PrecastConcretePlantBusinessLogic.BusinessLogic
}
var freePlaces = _shopStorage.GetFullList()
.Select(x => x.ReinforcedMaxCount - x.Reinforcedies
.Select(x => x.ReinforcedMaxCount - x.ShopReinforcedies
.Select(p => p.Value.Item2).Sum()).Sum() - count;
if (freePlaces < 0)
@ -202,7 +202,7 @@ namespace PrecastConcretePlantBusinessLogic.BusinessLogic
foreach (var shop in _shopStorage.GetFullList())
{
var temp = Math.Min(count, shop.ReinforcedMaxCount - shop.Reinforcedies.Select(x => x.Value.Item2).Sum());
var temp = Math.Min(count, shop.ReinforcedMaxCount - shop.ShopReinforcedies.Select(x => x.Value.Item2).Sum());
if (temp <= 0)
{

View File

@ -9,14 +9,14 @@ namespace PrecastConcretePlantContracts.BindingModels
{
public class ShopBindingModel : IShopModel
{
public string Name { get; set; } = string.Empty;
public string ShopName { get; set; } = string.Empty;
public string Address { get; set; } = string.Empty;
public int ReinforcedMaxCount { get; set; }
public DateTime DateOpening { get; set; } = DateTime.Now;
public Dictionary<int, (IReinforcedModel, int)> Reinforcedies { get; set; } = new();
public Dictionary<int, (IReinforcedModel, int)> ShopReinforcedies { get; set; } = new();
public int Id { get; set; }

View File

@ -9,6 +9,6 @@ namespace PrecastConcretePlantContracts.SearchModels
public class ShopSearchModel
{
public int? Id { get; set; }
public string? Name { get; set; }
public string? ShopName { get; set; }
}
}

View File

@ -11,7 +11,7 @@ namespace PrecastConcretePlantContracts.ViewModels
public class ShopViewModel : IShopModel
{
[DisplayName("Название магазина")]
public string Name { get; set; } = string.Empty;
public string ShopName { get; set; } = string.Empty;
[DisplayName("Адрес магазина")]
public string Address { get; set; } = string.Empty;
@ -22,7 +22,7 @@ namespace PrecastConcretePlantContracts.ViewModels
[DisplayName("Вместимость магазина")]
public int ReinforcedMaxCount { get; set; }
public Dictionary<int, (IReinforcedModel, int)> Reinforcedies { get; set; } = new();
public Dictionary<int, (IReinforcedModel, int)> ShopReinforcedies { get; set; } = new();
public int Id { get; set; }

View File

@ -8,11 +8,11 @@ namespace PrecastConcretePlantDataModels.Models
{
public interface IShopModel : IId
{
string Name { get; }
string ShopName { get; }
string Address { get; }
public int ReinforcedMaxCount { get; }
DateTime DateOpening { get; }
Dictionary<int, (IReinforcedModel, int)> Reinforcedies { get; }
Dictionary<int, (IReinforcedModel, int)> ShopReinforcedies { get; }
}
}

View File

@ -0,0 +1,160 @@
using Microsoft.EntityFrameworkCore;
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.SearchModels;
using PrecastConcretePlantContracts.StoragesContracts;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDatabaseImplement.Models;
using PrecastConcretePlantDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrecastConcretePlantDatabaseImplement.Implements
{
public class ShopStorage : IShopStorage
{
public List<ShopViewModel> GetFullList()
{
using var context = new PrecastConcretePlantDataBase();
return context.Shops
.Include(x => x.Reinforcedies)
.ThenInclude(x => x.Reinforced)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName))
{
return new();
}
using var context = new PrecastConcretePlantDataBase();
return context.Shops
.Include(x => x.Reinforcedies)
.ThenInclude(x => x.Reinforced)
.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 PrecastConcretePlantDataBase();
return context.Shops
.Include(x => x.Reinforcedies)
.ThenInclude(x => x.Reinforced)
.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 PrecastConcretePlantDataBase();
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 PrecastConcretePlantDataBase();
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.UpdateReinforcedies(context, model);
transaction.Commit();
return shop.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public ShopViewModel? Delete(ShopBindingModel model)
{
using var context = new PrecastConcretePlantDataBase();
var element = context.Shops
.Include(x => x.Reinforcedies)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Shops.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public bool SellReinforcedies(IReinforcedModel model, int count)
{
using var context = new PrecastConcretePlantDataBase();
using var transaction = context.Database.BeginTransaction();
try
{
var shops = context.ShopReinforcedies
.Include(x => x.Shop)
.ToList()
.Where(rec => rec.ReinforcedId == model.Id);
if (shops == null)
{
return false;
}
foreach (var shop in shops)
{
if (shop.Count < count)
{
shop.Count = 0;
count -= shop.Count;
}
else
{
shop.Count = shop.Count - count;
count -= count;
}
if (count == 0)
{
context.SaveChanges();
transaction.Commit();
return true;
}
}
transaction.Rollback();
return false;
}
catch
{
transaction.Rollback();
throw;
}
}
public bool SellReinforced(IReinforcedModel model, int quantity)
{
throw new NotImplementedException();
}
}
}

View File

@ -1,175 +0,0 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using PrecastConcretePlantDatabaseImplement;
#nullable disable
namespace PrecastConcretePlantDatabaseImplement.Migrations
{
[DbContext(typeof(PrecastConcretePlantDataBase))]
[Migration("20230226175226_InitMigration")]
partial class InitMigration
{
/// <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("PrecastConcretePlantDatabaseImplement.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("PrecastConcretePlantDatabaseImplement.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>("ReinforcedId")
.HasColumnType("int");
b.Property<string>("ReinforcedName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("ReinforcedId");
b.ToTable("Orders");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Reinforced", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<double>("Price")
.HasColumnType("float");
b.Property<string>("ReinforcedName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Reinforceds");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.ReinforcedComponent", 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>("ReinforcedId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("ReinforcedId");
b.ToTable("ReinforcedComponents");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Order", b =>
{
b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Reinforced", "Reinforced")
.WithMany("Orders")
.HasForeignKey("ReinforcedId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Reinforced");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.ReinforcedComponent", b =>
{
b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Component", "Component")
.WithMany("ReinforcedComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Reinforced", "Reinforced")
.WithMany("Components")
.HasForeignKey("ReinforcedId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("Reinforced");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Component", b =>
{
b.Navigation("ReinforcedComponents");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Reinforced", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -1,126 +0,0 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace PrecastConcretePlantDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class InitMigration : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Components",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ComponentName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Cost = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Components", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Reinforceds",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ReinforcedName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Price = table.Column<double>(type: "float", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Reinforceds", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Orders",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ReinforcedId = table.Column<int>(type: "int", nullable: false),
ReinforcedName = table.Column<string>(type: "nvarchar(max)", nullable: false),
Count = table.Column<int>(type: "int", nullable: false),
Sum = table.Column<double>(type: "float", nullable: false),
Status = table.Column<int>(type: "int", nullable: false),
DateCreate = table.Column<DateTime>(type: "datetime2", nullable: false),
DateImplement = table.Column<DateTime>(type: "datetime2", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Orders", x => x.Id);
table.ForeignKey(
name: "FK_Orders_Reinforceds_ReinforcedId",
column: x => x.ReinforcedId,
principalTable: "Reinforceds",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "ReinforcedComponents",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ReinforcedId = table.Column<int>(type: "int", nullable: false),
ComponentId = table.Column<int>(type: "int", nullable: false),
Count = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ReinforcedComponents", x => x.Id);
table.ForeignKey(
name: "FK_ReinforcedComponents_Components_ComponentId",
column: x => x.ComponentId,
principalTable: "Components",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ReinforcedComponents_Reinforceds_ReinforcedId",
column: x => x.ReinforcedId,
principalTable: "Reinforceds",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_Orders_ReinforcedId",
table: "Orders",
column: "ReinforcedId");
migrationBuilder.CreateIndex(
name: "IX_ReinforcedComponents_ComponentId",
table: "ReinforcedComponents",
column: "ComponentId");
migrationBuilder.CreateIndex(
name: "IX_ReinforcedComponents_ReinforcedId",
table: "ReinforcedComponents",
column: "ReinforcedId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Orders");
migrationBuilder.DropTable(
name: "ReinforcedComponents");
migrationBuilder.DropTable(
name: "Components");
migrationBuilder.DropTable(
name: "Reinforceds");
}
}
}

View File

@ -1,172 +0,0 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using PrecastConcretePlantDatabaseImplement;
#nullable disable
namespace PrecastConcretePlantDatabaseImplement.Migrations
{
[DbContext(typeof(PrecastConcretePlantDataBase))]
partial class PrecastConcretePlantDataBaseModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.3")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.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("PrecastConcretePlantDatabaseImplement.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>("ReinforcedId")
.HasColumnType("int");
b.Property<string>("ReinforcedName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<double>("Sum")
.HasColumnType("float");
b.HasKey("Id");
b.HasIndex("ReinforcedId");
b.ToTable("Orders");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Reinforced", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<double>("Price")
.HasColumnType("float");
b.Property<string>("ReinforcedName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id");
b.ToTable("Reinforceds");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.ReinforcedComponent", 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>("ReinforcedId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ComponentId");
b.HasIndex("ReinforcedId");
b.ToTable("ReinforcedComponents");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Order", b =>
{
b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Reinforced", "Reinforced")
.WithMany("Orders")
.HasForeignKey("ReinforcedId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Reinforced");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.ReinforcedComponent", b =>
{
b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Component", "Component")
.WithMany("ReinforcedComponents")
.HasForeignKey("ComponentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Reinforced", "Reinforced")
.WithMany("Components")
.HasForeignKey("ReinforcedId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Component");
b.Navigation("Reinforced");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Component", b =>
{
b.Navigation("ReinforcedComponents");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Reinforced", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -13,106 +13,94 @@ namespace PrecastConcretePlantDatabaseImplement.Models
{
public class Shop : IShopModel
{
public int Id { get; set; }
[Required]
public string Name { get; private set; } = string.Empty;
public string ShopName { get; set; } = string.Empty;
[Required]
public string Address { get; private set; } = string.Empty;
public string Address { get; set; } = string.Empty;
[Required]
public int ReinforcedMaxCount { get; private set; }
public DateTime DateOpening { get; set; }
[Required]
public int ReinforcedMaxCount { get; set; }
public DateTime DateOpening { get; private set; }
private Dictionary<int, (IReinforcedModel, int)>? _shopReinforcedies = null;
private Dictionary<int, (IReinforcedModel, int)>? _cachedReinforcedies = null;
[NotMapped]
public Dictionary<int, (IReinforcedModel, int)> Reinforcedies
public Dictionary<int, (IReinforcedModel, int)> ShopReinforcedies
{
get
{
if (_cachedReinforcedies == null)
if (_shopReinforcedies == null)
{
using var context = new PrecastConcretePlantDataBase();
_cachedReinforcedies = ShopReinforcedies
.ToDictionary(x => x.ReinforcedId, x => (context.Reinforcedies
.FirstOrDefault(y => y.Id == x.ReinforcedId)! as IReinforcedModel, x.Count));
_shopReinforcedies = Reinforcedies.ToDictionary(recST => recST.ReinforcedId, recST => (recST.Reinforced as IReinforcedModel, recST.Count));
}
return _cachedReinforcedies;
return _shopReinforcedies;
}
}
public int Id { get; private set; }
[ForeignKey("ShopId")]
public virtual List<ShopReinforced> ShopReinforcedies { get; set; } = new();
public static Shop? Create(PrecastConcretePlantDataBase context, ShopBindingModel? model)
public virtual List<ShopReinforced> Reinforcedies { get; set; } = new();
public static Shop Create(PrecastConcretePlantDataBase context, ShopBindingModel model)
{
if (model == null)
{
return null;
}
return new Shop()
{
Id = model.Id,
Name = model.Name,
ShopName = model.ShopName,
Address = model.Address,
DateOpening = model.DateOpening,
ReinforcedMaxCount = model.ReinforcedMaxCount,
ShopReinforcedies = model.Reinforcedies.Select(x => new ShopReinforced
Reinforcedies = model.ShopReinforcedies.Select(x => new ShopReinforced
{
Reinforced = context.Reinforcedies.FirstOrDefault(y => y.Id == x.Key)!,
Count = x.Value.Item2,
Reinforced = context.Reinforcedies.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(ShopBindingModel? model)
public void Update(ShopBindingModel model)
{
if (model == null)
{
return;
}
Name = model.Name;
ShopName = model.ShopName;
Address = model.Address;
DateOpening = model.DateOpening;
ReinforcedMaxCount = model.ReinforcedMaxCount;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
ShopName = ShopName,
Address = Address,
Reinforcedies = Reinforcedies,
DateOpening = DateOpening,
ReinforcedMaxCount = ReinforcedMaxCount,
ShopReinforcedies = ShopReinforcedies
};
public void UpdateReinforcedies(PrecastConcretePlantDataBase context, ShopBindingModel model)
{
var shopReinforcedies = context.ShopReinforcedies
.Where(rec => rec.ShopId == model.Id)
.ToList();
var shopReinforcedies = context.ShopReinforcedies.Where(rec => rec.ShopId == model.Id).ToList();
if (shopReinforcedies != null && shopReinforcedies.Count > 0)
{
context.ShopReinforcedies
.RemoveRange(shopReinforcedies
.Where(rec => !model.Reinforcedies
.ContainsKey(rec.ReinforcedId)));
{
context.ShopReinforcedies.RemoveRange(shopReinforcedies.Where(rec => !model.ShopReinforcedies.ContainsKey(rec.ReinforcedId)));
context.SaveChanges();
foreach (var updateReinforced in shopReinforcedies.Where(x => model.Reinforcedies.ContainsKey(x.ReinforcedId)))
foreach (var updateReinforced in shopReinforcedies)
{
updateReinforced.Count = model.Reinforcedies[updateReinforced.ReinforcedId].Item2;
model.Reinforcedies.Remove(updateReinforced.ReinforcedId);
updateReinforced.Count = model.ShopReinforcedies[updateReinforced.ReinforcedId].Item2;
model.ShopReinforcedies.Remove(updateReinforced.ReinforcedId);
}
context.SaveChanges();
}
var shop = context.Shops.First(x => x.Id == model.Id);
shop.ShopReinforcedies.AddRange(model.Reinforcedies.Select(x => new ShopReinforced
var shop = context.Shops.First(x => x.Id == Id);
foreach (var st in model.ShopReinforcedies)
{
Reinforced = context.Reinforcedies.First(y => y.Id == x.Key),
Count = x.Value.Item2,
}).Except(shopReinforcedies ?? new()));
context.SaveChanges();
_cachedReinforcedies = null;
context.ShopReinforcedies.Add(new ShopReinforced
{
Shop = shop,
Reinforced = context.Reinforcedies.First(x => x.Id == st.Key),
Count = st.Value.Item2
});
context.SaveChanges();
}
_shopReinforcedies = null;
}
}
}

View File

@ -34,22 +34,22 @@ namespace PrecastConcretePlantFileImplement.Implements
public ShopViewModel? GetElement(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue)
{
return null;
}
return source.Shops.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.Name) && x.Name ==
model.Name) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
(!string.IsNullOrEmpty(model.ShopName) && x.ShopName ==
model.ShopName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.Name))
if (string.IsNullOrEmpty(model.ShopName))
{
return new();
}
return source.Shops.Where(x => x.Name.Contains(model.Name)).Select(x => x.GetViewModel).ToList();
return source.Shops.Where(x => x.ShopName.Contains(model.ShopName)).Select(x => x.GetViewModel).ToList();
}
public List<ShopViewModel> GetFullList()

View File

@ -12,14 +12,14 @@ namespace PrecastConcretePlantFileImplement.Models
{
public class Shop : IShopModel
{
public string Name { get; private set; } = string.Empty;
public string ShopName { get; private set; } = string.Empty;
public string Address { get; private set; } = string.Empty;
public DateTime DateOpening { get; private set; }
public Dictionary<int, int> Reinforcedies { get; private set; } = new();
public Dictionary<int, (IReinforcedModel, int)> _shopReinforcedies = null;
public Dictionary<int, (IReinforcedModel, int)> Reinforcedies
public Dictionary<int, (IReinforcedModel, int)> ShopReinforcedies
{
get
{
@ -45,11 +45,11 @@ namespace PrecastConcretePlantFileImplement.Models
return new Shop()
{
Id = model.Id,
Name = model.Name,
ShopName = model.ShopName,
Address = model.Address,
ReinforcedMaxCount = model.ReinforcedMaxCount,
DateOpening = model.DateOpening,
Reinforcedies = model.Reinforcedies.ToDictionary(x => x.Key, x => x.Value.Item2)
Reinforcedies = model.ShopReinforcedies.ToDictionary(x => x.Key, x => x.Value.Item2)
};
}
public static Shop? Create(XElement element)
@ -61,7 +61,7 @@ namespace PrecastConcretePlantFileImplement.Models
return new()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
Name = element.Element("Name")!.Value,
ShopName = element.Element("Name")!.Value,
Address = element.Element("Address")!.Value,
DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value),
ReinforcedMaxCount = Convert.ToInt32(element.Element("ReinforcedMaxCount")!.Value),
@ -76,25 +76,25 @@ namespace PrecastConcretePlantFileImplement.Models
{
return;
}
Name = model.Name;
ShopName = model.ShopName;
Address = model.Address;
DateOpening = model.DateOpening;
ReinforcedMaxCount = model.ReinforcedMaxCount;
Reinforcedies = model.Reinforcedies.ToDictionary(x => x.Key, x => x.Value.Item2);
Reinforcedies = model.ShopReinforcedies.ToDictionary(x => x.Key, x => x.Value.Item2);
_shopReinforcedies = null;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
ShopName = ShopName,
Address = Address,
Reinforcedies = Reinforcedies,
ShopReinforcedies = Reinforcedies,
DateOpening = DateOpening,
ReinforcedMaxCount = ReinforcedMaxCount,
};
public XElement GetXElement => new("Shop",
new XAttribute("Id", Id),
new XElement("Name", Name),
new XElement("Name", ShopName),
new XElement("Address", Address),
new XElement("DateOpening", DateOpening),
new XElement("ReinforcedMaxCount", ReinforcedMaxCount),

View File

@ -39,14 +39,14 @@ namespace PrecastConcretePlantListImplement.Implements
}
public ShopViewModel? GetElement(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue)
{
return null;
}
foreach (var shop in _source.Shops)
{
if ((!string.IsNullOrEmpty(model.Name) &&
shop.Name == model.Name) ||
if ((!string.IsNullOrEmpty(model.ShopName) &&
shop.ShopName == model.ShopName) ||
(model.Id.HasValue && shop.Id == model.Id))
{
return shop.GetViewModel;
@ -58,13 +58,13 @@ namespace PrecastConcretePlantListImplement.Implements
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{
var result = new List<ShopViewModel>();
if (string.IsNullOrEmpty(model.Name))
if (string.IsNullOrEmpty(model.ShopName))
{
return result;
}
foreach (var shop in _source.Shops)
{
if (shop.Name.Contains(model.Name ?? string.Empty))
if (shop.ShopName.Contains(model.ShopName ?? string.Empty))
{
result.Add(shop.GetViewModel);
}

View File

@ -11,13 +11,13 @@ namespace PrecastConcretePlantListImplement.Models
{
public class Shop : IShopModel
{
public string Name { get; private set; } = string.Empty;
public string ShopName { get; private set; } = string.Empty;
public string Address { get; private set; } = string.Empty;
public DateTime DateOpening { get; private set; }
public Dictionary<int, (IReinforcedModel, int)> Reinforcedies { get; private set; } = new();
public Dictionary<int, (IReinforcedModel, int)> ShopReinforcedies { get; private set; } = new();
public int Id { get; private set; }
@ -30,10 +30,10 @@ namespace PrecastConcretePlantListImplement.Models
return new Shop()
{
Id = model.Id,
Name = model.Name,
ShopName = model.ShopName,
Address = model.Address,
DateOpening = model.DateOpening,
Reinforcedies = new()
ShopReinforcedies = new()
};
}
public void Update(ShopBindingModel? model)
@ -42,17 +42,17 @@ namespace PrecastConcretePlantListImplement.Models
{
return;
}
Name = model.Name;
ShopName = model.ShopName;
Address = model.Address;
DateOpening = model.DateOpening;
Reinforcedies = model.Reinforcedies;
ShopReinforcedies = model.ShopReinforcedies;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
ShopName = ShopName,
Address = Address,
Reinforcedies = Reinforcedies,
ShopReinforcedies = ShopReinforcedies,
DateOpening = DateOpening,
};
public int ReinforcedMaxCount => throw new NotImplementedException();