Kashin M.I. Lab work 3 hard #8

Closed
Sosees04ka wants to merge 7 commits from LabWork03_hard into LabWork02_hard
29 changed files with 1681 additions and 51 deletions

View File

@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PrecastConcretePlantBusines
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PrecastConcretePlantListImplement", "PrecastConcretePlantListImplement\PrecastConcretePlantListImplement.csproj", "{0A99F76D-7163-4E8E-B109-70B811BACE94}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrecastConcretePlantFileImplement", "PrecastConcretePlantFileImplement\PrecastConcretePlantFileImplement.csproj", "{29844787-85D7-4BE5-B39E-EC9C98D200BB}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PrecastConcretePlantFileImplement", "PrecastConcretePlantFileImplement\PrecastConcretePlantFileImplement.csproj", "{29844787-85D7-4BE5-B39E-EC9C98D200BB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrecastConcretePlantDatabaseImplement", "PrecastConcretePlantDatabaseImplement\PrecastConcretePlantDatabaseImplement.csproj", "{080D0A98-0474-4FD7-8ABD-EDC294C8CF0D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -45,6 +47,10 @@ Global
{29844787-85D7-4BE5-B39E-EC9C98D200BB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{29844787-85D7-4BE5-B39E-EC9C98D200BB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{29844787-85D7-4BE5-B39E-EC9C98D200BB}.Release|Any CPU.Build.0 = Release|Any CPU
{080D0A98-0474-4FD7-8ABD-EDC294C8CF0D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{080D0A98-0474-4FD7-8ABD-EDC294C8CF0D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{080D0A98-0474-4FD7-8ABD-EDC294C8CF0D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{080D0A98-0474-4FD7-8ABD-EDC294C8CF0D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -1,4 +1,4 @@
using ConfectioneryView;

using Microsoft.Extensions.Logging;
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.BusinessLogicsContracts;

View File

@ -1,4 +1,4 @@
namespace ConfectioneryView
namespace PrecastConcretePlantView
{
partial class FormReinforcedShop
{

View File

@ -12,7 +12,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ConfectioneryView
namespace PrecastConcretePlantView
{
public partial class FormReinforcedShop : Form
{

View File

@ -31,49 +31,45 @@
this.ReinforcedLabel = new System.Windows.Forms.Label();
this.QuantityLabel = new System.Windows.Forms.Label();
this.ReindorcedСomboBox = new System.Windows.Forms.ComboBox();
this.QuantityTextBox = new System.Windows.Forms.TextBox();
this.SaveButton = new System.Windows.Forms.Button();
this.ButtonCancel = new System.Windows.Forms.Button();
this.numericUpDownCount = new System.Windows.Forms.NumericUpDown();
((System.ComponentModel.ISupportInitialize)(this.numericUpDownCount)).BeginInit();
this.SuspendLayout();
//
// ReinforcedLabel
//
this.ReinforcedLabel.AutoSize = true;
this.ReinforcedLabel.Location = new System.Drawing.Point(12, 18);
this.ReinforcedLabel.Location = new System.Drawing.Point(14, 24);
this.ReinforcedLabel.Name = "ReinforcedLabel";
this.ReinforcedLabel.Size = new System.Drawing.Size(56, 15);
this.ReinforcedLabel.Size = new System.Drawing.Size(71, 20);
this.ReinforcedLabel.TabIndex = 0;
this.ReinforcedLabel.Text = "Изделие:";
//
// QuantityLabel
//
this.QuantityLabel.AutoSize = true;
this.QuantityLabel.Location = new System.Drawing.Point(12, 51);
this.QuantityLabel.Location = new System.Drawing.Point(14, 68);
this.QuantityLabel.Name = "QuantityLabel";
this.QuantityLabel.Size = new System.Drawing.Size(75, 15);
this.QuantityLabel.Size = new System.Drawing.Size(93, 20);
this.QuantityLabel.TabIndex = 1;
this.QuantityLabel.Text = "Количество:";
//
// ReindorcedСomboBox
//
this.ReindorcedСomboBox.FormattingEnabled = true;
this.ReindorcedСomboBox.Location = new System.Drawing.Point(88, 15);
this.ReindorcedСomboBox.Location = new System.Drawing.Point(101, 20);
this.ReindorcedСomboBox.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.ReindorcedСomboBox.Name = "ReindorcedСomboBox";
this.ReindorcedСomboBox.Size = new System.Drawing.Size(184, 23);
this.ReindorcedСomboBox.Size = new System.Drawing.Size(210, 28);
this.ReindorcedСomboBox.TabIndex = 2;
//
// QuantityTextBox
//
this.QuantityTextBox.Location = new System.Drawing.Point(88, 48);
this.QuantityTextBox.Name = "QuantityTextBox";
this.QuantityTextBox.Size = new System.Drawing.Size(184, 23);
this.QuantityTextBox.TabIndex = 3;
//
// SaveButton
//
this.SaveButton.Location = new System.Drawing.Point(72, 100);
this.SaveButton.Location = new System.Drawing.Point(82, 133);
this.SaveButton.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.SaveButton.Name = "SaveButton";
this.SaveButton.Size = new System.Drawing.Size(97, 29);
this.SaveButton.Size = new System.Drawing.Size(111, 39);
this.SaveButton.TabIndex = 4;
this.SaveButton.Text = "Сохранить";
this.SaveButton.UseVisualStyleBackColor = true;
@ -81,27 +77,37 @@
//
// ButtonCancel
//
this.ButtonCancel.Location = new System.Drawing.Point(175, 100);
this.ButtonCancel.Location = new System.Drawing.Point(200, 133);
this.ButtonCancel.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.ButtonCancel.Name = "ButtonCancel";
this.ButtonCancel.Size = new System.Drawing.Size(97, 29);
this.ButtonCancel.Size = new System.Drawing.Size(111, 39);
this.ButtonCancel.TabIndex = 5;
this.ButtonCancel.Text = "Отмена";
this.ButtonCancel.UseVisualStyleBackColor = true;
this.ButtonCancel.Click += new System.EventHandler(this.ButtonCancel_Click);
//
// numericUpDownCount
//
this.numericUpDownCount.Location = new System.Drawing.Point(113, 68);
this.numericUpDownCount.Name = "numericUpDownCount";
this.numericUpDownCount.Size = new System.Drawing.Size(198, 27);
this.numericUpDownCount.TabIndex = 7;
//
// FormSellReinforced
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 141);
this.ClientSize = new System.Drawing.Size(325, 188);
this.Controls.Add(this.numericUpDownCount);
this.Controls.Add(this.ButtonCancel);
this.Controls.Add(this.SaveButton);
this.Controls.Add(this.QuantityTextBox);
this.Controls.Add(this.ReindorcedСomboBox);
this.Controls.Add(this.QuantityLabel);
this.Controls.Add(this.ReinforcedLabel);
this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.Name = "FormSellReinforced";
this.Text = "Продать Изделие";
((System.ComponentModel.ISupportInitialize)(this.numericUpDownCount)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
@ -112,8 +118,8 @@
private Label ReinforcedLabel;
private Label QuantityLabel;
private ComboBox ReindorcedСomboBox;
private TextBox QuantityTextBox;
private Button SaveButton;
private Button ButtonCancel;
private NumericUpDown numericUpDownCount;
}
}

View File

@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging;
using PrecastConcretePlantBusinessLogic.BusinessLogic;
using PrecastConcretePlantContracts.BusinessLogicsContracts;
using PrecastConcretePlantContracts.SearchModels;
@ -47,40 +48,42 @@ namespace PrecastConcretePlantView
private void SaveButton_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(QuantityTextBox.Text))
{
MessageBox.Show("Укажите количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (ReindorcedСomboBox.SelectedValue == null)
{
MessageBox.Show("Выберите изделие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Product sale.");
if (string.IsNullOrEmpty(numericUpDownCount.Text))
{
MessageBox.Show("Заполните количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Продажа изделий");
try
{
var operationResult = _logicShop.SellReinforced(_logicReinforced.ReadElement(new ReinforcedSearchModel()
var reinforced = _logicReinforced.ReadElement(new()
{
Id = Convert.ToInt32(ReindorcedСomboBox.SelectedValue)
})!, Convert.ToInt32(QuantityTextBox.Text));
Id = (int)ReindorcedСomboBox.SelectedValue
});
if (reinforced == null)
{
throw new Exception("Изделие не найдено. Дополнительная информация в логах.");
}
var operationResult = _logicShop.SellReinforced(
reinforced: reinforced,
quantity: (int)numericUpDownCount.Value
);
if (!operationResult)
{
throw new Exception("Ошибка при продаже изделия. Дополнительная информация в логах.");
throw new Exception("Ошибка при продаже изделий. Дополнительная информация в логах.");
}
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
_logger.LogError(ex, "Product sale error.");
_logger.LogError(ex, "Ошибка сохранения поездки");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

View File

@ -1,4 +1,4 @@
namespace ConfectioneryView
namespace PrecastConcretePlantView
{
partial class FormShop
{

View File

@ -16,7 +16,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ConfectioneryView
namespace PrecastConcretePlantView
{
public partial class FormShop : Form
{

View File

@ -1,4 +1,4 @@
namespace ConfectioneryView
namespace PrecastConcretePlantView
{
partial class FormShops
{

View File

@ -3,7 +3,7 @@ using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.BusinessLogicsContracts;
using PrecastConcretePlantView;
namespace ConfectioneryView
namespace PrecastConcretePlantView
{
public partial class FormShops : Form
{

View File

@ -9,6 +9,10 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.2" />
@ -38,6 +42,7 @@
<ItemGroup>
<ProjectReference Include="..\PrecastConcretePlantContracts\PrecastConcretePlantContracts.csproj" />
<ProjectReference Include="..\PrecastConcretePlantBusinessLogic\PrecastConcretePlantBusinessLogic.csproj" />
<ProjectReference Include="..\PrecastConcretePlantDatabaseImplement\PrecastConcretePlantDatabaseImplement.csproj" />
<ProjectReference Include="..\PrecastConcretePlantFileImplement\PrecastConcretePlantFileImplement.csproj" />
<ProjectReference Include="..\PrecastConcretePlantListImplement\PrecastConcretePlantListImplement.csproj" />
</ItemGroup>

View File

@ -5,8 +5,7 @@ using PrecastConcretePlantContracts.StoragesContracts;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using PrecastConcretePlantBusinessLogic.PrecastConcretePlantBusinessLogic.BusinessLogic;
using PrecastConcretePlantFileImplement.Implements;
using ConfectioneryView;
using PrecastConcretePlantDatabaseImplement.Implements;
namespace PrecastConcretePlantView
{

View File

@ -12,6 +12,7 @@ namespace PrecastConcretePlantContracts.BindingModels
public string Name { get; set; } = string.Empty;
public string Address { get; set; } = string.Empty;
public int ReinforcedMaxCount { get; set; }
public DateTime DateOpening { get; set; } = DateTime.Now;
@ -19,6 +20,5 @@ namespace PrecastConcretePlantContracts.BindingModels
public int Id { get; set; }
public int ReinforcedMaxCount { get; set; }
}
}

View File

@ -10,8 +10,9 @@ namespace PrecastConcretePlantDataModels.Models
{
string Name { get; }
string Address { get; }
public int ReinforcedMaxCount { get; }
DateTime DateOpening { get; }
Dictionary<int, (IReinforcedModel, int)> ShopReinforcedies { get; }
public int ReinforcedMaxCount { get; }
}
}

View File

@ -0,0 +1,97 @@
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.SearchModels;
using PrecastConcretePlantContracts.StoragesContracts;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrecastConcretePlantDatabaseImplement.Implements
{
public class ComponentStorage : IComponentStorage
{
public ComponentViewModel? Delete(ComponentBindingModel model)
{
using var context = new PrecastConcretePlantDataBase();
var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Components.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public ComponentViewModel? GetElement(ComponentSearchModel model)
{
if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue)
{
return null;
}
using var context = new PrecastConcretePlantDataBase();
return context.Components.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel model)
{
if (string.IsNullOrEmpty(model.ComponentName))
{
return new();
}
using var context = new PrecastConcretePlantDataBase();
return context.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList();
}
public List<ComponentViewModel> GetFullList()
{
using var context = new PrecastConcretePlantDataBase();
return context.Components.Select(x => x.GetViewModel).ToList();
}
public ComponentViewModel? Insert(ComponentBindingModel model)
{
var newComponent = Component.Create(model);
if (newComponent == null)
{
return null;
}
using var context = new PrecastConcretePlantDataBase();
context.Components.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public ComponentViewModel? Update(ComponentBindingModel model)
{
using var context = new PrecastConcretePlantDataBase();
var component = context.Components.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
}
}

View File

@ -0,0 +1,98 @@
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.SearchModels;
using PrecastConcretePlantContracts.StoragesContracts;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrecastConcretePlantDatabaseImplement.Implements
{
public class OrderStorage : IOrderStorage
{
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new PrecastConcretePlantDataBase();
var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Orders.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new PrecastConcretePlantDataBase();
return context.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new PrecastConcretePlantDataBase();
return context.Orders.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList();
}
public List<OrderViewModel> GetFullList()
{
using var context = new PrecastConcretePlantDataBase();
return context.Orders.Select(x => x.GetViewModel).ToList();
}
public OrderViewModel? Insert(OrderBindingModel model)
{
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
using var context = new PrecastConcretePlantDataBase();
context.Orders.Add(newOrder);
context.SaveChanges();
return newOrder.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new PrecastConcretePlantDataBase();
var order = context.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return order.GetViewModel;
}
}
}

View File

@ -0,0 +1,112 @@

using Microsoft.EntityFrameworkCore;
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.SearchModels;
using PrecastConcretePlantContracts.StoragesContracts;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrecastConcretePlantDatabaseImplement.Implements
{
public class ReinforcedStorage : IReinforcedStorage
{
public ReinforcedViewModel? Delete(ReinforcedBindingModel model)
{
using var context = new PrecastConcretePlantDataBase();
var element = context.Reinforcedies.Include(x => x.Components).FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Reinforcedies.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public ReinforcedViewModel? GetElement(ReinforcedSearchModel model)
{
if (string.IsNullOrEmpty(model.ReinforcedName) && !model.Id.HasValue)
{
return null;
}
using var context = new PrecastConcretePlantDataBase();
return context.Reinforcedies.Include(x => x.Components).ThenInclude(x => x.Component).FirstOrDefault(x => (!string.IsNullOrEmpty(model.ReinforcedName) && x.ReinforcedName == model.ReinforcedName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<ReinforcedViewModel> GetFilteredList(ReinforcedSearchModel model)
{
if (string.IsNullOrEmpty(model.ReinforcedName))
{
return new();
}
using var context = new PrecastConcretePlantDataBase();
return context.Reinforcedies.Include(x => x.Components).ThenInclude(x => x.Component).Where(x => x.ReinforcedName.Contains(model.ReinforcedName)).ToList().Select(x => x.GetViewModel).ToList();
}
public List<ReinforcedViewModel> GetFullList()
{
using var context = new PrecastConcretePlantDataBase();
return context.Reinforcedies.Include(x => x.Components).ThenInclude(x => x.Component).ToList().Select(x => x.GetViewModel).ToList();
}
public ReinforcedViewModel? Insert(ReinforcedBindingModel model)
{
using var context = new PrecastConcretePlantDataBase();
var newReinforced = Reinforced.Create(context, model);
if (newReinforced == null)
{
return null;
}
context.Reinforcedies.Add(newReinforced);
context.SaveChanges();
return newReinforced.GetViewModel;
}
public ReinforcedViewModel? Update(ReinforcedBindingModel model)
{
using var context = new PrecastConcretePlantDataBase();
using var transaction = context.Database.BeginTransaction();
try
{
var reinforced = context.Reinforcedies.FirstOrDefault(rec => rec.Id == model.Id);
if (reinforced == null)
{
return null;
}
reinforced.Update(model);
context.SaveChanges();
reinforced.UpdateComponents(context, model);
transaction.Commit();
return reinforced.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

View File

@ -0,0 +1,156 @@
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.Name))
{
return new();
}
using var context = new PrecastConcretePlantDataBase();
return context.Shops
.Include(x => x.Reinforcedies)
.ThenInclude(x => x.Reinforced)
.Where(x => x.Name.Contains(model.Name))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public ShopViewModel? GetElement(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.Name) && !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.Name) && x.Name == model.Name) ||
(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 SellReinforced(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;
}
}
}
}

View File

@ -0,0 +1,252 @@
// <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("20230409150055_FirstMigr")]
partial class FirstMigr
{
/// <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("Reinforcedies");
});
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.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Address")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("DateOpening")
.HasColumnType("datetime2");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("ReinforcedMaxCount")
.HasColumnType("int");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.ShopReinforced", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("ReinforcedId")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ReinforcedId");
b.HasIndex("ShopId");
b.ToTable("ShopReinforcedies");
});
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.ShopReinforced", b =>
{
b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Reinforced", "Reinforced")
.WithMany()
.HasForeignKey("ReinforcedId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Shop", "Shop")
.WithMany("Reinforcedies")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Reinforced");
b.Navigation("Shop");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Component", b =>
{
b.Navigation("ReinforcedComponents");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Reinforced", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Shop", b =>
{
b.Navigation("Reinforcedies");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,185 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace PrecastConcretePlantDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class FirstMigr : 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: "Reinforcedies",
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_Reinforcedies", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Shops",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Name = 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),
ReinforcedMaxCount = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Shops", 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_Reinforcedies_ReinforcedId",
column: x => x.ReinforcedId,
principalTable: "Reinforcedies",
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_Reinforcedies_ReinforcedId",
column: x => x.ReinforcedId,
principalTable: "Reinforcedies",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "ShopReinforcedies",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
ReinforcedId = table.Column<int>(type: "int", nullable: false),
ShopId = table.Column<int>(type: "int", nullable: false),
Count = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_ShopReinforcedies", x => x.Id);
table.ForeignKey(
name: "FK_ShopReinforcedies_Reinforcedies_ReinforcedId",
column: x => x.ReinforcedId,
principalTable: "Reinforcedies",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ShopReinforcedies_Shops_ShopId",
column: x => x.ShopId,
principalTable: "Shops",
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");
migrationBuilder.CreateIndex(
name: "IX_ShopReinforcedies_ReinforcedId",
table: "ShopReinforcedies",
column: "ReinforcedId");
migrationBuilder.CreateIndex(
name: "IX_ShopReinforcedies_ShopId",
table: "ShopReinforcedies",
column: "ShopId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Orders");
migrationBuilder.DropTable(
name: "ReinforcedComponents");
migrationBuilder.DropTable(
name: "ShopReinforcedies");
migrationBuilder.DropTable(
name: "Components");
migrationBuilder.DropTable(
name: "Reinforcedies");
migrationBuilder.DropTable(
name: "Shops");
}
}
}

View File

@ -0,0 +1,249 @@
// <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("Reinforcedies");
});
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.Shop", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Address")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("DateOpening")
.HasColumnType("datetime2");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("ReinforcedMaxCount")
.HasColumnType("int");
b.HasKey("Id");
b.ToTable("Shops");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.ShopReinforced", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("Count")
.HasColumnType("int");
b.Property<int>("ReinforcedId")
.HasColumnType("int");
b.Property<int>("ShopId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ReinforcedId");
b.HasIndex("ShopId");
b.ToTable("ShopReinforcedies");
});
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.ShopReinforced", b =>
{
b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Reinforced", "Reinforced")
.WithMany()
Review

Связь настроена не до конца

Связь настроена не до конца
.HasForeignKey("ReinforcedId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Shop", "Shop")
.WithMany("Reinforcedies")
.HasForeignKey("ShopId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Reinforced");
b.Navigation("Shop");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Component", b =>
{
b.Navigation("ReinforcedComponents");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Reinforced", b =>
{
b.Navigation("Components");
b.Navigation("Orders");
});
modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Shop", b =>
{
b.Navigation("Reinforcedies");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,65 @@
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrecastConcretePlantDatabaseImplement.Models
{
public class Component : IComponentModel
{
public int Id { get; private set; }
[Required]
public string ComponentName { get; private set; } = string.Empty;
[Required]
public double Cost { get; set; }
[ForeignKey("ComponentId")]
public virtual List<ReinforcedComponent> ReinforcedComponents { get; set; } = new();
public static Component? Create(ComponentBindingModel model)
{
if (model == null)
{
return null;
}
return new Component()
{
Id = model.Id,
ComponentName = model.ComponentName,
Cost = model.Cost
};
}
public static Component Create(ComponentViewModel model)
{
return new Component
{
Id = model.Id,
ComponentName = model.ComponentName,
Cost = model.Cost
};
}
public void Update(ComponentBindingModel model)
{
if (model == null)
{
return;
}
ComponentName = model.ComponentName;
Cost = model.Cost;
}
public ComponentViewModel GetViewModel => new()
{
Id = Id,
ComponentName = ComponentName,
Cost = Cost
};
}
}

View File

@ -0,0 +1,85 @@
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDataModels.Enums;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrecastConcretePlantDatabaseImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
public int ReinforcedId { get; private set; }
public string ReinforcedName { get; private set; } = string.Empty;
[Required]
public int Count { get; private set; }
[Required]
public double Sum { get; private set; }
[Required]
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
[Required]
public DateTime DateCreate { get; private set; } = DateTime.Now;
public DateTime? DateImplement { get; private set; }
public virtual Reinforced Reinforced { get; set; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
ReinforcedId = model.ReinforcedId,
ReinforcedName = model.ReinforcedName,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement
};
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
ReinforcedId = model.ReinforcedId;
ReinforcedName = model.ReinforcedName;
Count = model.Count;
Sum = model.Sum;
Status = model.Status;
DateCreate = model.DateCreate;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
ReinforcedId = ReinforcedId,
ReinforcedName = ReinforcedName,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement
};
}
}

View File

@ -0,0 +1,105 @@
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrecastConcretePlantDatabaseImplement.Models
{
public class Reinforced : IReinforcedModel
{
public int Id { get; set; }
[Required]
public string ReinforcedName { get; set; } = string.Empty;
[Required]
public double Price { get; set; }
private Dictionary<int, (IComponentModel, int)>? _reinforcedComponents = null;
[NotMapped]
public Dictionary<int, (IComponentModel, int)> ReinforcedComponents
{
get
{
if (_reinforcedComponents == null)
{
_reinforcedComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => (recPC.Component as IComponentModel, recPC.Count));
}
return _reinforcedComponents;
}
}
[ForeignKey("ReinforcedId")]
public virtual List<ReinforcedComponent> Components { get; set; } = new();
[ForeignKey("ReinforcedId")]
public virtual List<Order> Orders { get; set; } = new();
public static Reinforced Create(PrecastConcretePlantDataBase context, ReinforcedBindingModel model)
{
return new Reinforced()
{
Id = model.Id,
ReinforcedName = model.ReinforcedName,
Price = model.Price,
Components = model.ReinforcedComponents.Select(x => new ReinforcedComponent
{
Component = context.Components.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(ReinforcedBindingModel model)
{
ReinforcedName = model.ReinforcedName;
Price = model.Price;
}
public ReinforcedViewModel GetViewModel => new()
{
Id = Id,
ReinforcedName = ReinforcedName,
Price = Price,
ReinforcedComponents = ReinforcedComponents
};
public void UpdateComponents(PrecastConcretePlantDataBase context, ReinforcedBindingModel model)
{
var reinforcedComponents = context.ReinforcedComponents.Where(rec => rec.ReinforcedId == model.Id).ToList();
if (reinforcedComponents != null && reinforcedComponents.Count > 0)
{
context.ReinforcedComponents.RemoveRange(reinforcedComponents.Where(rec => !model.ReinforcedComponents.ContainsKey(rec.ComponentId)));
context.SaveChanges();
foreach (var updateComponent in reinforcedComponents)
{
updateComponent.Count = model.ReinforcedComponents[updateComponent.ComponentId].Item2;
model.ReinforcedComponents.Remove(updateComponent.ComponentId);
}
context.SaveChanges();
}
var reinforced = context.Reinforcedies.First(x => x.Id == Id);
foreach (var pc in model.ReinforcedComponents)
{
context.ReinforcedComponents.Add(new ReinforcedComponent
{
Reinforced = reinforced,
Component = context.Components.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
});
context.SaveChanges();
}
_reinforcedComponents = null;
}
}
}

View File

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrecastConcretePlantDatabaseImplement.Models
{
public class ReinforcedComponent
{
public int Id { get; set; }
[Required]
public int ReinforcedId { get; set; }
[Required]
public int ComponentId { get; set; }
[Required]
public int Count { get; set; }
public virtual Component Component { get; set; } = new();
public virtual Reinforced Reinforced { get; set; } = new();
}
}

View File

@ -0,0 +1,106 @@
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrecastConcretePlantDatabaseImplement.Models
{
public class Shop : IShopModel
{
public int Id { get; set; }
[Required]
public string Name { get; set; } = string.Empty;
[Required]
public string Address { get; set; } = string.Empty;
[Required]
public DateTime DateOpening { get; set; }
[Required]
public int ReinforcedMaxCount { get; set; }
private Dictionary<int, (IReinforcedModel, int)>? _shopReinforcedies = null;
[NotMapped]
public Dictionary<int, (IReinforcedModel, int)> ShopReinforcedies
{
get
{
if (_shopReinforcedies == null)
{
_shopReinforcedies = Reinforcedies.ToDictionary(recST => recST.ReinforcedId, recST => (recST.Reinforced as IReinforcedModel, recST.Count));
}
return _shopReinforcedies;
}
}
[ForeignKey("ShopId")]
public virtual List<ShopReinforced> Reinforcedies { get; set; } = new();
public static Shop Create(PrecastConcretePlantDataBase context, ShopBindingModel model)
{
return new Shop()
{
Id = model.Id,
Name = model.Name,
Address = model.Address,
DateOpening = model.DateOpening,
ReinforcedMaxCount = model.ReinforcedMaxCount,
Reinforcedies = model.ShopReinforcedies.Select(x => new ShopReinforced
{
Reinforced = context.Reinforcedies.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
public void Update(ShopBindingModel model)
{
Name = model.Name;
Address = model.Address;
DateOpening = model.DateOpening;
ReinforcedMaxCount = model.ReinforcedMaxCount;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Address = Address,
DateOpening = DateOpening,
ReinforcedMaxCount = ReinforcedMaxCount,
ShopReinforcedies = ShopReinforcedies
};
public void UpdateReinforcedies(PrecastConcretePlantDataBase context, ShopBindingModel model)
{
var shopReinforcedies = context.ShopReinforcedies.Where(rec => rec.ShopId == model.Id).ToList();
if (shopReinforcedies != null && shopReinforcedies.Count > 0)
{
context.ShopReinforcedies.RemoveRange(shopReinforcedies.Where(rec => !model.ShopReinforcedies.ContainsKey(rec.ReinforcedId)));
context.SaveChanges();
foreach (var updateReinforced in shopReinforcedies)
{
updateReinforced.Count = model.ShopReinforcedies[updateReinforced.ReinforcedId].Item2;
model.ShopReinforcedies.Remove(updateReinforced.ReinforcedId);
}
context.SaveChanges();
}
var shop = context.Shops.First(x => x.Id == Id);
foreach (var st in model.ShopReinforcedies)
{
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

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrecastConcretePlantDatabaseImplement.Models
{
public class ShopReinforced
{
public int Id { get; set; }
[Required]
public int ReinforcedId { get; set; }
[Required]
public int ShopId { get; set; }
[Required]
public int Count { get; set; }
public virtual Shop Shop { get; set; } = new();
public virtual Reinforced Reinforced { get; set; } = new();
}
}

View File

@ -0,0 +1,28 @@
using Microsoft.EntityFrameworkCore;
using PrecastConcretePlantDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrecastConcretePlantDatabaseImplement
{
public class PrecastConcretePlantDataBase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Data Source=MAKSIM\SQLEXPRESS;Initial Catalog=PrecastConcretePlantDataBaseFullHard;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Component> Components { set; get; }
public virtual DbSet<Reinforced> Reinforcedies { set; get; }
public virtual DbSet<ReinforcedComponent> ReinforcedComponents { set; get; }
public virtual DbSet<Order> Orders { set; get; }
public virtual DbSet<Shop> Shops { set; get; }
public virtual DbSet<ShopReinforced> ShopReinforcedies { set; get; }
}
}

View File

@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\PrecastConcretePlantContracts\PrecastConcretePlantContracts.csproj" />
<ProjectReference Include="..\PrecastConcretePlantDataModels\PrecastConcretePlantDataModels.csproj" />
</ItemGroup>
</Project>