diff --git a/PrecastConcretePlant/PrecastConcretePlant/FormShop.cs b/PrecastConcretePlant/PrecastConcretePlant/FormShop.cs index 05b91b4..5f8fdc0 100644 --- a/PrecastConcretePlant/PrecastConcretePlant/FormShop.cs +++ b/PrecastConcretePlant/PrecastConcretePlant/FormShop.cs @@ -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(); + _listShops = view.ShopReinforcedies ?? new Dictionary(); LoadData(); } } diff --git a/PrecastConcretePlant/PrecastConcretePlantBusinessLogic/BusinessLogic/ShopLogic.cs b/PrecastConcretePlant/PrecastConcretePlantBusinessLogic/BusinessLogic/ShopLogic.cs index c1d9bd5..58c1e0e 100644 --- a/PrecastConcretePlant/PrecastConcretePlantBusinessLogic/BusinessLogic/ShopLogic.cs +++ b/PrecastConcretePlant/PrecastConcretePlantBusinessLogic/BusinessLogic/ShopLogic.cs @@ -26,7 +26,7 @@ namespace PrecastConcretePlantBusinessLogic.BusinessLogic public List? 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) { diff --git a/PrecastConcretePlant/PrecastConcretePlantContracts/BindingModels/ShopBindingModel.cs b/PrecastConcretePlant/PrecastConcretePlantContracts/BindingModels/ShopBindingModel.cs index f1a1afb..854bf31 100644 --- a/PrecastConcretePlant/PrecastConcretePlantContracts/BindingModels/ShopBindingModel.cs +++ b/PrecastConcretePlant/PrecastConcretePlantContracts/BindingModels/ShopBindingModel.cs @@ -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 Reinforcedies { get; set; } = new(); + public Dictionary ShopReinforcedies { get; set; } = new(); public int Id { get; set; } diff --git a/PrecastConcretePlant/PrecastConcretePlantContracts/SearchModels/ShopSearchModel.cs b/PrecastConcretePlant/PrecastConcretePlantContracts/SearchModels/ShopSearchModel.cs index c08ab42..b46f4a9 100644 --- a/PrecastConcretePlant/PrecastConcretePlantContracts/SearchModels/ShopSearchModel.cs +++ b/PrecastConcretePlant/PrecastConcretePlantContracts/SearchModels/ShopSearchModel.cs @@ -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; } } } diff --git a/PrecastConcretePlant/PrecastConcretePlantContracts/ViewModels/ShopViewModel.cs b/PrecastConcretePlant/PrecastConcretePlantContracts/ViewModels/ShopViewModel.cs index 6505969..c079c2f 100644 --- a/PrecastConcretePlant/PrecastConcretePlantContracts/ViewModels/ShopViewModel.cs +++ b/PrecastConcretePlant/PrecastConcretePlantContracts/ViewModels/ShopViewModel.cs @@ -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 Reinforcedies { get; set; } = new(); + public Dictionary ShopReinforcedies { get; set; } = new(); public int Id { get; set; } diff --git a/PrecastConcretePlant/PrecastConcretePlantDataModels/Models/IShopModel.cs b/PrecastConcretePlant/PrecastConcretePlantDataModels/Models/IShopModel.cs index 0e723cf..fb03ab3 100644 --- a/PrecastConcretePlant/PrecastConcretePlantDataModels/Models/IShopModel.cs +++ b/PrecastConcretePlant/PrecastConcretePlantDataModels/Models/IShopModel.cs @@ -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 Reinforcedies { get; } + Dictionary ShopReinforcedies { get; } } } diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/ShopStorage.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..ede4607 --- /dev/null +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Implements/ShopStorage.cs @@ -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 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 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(); + } + } +} diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20230226175226_InitMigration.Designer.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20230226175226_InitMigration.Designer.cs deleted file mode 100644 index 20c2331..0000000 --- a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20230226175226_InitMigration.Designer.cs +++ /dev/null @@ -1,175 +0,0 @@ -// -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 - { - /// - 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("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("ComponentName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("Cost") - .HasColumnType("float"); - - b.HasKey("Id"); - - b.ToTable("Components"); - }); - - modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Order", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("Count") - .HasColumnType("int"); - - b.Property("DateCreate") - .HasColumnType("datetime2"); - - b.Property("DateImplement") - .HasColumnType("datetime2"); - - b.Property("ReinforcedId") - .HasColumnType("int"); - - b.Property("ReinforcedName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("Status") - .HasColumnType("int"); - - b.Property("Sum") - .HasColumnType("float"); - - b.HasKey("Id"); - - b.HasIndex("ReinforcedId"); - - b.ToTable("Orders"); - }); - - modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Reinforced", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("Price") - .HasColumnType("float"); - - b.Property("ReinforcedName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.HasKey("Id"); - - b.ToTable("Reinforceds"); - }); - - modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.ReinforcedComponent", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("ComponentId") - .HasColumnType("int"); - - b.Property("Count") - .HasColumnType("int"); - - b.Property("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 - } - } -} diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20230226175226_InitMigration.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20230226175226_InitMigration.cs deleted file mode 100644 index b82c188..0000000 --- a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/20230226175226_InitMigration.cs +++ /dev/null @@ -1,126 +0,0 @@ -using System; -using Microsoft.EntityFrameworkCore.Migrations; - -#nullable disable - -namespace PrecastConcretePlantDatabaseImplement.Migrations -{ - /// - public partial class InitMigration : Migration - { - /// - protected override void Up(MigrationBuilder migrationBuilder) - { - migrationBuilder.CreateTable( - name: "Components", - columns: table => new - { - Id = table.Column(type: "int", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - ComponentName = table.Column(type: "nvarchar(max)", nullable: false), - Cost = table.Column(type: "float", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Components", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "Reinforceds", - columns: table => new - { - Id = table.Column(type: "int", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - ReinforcedName = table.Column(type: "nvarchar(max)", nullable: false), - Price = table.Column(type: "float", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Reinforceds", x => x.Id); - }); - - migrationBuilder.CreateTable( - name: "Orders", - columns: table => new - { - Id = table.Column(type: "int", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - ReinforcedId = table.Column(type: "int", nullable: false), - ReinforcedName = table.Column(type: "nvarchar(max)", nullable: false), - Count = table.Column(type: "int", nullable: false), - Sum = table.Column(type: "float", nullable: false), - Status = table.Column(type: "int", nullable: false), - DateCreate = table.Column(type: "datetime2", nullable: false), - DateImplement = table.Column(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(type: "int", nullable: false) - .Annotation("SqlServer:Identity", "1, 1"), - ReinforcedId = table.Column(type: "int", nullable: false), - ComponentId = table.Column(type: "int", nullable: false), - Count = table.Column(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"); - } - - /// - protected override void Down(MigrationBuilder migrationBuilder) - { - migrationBuilder.DropTable( - name: "Orders"); - - migrationBuilder.DropTable( - name: "ReinforcedComponents"); - - migrationBuilder.DropTable( - name: "Components"); - - migrationBuilder.DropTable( - name: "Reinforceds"); - } - } -} diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/PrecastConcretePlantDataBaseModelSnapshot.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/PrecastConcretePlantDataBaseModelSnapshot.cs deleted file mode 100644 index 9f8ee2e..0000000 --- a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Migrations/PrecastConcretePlantDataBaseModelSnapshot.cs +++ /dev/null @@ -1,172 +0,0 @@ -// -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("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("ComponentName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("Cost") - .HasColumnType("float"); - - b.HasKey("Id"); - - b.ToTable("Components"); - }); - - modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Order", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("Count") - .HasColumnType("int"); - - b.Property("DateCreate") - .HasColumnType("datetime2"); - - b.Property("DateImplement") - .HasColumnType("datetime2"); - - b.Property("ReinforcedId") - .HasColumnType("int"); - - b.Property("ReinforcedName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.Property("Status") - .HasColumnType("int"); - - b.Property("Sum") - .HasColumnType("float"); - - b.HasKey("Id"); - - b.HasIndex("ReinforcedId"); - - b.ToTable("Orders"); - }); - - modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Reinforced", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("Price") - .HasColumnType("float"); - - b.Property("ReinforcedName") - .IsRequired() - .HasColumnType("nvarchar(max)"); - - b.HasKey("Id"); - - b.ToTable("Reinforceds"); - }); - - modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.ReinforcedComponent", b => - { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("int"); - - SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); - - b.Property("ComponentId") - .HasColumnType("int"); - - b.Property("Count") - .HasColumnType("int"); - - b.Property("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 - } - } -} diff --git a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Shop.cs b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Shop.cs index 08512ff..a551341 100644 --- a/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Shop.cs +++ b/PrecastConcretePlant/PrecastConcretePlantDatabaseImplement/Models/Shop.cs @@ -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? _shopReinforcedies = null; - private Dictionary? _cachedReinforcedies = null; [NotMapped] - public Dictionary Reinforcedies + public Dictionary 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 ShopReinforcedies { get; set; } = new(); - - public static Shop? Create(PrecastConcretePlantDataBase context, ShopBindingModel? model) + public virtual List 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; } } } diff --git a/PrecastConcretePlant/PrecastConcretePlantFileImplement/Implements/ShopStorage.cs b/PrecastConcretePlant/PrecastConcretePlantFileImplement/Implements/ShopStorage.cs index c3d76db..2a95924 100644 --- a/PrecastConcretePlant/PrecastConcretePlantFileImplement/Implements/ShopStorage.cs +++ b/PrecastConcretePlant/PrecastConcretePlantFileImplement/Implements/ShopStorage.cs @@ -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 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 GetFullList() diff --git a/PrecastConcretePlant/PrecastConcretePlantFileImplement/Models/Shop.cs b/PrecastConcretePlant/PrecastConcretePlantFileImplement/Models/Shop.cs index 4c07c94..b39ab4e 100644 --- a/PrecastConcretePlant/PrecastConcretePlantFileImplement/Models/Shop.cs +++ b/PrecastConcretePlant/PrecastConcretePlantFileImplement/Models/Shop.cs @@ -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 Reinforcedies { get; private set; } = new(); public Dictionary _shopReinforcedies = null; - public Dictionary Reinforcedies + public Dictionary 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), diff --git a/PrecastConcretePlant/PrecastConcretePlantListImplement/Implements/ShopStorage.cs b/PrecastConcretePlant/PrecastConcretePlantListImplement/Implements/ShopStorage.cs index 5a8cbb5..cdd8b15 100644 --- a/PrecastConcretePlant/PrecastConcretePlantListImplement/Implements/ShopStorage.cs +++ b/PrecastConcretePlant/PrecastConcretePlantListImplement/Implements/ShopStorage.cs @@ -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 GetFilteredList(ShopSearchModel model) { var result = new List(); - 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); } diff --git a/PrecastConcretePlant/PrecastConcretePlantListImplement/Models/Shop.cs b/PrecastConcretePlant/PrecastConcretePlantListImplement/Models/Shop.cs index 58b6d45..a1fb8e0 100644 --- a/PrecastConcretePlant/PrecastConcretePlantListImplement/Models/Shop.cs +++ b/PrecastConcretePlant/PrecastConcretePlantListImplement/Models/Shop.cs @@ -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 Reinforcedies { get; private set; } = new(); + public Dictionary 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();