This commit is contained in:
goblinrf 2024-04-15 20:46:59 +04:00
parent 97b1187f1b
commit 315ebdf4a5
27 changed files with 822 additions and 449 deletions

View File

@ -3,7 +3,7 @@ using DinerContracts.BusinessLogicsContracts;
using DinerContracts.SearchModels;
using DinerContracts.StoragesContracts;
using DinerContracts.ViewModels;
using DinerShopDataModels.Models;
using DinerDataModels.Models;
using DinerDataModels.Enum;
using Microsoft.Extensions.Logging;
@ -16,9 +16,6 @@ namespace DinerBusinessLogic.BusinessLogics
private readonly IShopStorage _shopStorage;
private readonly IShopLogic _shopLogic;
private readonly IShopStorage _shopStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage, IShopStorage shopStorage)
{
_orderStorage = orderStorage;
@ -51,64 +48,7 @@ namespace DinerBusinessLogic.BusinessLogics
}
return true;
}
public bool CheckSupply(IDinerModel snack, int count)
{
if (count <= 0)
{
_logger.LogWarning("Check then supply operation error. IceCream count < 0.");
return false;
}
int sumCapacity = 0;
int sumCount = 0;
sumCapacity = _shopStorage.GetFullList().Select(x => x.MaxCapacity).Sum();
sumCount = _shopStorage.GetFullList().Select(x => x.ShopIceCreams.Select(y => y.Value.Item2).Sum()).Sum();
int freeSpace = sumCapacity - sumCount;
if (freeSpace - count < 0)
{
_logger.LogWarning("Check then supply operation error. There's no place for new IceCream in shops.");
return false;
}
foreach (var shop in _shopStorage.GetFullList())
{
freeSpace = shop.MaxCapacity;
foreach (var doc in shop.ShopIceCreams)
{
freeSpace -= doc.Value.Item2;
}
if (freeSpace == 0)
{
continue;
}
if (freeSpace - count >= 0)
{
if (_shopLogic.MakeSupply(new() { Id = shop.Id }, iceCream, count))
count = 0;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (freeSpace - count < 0)
{
if (_shopLogic.MakeSupply(new() { Id = shop.Id }, iceCream, freeSpace))
count -= freeSpace;
else
{
_logger.LogWarning("Supply error");
return false;
}
}
if (count <= 0)
{
return true;
}
}
return false;
}
public bool TakeOrderInWork(OrderBindingModel model)
{
return ChangeStatus(model, OrderStatus.Выполняется);
@ -118,10 +58,6 @@ namespace DinerBusinessLogic.BusinessLogics
return ChangeStatus(model, OrderStatus.Готов);
}
public bool DeliveryOrder(OrderBindingModel model)
{
return ChangeStatus(model, OrderStatus.Выдан);
}
public bool DeliveryOrder(OrderBindingModel model)
{
var order = _orderStorage.GetElement(new OrderSearchModel
{
@ -139,9 +75,9 @@ namespace DinerBusinessLogic.BusinessLogics
{
throw new ArgumentException("Недостаточно места");
}
return ChangeStatus(model, OrderStatus.Выдан);
}
private void CheckModel(OrderBindingModel model, bool withParams = true)
{
if (model == null)

View File

@ -159,11 +159,11 @@ namespace DinerBusinessLogic.BusinessLogics
}
public bool Sale(SupplySearchModel model)
{
if (!model.PizzaId.HasValue || !model.Count.HasValue)
if (!model.SnackId.HasValue || !model.Count.HasValue)
{
return false;
}
_logger.LogInformation("Check pizza count in all shops");
_logger.LogInformation("Check snack count in all shops");
if (_shopStorage.Sale(model))
{
_logger.LogInformation("Selling sucsess");

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>

View File

@ -109,5 +109,14 @@ namespace DinerListImplement.Implements
}
return null;
}
public bool Sale(SupplySearchModel model)
{
throw new NotImplementedException();
}
public bool RestockingShops(SupplyBindingModel model)
{
throw new NotImplementedException();
}
}
}

View File

@ -39,14 +39,9 @@ namespace DinerListImplement.Models
{
return;
}
Id = model.Id;
SnackID = model.SnackId;
SnackName = model.SnackName;
Count = model.Count;
Sum = model.Sum;
Status = model.Status;
DateCreate = model.DateCreate;
DateImplement = model.DateImplement;
if (model.Status == OrderStatus.Выдан) DateImplement = model.DateImplement;
if (model.Status == OrderStatus.Выдан) DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{

View File

@ -16,6 +16,7 @@ namespace DinerListImplement.Models
public string Adress { get; private set; } = string.Empty;
public DateTime OpeningDate { get; private set; }
public Dictionary<int, (ISnackModel, int)> ShopSnacks { get; private set; } = new();
public int SnackMaxCount { get; private set; }
public static Shop? Create(ShopBindingModel? model)
{
@ -28,7 +29,8 @@ namespace DinerListImplement.Models
Id = model.Id,
ShopName = model.ShopName,
Adress = model.Adress,
OpeningDate = model.OpeningDate
OpeningDate = model.OpeningDate,
SnackMaxCount = model.SnackMaxCount,
};
}
@ -41,6 +43,7 @@ namespace DinerListImplement.Models
ShopName = model.ShopName;
Adress = model.Adress;
OpeningDate = model.OpeningDate;
SnackMaxCount = model.SnackMaxCount;
}
public ShopViewModel GetViewModel => new()
@ -49,7 +52,8 @@ namespace DinerListImplement.Models
ShopName = ShopName,
Adress = Adress,
OpeningDate = OpeningDate,
ShopSnacks = ShopSnacks
ShopSnacks = ShopSnacks,
SnackMaxCount = SnackMaxCount,
};
}
}

View File

@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DinerBusinessLogic", "..\Ab
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DinerListImplement", "AbstractShopListImplement\DinerListImplement.csproj", "{A24E7474-4B43-4E81-A6BF-2B7323D5C26A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DinerFileImplement", "DinerFileImplement\DinerFileImplement.csproj", "{13294C1E-C8DF-4E4B-B645-A61900A797EB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -39,6 +41,10 @@ Global
{A24E7474-4B43-4E81-A6BF-2B7323D5C26A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A24E7474-4B43-4E81-A6BF-2B7323D5C26A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A24E7474-4B43-4E81-A6BF-2B7323D5C26A}.Release|Any CPU.Build.0 = Release|Any CPU
{13294C1E-C8DF-4E4B-B645-A61900A797EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{13294C1E-C8DF-4E4B-B645-A61900A797EB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{13294C1E-C8DF-4E4B-B645-A61900A797EB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{13294C1E-C8DF-4E4B-B645-A61900A797EB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -9,12 +9,18 @@
</PropertyGroup>
<ItemGroup>
<None Remove="FormSellSnack" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\AbstractShopBusinessLogic\DinerBusinessLogic.csproj" />
<ProjectReference Include="..\AbstractShopListImplement\DinerListImplement.csproj" />
<ProjectReference Include="..\DinerFileImplement\DinerFileImplement.csproj" />
</ItemGroup>
<ItemGroup>

View File

@ -41,7 +41,7 @@
магазиныToolStripMenuItem = new ToolStripMenuItem();
toolStripDropDownButton1 = new ToolStripDropDownButton();
поставкаToolStripMenuItem = new ToolStripMenuItem();
продажаToolStripMenuItem = new ToolStripMenuItem();
SellToolStripMenuItem = new ToolStripMenuItem();
((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit();
toolStrip1.SuspendLayout();
SuspendLayout();
@ -148,7 +148,7 @@
// toolStripDropDownButton1
//
toolStripDropDownButton1.DisplayStyle = ToolStripItemDisplayStyle.Text;
toolStripDropDownButton1.DropDownItems.AddRange(new ToolStripItem[] { поставкаToolStripMenuItem, продажаToolStripMenuItem });
toolStripDropDownButton1.DropDownItems.AddRange(new ToolStripItem[] { поставкаToolStripMenuItem, SellToolStripMenuItem });
toolStripDropDownButton1.ImageTransparentColor = Color.Magenta;
toolStripDropDownButton1.Name = "toolStripDropDownButton1";
toolStripDropDownButton1.Size = new Size(95, 24);
@ -161,12 +161,12 @@
поставкаToolStripMenuItem.Text = "Поставка";
поставкаToolStripMenuItem.Click += transactionToolStripMenuItem_Click;
//
// продажаToolStripMenuItem
// SellToolStripMenuItem
//
продажаToolStripMenuItem.Name = "продажаToolStripMenuItem";
продажаToolStripMenuItem.Size = new Size(224, 26);
продажаToolStripMenuItem.Text = "Продажа";
продажаToolStripMenuItem.Click += SellToolStripMenuItem_Click;
SellToolStripMenuItem.Name = "SellToolStripMenuItem";
SellToolStripMenuItem.Size = new Size(224, 26);
SellToolStripMenuItem.Text = "Продажа";
SellToolStripMenuItem.Click += SellToolStripMenuItem_Click;
//
// FormMain
//
@ -205,6 +205,6 @@
private ToolStripMenuItem магазиныToolStripMenuItem;
private ToolStripDropDownButton toolStripDropDownButton1;
private ToolStripMenuItem поставкаToolStripMenuItem;
private ToolStripMenuItem продажаToolStripMenuItem;
private ToolStripMenuItem SellToolStripMenuItem;
}
}

View File

@ -182,7 +182,7 @@ namespace Diner
}
private void SellToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormSellPizza));
var service = Program.ServiceProvider?.GetService(typeof(FormSellSnack));
if (service is FormSellSnack form)
{
form.ShowDialog();

View File

@ -1,117 +0,0 @@
namespace IceCreamShop
{
partial class SellForm
{
/// <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()
{
IceCreamComboBox = new ComboBox();
IceCreamLabel = new Label();
CountLabel = new Label();
CountTextBox = new TextBox();
SaveButton = new Button();
CancelButton = new Button();
SuspendLayout();
//
// IceCreamComboBox
//
IceCreamComboBox.FormattingEnabled = true;
IceCreamComboBox.Location = new Point(99, 12);
IceCreamComboBox.Name = "IceCreamComboBox";
IceCreamComboBox.Size = new Size(121, 23);
IceCreamComboBox.TabIndex = 0;
//
// IceCreamLabel
//
IceCreamLabel.AutoSize = true;
IceCreamLabel.Location = new Point(12, 20);
IceCreamLabel.Name = "IceCreamLabel";
IceCreamLabel.Size = new Size(81, 15);
IceCreamLabel.TabIndex = 1;
IceCreamLabel.Text = "Мороженное";
//
// CountLabel
//
CountLabel.AutoSize = true;
CountLabel.Location = new Point(21, 49);
CountLabel.Name = "CountLabel";
CountLabel.Size = new Size(72, 15);
CountLabel.TabIndex = 2;
CountLabel.Text = "Количество";
//
// CountTextBox
//
CountTextBox.Location = new Point(99, 41);
CountTextBox.Name = "CountTextBox";
CountTextBox.Size = new Size(121, 23);
CountTextBox.TabIndex = 3;
//
// SaveButton
//
SaveButton.Location = new Point(64, 73);
SaveButton.Name = "SaveButton";
SaveButton.Size = new Size(75, 23);
SaveButton.TabIndex = 4;
SaveButton.Text = "Сохранить";
SaveButton.UseVisualStyleBackColor = true;
SaveButton.Click += SaveButton_Click;
//
// CancelButton
//
CancelButton.Location = new Point(145, 73);
CancelButton.Name = "CancelButton";
CancelButton.Size = new Size(75, 23);
CancelButton.TabIndex = 5;
CancelButton.Text = "Отмена";
CancelButton.UseVisualStyleBackColor = true;
//
// SellForm
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(224, 108);
Controls.Add(CancelButton);
Controls.Add(SaveButton);
Controls.Add(CountTextBox);
Controls.Add(CountLabel);
Controls.Add(IceCreamLabel);
Controls.Add(IceCreamComboBox);
Name = "SellForm";
Text = "Форма продажи";
ResumeLayout(false);
PerformLayout();
}
#endregion
private ComboBox IceCreamComboBox;
private Label IceCreamLabel;
private Label CountLabel;
private TextBox CountTextBox;
private Button SaveButton;
private Button CancelButton;
}
}

View File

@ -1,117 +0,0 @@
using IceCreamShopContracts.BusinessLogicsContracts;
using IceCreamShopContracts.SearchModels;
using IceCreamShopContracts.ViewModels;
using IceCreamShopDataModels.Models;
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 IceCreamShop
{
public partial class SellForm : Form
{
private readonly List<IceCreamViewModel>? _iceCreamList;
IShopLogic _shopLogic;
IIceCreamLogic _iceCreamLogic;
public SellForm(IIceCreamLogic iceCreamLogic, IShopLogic shopLogic)
{
InitializeComponent();
_shopLogic = shopLogic;
_iceCreamLogic = iceCreamLogic;
_iceCreamList = iceCreamLogic.ReadList(null);
if (_iceCreamList != null)
{
IceCreamComboBox.DisplayMember = "IceCreamName";
IceCreamComboBox.ValueMember = "Id";
IceCreamComboBox.DataSource = _iceCreamList;
IceCreamComboBox.SelectedItem = null;
}
}
public int IceCreamId
{
get
{
return
Convert.ToInt32(IceCreamComboBox.SelectedValue);
}
set
{
IceCreamComboBox.SelectedValue = value;
}
}
public IIceCreamModel? IceCreamModel
{
get
{
if (_iceCreamList == null)
{
return null;
}
foreach (var elem in _iceCreamList)
{
if (elem.Id == IceCreamId)
{
return elem;
}
}
return null;
}
}
public int Count
{
get { return Convert.ToInt32(CountTextBox.Text); }
set
{ CountTextBox.Text = value.ToString(); }
}
private void SaveButton_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(CountTextBox.Text))
{
MessageBox.Show("Заполните поле Количество", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (IceCreamComboBox.SelectedValue == null)
{
MessageBox.Show("Выберите мороженное", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
int count = Convert.ToInt32(CountTextBox.Text);
bool res = _shopLogic.MakeSell(
_iceCreamLogic.ReadElement(new() { Id = Convert.ToInt32(IceCreamComboBox.SelectedValue) }),
count
);
if (!res)
{
throw new Exception("Ошибка при продаже.");
}
MessageBox.Show("Продажа прошла успешно");
DialogResult = DialogResult.OK;
Close();
}
catch (Exception err)
{
MessageBox.Show("Ошибка продажи");
return;
}
}
}
}

View File

@ -1,120 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<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

@ -1,4 +1,4 @@
namespace PizzeriaView
namespace DinerView
{
partial class FormSellSnack
{
@ -101,8 +101,8 @@
Controls.Add(comboBoxSnack);
Controls.Add(labelSnack);
Name = "FormSellSnack";
Text = "Продажа пиццы";
Load += FormSellingSnack_Load;
Text = "Продажа закуски";
Load += FormSellingPizza_Load;
ResumeLayout(false);
PerformLayout();
}

View File

@ -13,7 +13,7 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PizzeriaView
namespace DinerView
{
public partial class FormSellSnack : Form
{
@ -28,12 +28,12 @@ namespace PizzeriaView
_logicP = logicP;
_logicS = logicS;
}
private void FormSellingSnack_Load(object sender, EventArgs e)
private void FormSellingPizza_Load(object sender, EventArgs e)
{
_snackList = _logicP.ReadList(null);
if (_snackList != null)
{
comboBoxSnack.DisplayMember = "PizzaName";
comboBoxSnack.DisplayMember = "SnackName";
comboBoxSnack.ValueMember = "Id";
comboBoxSnack.DataSource = _snackList;
comboBoxSnack.SelectedItem = null;
@ -52,7 +52,7 @@ namespace PizzeriaView
{
bool resout = _logicS.Sale(new SupplySearchModel
{
PizzaId = Convert.ToInt32(comboBoxSnack.SelectedValue),
SnackId = Convert.ToInt32(comboBoxSnack.SelectedValue),
Count = Convert.ToInt32(textBoxCount.Text)
});
if (resout)

View File

@ -40,7 +40,7 @@ namespace DinerView
{
dataGridView.DataSource = list;
dataGridView.Columns["Id"].Visible = false;
dataGridView.Columns["ShopDiners"].Visible = false;
dataGridView.Columns["ShopName"].AutoSizeMode =
DataGridViewAutoSizeColumnMode.Fill;
}

View File

@ -52,6 +52,7 @@ namespace Diner
services.AddTransient<FormShop>();
services.AddTransient<FormShops>();
services.AddTransient<FormCreateSupply>();
services.AddTransient<FormSellSnack>();
}
}
}

View File

@ -0,0 +1,55 @@
using DinerFileImplement.Models;
using System.Xml.Linq;
namespace DinerFileImplement
{
public class DataFileSingleton
{
private static DataFileSingleton? instance;
private readonly string ComponentFileName = "Component.xml";
private readonly string OrderFileName = "Order.xml";
private readonly string SnackFileName = "Snack.xml";
private readonly string ShopFileName = "Shop.xml";
public List<Component> Components { get; private set; }
public List<Order> Orders { get; private set; }
public List<Snack> Snacks { 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 SaveSnacks() => SaveData(Snacks, SnackFileName, "Snacks", 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)!)!;
Snacks = LoadData(SnackFileName, "Snack", x => Snack.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="..\..\AbstractShopContracts\DinerContracts.csproj" />
<ProjectReference Include="..\..\AbstractShopDataModels\DinerDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,74 @@
using DinerContracts.BindingModels;
using DinerContracts.SearchModels;
using DinerContracts.StoragesContracts;
using DinerContracts.ViewModels;
using DinerFileImplement.Models;
namespace DinerFileImplement.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();
}
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,84 @@
using DinerContracts.BindingModels;
using DinerContracts.SearchModels;
using DinerContracts.StoragesContracts;
using DinerContracts.ViewModels;
using DinerFileImplement.Models;
namespace DinerFileImplement.Implements
{
public class OrderStorage : IOrderStorage
{
private readonly DataFileSingleton source;
public OrderStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<OrderViewModel> GetFullList() => source.Orders.Select(x => AttachPizzaName(x.GetViewModel)).ToList();
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
return source.Orders.Where(x => x.Id == model.Id)
.Select(x => AttachPizzaName(x.GetViewModel))
.ToList();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
return AttachPizzaName(source.Orders.FirstOrDefault(x => x.Id == model.Id)?.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 AttachPizzaName(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 AttachPizzaName(order.GetViewModel);
}
public OrderViewModel? Delete(OrderBindingModel model)
{
var order = source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order != null)
{
source.Orders.Remove(order);
source.SaveOrders();
return AttachPizzaName(order.GetViewModel);
}
return null;
}
private OrderViewModel? AttachPizzaName(OrderViewModel? model)
{
if (model == null)
{
return null;
}
var snack = source.Snacks.FirstOrDefault(x => x.Id == model.SnackId);
if (snack != null)
{
model.SnackName = snack.SnackName;
}
return model;
}
}
}

View File

@ -0,0 +1,144 @@
using DinerContracts.BindingModels;
using DinerContracts.SearchModels;
using DinerContracts.StoragesContracts;
using DinerContracts.ViewModels;
using DinerFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DinerFileImplement.Implements
{
public class ShopStorage : IShopStorage
{
private readonly DataFileSingleton source;
public ShopStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<ShopViewModel> GetFullList()
{
return source.Shops.Select(x=>x.GetViewModel).ToList();
}
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 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 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 ShopViewModel? Delete(ShopBindingModel model)
{
var shop = source.Shops.FirstOrDefault(x => x.Id == model.Id);
if(shop != null)
{
source.Shops.Remove(shop);
source.SaveShops();
return shop.GetViewModel;
}
return null;
}
public bool Sale(SupplySearchModel model) {
if (model == null || !model.SnackId.HasValue || !model.Count.HasValue)
return false;
int remainingSpace = source.Shops.Select(x => x.Snacks.ContainsKey(model.SnackId.Value) ? x.Snacks[model.SnackId.Value] : 0).Sum();
if(remainingSpace < model.Count)
{
return false;
}
var shops = source.Shops.Where(x => x.Snacks.ContainsKey(model.SnackId.Value)).OrderByDescending(x => x.Snacks[model.SnackId.Value]).ToList();
foreach (var shop in shops)
{
int residue = model.Count.Value - shop.Snacks[model.SnackId.Value];
if (residue > 0)
{
shop.Snacks.Remove(model.SnackId.Value);
shop.SnackUpdate();
model.Count = residue;
}
else
{
if (residue == 0)
{
shop.Snacks.Remove(model.SnackId.Value);
}
else
{
shop.Snacks[model.SnackId.Value] = -residue;
}
shop.SnackUpdate();
source.SaveShops();
return true;
}
}
source.SaveShops();
return false;
}
public bool RestockingShops(SupplyBindingModel model)
{
if (model==null || source.Shops.Select(x => x.SnackMaxCount - x.ShopSnacks.Select(y => y.Value.Item2).Sum()).Sum() < model.Count)
{
return false;
}
foreach (Shop shop in source.Shops)
{
int free_places = shop.SnackMaxCount - shop.ShopSnacks.Select(x => x.Value.Item2).Sum();
if (free_places <= 0)
continue;
free_places = Math.Min(free_places, model.Count);
model.Count -= free_places;
if (shop.Snacks.ContainsKey(model.SnackId))
{
shop.Snacks[model.SnackId] += free_places;
}
else
{
shop.Snacks.Add(model.SnackId, free_places);
}
shop.SnackUpdate();
if (model.Count == 0)
{
source.SaveShops();
return true;
}
}
return false;
}
}
}

View File

@ -0,0 +1,74 @@
using DinerContracts.BindingModels;
using DinerContracts.SearchModels;
using DinerContracts.StoragesContracts;
using DinerContracts.ViewModels;
using DinerFileImplement.Models;
namespace DinerFileImplement.Implements
{
public class SnackStorage : ISnackStorage
{
private readonly DataFileSingleton source;
public SnackStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<SnackViewModel> GetFullList()
{
return source.Snacks.Select(x => x.GetViewModel).ToList();
}
public List<SnackViewModel> GetFilteredList(SnackSearchModel model)
{
if (string.IsNullOrEmpty(model.SnackName))
{
return new();
}
return source.Snacks.Where(x => x.SnackName.Contains(model.SnackName)).Select(x => x.GetViewModel).ToList();
}
public SnackViewModel? GetElement(SnackSearchModel model)
{
if (string.IsNullOrEmpty(model.SnackName) && !model.Id.HasValue)
{
return null;
}
return source.Snacks.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.SnackName) && x.SnackName == model.SnackName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public SnackViewModel? Insert(SnackBindingModel model)
{
model.Id = source.Snacks.Count > 0 ? source.Snacks.Max(x => x.Id) + 1 : 1;
var newSnack = Snack.Create(model);
if (newSnack == null)
{
return null;
}
source.Snacks.Add(newSnack);
source.SaveSnacks();
return newSnack.GetViewModel;
}
public SnackViewModel? Update(SnackBindingModel model)
{
var Snack = source.Snacks.FirstOrDefault(x => x.Id == model.Id);
if (Snack == null)
{
return null;
}
Snack.Update(model);
source.SaveSnacks();
return Snack.GetViewModel;
}
public SnackViewModel? Delete(SnackBindingModel model)
{
var Snack = source.Snacks.FirstOrDefault(x => x.Id == model.Id);
if (Snack != null)
{
source.Snacks.Remove(Snack);
source.SaveSnacks();
return Snack.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,59 @@
using DinerContracts.BindingModels;
using DinerContracts.ViewModels;
using DinerDataModels.Models;
using System.Xml.Linq;
namespace DinerFileImplement.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,83 @@
using DinerDataModels.Enum;
using DinerDataModels.Models;
using DinerContracts.BindingModels;
using DinerContracts.ViewModels;
using System.Xml.Linq;
namespace DinerFileImplement.Models
{
public class Order : IOrderModel
{
public int Id { get; private set; }
public int SnackId { get; private set; }
public int Count { get; private set; }
public double Sum { get; private set; }
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
public DateTime DateCreate { get; private set; } = DateTime.Now;
public DateTime? DateImplement { get; private set; }
public static Order? Create(OrderBindingModel? model)
{
if (model == null)
{
return null;
}
return new Order()
{
Id = model.Id,
SnackId = model.SnackId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
DateCreate = model.DateCreate,
DateImplement = model.DateImplement,
};
}
public static Order? Create(XElement element)
{
if (element == null)
{
return null;
}
string dateImplement = element.Element("DateImplement")!.Value;
return new Order()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
SnackId = Convert.ToInt32(element.Element("SnackId")!.Value),
Count = Convert.ToInt32(element.Element("Count")!.Value),
Sum = Convert.ToDouble(element.Element("Sum")!.Value),
Status = (OrderStatus)(Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value)),
DateCreate = Convert.ToDateTime(element.Element("DateCreate")!.Value),
DateImplement = (dateImplement == "" || dateImplement is null) ? Convert.ToDateTime(null) : Convert.ToDateTime(dateImplement)
};
}
public void Update(OrderBindingModel? model)
{
if (model == null)
{
return;
}
Status = model.Status;
if (model.Status == OrderStatus.Выдан) DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
SnackId = SnackId,
Count = Count,
Sum = Sum,
Status = Status,
DateCreate = DateCreate,
DateImplement = DateImplement,
};
public XElement GetXElement => new("Order",
new XAttribute("Id", Id),
new XElement("SnackId", SnackId.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,102 @@
using DinerDataModels.Models;
using DinerContracts.BindingModels;
using DinerContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace DinerFileImplement.Models
{
public class Shop : IShopModel
{
public int Id { get; private set; }
public string ShopName { get; private set; } = string.Empty;
public string Adress { get; private set; } = string.Empty;
public DateTime OpeningDate { get; private set; }
public Dictionary<int, int> Snacks { get; private set; } = new();
private Dictionary<int, (ISnackModel, int)>? _shopSnacks = null;
public Dictionary<int, (ISnackModel, int)> ShopSnacks {
get {
if (_shopSnacks == null)
{
var source = DataFileSingleton.GetInstance();
_shopSnacks = Snacks.ToDictionary(x => x.Key, y => ((source.Snacks.FirstOrDefault(z => z.Id == y.Key) as ISnackModel)!, y.Value));
}
return _shopSnacks;
}
}
public int SnackMaxCount { get; private set; }
public static Shop? Create(ShopBindingModel? model)
{
if (model == null)
{
return null;
}
return new Shop()
{
Id = model.Id,
ShopName = model.ShopName,
Adress = model.Adress,
OpeningDate = model.OpeningDate,
Snacks = model.ShopSnacks.ToDictionary(x => x.Key, x => x.Value.Item2),
SnackMaxCount = model.SnackMaxCount
};
}
public static Shop? Create(XElement element)
{
if (element == null)
{
return null;
}
return new()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
ShopName = element.Element("ShopName")!.Value,
Adress = element.Element("Adress")!.Value,
OpeningDate = Convert.ToDateTime(element.Element("OpeningDate")!.Value),
Snacks = element.Element("ShopSnacks")!.Elements("ShopSnack")!.ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value),
x => Convert.ToInt32(x.Element("Value")?.Value)),
SnackMaxCount = Convert.ToInt32(element.Element("SnackMaxCount")!.Value)
};
}
public void Update(ShopBindingModel? model)
{
if (model == null)
{
return;
}
ShopName = model.ShopName;
Adress = model.Adress;
OpeningDate = model.OpeningDate;
SnackMaxCount = model.SnackMaxCount;
Snacks = model.ShopSnacks.ToDictionary(x => x.Key, x => x.Value.Item2);
_shopSnacks = null;
}
public ShopViewModel GetViewModel => new()
{
Id = Id,
ShopName = ShopName,
Adress = Adress,
OpeningDate = OpeningDate,
ShopSnacks = ShopSnacks,
SnackMaxCount = SnackMaxCount
};
public XElement GetXElement => new("Shop",
new XAttribute("Id", Id),
new XElement("ShopName",ShopName),
new XElement("Adress", Adress),
new XElement("OpeningDate", OpeningDate.ToString()),
new XElement("ShopSnacks", Snacks.Select(
x => new XElement("ShopSnack", new XElement("Key",x.Key),new XElement("Value",x.Value))).ToArray()),
new XElement("SnackMaxCount", SnackMaxCount.ToString())
);
public void SnackUpdate() {
_shopSnacks = null;
}
}
}

View File

@ -0,0 +1,81 @@
using DinerContracts.BindingModels;
using DinerContracts.ViewModels;
using DinerDataModels.Models;
using System.Xml.Linq;
namespace DinerFileImplement.Models
{
public class Snack : ISnackModel
{
public int Id { get; private set; }
public string SnackName { get; private set; } = string.Empty;
public double Price { get; private set; }
public Dictionary<int, int> Components { get; private set; } = new();
private Dictionary<int, (IComponentModel, int)>? _snackComponents = null;
public Dictionary<int, (IComponentModel, int)> SnackComponents
{
get
{
if (_snackComponents == null)
{
var source = DataFileSingleton.GetInstance();
_snackComponents = Components.ToDictionary(x => x.Key, y => ((source.Components.FirstOrDefault(z => z.Id == y.Key) as IComponentModel)!, y.Value));
}
return _snackComponents;
}
}
public static Snack? Create(SnackBindingModel model)
{
if (model == null)
{
return null;
}
return new Snack()
{
Id = model.Id,
SnackName = model.SnackName,
Price = model.Price,
Components = model.SnackComponents.ToDictionary(x => x.Key, x => x.Value.Item2)
};
}
public static Snack? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Snack()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
SnackName = element.Element("SnackName")!.Value,
Price = Convert.ToDouble(element.Element("Price")!.Value),
Components = element.Element("SnackComponents")!.Elements("SnackComponent").ToDictionary(x => Convert.ToInt32(x.Element("Key")?.Value),
x => Convert.ToInt32(x.Element("Value")?.Value))
};
}
public void Update(SnackBindingModel model)
{
if (model == null)
{
return;
}
SnackName = model.SnackName;
Price = model.Price;
Components = model.SnackComponents.ToDictionary(x => x.Key, x => x.Value.Item2);
_snackComponents = null;
}
public SnackViewModel GetViewModel => new()
{
Id = Id,
SnackName = SnackName,
Price = Price,
SnackComponents = SnackComponents
};
public XElement GetXElement => new("Snack",
new XAttribute("Id", Id),
new XElement("SnackName", SnackName),
new XElement("Price", Price.ToString()),
new XElement("SnackComponents", Components.Select(
x => new XElement("SnackComponent", new XElement("Key", x.Key), new XElement("Value", x.Value))).ToArray()));
}
}