лаба 2 почти полная
This commit is contained in:
parent
dbeb844986
commit
1e9c5a2ae1
@ -161,7 +161,11 @@ namespace Pizzeria
|
||||
|
||||
private void ButtonSell_Click(object sender, EventArgs e)
|
||||
{
|
||||
LoadData();
|
||||
var service = Program.ServiceProvider?.GetService(typeof(FormSellPizza));
|
||||
if (service is FormSellPizza form)
|
||||
{
|
||||
form.ShowDialog();
|
||||
}
|
||||
}
|
||||
|
||||
private void ButtonRestore_Click(object sender, EventArgs e)
|
||||
|
113
Pizzeria/Pizzeria/FormPizzaComponent.Designer.cs
generated
113
Pizzeria/Pizzeria/FormPizzaComponent.Designer.cs
generated
@ -28,84 +28,83 @@
|
||||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
this.buttonCancel = new System.Windows.Forms.Button();
|
||||
this.buttonSave = new System.Windows.Forms.Button();
|
||||
this.textBoxCount = new System.Windows.Forms.TextBox();
|
||||
this.label2 = new System.Windows.Forms.Label();
|
||||
this.label1 = new System.Windows.Forms.Label();
|
||||
this.comboBoxComponent = new System.Windows.Forms.ComboBox();
|
||||
this.SuspendLayout();
|
||||
buttonCancel = new Button();
|
||||
buttonSave = new Button();
|
||||
textBoxCount = new TextBox();
|
||||
label2 = new Label();
|
||||
label1 = new Label();
|
||||
comboBoxComponent = new ComboBox();
|
||||
SuspendLayout();
|
||||
//
|
||||
// buttonCancel
|
||||
//
|
||||
this.buttonCancel.Location = new System.Drawing.Point(249, 101);
|
||||
this.buttonCancel.Name = "buttonCancel";
|
||||
this.buttonCancel.Size = new System.Drawing.Size(94, 29);
|
||||
this.buttonCancel.TabIndex = 11;
|
||||
this.buttonCancel.Text = "Отмена";
|
||||
this.buttonCancel.UseVisualStyleBackColor = true;
|
||||
this.buttonCancel.Click += new System.EventHandler(this.ButtonCancel_Click);
|
||||
buttonCancel.Location = new Point(249, 101);
|
||||
buttonCancel.Name = "buttonCancel";
|
||||
buttonCancel.Size = new Size(94, 29);
|
||||
buttonCancel.TabIndex = 11;
|
||||
buttonCancel.Text = "Отмена";
|
||||
buttonCancel.UseVisualStyleBackColor = true;
|
||||
buttonCancel.Click += ButtonCancel_Click;
|
||||
//
|
||||
// buttonSave
|
||||
//
|
||||
this.buttonSave.Location = new System.Drawing.Point(135, 101);
|
||||
this.buttonSave.Name = "buttonSave";
|
||||
this.buttonSave.Size = new System.Drawing.Size(94, 29);
|
||||
this.buttonSave.TabIndex = 10;
|
||||
this.buttonSave.Text = "Сохранить";
|
||||
this.buttonSave.UseVisualStyleBackColor = true;
|
||||
this.buttonSave.Click += new System.EventHandler(this.ButtonSave_Click);
|
||||
buttonSave.Location = new Point(135, 101);
|
||||
buttonSave.Name = "buttonSave";
|
||||
buttonSave.Size = new Size(94, 29);
|
||||
buttonSave.TabIndex = 10;
|
||||
buttonSave.Text = "Сохранить";
|
||||
buttonSave.UseVisualStyleBackColor = true;
|
||||
buttonSave.Click += ButtonSave_Click;
|
||||
//
|
||||
// textBoxCount
|
||||
//
|
||||
this.textBoxCount.Location = new System.Drawing.Point(118, 59);
|
||||
this.textBoxCount.Name = "textBoxCount";
|
||||
this.textBoxCount.Size = new System.Drawing.Size(225, 27);
|
||||
this.textBoxCount.TabIndex = 9;
|
||||
textBoxCount.Location = new Point(118, 59);
|
||||
textBoxCount.Name = "textBoxCount";
|
||||
textBoxCount.Size = new Size(225, 27);
|
||||
textBoxCount.TabIndex = 9;
|
||||
//
|
||||
// label2
|
||||
//
|
||||
this.label2.AutoSize = true;
|
||||
this.label2.Location = new System.Drawing.Point(23, 66);
|
||||
this.label2.Name = "label2";
|
||||
this.label2.Size = new System.Drawing.Size(93, 20);
|
||||
this.label2.TabIndex = 7;
|
||||
this.label2.Text = "Количество:";
|
||||
label2.AutoSize = true;
|
||||
label2.Location = new Point(23, 66);
|
||||
label2.Name = "label2";
|
||||
label2.Size = new Size(93, 20);
|
||||
label2.TabIndex = 7;
|
||||
label2.Text = "Количество:";
|
||||
//
|
||||
// label1
|
||||
//
|
||||
this.label1.AutoSize = true;
|
||||
this.label1.Location = new System.Drawing.Point(23, 33);
|
||||
this.label1.Name = "label1";
|
||||
this.label1.Size = new System.Drawing.Size(91, 20);
|
||||
this.label1.TabIndex = 6;
|
||||
this.label1.Text = "Компонент:";
|
||||
label1.AutoSize = true;
|
||||
label1.Location = new Point(23, 33);
|
||||
label1.Name = "label1";
|
||||
label1.Size = new Size(91, 20);
|
||||
label1.TabIndex = 6;
|
||||
label1.Text = "Компонент:";
|
||||
//
|
||||
// comboBoxComponent
|
||||
//
|
||||
this.comboBoxComponent.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
|
||||
this.comboBoxComponent.FormattingEnabled = true;
|
||||
this.comboBoxComponent.Location = new System.Drawing.Point(120, 25);
|
||||
this.comboBoxComponent.Name = "comboBoxComponent";
|
||||
this.comboBoxComponent.Size = new System.Drawing.Size(224, 28);
|
||||
this.comboBoxComponent.TabIndex = 12;
|
||||
comboBoxComponent.DropDownStyle = ComboBoxStyle.DropDownList;
|
||||
comboBoxComponent.FormattingEnabled = true;
|
||||
comboBoxComponent.Location = new Point(120, 25);
|
||||
comboBoxComponent.Name = "comboBoxComponent";
|
||||
comboBoxComponent.Size = new Size(224, 28);
|
||||
comboBoxComponent.TabIndex = 12;
|
||||
//
|
||||
// FormPizzaComponent
|
||||
//
|
||||
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
|
||||
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
|
||||
this.ClientSize = new System.Drawing.Size(370, 153);
|
||||
this.Controls.Add(this.comboBoxComponent);
|
||||
this.Controls.Add(this.buttonCancel);
|
||||
this.Controls.Add(this.buttonSave);
|
||||
this.Controls.Add(this.textBoxCount);
|
||||
this.Controls.Add(this.label2);
|
||||
this.Controls.Add(this.label1);
|
||||
this.Name = "FormPizzaComponent";
|
||||
this.Text = "Компонент пиццы";
|
||||
this.ResumeLayout(false);
|
||||
this.PerformLayout();
|
||||
|
||||
AutoScaleDimensions = new SizeF(8F, 20F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(370, 153);
|
||||
Controls.Add(comboBoxComponent);
|
||||
Controls.Add(buttonCancel);
|
||||
Controls.Add(buttonSave);
|
||||
Controls.Add(textBoxCount);
|
||||
Controls.Add(label2);
|
||||
Controls.Add(label1);
|
||||
Name = "FormPizzaComponent";
|
||||
Text = "Компонент пиццы";
|
||||
ResumeLayout(false);
|
||||
PerformLayout();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
118
Pizzeria/Pizzeria/FormSellPizza.Designer.cs
generated
Normal file
118
Pizzeria/Pizzeria/FormSellPizza.Designer.cs
generated
Normal file
@ -0,0 +1,118 @@
|
||||
namespace Pizzeria
|
||||
{
|
||||
partial class FormSellPizza
|
||||
{
|
||||
/// <summary>
|
||||
/// Required designer variable.
|
||||
/// </summary>
|
||||
private System.ComponentModel.IContainer components = null;
|
||||
|
||||
/// <summary>
|
||||
/// Clean up any resources being used.
|
||||
/// </summary>
|
||||
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (disposing && (components != null))
|
||||
{
|
||||
components.Dispose();
|
||||
}
|
||||
base.Dispose(disposing);
|
||||
}
|
||||
|
||||
#region Windows Form Designer generated code
|
||||
|
||||
/// <summary>
|
||||
/// Required method for Designer support - do not modify
|
||||
/// the contents of this method with the code editor.
|
||||
/// </summary>
|
||||
private void InitializeComponent()
|
||||
{
|
||||
comboBoxPizza = new ComboBox();
|
||||
buttonCancel = new Button();
|
||||
buttonSell = new Button();
|
||||
textBoxCount = new TextBox();
|
||||
label2 = new Label();
|
||||
label1 = new Label();
|
||||
SuspendLayout();
|
||||
//
|
||||
// comboBoxPizza
|
||||
//
|
||||
comboBoxPizza.DropDownStyle = ComboBoxStyle.DropDownList;
|
||||
comboBoxPizza.FormattingEnabled = true;
|
||||
comboBoxPizza.Location = new Point(113, 25);
|
||||
comboBoxPizza.Name = "comboBoxPizza";
|
||||
comboBoxPizza.Size = new Size(224, 28);
|
||||
comboBoxPizza.TabIndex = 18;
|
||||
//
|
||||
// buttonCancel
|
||||
//
|
||||
buttonCancel.Location = new Point(242, 101);
|
||||
buttonCancel.Name = "buttonCancel";
|
||||
buttonCancel.Size = new Size(94, 29);
|
||||
buttonCancel.TabIndex = 17;
|
||||
buttonCancel.Text = "Отмена";
|
||||
buttonCancel.UseVisualStyleBackColor = true;
|
||||
//
|
||||
// buttonSell
|
||||
//
|
||||
buttonSell.Location = new Point(128, 101);
|
||||
buttonSell.Name = "buttonSell";
|
||||
buttonSell.Size = new Size(94, 29);
|
||||
buttonSell.TabIndex = 16;
|
||||
buttonSell.Text = "Продать";
|
||||
buttonSell.UseVisualStyleBackColor = true;
|
||||
buttonSell.Click += buttonSell_Click;
|
||||
//
|
||||
// textBoxCount
|
||||
//
|
||||
textBoxCount.Location = new Point(111, 59);
|
||||
textBoxCount.Name = "textBoxCount";
|
||||
textBoxCount.Size = new Size(225, 27);
|
||||
textBoxCount.TabIndex = 15;
|
||||
//
|
||||
// label2
|
||||
//
|
||||
label2.AutoSize = true;
|
||||
label2.Location = new Point(16, 66);
|
||||
label2.Name = "label2";
|
||||
label2.Size = new Size(93, 20);
|
||||
label2.TabIndex = 14;
|
||||
label2.Text = "Количество:";
|
||||
//
|
||||
// label1
|
||||
//
|
||||
label1.AutoSize = true;
|
||||
label1.Location = new Point(16, 33);
|
||||
label1.Name = "label1";
|
||||
label1.Size = new Size(55, 20);
|
||||
label1.TabIndex = 13;
|
||||
label1.Text = "Пицца";
|
||||
//
|
||||
// FormSellPizza
|
||||
//
|
||||
AutoScaleDimensions = new SizeF(8F, 20F);
|
||||
AutoScaleMode = AutoScaleMode.Font;
|
||||
ClientSize = new Size(352, 145);
|
||||
Controls.Add(comboBoxPizza);
|
||||
Controls.Add(buttonCancel);
|
||||
Controls.Add(buttonSell);
|
||||
Controls.Add(textBoxCount);
|
||||
Controls.Add(label2);
|
||||
Controls.Add(label1);
|
||||
Name = "FormSellPizza";
|
||||
Text = "Продажа";
|
||||
ResumeLayout(false);
|
||||
PerformLayout();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private ComboBox comboBoxPizza;
|
||||
private Button buttonCancel;
|
||||
private Button buttonSell;
|
||||
private TextBox textBoxCount;
|
||||
private Label label2;
|
||||
private Label label1;
|
||||
}
|
||||
}
|
93
Pizzeria/Pizzeria/FormSellPizza.cs
Normal file
93
Pizzeria/Pizzeria/FormSellPizza.cs
Normal file
@ -0,0 +1,93 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PizzeriaContracts.BusinessLogicsContracts;
|
||||
using PizzeriaContracts.ViewModels;
|
||||
using PizzeriaDataModels;
|
||||
|
||||
namespace Pizzeria
|
||||
{
|
||||
public partial class FormSellPizza : Form
|
||||
{
|
||||
private readonly List<PizzaViewModel>? _list;
|
||||
private readonly IShopLogic _shopLogic;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public int Id
|
||||
{
|
||||
get
|
||||
{
|
||||
return Convert.ToInt32(comboBoxPizza.SelectedValue);
|
||||
}
|
||||
set
|
||||
{
|
||||
comboBoxPizza.SelectedValue = value;
|
||||
}
|
||||
}
|
||||
public IPizzaModel? PizzaModel
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_list == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
foreach (var elem in _list)
|
||||
{
|
||||
if (elem.Id == Id)
|
||||
{
|
||||
return elem;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
public FormSellPizza(ILogger<FormSellPizza> logger, IPizzaLogic pizzaLogic, IShopLogic shopLogic)
|
||||
{
|
||||
InitializeComponent();
|
||||
_shopLogic = shopLogic;
|
||||
_logger = logger;
|
||||
_list = pizzaLogic.ReadList(null);
|
||||
if (_list != null)
|
||||
{
|
||||
comboBoxPizza.DisplayMember = "PizzaName";
|
||||
comboBoxPizza.ValueMember = "Id";
|
||||
comboBoxPizza.DataSource = _list;
|
||||
comboBoxPizza.SelectedItem = null;
|
||||
}
|
||||
_shopLogic = shopLogic;
|
||||
}
|
||||
|
||||
private void buttonSell_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (string.IsNullOrEmpty(textBoxCount.Text))
|
||||
{
|
||||
MessageBox.Show("Заполните поле Количество", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
return;
|
||||
}
|
||||
if (comboBoxPizza.SelectedValue == null)
|
||||
{
|
||||
MessageBox.Show("Выберите пиццу", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
_shopLogic.sellPizza(PizzaModel, Convert.ToInt32(textBoxCount.Text));
|
||||
MessageBox.Show("Продано успешно!");
|
||||
}
|
||||
catch (ArgumentException ex)
|
||||
{
|
||||
MessageBox.Show("Ошибка продажи. Слишком большое количество");
|
||||
_logger.LogWarning(ex, "Ошибка продажи");
|
||||
return;
|
||||
} catch(Exception err)
|
||||
{
|
||||
MessageBox.Show("Ошибка продажи.");
|
||||
_logger.LogError(err, "Ошибка продажи");
|
||||
return;
|
||||
}
|
||||
|
||||
DialogResult = DialogResult.OK;
|
||||
Close();
|
||||
}
|
||||
}
|
||||
}
|
60
Pizzeria/Pizzeria/FormSellPizza.resx
Normal file
60
Pizzeria/Pizzeria/FormSellPizza.resx
Normal file
@ -0,0 +1,60 @@
|
||||
<root>
|
||||
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
|
||||
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
|
||||
<xsd:element name="root" msdata:IsDataSet="true">
|
||||
<xsd:complexType>
|
||||
<xsd:choice maxOccurs="unbounded">
|
||||
<xsd:element name="metadata">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" use="required" type="xsd:string" />
|
||||
<xsd:attribute name="type" type="xsd:string" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="assembly">
|
||||
<xsd:complexType>
|
||||
<xsd:attribute name="alias" type="xsd:string" />
|
||||
<xsd:attribute name="name" type="xsd:string" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="data">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
|
||||
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
|
||||
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
|
||||
<xsd:attribute ref="xml:space" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
<xsd:element name="resheader">
|
||||
<xsd:complexType>
|
||||
<xsd:sequence>
|
||||
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
|
||||
</xsd:sequence>
|
||||
<xsd:attribute name="name" type="xsd:string" use="required" />
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:choice>
|
||||
</xsd:complexType>
|
||||
</xsd:element>
|
||||
</xsd:schema>
|
||||
<resheader name="resmimetype">
|
||||
<value>text/microsoft-resx</value>
|
||||
</resheader>
|
||||
<resheader name="version">
|
||||
<value>2.0</value>
|
||||
</resheader>
|
||||
<resheader name="reader">
|
||||
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
<resheader name="writer">
|
||||
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
|
||||
</resheader>
|
||||
</root>
|
@ -37,6 +37,7 @@ namespace Pizzeria
|
||||
textBoxName.Text = view.ShopName;
|
||||
textBoxAddres.Text = view.Addres.ToString();
|
||||
dateTimePicker.Text = view.OpenTime.ToString();
|
||||
textBoxCapacity.Text = view.Capacity.ToString();
|
||||
_shopPizzas = view.ShopPizzas ?? new Dictionary<int, (IPizzaModel, int)>();
|
||||
LoadData();
|
||||
}
|
||||
|
@ -53,6 +53,7 @@ namespace Pizzeria
|
||||
services.AddTransient<FormShopPizza>();
|
||||
services.AddTransient<FormShops>();
|
||||
services.AddTransient<FormPizzaComponent>();
|
||||
services.AddTransient<FormSellPizza>();
|
||||
}
|
||||
}
|
||||
}
|
@ -121,9 +121,17 @@ namespace PizzeriaBusinessLogic
|
||||
|
||||
if(model.ShopPizzas.TryGetValue(pizza.Id, out var pair))
|
||||
{
|
||||
if(model.Capacity - pair.Item2 < count)
|
||||
{
|
||||
throw new ArgumentException("Количество добавляемого изделия слишком велико");
|
||||
}
|
||||
model.ShopPizzas[pizza.Id] = (pair.Item1, pair.Item2 + count);
|
||||
} else
|
||||
{
|
||||
if (model.Capacity < count)
|
||||
{
|
||||
throw new ArgumentException("Количество добавляемого изделия слишком велико");
|
||||
}
|
||||
model.ShopPizzas.Add(pizza.Id, (pizza, count));
|
||||
}
|
||||
|
||||
@ -131,11 +139,24 @@ namespace PizzeriaBusinessLogic
|
||||
{
|
||||
Id = model.Id,
|
||||
ShopName = model.ShopName,
|
||||
OpenTime = model.OpenTime,
|
||||
Addres = model.Addres,
|
||||
ShopPizzas = model.ShopPizzas,
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool sellPizza(IPizzaModel pizza, int count)
|
||||
{
|
||||
bool result = _shopStorage.TrySellPizza(pizza, count);
|
||||
|
||||
if(!result)
|
||||
{
|
||||
throw new ArgumentException("Количество продаваемого изделия слишком велико");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,6 +10,7 @@ namespace PizzeriaContracts.BusinessLogicsContracts
|
||||
List<ShopViewModel>? ReadList(ShopSearchModel? model);
|
||||
ShopViewModel? ReadElement(ShopSearchModel model);
|
||||
bool AddPizza(ShopSearchModel shop, IPizzaModel pizza, int count);
|
||||
bool sellPizza(IPizzaModel pizza, int count);
|
||||
bool Create(ShopBindingModel model);
|
||||
bool Update(ShopBindingModel model);
|
||||
bool Delete(ShopBindingModel model);
|
||||
|
@ -1,6 +1,7 @@
|
||||
using PizzeriaContracts.BindingModels;
|
||||
using PizzeriaContracts.SearchModels;
|
||||
using PizzeriaContracts.ViewModels;
|
||||
using PizzeriaDataModels;
|
||||
|
||||
namespace PizzeriaContracts.StoragesContracts
|
||||
{
|
||||
@ -9,6 +10,7 @@ namespace PizzeriaContracts.StoragesContracts
|
||||
List<ShopViewModel> GetFullList();
|
||||
List<ShopViewModel> GetFilteredList(ShopSearchModel model);
|
||||
ShopViewModel? GetElement(ShopSearchModel model);
|
||||
bool TrySellPizza(IPizzaModel pizza, int count);
|
||||
ShopViewModel? Insert(ShopBindingModel model);
|
||||
ShopViewModel? Update(ShopBindingModel model);
|
||||
ShopViewModel? Delete(ShopBindingModel model);
|
||||
|
@ -10,6 +10,8 @@ namespace PizzeriaListImplement.Models
|
||||
public string ShopName { get; private set; } = string.Empty;
|
||||
public string Addres { get; private set; } = string.Empty;
|
||||
public DateTime OpenTime { get; private set; } = DateTime.Now;
|
||||
public int Capacity { get; private set; }
|
||||
|
||||
|
||||
public Dictionary<int, (IPizzaModel, int)> ShopPizzas
|
||||
{
|
||||
@ -29,6 +31,7 @@ namespace PizzeriaListImplement.Models
|
||||
ShopName = model.ShopName,
|
||||
Addres = model.Addres,
|
||||
OpenTime = model.OpenTime,
|
||||
Capacity = model.Capacity,
|
||||
ShopPizzas = model.ShopPizzas
|
||||
};
|
||||
}
|
||||
@ -49,8 +52,10 @@ namespace PizzeriaListImplement.Models
|
||||
ShopName = ShopName,
|
||||
Addres = Addres,
|
||||
OpenTime = OpenTime,
|
||||
Capacity = Capacity,
|
||||
ShopPizzas = ShopPizzas
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
using PizzeriaContracts.SearchModels;
|
||||
using PizzeriaContracts.StoragesContracts;
|
||||
using PizzeriaContracts.ViewModels;
|
||||
using PizzeriaDataModels;
|
||||
using PizzeriaFileImplement.models;
|
||||
|
||||
namespace PizzeriaFileImplement.Implements
|
||||
@ -41,6 +42,42 @@ namespace PizzeriaFileImplement.Implements
|
||||
(!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) ||
|
||||
(model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
||||
}
|
||||
|
||||
public bool TrySellPizza(IPizzaModel pizza, int count)
|
||||
{
|
||||
int hasCount = 0;
|
||||
|
||||
source.Shops.ForEach(x =>
|
||||
{
|
||||
if (x.ShopPizzas.TryGetValue(pizza.Id, out var pair))
|
||||
{
|
||||
hasCount += pair.Item2;
|
||||
}
|
||||
});
|
||||
|
||||
if (hasCount < count) return false;
|
||||
|
||||
source.Shops.ForEach(x =>
|
||||
{
|
||||
if (count < 0) return;
|
||||
if (x.ShopPizzas.TryGetValue(pizza.Id, out var pair))
|
||||
{
|
||||
if(count >= pair.Item2)
|
||||
{
|
||||
count -= pair.Item2;
|
||||
x.ShopPizzas[pizza.Id] = (pizza, 0);
|
||||
} else
|
||||
{
|
||||
x.ShopPizzas[pizza.Id] = (pizza, pair.Item2 - count);
|
||||
count = 0;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
source.SaveShops();
|
||||
return true;
|
||||
}
|
||||
|
||||
public ShopViewModel? Insert(ShopBindingModel model)
|
||||
{
|
||||
model.Id = source.Shops.Count > 0 ? source.Shops.Max(x => x.Id) + 1 : 1;
|
||||
|
@ -22,7 +22,7 @@ namespace PizzeriaFileImplement.models
|
||||
{
|
||||
var source = DataFileSingleton.GetInstance();
|
||||
_shopPizzas = Pizzas.ToDictionary(x => x.Key, y =>
|
||||
((source.Components.FirstOrDefault(z => z.Id == y.Key) as IPizzaModel)!,
|
||||
((source.Pizzas.FirstOrDefault(z => z.Id == y.Key) as IPizzaModel)!,
|
||||
y.Value));
|
||||
}
|
||||
return _shopPizzas;
|
||||
|
Loading…
Reference in New Issue
Block a user