конец

This commit is contained in:
Марат Заргаров 2023-03-05 22:35:21 +04:00
parent 0da9e0b8db
commit 79bb717315
20 changed files with 160 additions and 129 deletions

View File

@ -9,9 +9,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaContracts", "Pizzer
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaBusinessLogic", "PizzeriaBusinessLogic\PizzeriaBusinessLogic.csproj", "{D8EAAB67-47C6-443D-83FC-E1337F105F67}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaListImplement", "PizzeriaListImplement\PizzeriaListImplement.csproj", "{922551CE-15B0-42C8-AA1A-368C8399CAD6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaListImplement", "PizzeriaListImplement\PizzeriaListImplement.csproj", "{922551CE-15B0-42C8-AA1A-368C8399CAD6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PizzeriaView", "PizzeriaView\PizzeriaView.csproj", "{25FCC09C-88AE-4515-9235-FB9C17972034}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzeriaView", "PizzeriaView\PizzeriaView.csproj", "{25FCC09C-88AE-4515-9235-FB9C17972034}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution

View File

@ -17,14 +17,74 @@ namespace PizzeriaBusinessLogic
{
private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
{
_logger = logger;
_orderStorage = orderStorage;
}
public bool CreateOrder(OrderBindingModel model)
{
CheckModel(model);
if (model.Status != OrderStatus.Неизвестен) return false;
model.Status = OrderStatus.Принят;
if (_orderStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool DeliveryOrder(OrderBindingModel model)
{
CheckModel(model, false);
var element = _orderStorage.GetElement(new OrderSearchModel
{
Id = model.Id
});
if (element == null)
{
_logger.LogWarning("Read operation failed");
return false;
}
if (element.Status != OrderStatus.Готов)
{
_logger.LogWarning("Status change operation failed");
throw new InvalidOperationException("Заказ должен быть переведен в статус готовности перед выдачей!");
}
model.Status = OrderStatus.Выдан;
model.DateImplement = DateTime.Now;
_orderStorage.Update(model);
return true;
}
public bool FinishOrder(OrderBindingModel model)
{
CheckModel(model, false);
var element = _orderStorage.GetElement(new OrderSearchModel
{
Id = model.Id
});
if (element == null)
{
_logger.LogWarning("Read operation failed");
return false;
}
if (element.Status != OrderStatus.Выполняется)
{
_logger.LogWarning("Status change operation failed");
throw new InvalidOperationException("Заказ должен быть переведен в статус выполнения перед готовностью!");
}
model.Status = OrderStatus.Готов;
_orderStorage.Update(model);
return true;
}
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{
_logger.LogInformation("ReadList. OrderId:{Id}", model?.Id);
_logger.LogInformation("ReadList. Id:{Id}", model?.Id);
var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model);
if (list == null)
{
@ -34,31 +94,29 @@ namespace PizzeriaBusinessLogic
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public bool CreateOrder(OrderBindingModel model)
{
CheckModel(model);
if (model.Status != OrderStatus.Неизвестен)
return false;
model.Status = OrderStatus.Принят;
if (_orderStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool TakeOrderInWork(OrderBindingModel model)
{
return ChangeStatus(model, OrderStatus.Выполняется);
}
public bool FinishOrder(OrderBindingModel model)
{
return ChangeStatus(model, OrderStatus.Готов);
}
public bool DeliveryOrder(OrderBindingModel model)
{
return ChangeStatus(model, OrderStatus.Выдан);
CheckModel(model, false);
var element = _orderStorage.GetElement(new OrderSearchModel
{
Id = model.Id
});
if (element == null)
{
_logger.LogWarning("Read operation failed");
return false;
}
if (element.Status != OrderStatus.Принят)
{
_logger.LogWarning("Status change operation failed");
throw new InvalidOperationException("Заказ должен быть переведен в статус принятого перед его выполнением!");
}
model.Status = OrderStatus.Выполняется;
_orderStorage.Update(model);
return true;
}
private void CheckModel(OrderBindingModel model, bool withParams = true)
{
if (model == null)
@ -69,52 +127,23 @@ namespace PizzeriaBusinessLogic
{
return;
}
if (model.Count <= 0)
{
throw new ArgumentException("Количество пиццы в заказе не может быть меньше 1", nameof(model.Count));
}
if (model.Sum <= 0)
{
throw new ArgumentException("Стоимость заказа на может быть меньше 1", nameof(model.Sum));
throw new ArgumentNullException("Сумма заказа должна быть больше 0", nameof(model.Sum));
}
if (model.DateImplement.HasValue && model.DateImplement < model.DateCreate)
if (model.Count <= 0)
{
throw new ArithmeticException($"Дата выдачи заказа {model.DateImplement} не может быть раньше даты его создания {model.DateCreate}");
throw new ArgumentNullException("Количество изделий должно быть больше 0", nameof(model.Count));
}
_logger.LogInformation("Pizza. PizzaId:{PizzaId}.Count:{Count}.Sum:{Sum}Id:{Id}",
model.ProductId, model.Count, model.Sum, model.Id);
}
private bool ChangeStatus(OrderBindingModel model, OrderStatus requiredStatus)
{
CheckModel(model, false);
var element = _orderStorage.GetElement(new OrderSearchModel()
_logger.LogInformation("Order. Sum:{Sum}. Id:{Id}", model.Sum, model.Id);
var element = _orderStorage.GetElement(new OrderSearchModel
{
Id = model.Id
});
if (element == null)
if (element != null && element.Id != model.Id)
{
throw new ArgumentNullException(nameof(element));
throw new InvalidOperationException("Заказ с таким ID уже существует");
}
model.DateCreate = element.DateCreate;
model.ProductId = element.ProductId;
model.DateImplement = element.DateImplement;
model.Status = element.Status;
model.Count = element.Count;
model.Sum = element.Sum;
if (requiredStatus - model.Status == 1)
{
model.Status = requiredStatus;
if (model.Status == OrderStatus.Выдан)
model.DateImplement = DateTime.Now;
if (_orderStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
_logger.LogWarning("Changing status operation faled: Current-{Status}:required-{requiredStatus}.", model.Status, requiredStatus);
throw new ArgumentException($"Невозможно присвоить статус {requiredStatus} заказу с текущим статусом {model.Status}");
}
}
}

View File

@ -23,7 +23,7 @@ namespace PizzeriaBusinessLogic
}
public List<PizzaViewModel>? ReadList(PizzaSearchModel? model)
{
_logger.LogInformation("ReadList. PizzaName:{PizzaName}.Id:{ Id}", model?.ProductName, model?.Id);
_logger.LogInformation("ReadList. PizzaName:{PizzaName}.Id:{ Id}", model?.PizzaName, model?.Id);
var list = model == null ? _PizzaStorage.GetFullList() : _PizzaStorage.GetFilteredList(model);
if (list == null)
{
@ -39,7 +39,7 @@ namespace PizzeriaBusinessLogic
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. PizzaName:{PizzaName}.Id:{ Id}", model.ProductName, model.Id);
_logger.LogInformation("ReadElement. PizzaName:{PizzaName}.Id:{ Id}", model.PizzaName, model.Id);
var element = _PizzaStorage.GetElement(model);
if (element == null)
{
@ -90,22 +90,22 @@ namespace PizzeriaBusinessLogic
{
return;
}
if (string.IsNullOrEmpty(model.ProductName))
if (string.IsNullOrEmpty(model.PizzaName))
{
throw new ArgumentNullException("Нет названия пиццы", nameof(model.ProductName));
throw new ArgumentNullException("Нет названия пиццы", nameof(model.PizzaName));
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Цена пиццы должна быть больше 0", nameof(model.Price));
}
if (model.ProductComponents == null || model.ProductComponents.Count == 0)
if (model.PizzaComponents == null || model.PizzaComponents.Count == 0)
{
throw new ArgumentNullException("Перечень ингредиентов не может быть пустым", nameof(model.ProductComponents));
throw new ArgumentNullException("Перечень ингредиентов не может быть пустым", nameof(model.PizzaComponents));
}
_logger.LogInformation("Pizza. PizzaName:{PizzaName}.Price:{Price}.Id: { Id}", model.ProductName, model.Price, model.Id);
_logger.LogInformation("Pizza. PizzaName:{PizzaName}.Price:{Price}.Id: { Id}", model.PizzaName, model.Price, model.Id);
var element = _PizzaStorage.GetElement(new PizzaSearchModel
{
ProductName = model.ProductName
PizzaName = model.PizzaName
});
if (element != null && element.Id != model.Id)
{

View File

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

View File

@ -10,9 +10,9 @@ namespace PizzeriaContracts.BindingModels
public class PizzaBindingModel : IPizzaModel
{
public int Id { get; set; }
public string ProductName { get; set; } = string.Empty;
public string PizzaName { get; set; } = string.Empty;
public double Price { get; set; }
public Dictionary<int, (IComponentModel, int)> ProductComponents
public Dictionary<int, (IComponentModel, int)> PizzaComponents
{
get;
set;

View File

@ -9,7 +9,7 @@ namespace PizzeriaContracts.SearchModels
public class PizzaSearchModel
{
public int? Id { get; set; }
public string? ProductName { get; set; }
public string? PizzaName { get; set; }
}
}

View File

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

View File

@ -12,10 +12,10 @@ namespace PizzeriaContracts.ViewModels
{
public int Id { get; set; }
[DisplayName("Название изделия")]
public string ProductName { get; set; } = string.Empty;
public string PizzaName { get; set; } = string.Empty;
[DisplayName("Цена")]
public double Price { get; set; }
public Dictionary<int, (IComponentModel, int)> ProductComponents
public Dictionary<int, (IComponentModel, int)> PizzaComponents
{
get;
set;

View File

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

View File

@ -8,8 +8,8 @@ namespace PizzeriaDataModels
{
public interface IPizzaModel : IId
{
string ProductName { get; }
string PizzaName { get; }
double Price { get; }
Dictionary<int, (IComponentModel, int)> ProductComponents { get; }
Dictionary<int, (IComponentModel, int)> PizzaComponents { get; }
}
}

View File

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

View File

@ -105,11 +105,11 @@ namespace PizzeriaListImplement.Implements
}
private OrderViewModel AttachPizzaName(OrderViewModel model)
{
foreach (var pizza in _source.Products)
foreach (var pizza in _source.Pizzas)
{
if (pizza.Id == model.ProductId)
if (pizza.Id == model.PizzaId)
{
model.ProductName = pizza.ProductName;
model.PizzaName = pizza.PizzaName;
return model;
}
}

View File

@ -21,7 +21,7 @@ namespace PizzeriaListImplement.Implements
public List<PizzaViewModel> GetFullList()
{
var result = new List<PizzaViewModel>();
foreach (var pizzas in _source.Products)
foreach (var pizzas in _source.Pizzas)
{
result.Add(pizzas.GetViewModel);
}
@ -30,13 +30,13 @@ namespace PizzeriaListImplement.Implements
public List<PizzaViewModel> GetFilteredList(PizzaSearchModel model)
{
var result = new List<PizzaViewModel>();
if (string.IsNullOrEmpty(model.ProductName))
if (string.IsNullOrEmpty(model.PizzaName))
{
return result;
}
foreach (var pizzas in _source.Products)
foreach (var pizzas in _source.Pizzas)
{
if (pizzas.ProductName.Contains(model.ProductName))
if (pizzas.PizzaName.Contains(model.PizzaName))
{
result.Add(pizzas.GetViewModel);
}
@ -45,13 +45,13 @@ namespace PizzeriaListImplement.Implements
}
public PizzaViewModel? GetElement(PizzaSearchModel model)
{
if (string.IsNullOrEmpty(model.ProductName) && !model.Id.HasValue)
if (string.IsNullOrEmpty(model.PizzaName) && !model.Id.HasValue)
{
return null;
}
foreach (var pizzas in _source.Products)
foreach (var pizzas in _source.Pizzas)
{
if ((!string.IsNullOrEmpty(model.ProductName) && pizzas.ProductName == model.ProductName) ||
if ((!string.IsNullOrEmpty(model.PizzaName) && pizzas.PizzaName == model.PizzaName) ||
(model.Id.HasValue && pizzas.Id == model.Id))
{
return pizzas.GetViewModel;
@ -62,7 +62,7 @@ namespace PizzeriaListImplement.Implements
public PizzaViewModel? Insert(PizzaBindingModel model)
{
model.Id = 1;
foreach (var pizzas in _source.Products)
foreach (var pizzas in _source.Pizzas)
{
if (model.Id <= pizzas.Id)
{
@ -74,12 +74,12 @@ namespace PizzeriaListImplement.Implements
{
return null;
}
_source.Products.Add(newPizzas);
_source.Pizzas.Add(newPizzas);
return newPizzas.GetViewModel;
}
public PizzaViewModel? Update(PizzaBindingModel model)
{
foreach (var pizzas in _source.Products)
foreach (var pizzas in _source.Pizzas)
{
if (pizzas.Id == model.Id)
{
@ -91,12 +91,12 @@ namespace PizzeriaListImplement.Implements
}
public PizzaViewModel? Delete(PizzaBindingModel model)
{
for (int i = 0; i < _source.Products.Count; ++i)
for (int i = 0; i < _source.Pizzas.Count; ++i)
{
if (_source.Products[i].Id == model.Id)
if (_source.Pizzas[i].Id == model.Id)
{
var element = _source.Products[i];
_source.Products.RemoveAt(i);
var element = _source.Pizzas[i];
_source.Pizzas.RemoveAt(i);
return element.GetViewModel;
}
}

View File

@ -12,7 +12,7 @@ namespace PizzeriaListImplement.Models
public class Order : IOrderModel
{
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 double Sum { get; private set; }
public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен;
@ -27,7 +27,7 @@ namespace PizzeriaListImplement.Models
return new Order()
{
Id = model.Id,
ProductId = model.ProductId,
PizzaId = model.PizzaId,
Count = model.Count,
Sum = model.Sum,
Status = model.Status,
@ -42,11 +42,12 @@ namespace PizzeriaListImplement.Models
return;
}
Status = model.Status;
if (model.DateImplement.HasValue) DateImplement = model.DateImplement;
}
public OrderViewModel GetViewModel => new()
{
Id = Id,
ProductId = ProductId,
PizzaId = PizzaId,
Count = Count,
Sum = Sum,
Status = Status,

View File

@ -12,9 +12,9 @@ namespace PizzeriaListImplement.Models
public class Pizza : IPizzaModel
{
public int Id { get; private set; }
public string ProductName { get; private set; } = string.Empty;
public string PizzaName { get; private set; } = string.Empty;
public double Price { get; private set; }
public Dictionary<int, (IComponentModel, int)> ProductComponents
public Dictionary<int, (IComponentModel, int)> PizzaComponents
{
get;
private set;
@ -28,9 +28,9 @@ namespace PizzeriaListImplement.Models
return new Pizza()
{
Id = model.Id,
ProductName = model.ProductName,
PizzaName = model.PizzaName,
Price = model.Price,
ProductComponents = model.ProductComponents
PizzaComponents = model.PizzaComponents
};
}
public void Update(PizzaBindingModel? model)
@ -39,16 +39,16 @@ namespace PizzeriaListImplement.Models
{
return;
}
ProductName = model.ProductName;
PizzaName = model.PizzaName;
Price = model.Price;
ProductComponents = model.ProductComponents;
PizzaComponents = model.PizzaComponents;
}
public PizzaViewModel GetViewModel => new()
{
Id = Id,
ProductName = ProductName,
PizzaName = PizzaName,
Price = Price,
ProductComponents = ProductComponents
PizzaComponents = PizzaComponents
};
}

View File

@ -21,14 +21,14 @@ namespace PizzeriaView
private readonly ILogger _logger;
private readonly IPizzaLogic _logic;
private int? _id;
private Dictionary<int, (IComponentModel, int)> _ProductComponents;
private Dictionary<int, (IComponentModel, int)> _PizzaComponents;
public int Id { set { _id = value; } }
public FormPizza(ILogger<FormPizza> logger, IPizzaLogic logic)
{
InitializeComponent();
_logger = logger;
_logic = logic;
_ProductComponents = new Dictionary<int, (IComponentModel, int)>();
_PizzaComponents = new Dictionary<int, (IComponentModel, int)>();
}
private void FormPizza_Load(object sender, EventArgs e)
{
@ -43,9 +43,9 @@ namespace PizzeriaView
});
if (view != null)
{
textBoxName.Text = view.ProductName;
textBoxName.Text = view.PizzaName;
textBoxPrice.Text = view.Price.ToString();
_ProductComponents = view.ProductComponents ?? new
_PizzaComponents = view.PizzaComponents ?? new
Dictionary<int, (IComponentModel, int)>();
LoadData();
}
@ -63,10 +63,10 @@ namespace PizzeriaView
_logger.LogInformation("Загрузка ингредиент пиццы");
try
{
if (_ProductComponents != null)
if (_PizzaComponents != null)
{
dataGridView.Rows.Clear();
foreach (var pc in _ProductComponents)
foreach (var pc in _PizzaComponents)
{
dataGridView.Rows.Add(new object[] { pc.Key, pc.Value.Item1.ComponentName, pc.Value.Item2 });
}
@ -92,13 +92,13 @@ namespace PizzeriaView
return;
}
_logger.LogInformation("Добавление нового ингредиента:{ ComponentName}-{ Count}", form.ComponentModel.ComponentName, form.Count);
if (_ProductComponents.ContainsKey(form.Id))
if (_PizzaComponents.ContainsKey(form.Id))
{
_ProductComponents[form.Id] = (form.ComponentModel,form.Count);
_PizzaComponents[form.Id] = (form.ComponentModel,form.Count);
}
else
{
_ProductComponents.Add(form.Id, (form.ComponentModel,form.Count));
_PizzaComponents.Add(form.Id, (form.ComponentModel,form.Count));
}
LoadData();
}
@ -113,7 +113,7 @@ namespace PizzeriaView
{
int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value);
form.Id = id;
form.Count = _ProductComponents[id].Item2;
form.Count = _PizzaComponents[id].Item2;
if (form.ShowDialog() == DialogResult.OK)
{
if (form.ComponentModel == null)
@ -121,7 +121,7 @@ namespace PizzeriaView
return;
}
_logger.LogInformation("Изменение ингредиента:{ ComponentName}-{ Count}", form.ComponentModel.ComponentName, form.Count);
_ProductComponents[form.Id] = (form.ComponentModel, form.Count);
_PizzaComponents[form.Id] = (form.ComponentModel, form.Count);
LoadData();
}
}
@ -136,7 +136,7 @@ namespace PizzeriaView
try
{
_logger.LogInformation("Удаление ингредиента:{ ComponentName}-{ Count}", dataGridView.SelectedRows[0].Cells[1].Value);
_ProductComponents?.Remove(Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value));
_PizzaComponents?.Remove(Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value));
}
catch (Exception ex)
{
@ -163,7 +163,7 @@ namespace PizzeriaView
MessageBox.Show("Заполните цену", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (_ProductComponents == null || _ProductComponents.Count == 0)
if (_PizzaComponents == null || _PizzaComponents.Count == 0)
{
MessageBox.Show("Заполните ингредиенты", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
@ -174,9 +174,9 @@ namespace PizzeriaView
var model = new PizzaBindingModel
{
Id = _id ?? 0,
ProductName = textBoxName.Text,
PizzaName = textBoxName.Text,
Price = Convert.ToDouble(textBoxPrice.Text),
ProductComponents = _ProductComponents
PizzaComponents = _PizzaComponents
};
var operationResult = _id.HasValue ? _logic.Update(model) :
_logic.Create(model);
@ -202,7 +202,7 @@ namespace PizzeriaView
private double CalcPrice()
{
double price = 0;
foreach (var elem in _ProductComponents)
foreach (var elem in _PizzaComponents)
{
price += ((elem.Value.Item1?.Cost ?? 0) * elem.Value.Item2);
}

View File

@ -1,5 +1,4 @@
using PizzeriaDataModels.Models;
using Microsoft.VisualBasic.Logging;
using Microsoft.VisualBasic.Logging;
using PizzeriaContracts.BusinessLogicsContracts;
using PizzeriaContracts.ViewModels;
using System;

View File

@ -11,6 +11,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PackageReference Include="NLog.Extensions.Logging" Version="5.2.2" />
</ItemGroup>
<ItemGroup>

View File

@ -1,5 +1,6 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;
using PizzeriaBusinessLogic;
using PizzeriaContracts.BusinessLogicsContracts;
using PizzeriaContracts.StorageContracts;

View File

@ -5,7 +5,7 @@
autoReload="true" internalLogLevel="Info">
<targets>
<target xsi:type="File" name="tofile" fileName="${basedir}/carlog-${shortdate}.log" />
<target xsi:type="File" name="tofile" fileName="${basedir}/${shortdate}.log" />
</targets>
<rules>