This commit is contained in:
revengel66 2024-03-11 00:29:19 +04:00
parent 10698313a9
commit 8eee668ae4
23 changed files with 123 additions and 474 deletions

View File

@ -70,6 +70,7 @@
// //
// buttonSave // buttonSave
// //
buttonSave.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonSave.Location = new Point(177, 93); buttonSave.Location = new Point(177, 93);
buttonSave.Name = "buttonSave"; buttonSave.Name = "buttonSave";
buttonSave.Size = new Size(75, 23); buttonSave.Size = new Size(75, 23);
@ -80,6 +81,7 @@
// //
// buttonCancel // buttonCancel
// //
buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonCancel.Location = new Point(258, 93); buttonCancel.Location = new Point(258, 93);
buttonCancel.Name = "buttonCancel"; buttonCancel.Name = "buttonCancel";
buttonCancel.Size = new Size(75, 23); buttonCancel.Size = new Size(75, 23);
@ -100,7 +102,7 @@
Controls.Add(labelPrice); Controls.Add(labelPrice);
Controls.Add(labelName); Controls.Add(labelName);
Name = "FormComponent"; Name = "FormComponent";
Text = "Компонент"; Text = "Ингредиент";
Load += FormComponent_Load; Load += FormComponent_Load;
ResumeLayout(false); ResumeLayout(false);
PerformLayout(); PerformLayout();

View File

@ -27,11 +27,10 @@ namespace Pizzeria
{ {
try try
{ {
_logger.LogInformation("Получение компонента"); _logger.LogInformation("Получение ингредиента");
var view = _logic.ReadElement(new ComponentSearchModel var view = _logic.ReadElement(new ComponentSearchModel
{ {
Id = Id =_id.Value
_id.Value
}); });
if (view != null) if (view != null)
{ {
@ -41,9 +40,8 @@ namespace Pizzeria
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Ошибка получения компонента"); _logger.LogError(ex, "Ошибка получения ингредиента");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBoxIcon.Error);
} }
} }
@ -53,11 +51,10 @@ namespace Pizzeria
{ {
if (string.IsNullOrEmpty(textBoxName.Text)) if (string.IsNullOrEmpty(textBoxName.Text))
{ {
MessageBox.Show("Заполните название", "Ошибка", MessageBox.Show("Заполните название", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBoxButtons.OK, MessageBoxIcon.Error);
return; return;
} }
_logger.LogInformation("Сохранение компонента"); _logger.LogInformation("Сохранение ингредиента");
try try
{ {
var model = new ComponentBindingModel var model = new ComponentBindingModel
@ -66,22 +63,19 @@ namespace Pizzeria
ComponentName = textBoxName.Text, ComponentName = textBoxName.Text,
Cost = Convert.ToDouble(textBoxCost.Text) Cost = Convert.ToDouble(textBoxCost.Text)
}; };
var operationResult = _id.HasValue ? _logic.Update(model) : var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model);
_logic.Create(model);
if (!operationResult) if (!operationResult)
{ {
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
} }
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
MessageBoxButtons.OK, MessageBoxIcon.Information);
DialogResult = DialogResult.OK; DialogResult = DialogResult.OK;
Close(); Close();
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Ошибка сохранения компонента"); _logger.LogError(ex, "Ошибка сохранения ингредиента");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBoxIcon.Error);
} }
} }

View File

@ -30,9 +30,9 @@
{ {
dataGridView = new DataGridView(); dataGridView = new DataGridView();
ButtonAdd = new Button(); ButtonAdd = new Button();
ButtonRef = new Button();
ButtonDel = new Button();
ButtonUpd = new Button(); ButtonUpd = new Button();
ButtonDel = new Button();
ButtonRef = new Button();
((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit();
SuspendLayout(); SuspendLayout();
// //
@ -41,6 +41,7 @@
dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridView.Dock = DockStyle.Left; dataGridView.Dock = DockStyle.Left;
dataGridView.Location = new Point(0, 0); dataGridView.Location = new Point(0, 0);
dataGridView.Margin = new Padding(3, 2, 3, 2);
dataGridView.MultiSelect = false; dataGridView.MultiSelect = false;
dataGridView.Name = "dataGridView"; dataGridView.Name = "dataGridView";
dataGridView.ReadOnly = true; dataGridView.ReadOnly = true;
@ -48,62 +49,71 @@
dataGridView.RowHeadersWidth = 51; dataGridView.RowHeadersWidth = 51;
dataGridView.RowTemplate.Height = 29; dataGridView.RowTemplate.Height = 29;
dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView.Size = new Size(628, 450); dataGridView.Size = new Size(550, 338);
dataGridView.TabIndex = 0; dataGridView.TabIndex = 0;
// //
// ButtonAdd // ButtonAdd
// //
ButtonAdd.Location = new Point(671, 47); ButtonAdd.Anchor = AnchorStyles.Top | AnchorStyles.Right;
ButtonAdd.Location = new Point(587, 35);
ButtonAdd.Margin = new Padding(3, 2, 3, 2);
ButtonAdd.Name = "ButtonAdd"; ButtonAdd.Name = "ButtonAdd";
ButtonAdd.Size = new Size(94, 29); ButtonAdd.Size = new Size(82, 22);
ButtonAdd.TabIndex = 1; ButtonAdd.TabIndex = 1;
ButtonAdd.Text = "Добавить"; ButtonAdd.Text = "Добавить";
ButtonAdd.UseVisualStyleBackColor = true; ButtonAdd.UseVisualStyleBackColor = true;
ButtonAdd.Click += ButtonAdd_Click; ButtonAdd.Click += ButtonAdd_Click;
// //
// ButtonRef // ButtonUpd
// //
ButtonRef.Location = new Point(671, 94); ButtonUpd.Anchor = AnchorStyles.Top | AnchorStyles.Right;
ButtonRef.Name = "ButtonRef"; ButtonUpd.Location = new Point(587, 70);
ButtonRef.Size = new Size(94, 29); ButtonUpd.Margin = new Padding(3, 2, 3, 2);
ButtonRef.TabIndex = 2; ButtonUpd.Name = "ButtonUpd";
ButtonRef.Text = "Изменить"; ButtonUpd.Size = new Size(82, 22);
ButtonRef.UseVisualStyleBackColor = true; ButtonUpd.TabIndex = 2;
ButtonRef.Click += ButtonRef_Click; ButtonUpd.Text = "Изменить";
ButtonUpd.UseVisualStyleBackColor = true;
ButtonUpd.Click += ButtonUpd_Click;
// //
// ButtonDel // ButtonDel
// //
ButtonDel.Location = new Point(671, 144); ButtonDel.Anchor = AnchorStyles.Top | AnchorStyles.Right;
ButtonDel.Location = new Point(587, 108);
ButtonDel.Margin = new Padding(3, 2, 3, 2);
ButtonDel.Name = "ButtonDel"; ButtonDel.Name = "ButtonDel";
ButtonDel.Size = new Size(94, 29); ButtonDel.Size = new Size(82, 22);
ButtonDel.TabIndex = 3; ButtonDel.TabIndex = 3;
ButtonDel.Text = "Удалить"; ButtonDel.Text = "Удалить";
ButtonDel.UseVisualStyleBackColor = true; ButtonDel.UseVisualStyleBackColor = true;
ButtonDel.Click += ButtonDel_Click; ButtonDel.Click += ButtonDel_Click;
// //
// ButtonUpd // ButtonRef
// //
ButtonUpd.Location = new Point(671, 191); ButtonRef.Anchor = AnchorStyles.Top | AnchorStyles.Right;
ButtonUpd.Name = "ButtonUpd"; ButtonRef.Location = new Point(587, 143);
ButtonUpd.Size = new Size(94, 29); ButtonRef.Margin = new Padding(3, 2, 3, 2);
ButtonUpd.TabIndex = 4; ButtonRef.Name = "ButtonRef";
ButtonUpd.Text = "Обновить"; ButtonRef.Size = new Size(82, 22);
ButtonUpd.UseVisualStyleBackColor = true; ButtonRef.TabIndex = 4;
ButtonUpd.Click += ButtonUpd_Click; ButtonRef.Text = "Обновить";
ButtonRef.UseVisualStyleBackColor = true;
ButtonRef.Click += ButtonRef_Click;
// //
// FormComponents // FormComponents
// //
AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450); ClientSize = new Size(700, 338);
Controls.Add(ButtonUpd);
Controls.Add(ButtonDel);
Controls.Add(ButtonRef); Controls.Add(ButtonRef);
Controls.Add(ButtonDel);
Controls.Add(ButtonUpd);
Controls.Add(ButtonAdd); Controls.Add(ButtonAdd);
Controls.Add(dataGridView); Controls.Add(dataGridView);
Margin = new Padding(3, 2, 3, 2);
Name = "FormComponents"; Name = "FormComponents";
Text = "Компоненты"; Text = "Ингредиенты";
Load += FormComponent_Load; Load += FormComponents_Load;
((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit();
ResumeLayout(false); ResumeLayout(false);
} }
@ -112,8 +122,8 @@
private DataGridView dataGridView; private DataGridView dataGridView;
private Button ButtonAdd; private Button ButtonAdd;
private Button ButtonRef;
private Button ButtonDel;
private Button ButtonUpd; private Button ButtonUpd;
private Button ButtonDel;
private Button ButtonRef;
} }
} }

View File

@ -1,8 +1,6 @@
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.VisualBasic.Logging;
using PizzeriaContracts.BindingModels; using PizzeriaContracts.BindingModels;
using PizzeriaContracts.BusinessLogicsContracts; using PizzeriaContracts.BusinessLogicsContracts;
namespace Pizzeria namespace Pizzeria
{ {
public partial class FormComponents : Form public partial class FormComponents : Form
@ -16,7 +14,7 @@ namespace Pizzeria
_logic = logic; _logic = logic;
} }
private void FormComponent_Load(object sender, EventArgs e) private void FormComponents_Load(object sender, EventArgs e)
{ {
LoadData(); LoadData();
} }
@ -31,11 +29,11 @@ namespace Pizzeria
dataGridView.Columns["Id"].Visible = false; dataGridView.Columns["Id"].Visible = false;
dataGridView.Columns["ComponentName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; dataGridView.Columns["ComponentName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
} }
_logger.LogInformation("Загрузка компонентов"); _logger.LogInformation("Загрузка ингредиентов");
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Ошибка загрузки компонентов"); _logger.LogError(ex, "Ошибка загрузки ингредиентов");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,MessageBoxIcon.Error); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,MessageBoxIcon.Error);
} }
} }
@ -62,12 +60,10 @@ namespace Pizzeria
{ {
if (dataGridView.SelectedRows.Count == 1) if (dataGridView.SelectedRows.Count == 1)
{ {
if (MessageBox.Show("Удалить запись?", "Вопрос", if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{ {
int id = int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); _logger.LogInformation("Удаление ингредиента");
_logger.LogInformation("Удаление компонента");
try try
{ {
if (!_logic.Delete(new ComponentBindingModel if (!_logic.Delete(new ComponentBindingModel
@ -81,9 +77,8 @@ namespace Pizzeria
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.LogError(ex, "Ошибка удаления компонента"); _logger.LogError(ex, "Ошибка удаления ингредиента");
MessageBox.Show(ex.Message, "Ошибка", MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBoxButtons.OK, MessageBoxIcon.Error);
} }
} }
} }
@ -94,12 +89,10 @@ namespace Pizzeria
{ {
if (dataGridView.SelectedRows.Count == 1) if (dataGridView.SelectedRows.Count == 1)
{ {
var service = var service = Program.ServiceProvider?.GetService(typeof(FormComponent));
Program.ServiceProvider?.GetService(typeof(FormComponent));
if (service is FormComponent form) if (service is FormComponent form)
{ {
form.Id = form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
if (form.ShowDialog() == DialogResult.OK) if (form.ShowDialog() == DialogResult.OK)
{ {
LoadData(); LoadData();

View File

@ -20,15 +20,12 @@ namespace Pizzeria
[STAThread] [STAThread]
static void Main() static void Main()
{ {
/*/ To customize application configuration such as set high DPI
settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize(); ApplicationConfiguration.Initialize();
var services = new ServiceCollection(); var services = new ServiceCollection();
ConfigureServices(services); ConfigureServices(services);
_serviceProvider = services.BuildServiceProvider(); _serviceProvider = services.BuildServiceProvider();
Application.Run(_serviceProvider.GetRequiredService<FormMain>()); Application.Run(_serviceProvider.GetRequiredService<FormMain>());
*/
} }
private static void ConfigureServices(ServiceCollection services) private static void ConfigureServices(ServiceCollection services)
{ {
@ -39,17 +36,17 @@ namespace Pizzeria
}); });
services.AddTransient<IComponentStorage, ComponentStorage>(); services.AddTransient<IComponentStorage, ComponentStorage>();
services.AddTransient<IOrderStorage, OrderStorage>(); services.AddTransient<IOrderStorage, OrderStorage>();
services.AddTransient<IProductStorage, ProductStorage>(); services.AddTransient<IPizzaStorage, PizzaStorage>();
services.AddTransient<IComponentLogic, ComponentLogic>(); services.AddTransient<IComponentLogic, ComponentLogic>();
services.AddTransient<IOrderLogic, OrderLogic>(); services.AddTransient<IOrderLogic, OrderLogic>();
services.AddTransient<IProductLogic, ProductLogic>(); services.AddTransient<IPizzaLogic, PizzaLogic>();
services.AddTransient<FormMain>(); services.AddTransient<FormMain>();
services.AddTransient<FormComponent>(); services.AddTransient<FormComponent>();
services.AddTransient<FormComponents>(); services.AddTransient<FormComponents>();
services.AddTransient<FormCreateOrder>(); services.AddTransient<FormCreateOrder>();
services.AddTransient<FormProduct>(); services.AddTransient<FormPizza>();
services.AddTransient<FormProductComponent>(); services.AddTransient<FormPizzaComponent>();
services.AddTransient<FormProducts>(); services.AddTransient<FormPizzas>();
} }
} }
} }

View File

@ -13,9 +13,10 @@ namespace PizzeriaBusinessLogic.BusinessLogic
private readonly IComponentStorage _componentStorage; private readonly IComponentStorage _componentStorage;
public ComponentLogic(ILogger<ComponentLogic> logger, IComponentStorage componentStorage) public ComponentLogic(ILogger<ComponentLogic> logger, IComponentStorage componentStorage)
{ {
//_logger = logger; _logger = logger;
_componentStorage = componentStorage; _componentStorage = componentStorage;
} }
public List<ComponentViewModel>? ReadList(ComponentSearchModel? model) public List<ComponentViewModel>? ReadList(ComponentSearchModel? model)
{ {
_logger.LogInformation("ReadList. ComponentName:{ComponentName}.Id:{ Id}", model?.ComponentName, model?.Id); _logger.LogInformation("ReadList. ComponentName:{ComponentName}.Id:{ Id}", model?.ComponentName, model?.Id);
@ -88,17 +89,17 @@ namespace PizzeriaBusinessLogic.BusinessLogic
} }
if (string.IsNullOrEmpty(model.ComponentName)) if (string.IsNullOrEmpty(model.ComponentName))
{ {
throw new ArgumentNullException("Нет названия компонента", nameof(model.ComponentName)); throw new ArgumentNullException("Нет названия ингредиента", nameof(model.ComponentName));
} }
if (model.Cost <= 0) if (model.Cost <= 0)
{ {
throw new ArgumentNullException("Цена компонента должна быть больше 0", nameof(model.Cost)); throw new ArgumentNullException("Цена ингредиента должна быть больше 0", nameof(model.Cost));
} }
_logger.LogInformation("Component. ComponentName:{ComponentName}.Cost:{ Cost}. Id: { Id}", model.ComponentName, model.Cost, model.Id); _logger.LogInformation("Component. ComponentName:{ComponentName}.Cost:{ Cost}. Id: { Id}", model.ComponentName, model.Cost, model.Id);
var element = _componentStorage.GetElement(new ComponentSearchModel{ ComponentName = model.ComponentName }); var element = _componentStorage.GetElement(new ComponentSearchModel{ ComponentName = model.ComponentName });
if (element != null && element.Id != model.Id) if (element != null && element.Id != model.Id)
{ {
throw new InvalidOperationException("Компонент с таким названием уже есть"); throw new InvalidOperationException("ингредиент с таким названием уже есть");
} }
} }
} }

View File

@ -4,8 +4,7 @@ using PizzeriaContracts.BusinessLogicsContracts;
using PizzeriaContracts.SearchModels; using PizzeriaContracts.SearchModels;
using PizzeriaContracts.StorageContracts; using PizzeriaContracts.StorageContracts;
using PizzeriaContracts.ViewModels; using PizzeriaContracts.ViewModels;
using PizzeriaDataModels.Enums; using PizzeriaDataModels.Enums;
using System.Xml.Linq;
namespace PizzeriaBusinessLogic.BusinessLogic namespace PizzeriaBusinessLogic.BusinessLogic
{ {
@ -20,15 +19,15 @@ namespace PizzeriaBusinessLogic.BusinessLogic
_logger = logger; _logger = logger;
_orderStorage = orderStorage; _orderStorage = orderStorage;
} }
public bool CreateOrder(OrderBindingModel model) public bool CreateOrder(OrderBindingModel model)
{ {
CheckModel(model); CheckModel(model);
if (model.Status == OrderStatus.Неизвестен) if (model.Status != OrderStatus.Неизвестен)
{ {
_logger.LogWarning("Order status unknown"); _logger.LogWarning("Invalid order status");
return false; return false;
} }
model.Status = OrderStatus.Принят; model.Status = OrderStatus.Принят;
@ -39,6 +38,8 @@ namespace PizzeriaBusinessLogic.BusinessLogic
} }
return true; return true;
} }
public List<OrderViewModel>? ReadList(OrderSearchModel? model) public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{ {
_logger.LogInformation("ReadList. OrderId: {Id}.", model?.Id); _logger.LogInformation("ReadList. OrderId: {Id}.", model?.Id);
@ -68,73 +69,44 @@ namespace PizzeriaBusinessLogic.BusinessLogic
} }
public bool ChangeStatus(OrderBindingModel model, OrderStatus status) public bool ChangeStatus(OrderBindingModel model, OrderStatus status)
{ {
/*Неизвестен = -1,
Принят = 0,
Выполняется = 1,
Готов = 2,
Выдан = 3*/
CheckModel(model, false); CheckModel(model, false);
var order = _orderStorage.GetElement(new OrderSearchModel { var order = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
Id = model.Id
});
if (order == null) if (order == null)
{ {
throw new ArgumentNullException(nameof(order)); throw new ArgumentNullException(nameof(order));
} }
model.PizzaId = order.PizzaId;
model.ProductId = order.ProductId; model.Count = order.Count;
model.Sum = order.Count;
model.DateCreate = order.DateCreate; model.DateCreate = order.DateCreate;
model.DateImplement = order.DateImplement; model.DateImplement = order.DateImplement;
model.Status = order.Status; model.Status = order.Status;
model.Count = order.Count;
model.Sum = order.Sum;
return true;
/*
if (model.Status != status - 1) if (model.Status != status - 1)
{ {
_logger.LogWarning("Status update to " + status + " operation failed"); _logger.LogWarning("Status update to " + status + " operation failed");
return false; return false;
} }
model.Status = status; model.Status = status;
if (model.Status == OrderStatus.Выдан) if (model.Status == OrderStatus.Выдан)
{ {
model.DateImplement = DateTime.Now; model.DateImplement = DateTime.Now;
} }
if (_orderStorage.Update(model) == null) if (_orderStorage.Update(model) == null)
{ {
model.Status--; model.Status--;
_logger.LogWarning("Changing status operation faled"); _logger.LogWarning("Changing status operation faled");
return false; return false;
} }
*/
/*
if (model.Status == OrderStatus.Готов)
{
model.DateImplement = DateTime.Now;
}
else
{
model.DateImplement = order.DateImplement;
}
if (_orderStorage.Update(model) == null)
{
_logger.LogWarning("Change status operation failed");
return false;
}
return true; return true;
*/
} }
private void CheckModel(OrderBindingModel model, bool withParams = true) private void CheckModel(OrderBindingModel model, bool withParams = true)
{ {
if (model == null) if (model == null)
{ {
throw new ArgumentNullException(nameof(model)); throw new ArgumentNullException(nameof(model));
@ -143,22 +115,22 @@ namespace PizzeriaBusinessLogic.BusinessLogic
{ {
return; return;
} }
if (model.ProductId <= 0) if (model.PizzaId <= 0)
{ {
throw new ArgumentNullException("Некорректный идентификатор изделия", nameof(model.ProductId)); throw new ArgumentNullException("Некорректный идентификатор пиццы", nameof(model.PizzaId));
} }
if (model.Count <= 0) if (model.Count <= 0)
{ {
throw new ArgumentNullException("В заказе должно быть хотя бы одно изделие", nameof(model.Count)); throw new ArgumentNullException("В заказе должно быть хотя бы одно пицца", nameof(model.Count));
} }
if (model.Sum <= 0) if (model.Sum <= 0)
{ {
throw new ArgumentNullException("Стоимость заказа должна быть больше 0", nameof(model.Sum)); throw new ArgumentNullException("Стоимость заказа должна быть больше 0", nameof(model.Sum));
} }
_logger.LogInformation("Order. Id: {Id}. Sum: {Sum}. ProductId: {ProductId}. ReinforcedCount: {Count}", model.Id, model.Sum, model.ProductId, model.Count); _logger.LogInformation("Order. Id: {Id}. Sum: {Sum}. PizzaId: {PizzaId}. ReinforcedCount: {Count}", model.Id, model.Sum, model.PizzaId, model.Count);
} }
} }
} }

View File

@ -1,106 +0,0 @@
using Microsoft.Extensions.Logging;
using PizzeriaContracts.BindingModels;
using PizzeriaContracts.BusinessLogicsContracts;
using PizzeriaContracts.SearchModels;
using PizzeriaContracts.StorageContracts;
using PizzeriaContracts.ViewModels;
namespace PizzeriaBusinessLogic.BusinessLogic
{
public class ProductLogic : IProductLogic
{
private readonly ILogger _logger;
private readonly IProductStorage _productStorage;
public ProductLogic(ILogger<ProductLogic> logger, IProductStorage ProductStorage)
{
//_logger = logger;
_productStorage = ProductStorage;
}
public List<ProductViewModel>? ReadList(ProductSearchModel? model)
{
_logger.LogInformation("ReadList. ProductName:{ProductName}.Id:{ Id}", model?.ProductName, model?.Id);
var list = model == null ? _productStorage.GetFullList() :
_productStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public ProductViewModel? ReadElement(ProductSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. ProductName:{ProductName}.Id:{ Id} ", model.ProductName, model.Id);
var element = _productStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public bool Create(ProductBindingModel model)
{
CheckModel(model);
if (_productStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(ProductBindingModel model)
{
CheckModel(model);
if (_productStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(ProductBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_productStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(ProductBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.ProductName))
{
throw new ArgumentNullException("Нет названия продукта", nameof(model.ProductName));
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Цена продукта должна быть больше 0", nameof(model.Price));
}
_logger.LogInformation("Product. ProductName:{ProductName}.Price:{ Price}. Id: { Id}", model.ProductName, model.Price, model.Id);
var element = _productStorage.GetElement(new ProductSearchModel { ProductName = model.ProductName });
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Продукт с таким названием уже есть");
}
}
}
}

View File

@ -6,7 +6,7 @@ namespace PizzeriaContracts.BindingModels
public class OrderBindingModel : IOrderModel public class OrderBindingModel : IOrderModel
{ {
public int Id { get; set; } public int Id { get; set; }
public int ProductId { get; set; } public int PizzaId { get; set; }
public int Count { get; set; } public int Count { get; set; }
public double Sum { get; set; } public double Sum { get; set; }
public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; public OrderStatus Status { get; set; } = OrderStatus.Неизвестен;

View File

@ -1,12 +0,0 @@
using PizzeriaDataModels.Models;
namespace PizzeriaContracts.BindingModels
{
public class ProductBindingModel : IProductModel
{
public int Id { get; set; }
public string ProductName { get; set; } = string.Empty;
public double Price { get; set; }
public Dictionary<int, (IComponentModel, int)> ProductComponents {get;set;} = new();
}
}

View File

@ -1,15 +0,0 @@
using PizzeriaContracts.BindingModels;
using PizzeriaContracts.SearchModels;
using PizzeriaContracts.ViewModels;
namespace PizzeriaContracts.BusinessLogicsContracts
{
public interface IProductLogic
{
List<ProductViewModel>? ReadList(ProductSearchModel? model);
ProductViewModel? ReadElement(ProductSearchModel model);
bool Create(ProductBindingModel model);
bool Update(ProductBindingModel model);
bool Delete(ProductBindingModel model);
}
}

View File

@ -1,8 +0,0 @@
namespace PizzeriaContracts.SearchModels
{
public class ProductSearchModel
{
public int? Id { get; set; }
public string? ProductName { get; set; }
}
}

View File

@ -1,17 +0,0 @@
using PizzeriaContracts.BindingModels;
using PizzeriaContracts.SearchModels;
using PizzeriaContracts.ViewModels;
namespace PizzeriaContracts.StorageContracts
{
public interface IProductStorage
{
List<ProductViewModel> GetFullList();
List<ProductViewModel> GetFilteredList(ProductSearchModel model);
ProductViewModel? GetElement(ProductSearchModel model);
ProductViewModel? Insert(ProductBindingModel model);
ProductViewModel? Update(ProductBindingModel model);
ProductViewModel? Delete(ProductBindingModel model);
}
}

View File

@ -6,7 +6,7 @@ namespace PizzeriaContracts.ViewModels
public class ComponentViewModel : IComponentModel public class ComponentViewModel : IComponentModel
{ {
public int Id { get; set; } public int Id { get; set; }
[DisplayName("Название компонента")] [DisplayName("Название ингредиента")]
public string ComponentName { get; set; } = string.Empty; public string ComponentName { get; set; } = string.Empty;
[DisplayName("Цена")] [DisplayName("Цена")]
public double Cost { get; set; } public double Cost { get; set; }

View File

@ -8,10 +8,10 @@ namespace PizzeriaContracts.ViewModels
{ {
[DisplayName("Номер")] [DisplayName("Номер")]
public int Id { get; set; } public int Id { get; set; }
public int ProductId { get; set; } public int PizzaId { get; set; }
[DisplayName("Изделие")] [DisplayName("Пицца")]
public string ProductName { get; set; } = string.Empty; public string PizzaName { get; set; } = string.Empty;
[DisplayName("Количество")] [DisplayName("Количество")]
public int Count { get; set; } public int Count { get; set; }

View File

@ -1,15 +0,0 @@
using PizzeriaDataModels.Models;
using System.ComponentModel;
namespace PizzeriaContracts.ViewModels
{
public class ProductViewModel : IProductModel
{
public int Id { get; set; }
[DisplayName("Название изделия")]
public string ProductName { get; set; } = string.Empty;
[DisplayName("Цена")]
public double Price { get; set; }
public Dictionary<int, (IComponentModel, int)> ProductComponents {get;set;} = new();
}
}

View File

@ -4,7 +4,7 @@ namespace PizzeriaDataModels.Models
{ {
public interface IOrderModel : IId public interface IOrderModel : IId
{ {
int ProductId { get; } int PizzaId { get; }
int Count { get; } int Count { get; }
double Sum { get; } double Sum { get; }
OrderStatus Status { get; } OrderStatus Status { get; }

View File

@ -1,9 +0,0 @@
namespace PizzeriaDataModels.Models
{
public interface IProductModel : IId
{
string ProductName { get; }
double Price { get; }
Dictionary<int, (IComponentModel, int)> ProductComponents { get; }
}
}

View File

@ -7,12 +7,12 @@ namespace PizzeriaListImplement
private static DataListSingleton? _instance; private static DataListSingleton? _instance;
public List<Component> Components { get; set; } public List<Component> Components { get; set; }
public List<Order> Orders { get; set; } public List<Order> Orders { get; set; }
public List<Product> Products { get; set; } public List<Pizza> Pizzas { get; set; }
private DataListSingleton() private DataListSingleton()
{ {
Components = new List<Component>(); Components = new List<Component>();
Orders = new List<Order>(); Orders = new List<Order>();
Products = new List<Product>(); Pizzas = new List<Pizza>();
} }
public static DataListSingleton GetInstance() public static DataListSingleton GetInstance()
{ {

View File

@ -3,6 +3,7 @@ using PizzeriaContracts.SearchModels;
using PizzeriaContracts.StorageContracts; using PizzeriaContracts.StorageContracts;
using PizzeriaContracts.ViewModels; using PizzeriaContracts.ViewModels;
using PizzeriaListImplement.Models; using PizzeriaListImplement.Models;
using System.Xml.Linq;
namespace PizzeriaListImplement.Implements namespace PizzeriaListImplement.Implements
{ {
@ -22,7 +23,7 @@ namespace PizzeriaListImplement.Implements
{ {
var element = _source.Orders[i]; var element = _source.Orders[i];
_source.Orders.RemoveAt(i); _source.Orders.RemoveAt(i);
return element.GetViewModel; return AddPizzaName(element.GetViewModel);
} }
} }
return null; return null;
@ -37,7 +38,7 @@ namespace PizzeriaListImplement.Implements
{ {
if ((model.Id.HasValue && order.Id == model.Id)) if ((model.Id.HasValue && order.Id == model.Id))
{ {
return order.GetViewModel; return AddPizzaName(order.GetViewModel);
} }
} }
return null; return null;
@ -54,7 +55,7 @@ namespace PizzeriaListImplement.Implements
{ {
if (order.Id == model.Id) if (order.Id == model.Id)
{ {
result.Add(order.GetViewModel); result.Add(AddPizzaName(order.GetViewModel));
} }
} }
return result; return result;
@ -65,7 +66,7 @@ namespace PizzeriaListImplement.Implements
var result = new List<OrderViewModel>(); var result = new List<OrderViewModel>();
foreach (var order in _source.Orders) foreach (var order in _source.Orders)
{ {
result.Add(order.GetViewModel); result.Add(AddPizzaName(order.GetViewModel));
} }
return result; return result;
} }
@ -85,7 +86,7 @@ namespace PizzeriaListImplement.Implements
return null; return null;
} }
_source.Orders.Add(newOrder); _source.Orders.Add(newOrder);
return newOrder.GetViewModel; return AddPizzaName(newOrder.GetViewModel);
} }
public OrderViewModel? Update(OrderBindingModel model) public OrderViewModel? Update(OrderBindingModel model)
{ {
@ -94,11 +95,21 @@ namespace PizzeriaListImplement.Implements
if (order.Id == model.Id) if (order.Id == model.Id)
{ {
order.Update(model); order.Update(model);
return order.GetViewModel; return AddPizzaName(order.GetViewModel);
} }
} }
return null; return null;
} }
private OrderViewModel AddPizzaName(OrderViewModel model)
{
var selectedPizza = _source.Pizzas.Find(pizza => pizza.Id == model.PizzaId);
if (selectedPizza != null)
{
model.PizzaName = selectedPizza.PizzaName;
}
return model;
}
} }
} }

View File

@ -1,100 +0,0 @@
using PizzeriaContracts.BindingModels;
using PizzeriaContracts.SearchModels;
using PizzeriaContracts.StorageContracts;
using PizzeriaContracts.ViewModels;
using PizzeriaListImplement.Models;
namespace PizzeriaListImplement.Implements
{
public class ProductStorage : IProductStorage
{
private readonly DataListSingleton _source;
public ProductStorage()
{
_source = DataListSingleton.GetInstance();
}
public List<ProductViewModel> GetFullList()
{
var result = new List<ProductViewModel>();
foreach (var product in _source.Products)
{
result.Add(product.GetViewModel);
}
return result;
}
public List<ProductViewModel> GetFilteredList(ProductSearchModel model)
{
var result = new List<ProductViewModel>();
if (string.IsNullOrEmpty(model.ProductName))
{
return result;
}
foreach (var product in _source.Products)
{
if (product.ProductName.Contains(model.ProductName))
{
result.Add(product.GetViewModel);
}
}
return result;
}
public ProductViewModel? GetElement(ProductSearchModel model)
{
if (string.IsNullOrEmpty(model.ProductName) && !model.Id.HasValue)
{
return null;
}
foreach (var product in _source.Products)
{
if ((!string.IsNullOrEmpty(model.ProductName) && product.ProductName == model.ProductName) || (model.Id.HasValue && product.Id == model.Id))
{
return product.GetViewModel;
}
}
return null;
}
public ProductViewModel? Insert(ProductBindingModel model)
{
model.Id = 1;
foreach (var product in _source.Products)
{
if (model.Id <= product.Id)
{
model.Id = product.Id + 1;
}
}
var newProduct = Product.Create(model);
if (newProduct == null)
{
return null;
}
_source.Products.Add(newProduct);
return newProduct.GetViewModel;
}
public ProductViewModel? Update(ProductBindingModel model)
{
foreach (var product in _source.Products)
{
if (product.Id == model.Id)
{
product.Update(model);
return product.GetViewModel;
}
}
return null;
}
public ProductViewModel? Delete(ProductBindingModel model)
{
for (int i = 0; i < _source.Products.Count; ++i)
{
if (_source.Products[i].Id == model.Id)
{
var element = _source.Products[i];
_source.Products.RemoveAt(i);
return element.GetViewModel;
}
}
return null;
}
}
}

View File

@ -16,7 +16,7 @@ namespace PizzeriaListImplement.Models
{ {
public int Id { get; private set; } public int Id { get; private set; }
public int ProductId { get; private set; } public int PizzaId { get; private set; }
public int Count { get; private set; } public int Count { get; private set; }
@ -37,7 +37,7 @@ namespace PizzeriaListImplement.Models
return new Order() return new Order()
{ {
Id = model.Id, Id = model.Id,
ProductId = model.ProductId, PizzaId = model.PizzaId,
Count = model.Count, Count = model.Count,
Sum = model.Sum, Sum = model.Sum,
Status = model.Status, Status = model.Status,
@ -58,7 +58,7 @@ namespace PizzeriaListImplement.Models
public OrderViewModel GetViewModel => new() public OrderViewModel GetViewModel => new()
{ {
Id = Id, Id = Id,
ProductId = ProductId, PizzaId = PizzaId,
Count = Count, Count = Count,
Sum = Sum, Sum = Sum,
Status = Status, Status = Status,

View File

@ -1,49 +0,0 @@
using PizzeriaContracts.BindingModels;
using PizzeriaContracts.ViewModels;
using PizzeriaDataModels.Models;
namespace PizzeriaListImplement.Models
{
public class Product : IProductModel
{
public int Id { get; private set; }
public string ProductName { get; private set; } = string.Empty;
public double Price { get; private set; }
public Dictionary<int, (IComponentModel, int)> ProductComponents
{
get;
private set;
} = new Dictionary<int, (IComponentModel, int)>();
public static Product? Create(ProductBindingModel? model)
{
if (model == null)
{
return null;
}
return new Product()
{
Id = model.Id,
ProductName = model.ProductName,
Price = model.Price,
ProductComponents = model.ProductComponents
};
}
public void Update(ProductBindingModel? model)
{
if (model == null)
{
return;
}
ProductName = model.ProductName;
Price = model.Price;
ProductComponents = model.ProductComponents;
}
public ProductViewModel GetViewModel => new()
{
Id = Id,
ProductName = ProductName,
Price = Price,
ProductComponents = ProductComponents
};
}
}