PIbd-21. Raspaev N. I. HardLabWork02 #4

Closed
Nikolay-Raspaev wants to merge 15 commits from HardLabWork02 into HardLabWork01
30 changed files with 1165 additions and 10 deletions

View File

@ -9,9 +9,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FoodOrdersDataModels", "Foo
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FoodOrdersContracts", "FoodOrdersContracts\FoodOrdersContracts.csproj", "{5527E07B-9B94-4735-93FB-49267F0B8038}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FoodOrdersBusinessLogic", "FoodOrdersBusinessLogic\FoodOrdersBusinessLogic.csproj", "{4C44A99E-7D02-4D07-9714-9414423B560E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FoodOrdersBusinessLogic", "FoodOrdersBusinessLogic\FoodOrdersBusinessLogic.csproj", "{4C44A99E-7D02-4D07-9714-9414423B560E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FoodOrdersListImplement", "FoodOrdersListImplement\FoodOrdersListImplement.csproj", "{A4012AB3-40FB-4FF6-A55A-E593AB80A153}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FoodOrdersListImplement", "FoodOrdersListImplement\FoodOrdersListImplement.csproj", "{A4012AB3-40FB-4FF6-A55A-E593AB80A153}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FoodOrdersFileImplement", "FoodOrdersFileImplement\FoodOrdersFileImplement.csproj", "{E5B30B3F-30CF-4D4D-80E6-22A2EA20CC9C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -39,6 +41,10 @@ Global
{A4012AB3-40FB-4FF6-A55A-E593AB80A153}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A4012AB3-40FB-4FF6-A55A-E593AB80A153}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A4012AB3-40FB-4FF6-A55A-E593AB80A153}.Release|Any CPU.Build.0 = Release|Any CPU
{E5B30B3F-30CF-4D4D-80E6-22A2EA20CC9C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E5B30B3F-30CF-4D4D-80E6-22A2EA20CC9C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E5B30B3F-30CF-4D4D-80E6-22A2EA20CC9C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E5B30B3F-30CF-4D4D-80E6-22A2EA20CC9C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -27,6 +27,7 @@
<ItemGroup>
<ProjectReference Include="..\FoodOrdersBusinessLogic\FoodOrdersBusinessLogic.csproj" />
<ProjectReference Include="..\FoodOrdersContracts\FoodOrdersContracts.csproj" />
<ProjectReference Include="..\FoodOrdersFileImplement\FoodOrdersFileImplement.csproj" />
<ProjectReference Include="..\FoodOrdersListImplement\FoodOrdersListImplement.csproj" />
</ItemGroup>

View File

@ -45,9 +45,9 @@ namespace FoodOrdersView
try
{
int id = Convert.ToInt32(comboBoxDish.SelectedValue);
var product = _logicD.ReadElement(new DishSearchModel { Id = id });
var dish = _logicD.ReadElement(new DishSearchModel { Id = id });
int count = Convert.ToInt32(textBoxCount.Text);
textBoxSum.Text = Math.Round(count * (product?.Price ?? 0), 2).ToString();
textBoxSum.Text = Math.Round(count * (dish?.Price ?? 0), 2).ToString();
_logger.LogInformation("Расчет суммы заказа");
}
catch (Exception ex)

View File

@ -0,0 +1,120 @@
namespace FoodOrdersView
{
partial class FormSellDishes
{
/// <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()
{
buttonCancel = new Button();
buttonSell = new Button();
labelDish = new Label();
labelCount = new Label();
comboBoxDish = new ComboBox();
textBoxCount = new TextBox();
SuspendLayout();
//
// buttonCancel
//
buttonCancel.Location = new Point(383, 167);
buttonCancel.Name = "buttonCancel";
buttonCancel.Size = new Size(112, 34);
buttonCancel.TabIndex = 0;
buttonCancel.Text = "Отмена";
buttonCancel.UseVisualStyleBackColor = true;
buttonCancel.Click += ButtonCancel_Click;
//
// buttonSell
//
buttonSell.Location = new Point(265, 167);
buttonSell.Name = "buttonSell";
buttonSell.Size = new Size(112, 34);
buttonSell.TabIndex = 1;
buttonSell.Text = "Продать";
buttonSell.UseVisualStyleBackColor = true;
buttonSell.Click += ButtonSell_Click;
//
// labelDish
//
labelDish.AutoSize = true;
labelDish.Location = new Point(12, 37);
labelDish.Name = "labelDish";
labelDish.Size = new Size(118, 25);
labelDish.TabIndex = 2;
labelDish.Text = "Блюдо:";
//
// labelCount
//
labelCount.AutoSize = true;
labelCount.Location = new Point(12, 81);
labelCount.Name = "labelCount";
labelCount.Size = new Size(111, 25);
labelCount.TabIndex = 3;
labelCount.Text = "Количество:";
//
// comboBoxDish
//
comboBoxDish.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxDish.FormattingEnabled = true;
comboBoxDish.Location = new Point(129, 37);
comboBoxDish.Name = "comboBoxDish";
comboBoxDish.Size = new Size(318, 33);
comboBoxDish.TabIndex = 4;
//
// textBoxCount
//
textBoxCount.Location = new Point(129, 81);
textBoxCount.Name = "textBoxCount";
textBoxCount.Size = new Size(318, 31);
textBoxCount.TabIndex = 5;
//
// FormSellDishes
//
AutoScaleDimensions = new SizeF(10F, 25F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(507, 213);
Controls.Add(textBoxCount);
Controls.Add(comboBoxDish);
Controls.Add(labelCount);
Controls.Add(labelDish);
Controls.Add(buttonSell);
Controls.Add(buttonCancel);
Name = "FormSellDishes";
Text = "Продажа блюд";
Load += FormSellDishes_Load;
ResumeLayout(false);
PerformLayout();
}
#endregion
private Button buttonCancel;
private Button buttonSell;
private Label labelDish;
private Label labelCount;
private ComboBox comboBoxDish;
private TextBox textBoxCount;
}
}

View File

@ -0,0 +1,92 @@
using FoodOrdersContracts.BindingModels;
using FoodOrdersContracts.BusinessLogicsContracts;
using FoodOrdersContracts.SearchModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FoodOrdersView
{
public partial class FormSellDishes : Form
{
private readonly ILogger _logger;
private readonly IDishLogic _logicC;
private readonly IShopLogic _logicS;
public FormSellDishes(ILogger<FormCreateOrder> logger, IDishLogic logicC, IShopLogic logicS)
{
InitializeComponent();
_logger = logger;
_logicC = logicC;
_logicS = logicS;
}
private void FormSellDishes_Load(object sender, EventArgs e)
{
_logger.LogInformation("Загрузка блюд под продажу");
try
{
var list = _logicC.ReadList(null);
if (list != null)
{
comboBoxDish.DisplayMember = "DishName";
comboBoxDish.ValueMember = "Id";
comboBoxDish.DataSource = list;
comboBoxDish.SelectedItem = null;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки списка блюд");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonSell_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBoxCount.Text))
{
MessageBox.Show("Заполните поле 'Количество'", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (comboBoxDish.SelectedValue == null)
{
MessageBox.Show("Выберите блюдо", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Продажа блюд");
try
{
var operationResult = _logicS.SellDishes(_logicC.ReadElement
(new DishSearchModel { DishName = comboBoxDish.Text})!,
Convert.ToInt32(textBoxCount.Text));
if (!operationResult)
{
throw new Exception("Ошибка при продаже блюд. В магазинах недостаточно блюд.");
}
MessageBox.Show("Сохранение прошло успешно", "Сообщение",
MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка продажи блюд");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
}
}

View 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>

View File

@ -41,6 +41,8 @@
this.textBoxAddress = new System.Windows.Forms.TextBox();
this.labelDateOfOpening = new System.Windows.Forms.Label();
this.dateTimePicker = new System.Windows.Forms.DateTimePicker();
this.textBoxCapacity = new System.Windows.Forms.TextBox();
this.labelCapacity = new System.Windows.Forms.Label();
this.groupBox.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit();
this.SuspendLayout();
@ -157,11 +159,31 @@
this.dateTimePicker.Size = new System.Drawing.Size(200, 23);
this.dateTimePicker.TabIndex = 6;
//
// textBoxCapacity
//
this.textBoxCapacity.Location = new System.Drawing.Point(417, 57);
this.textBoxCapacity.Margin = new System.Windows.Forms.Padding(2);
this.textBoxCapacity.Name = "textBoxCapacity";
this.textBoxCapacity.Size = new System.Drawing.Size(200, 23);
this.textBoxCapacity.TabIndex = 10;
//
// labelCapacity
//
this.labelCapacity.AutoSize = true;
this.labelCapacity.Location = new System.Drawing.Point(324, 60);
this.labelCapacity.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
this.labelCapacity.Name = "labelCapacity";
this.labelCapacity.Size = new System.Drawing.Size(80, 15);
this.labelCapacity.TabIndex = 9;
this.labelCapacity.Text = "Вместимость";
//
// FormShop
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(639, 335);
this.Controls.Add(this.textBoxCapacity);
this.Controls.Add(this.labelCapacity);
this.Controls.Add(this.dateTimePicker);
this.Controls.Add(this.labelDateOfOpening);
this.Controls.Add(this.textBoxAddress);
@ -194,5 +216,7 @@
private TextBox textBoxAddress;
private Label labelDateOfOpening;
private DateTimePicker dateTimePicker;
private TextBox textBoxCapacity;
private Label labelCapacity;
}
}

View File

@ -35,6 +35,7 @@ namespace FoodOrdersView
textBoxName.Text = view.ShopName;
textBoxAddress.Text = view.Address;
dateTimePicker.Value = view.DateOfOpening;
textBoxCapacity.Text = view.Capacity.ToString();
_shopDishes = view.ShopDishes ?? new Dictionary<int, (IDishModel, int)>();
LoadData();
}
@ -81,6 +82,11 @@ namespace FoodOrdersView
MessageBoxIcon.Error);
return;
}
if (string.IsNullOrEmpty(textBoxCapacity.Text) || dataGridView.Rows.Cast<DataGridViewRow>().Sum(x => Convert.ToInt32(x.Cells[2].Value)) > Convert.ToInt32(textBoxCapacity.Text))
{
MessageBox.Show("Заполните вместимость корректно", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Сохранение магазина");
try
{
@ -90,7 +96,8 @@ namespace FoodOrdersView
ShopName = textBoxName.Text,
Address = textBoxAddress.Text,
DateOfOpening = dateTimePicker.Value.Date,
ShopDishes = _shopDishes
ShopDishes = _shopDishes,
Capacity = Convert.ToInt32(textBoxCapacity.Text)
};
var operationResult = _id.HasValue ? _logicS.Update(model) : _logicS.Create(model);
if (!operationResult)

View File

@ -33,6 +33,7 @@
this.buttonDelete = new System.Windows.Forms.Button();
this.buttonUpdate = new System.Windows.Forms.Button();
this.dataGridView = new System.Windows.Forms.DataGridView();
this.buttonSellDishes = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit();
this.SuspendLayout();
//
@ -85,11 +86,23 @@
this.dataGridView.Size = new System.Drawing.Size(404, 342);
this.dataGridView.TabIndex = 4;
//
// buttonSellDishes
//
this.buttonSellDishes.Location = new System.Drawing.Point(409, 264);
this.buttonSellDishes.Margin = new System.Windows.Forms.Padding(2);
this.buttonSellDishes.Name = "buttonSellDishes";
this.buttonSellDishes.Size = new System.Drawing.Size(133, 23);
this.buttonSellDishes.TabIndex = 6;
this.buttonSellDishes.Text = "Продажа блюд";
this.buttonSellDishes.UseVisualStyleBackColor = true;
this.buttonSellDishes.Click += new System.EventHandler(this.ButtonSellDishes_Click);
//
// FormShops
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(554, 342);
this.Controls.Add(this.buttonSellDishes);
this.Controls.Add(this.dataGridView);
this.Controls.Add(this.buttonUpdate);
this.Controls.Add(this.buttonDelete);
@ -110,5 +123,6 @@
private Button buttonDelete;
private Button buttonUpdate;
private DataGridView dataGridView;
private Button buttonSellDishes;
}
}

View File

@ -95,5 +95,16 @@ namespace FoodOrdersView
}
}
}
private void ButtonSellDishes_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormSellDishes));
if (service is FormSellDishes form)
{
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
}
}
}

View File

@ -3,7 +3,7 @@ using FoodOrdersContracts.StoragesContracts;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using FoodOrdersListImplement.Implements;
using FoodOrdersFileImplement.Implements;
using FoodOrdersBusinessLogic.BusinessLogics;
namespace FoodOrdersView
@ -54,6 +54,7 @@ namespace FoodOrdersView
services.AddTransient<FormShops>();
services.AddTransient<FormShop>();
services.AddTransient<FormDeliveryDishes>();
services.AddTransient<FormSellDishes>();
}
}
}

View File

@ -12,10 +12,14 @@ namespace FoodOrdersBusinessLogic.BusinessLogics
{
private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
private readonly IShopLogic _logicS;
private readonly IDishStorage _dishStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopLogic logicS, IDishStorage dishStorage)
{
_logger = logger;
_orderStorage = orderStorage;
_logicS = logicS;
_dishStorage = dishStorage;
}
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
@ -110,6 +114,10 @@ namespace FoodOrdersBusinessLogic.BusinessLogics
{
model.DateImplement = viewModel.DateImplement;
}
if (model.Status == OrderStatus.Выдан && !_logicS.AddDishes(_dishStorage.GetElement(new DishSearchModel { Id = viewModel.DishId })!, viewModel.Count))
{
throw new Exception("В магазинах нет места под блюда из заказа.");
}
CheckModel(model, false);
if (_orderStorage.Update(model) == null)
{

View File

@ -60,6 +60,10 @@ namespace FoodOrdersBusinessLogic.BusinessLogics
_logger.LogWarning("Shop not found");
return false;
}
if (element.Capacity - element.ShopDishes.Sum(x => x.Value.Item2) < count)
{
throw new InvalidOperationException("В магазине нет места под такое количество блюд");
}
if (element.ShopDishes.ContainsKey(dish.Id))
{
element.ShopDishes[dish.Id] = (dish, element.ShopDishes[dish.Id].Item2 + count);
@ -74,7 +78,8 @@ namespace FoodOrdersBusinessLogic.BusinessLogics
ShopName = element.ShopName,
Address = element.Address,
DateOfOpening = element.DateOfOpening,
ShopDishes = element.ShopDishes
ShopDishes = element.ShopDishes,
Capacity = element.Capacity
});
return true;
}
@ -144,5 +149,36 @@ namespace FoodOrdersBusinessLogic.BusinessLogics
throw new InvalidOperationException("Магазин с таким названием уже есть");
}
}
public bool SellDishes(IDishModel dish, int count)
{
return _shopStorage.SellDishes(dish, count);
}
public bool AddDishes(IDishModel dish, int count)
{
if (dish == null || count < 1)
{
return false;
}
List<ShopViewModel> shopsList = _shopStorage.GetFullList();
if (shopsList.Sum(x => x.Capacity) - shopsList.Sum(x => x.ShopDishes.Sum(y => y.Value.Item2)) < count)
{
return false;
}
foreach (ShopViewModel shop in shopsList)
{
int emptySpace = shop.Capacity - shop.ShopDishes.Sum(x => x.Value.Item2);
if (emptySpace < count)
{
DeliveryDishes(new ShopSearchModel { Id = shop.Id }, dish, emptySpace);
count -= emptySpace;
}
else
{
DeliveryDishes(new ShopSearchModel { Id = shop.Id }, dish, count);
break;
}
}
return true;
}
}
}

View File

@ -9,5 +9,6 @@ namespace FoodOrdersContracts.BindingModels
public string Address { get; set; } = string.Empty;
public DateTime DateOfOpening { get; set; } = DateTime.Now;
public Dictionary<int, (IDishModel, int)> ShopDishes { get; set; } = new();
public int Capacity { get; set; }
}
}

View File

@ -13,5 +13,7 @@ namespace FoodOrdersContracts.BusinessLogicsContracts
bool Update(ShopBindingModel model);
bool Delete(ShopBindingModel model);
bool DeliveryDishes(ShopSearchModel model, IDishModel dish, int count);
bool SellDishes(IDishModel dish, int count);
bool AddDishes(IDishModel dish, int count);
}
}

View File

@ -1,6 +1,7 @@
using FoodOrdersContracts.BindingModels;
using FoodOrdersContracts.SearchModels;
using FoodOrdersContracts.ViewModels;
using FoodOrdersDataModels.Models;
namespace FoodOrdersContracts.StoragesContracts
{
@ -12,5 +13,6 @@ namespace FoodOrdersContracts.StoragesContracts
ShopViewModel? Insert(ShopBindingModel model);
ShopViewModel? Update(ShopBindingModel model);
ShopViewModel? Delete(ShopBindingModel model);
bool SellDishes(IDishModel dish, int count);
}
}

View File

@ -12,6 +12,8 @@ namespace FoodOrdersContracts.ViewModels
public string Address { get; set; } = string.Empty;
[DisplayName("Дата открытия")]
public DateTime DateOfOpening { get; set; } = DateTime.Now;
[DisplayName("Вместимость")]
public int Capacity { get; set; } = 0;
public Dictionary<int, (IDishModel, int)> ShopDishes { get; set; } = new();
}
}

View File

@ -6,5 +6,6 @@
string Address { get; }
DateTime DateOfOpening { get; }
Dictionary<int, (IDishModel, int)> ShopDishes { get; }
int Capacity { get; }
}
}

View File

@ -0,0 +1,52 @@
using FoodOrdersFileImplement.Models;
using System.Xml.Linq;
namespace FoodOrdersFileImplement
{
public class DataFileSingleton
{
private static DataFileSingleton? instance;
private readonly string ComponentFileName = "Component.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string DishFileName = "Dish.xml";
private readonly string ShopFileName = "Shop.xml";
public List<Component> Components { get; private set; }
public List<Order> Orders { get; private set; }
public List<Dish> Dishes { get; private set; }
public List<Shop> Shops { get; private set; }
public static DataFileSingleton GetInstance()
{
if (instance == null)
{
instance = new DataFileSingleton();
}
return instance;
}
public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement);
public void SaveDishes() => SaveData(Dishes, DishFileName, "Dishes", x => x.GetXElement);
public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement);
public void SaveShops() => SaveData(Shops, ShopFileName, "Shops", x => x.GetXElement);
private DataFileSingleton()
{
Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!;
Dishes = LoadData(DishFileName, "Dish", x => Dish.Create(x)!)!;
Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!;
Shops = LoadData(ShopFileName, "Shop", x => Shop.Create(x)!)!;
}
private static List<T>? LoadData<T>(string filename, string xmlNodeName, Func<XElement, T> selectFunction)
{
if (File.Exists(filename))
{
return XDocument.Load(filename)?.Root?.Elements(xmlNodeName)?.Select(selectFunction)?.ToList();
}
return new List<T>();
}
private static void SaveData<T>(List<T> data, string filename, string xmlNodeName, Func<T, XElement> selectFunction)
{
if (data != null)
{
new XDocument(new XElement(xmlNodeName, data.Select(selectFunction).ToArray())).Save(filename);
}
}
}
}

View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\FoodOrdersContracts\FoodOrdersContracts.csproj" />
<ProjectReference Include="..\FoodOrdersDataModels\FoodOrdersDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,74 @@
using FoodOrdersContracts.BindingModels;
using FoodOrdersContracts.SearchModels;
using FoodOrdersContracts.StoragesContracts;
using FoodOrdersContracts.ViewModels;
using FoodOrdersFileImplement.Models;
using FoodOrdersFileImplement;
namespace FoodOrdersFileImplement.Implements
{
public class ComponentStorage : IComponentStorage
{
private readonly DataFileSingleton _source;
public ComponentStorage()
{
_source = DataFileSingleton.GetInstance();
}
public List<ComponentViewModel> GetFullList()
{
return _source.Components.Select(x => x.GetViewModel).ToList();
}
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel model)
{
if (string.IsNullOrEmpty(model.ComponentName))
{
return new();
}
return _source.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList();
}
//FirstOrDefault выбирается первый или ничего, то есть вернёт первое совпадение или null
public ComponentViewModel? GetElement(ComponentSearchModel model)
{
if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue)
{
return null;
}
return _source.Components.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public ComponentViewModel? Insert(ComponentBindingModel model)
{
model.Id = _source.Components.Count > 0 ? _source.Components.Max(x => x.Id) + 1 : 1;
var newComponent = Component.Create(model);
if (newComponent == null)
{
return null;
}
_source.Components.Add(newComponent);
_source.SaveComponents();
return newComponent.GetViewModel;
}
public ComponentViewModel? Update(ComponentBindingModel model)
{
var component = _source.Components.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
component.Update(model);
_source.SaveComponents();
return component.GetViewModel;
}
public ComponentViewModel? Delete(ComponentBindingModel model)
{
var element = _source.Components.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
_source.Components.Remove(element);
_source.SaveComponents();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,74 @@
using FoodOrdersContracts.BindingModels;
using FoodOrdersContracts.SearchModels;
using FoodOrdersContracts.StoragesContracts;
using FoodOrdersContracts.ViewModels;
using FoodOrdersFileImplement.Models;
using System.Reflection.Metadata;
namespace FoodOrdersFileImplement.Implements
{
public class DishStorage : IDishStorage
{
private readonly DataFileSingleton _source;
public DishStorage()
{
_source = DataFileSingleton.GetInstance();
}
public List<DishViewModel> GetFullList()
{
return _source.Dishes.Select(x => x.GetViewModel).ToList();
}
public List<DishViewModel> GetFilteredList(DishSearchModel model)
{
if (string.IsNullOrEmpty(model.DishName))
{
return new();
}
return _source.Dishes.Where(x => x.DishName.Contains(model.DishName)).Select(x => x.GetViewModel).ToList();
}
public DishViewModel? GetElement(DishSearchModel model)
{
if (string.IsNullOrEmpty(model.DishName) && !model.Id.HasValue)
{
return null;
}
return _source.Dishes.FirstOrDefault(x => (!string.IsNullOrEmpty(model.DishName) && x.DishName == model.DishName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public DishViewModel? Insert(DishBindingModel model)
{
model.Id = _source.Dishes.Count > 0 ? _source.Dishes.Max(x => x.Id) + 1 : 1;
var newDoc = Dish.Create(model);
if (newDoc == null)
{
return null;
}
_source.Dishes.Add(newDoc);
_source.SaveDishes();
return newDoc.GetViewModel;
}
public DishViewModel? Update(DishBindingModel model)
{
var dish = _source.Dishes.FirstOrDefault(x => x.Id == model.Id);
if (dish == null)
{
return null;
}
dish.Update(model);
_source.SaveDishes();
return dish.GetViewModel;
}
public DishViewModel? Delete(DishBindingModel model)
{
var document = _source.Dishes.FirstOrDefault(x => x.Id == model.Id);
if (document == null)
{
return null;
}
document.Update(model);
_source.SaveDishes();
return document.GetViewModel;
}
}
}

View File

@ -0,0 +1,84 @@
using FoodOrdersContracts.BindingModels;
using FoodOrdersContracts.SearchModels;
using FoodOrdersContracts.StoragesContracts;
using FoodOrdersContracts.ViewModels;
using FoodOrdersFileImplement.Models;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
namespace FoodOrdersFileImplement.Implements
{
public class OrderStorage : IOrderStorage
{
private readonly DataFileSingleton _source;
public OrderStorage()
{
_source = DataFileSingleton.GetInstance();
}
public List<OrderViewModel> GetFullList()
{
return _source.Orders.Select(x => GetViewModel(x)).ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
return _source.Orders.Where(x => x.Id == model.Id).Select(x => GetViewModel(x)).ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
return _source.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
private OrderViewModel GetViewModel(Order order)
{
var viewModel = order.GetViewModel;
viewModel.DishName = _source.Dishes.FirstOrDefault(x => (x.Id == order.DishId))?.DishName;
return viewModel;
}
public OrderViewModel? Delete(OrderBindingModel model)
{
var order = _source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
_source.SaveOrders();
return order.GetViewModel;
}
public OrderViewModel? Insert(OrderBindingModel model)
{
model.Id = _source.Orders.Count > 0 ? _source.Orders.Max(x => x.Id) + 1 : 1;
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
_source.Orders.Add(newOrder);
_source.SaveOrders();
return newOrder.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
var order = _source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
_source.SaveOrders();
return order.GetViewModel;
}
}
}

View File

@ -0,0 +1,123 @@
using FoodOrdersContracts.BindingModels;
using FoodOrdersContracts.SearchModels;
using FoodOrdersContracts.StoragesContracts;
using FoodOrdersContracts.ViewModels;
using FoodOrdersDataModels.Models;
using FoodOrdersFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FoodOrdersFileImplement.Implements
{
public class ShopStorage : IShopStorage
{
private readonly DataFileSingleton source;
public ShopStorage()
{
source = DataFileSingleton.GetInstance();
}
public ShopViewModel? Delete(ShopBindingModel model)
{
var element = source.Shops.FirstOrDefault(x => x.Id == model.Id);
if (element != null)
{
source.Shops.Remove(element);
source.SaveShops();
return element.GetViewModel;
}
return null;
}
public ShopViewModel? GetElement(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue)
{
return null;
}
return source.Shops
.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<ShopViewModel> GetFilteredList(ShopSearchModel model)
{
if (string.IsNullOrEmpty(model.ShopName))
{
return new();
}
return source.Shops
.Where(x => x.ShopName.Contains(model.ShopName))
.Select(x => x.GetViewModel)
.ToList();
}
public List<ShopViewModel> GetFullList()
{
return source.Shops.Select(x => x.GetViewModel).ToList();
}
public ShopViewModel? Insert(ShopBindingModel model)
{
model.Id = source.Shops.Count > 0 ? source.Shops.Max(x =>
x.Id) + 1 : 1;
var newShop = Shop.Create(model);
if (newShop == null)
{
return null;
}
source.Shops.Add(newShop);
source.SaveShops();
return newShop.GetViewModel;
}
public ShopViewModel? Update(ShopBindingModel model)
{
var shop = source.Shops.FirstOrDefault(x => x.Id ==
model.Id);
if (shop == null)
{
return null;
}
shop.Update(model);
source.SaveShops();
return shop.GetViewModel;
}
public bool SellDishes(IDishModel dish, int count)
{
if (dish == null || count < 1) return false;
List<Shop> shopsWithDish = source.Shops.Where(x => x.ShopDishes.ContainsKey(dish.Id)).ToList();
if (shopsWithDish.Sum(x => x.ShopDishes[dish.Id].Item2) < count)
{
return false;
}
foreach(var shop in shopsWithDish)
{
int dishInShopCount = shop.ShopDishes[dish.Id].Item2;
if(count - dishInShopCount >= 0)
{
count -= dishInShopCount;
shop.ShopDishes.Remove(dish.Id);
}
else
{
shop.ShopDishes[dish.Id] = (dish, shop.ShopDishes[dish.Id].Item2 - count);
count = 0;
}
Update(new ShopBindingModel
{
Id = shop.Id,
ShopName = shop.ShopName,
Address = shop.Address,
DateOfOpening = shop.DateOfOpening,
ShopDishes = shop.ShopDishes,
Capacity = shop.Capacity
});
if (count == 0) break;
}
return true;
}
}
}

View File

@ -0,0 +1,60 @@
using FoodOrdersContracts.BindingModels;
using FoodOrdersContracts.ViewModels;
using FoodOrdersDataModels.Models;
using System.Xml.Linq;
namespace FoodOrdersFileImplement.Models
{
public class Component : IComponentModel
{
public int Id { get; private set; }
public string ComponentName { get; private set; } = string.Empty;
public double Cost { get; set; }
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(XElement element)
{
if (element == null)
{
return null;
}
return new Component()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
ComponentName = element.Element("ComponentName")!.Value,
Cost = Convert.ToDouble(element.Element("Cost")!.Value)
};
}
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
};
public XElement GetXElement => new(
"Component",
new XAttribute("Id", Id),
new XElement("ComponentName", ComponentName),
new XElement("Cost", Cost.ToString())
);
}
}

View File

@ -0,0 +1,94 @@
using FoodOrdersContracts.BindingModels;
using FoodOrdersContracts.ViewModels;
using FoodOrdersDataModels.Models;
using FoodOrdersFileImplement;
using System.Xml.Linq;
namespace FoodOrdersFileImplement.Models
{
public class Dish : IDishModel
{
public int Id { get; private set; }
public string DishName { get; private set; } = string.Empty;
public double Price { get; private set; }
//словарь для файла, так как нам в файле нужно хранить просто id компонента и его количество
public Dictionary<int, int> Components { get; private set; } = new();
private Dictionary<int, (IComponentModel, int)>? _dishComponents = null;
public Dictionary<int, (IComponentModel, int)> DishComponents
{
get
{
if (_dishComponents == null)
{
var _source = DataFileSingleton.GetInstance();
_dishComponents = Components.ToDictionary(x => x.Key, y => ((_source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value));
}
return _dishComponents;
}
}
public static Dish? Create(DishBindingModel model)
{
if (model == null)
{
return null;
}
return new Dish()
{
Id = model.Id,
DishName = model.DishName,
Price = model.Price,
Components = model.DishComponents.ToDictionary(x => x.Key, x => x.Value.Item2)
};
}
public static Dish? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Dish()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
DishName = element.Element("DishName")!.Value,
Price = Convert.ToDouble(element.Element("Price")!.Value),
Components = element.Element("DishComponents")!.Elements("DishComponent").ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value))
};
}
public void Update(DishBindingModel model)
{
if (model == null)
{
return;
}
DishName = model.DishName;
Price = model.Price;
Components = model.DishComponents.ToDictionary(x => x.Key, x => x.Value.Item2);
//обнуляем словарь, чтобы в случае обновления, у нас был в дальнейшем сформирован актуальный словарь
// с помощью get метода
_dishComponents = null;
}
public DishViewModel GetViewModel => new()
{
Id = Id,
DishName = DishName,
Price = Price,
DishComponents = DishComponents
};
public XElement GetXElement => new(
"Dish",
new XAttribute("Id", Id),
new XElement("DishName", DishName),
new XElement("Price", Price.ToString()),
new XElement("DishComponents", Components.Select(x =>
new XElement("DishComponent",
new XElement("Key", x.Key),
new XElement("Value", x.Value))).ToArray()
)
);
}
}

View File

@ -0,0 +1,86 @@
using FoodOrdersContracts.BindingModels;
using FoodOrdersContracts.ViewModels;
using FoodOrdersDataModels.Enums;
using FoodOrdersDataModels.Models;
using System.Reflection.Metadata;
using System.Xml.Linq;
namespace FoodOrdersFileImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
public int DishId { get; private set; }
public int Count { get; private set; }
public double Sum { get; private set; }
public OrderStatus Status { get; private set; }
public DateTime DateCreate { get; private set; }
public DateTime? DateImplement { get; private set; }
public static Order? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Order()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
DishId = Convert.ToInt32(element.Element("DishId")!.Value),
Sum = Convert.ToDouble(element.Element("Sum")!.Value),
Count = Convert.ToInt32(element.Element("Count")!.Value),
Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value),
DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value),
DateImplement = string.IsNullOrEmpty(element.Element("DateImplement")!.Value) ? null : Convert.ToDateTime(element.Element("DateImplement")!.Value)
};
}
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order
{
Id = model.Id,
DishId = model.DishId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
};
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
DishId = DishId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement
};
public XElement GetXElement => new(
"Order",
new XAttribute("Id", Id),
new XElement("DishId", DishId.ToString()),
new XElement("Count", Count.ToString()),
new XElement("Sum", Sum.ToString()),
new XElement("Status", Status.ToString()),
new XElement("DateCreate", DateCreate.ToString()),
new XElement("DateImplement", DateImplement.ToString())
);
}
}

View File

@ -0,0 +1,97 @@
using FoodOrdersContracts.BindingModels;
using FoodOrdersContracts.ViewModels;
using FoodOrdersDataModels.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace FoodOrdersFileImplement.Models
{
public class Shop : IShopModel
{
public string ShopName { get; private set; } = string.Empty;
public string Address { get; private set; } = string.Empty;
public DateTime DateOfOpening { get; private set; } = DateTime.Now;
public int Capacity { get; private set; } = 0;
public int Id { get; private set; }
public Dictionary<int, int> Dishes { get; private set; } = new();
public Dictionary<int, (IDishModel, int)>? _shopDishes = null;
public Dictionary<int, (IDishModel, int)> ShopDishes
{
get
{
if (_shopDishes == null)
{
var source = DataFileSingleton.GetInstance();
_shopDishes = Dishes.ToDictionary(x => x.Key, y =>
((source.Dishes.FirstOrDefault(z => z.Id == y.Key) as IDishModel)!, y.Value));
}
return _shopDishes;
}
}
public static Shop? Create(ShopBindingModel? model)
{
if (model == null)
{
return null;
}
return new Shop()
{
Id = model.Id,
ShopName = model.ShopName,
Address = model.Address,
DateOfOpening = model.DateOfOpening,
Dishes = model.ShopDishes.ToDictionary(x => x.Key, x => x.Value.Item2),
Capacity = model.Capacity
};
}
public static Shop? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Shop()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
ShopName = element.Element("ShopName")!.Value,
Address = element.Element("Address")!.Value,
DateOfOpening = Convert.ToDateTime(element.Element("DateOfOpening")!.Value),
Dishes = element.Element("ShopDishes")!.Elements("ShopDish").ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value)),
Capacity = Convert.ToInt32(element.Element("Capacity")!.Value)
};
}
public void Update(ShopBindingModel? model)
{
if (model == null)
{
return;
}
ShopName = model.ShopName;
Address = model.Address;
DateOfOpening = model.DateOfOpening;
Dishes = model.ShopDishes.ToDictionary(x => x.Key, x => x.Value.Item2);
Capacity = model.Capacity;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
ShopName = ShopName,
Address = Address,
DateOfOpening = DateOfOpening,
ShopDishes = ShopDishes,
Capacity = Capacity
};
public XElement GetXElement => new("Shop",
new XAttribute("Id", Id),
new XElement("ShopName", ShopName),
new XElement("Address", Address),
new XElement("DateOfOpening", DateOfOpening.ToString()),
new XElement("ShopDishes", Dishes.Select(x => new XElement("ShopDish", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray()),
new XElement("Capacity",Capacity.ToString()));
}
}

View File

@ -3,6 +3,7 @@ using FoodOrdersContracts.SearchModels;
using FoodOrdersContracts.StoragesContracts;
using FoodOrdersContracts.ViewModels;
using FoodOrdersListImplement.Models;
using FoodOrdersDataModels.Models;
namespace FoodOrdersListImplement.Implements
{
@ -96,5 +97,9 @@ namespace FoodOrdersListImplement.Implements
}
return null;
}
public bool SellDishes(IDishModel dish, int count)
{
throw new NotImplementedException();
}
}
}

View File

@ -13,6 +13,7 @@ namespace FoodOrdersListImplement.Models
public string Address { get; private set; } = string.Empty;
public DateTime DateOfOpening { get; private set; } = DateTime.Now;
public Dictionary<int, (IDishModel, int)> ShopDishes { get; private set; } = new Dictionary<int, (IDishModel, int)>();
public int Capacity { get; private set; } = 0;
public static Shop? Create(ShopBindingModel? model)
{
if (model == null)
@ -25,7 +26,8 @@ namespace FoodOrdersListImplement.Models
ShopName = model.ShopName,
Address = model.Address,
DateOfOpening = model.DateOfOpening,
ShopDishes = model.ShopDishes
ShopDishes = model.ShopDishes,
Capacity = model.Capacity
};
}
public void Update(ShopBindingModel? model)
@ -38,6 +40,7 @@ namespace FoodOrdersListImplement.Models
Address = model.Address;
DateOfOpening = model.DateOfOpening;
ShopDishes = model.ShopDishes;
Capacity = model.Capacity;
}
public ShopViewModel GetViewModel => new()
{
@ -45,7 +48,8 @@ namespace FoodOrdersListImplement.Models
ShopName = ShopName,
Address = Address,
DateOfOpening = DateOfOpening,
ShopDishes = ShopDishes
ShopDishes = ShopDishes,
Capacity = Capacity
};
}
}