add businessLogic

This commit is contained in:
Alina Batylkina 2023-04-09 13:00:51 +04:00
parent 1a61c57b4c
commit 5f49651fe5
33 changed files with 1425 additions and 24 deletions

View File

@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CanteenContracts", "Canteen
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CanteenDatabaseImplement", "CanteenDatabaseImplement\CanteenDatabaseImplement.csproj", "{3B63A55E-B35A-4720-B784-014E52A33112}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CanteenBusinessLogic", "CanteenBusinessLogic\CanteenBusinessLogic.csproj", "{C5D61A2C-831A-4E4F-921F-247E8E5590EE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -27,6 +29,10 @@ Global
{3B63A55E-B35A-4720-B784-014E52A33112}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3B63A55E-B35A-4720-B784-014E52A33112}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3B63A55E-B35A-4720-B784-014E52A33112}.Release|Any CPU.Build.0 = Release|Any CPU
{C5D61A2C-831A-4E4F-921F-247E8E5590EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C5D61A2C-831A-4E4F-921F-247E8E5590EE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C5D61A2C-831A-4E4F-921F-247E8E5590EE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C5D61A2C-831A-4E4F-921F-247E8E5590EE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,136 @@
using CanteenContracts.BindingModels;
using CanteenContracts.BusinessLogicsContracts;
using CanteenContracts.SearchModel;
using CanteenContracts.StoragesContracts;
using CanteenContracts.View;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CanteenBusinessLogic.BusinessLogics
{
public class CookLogic : ICookLogic
{
private readonly ILogger _logger;
private readonly ICookStorage _cookStorage;
public CookLogic(ILogger<CookLogic> logger, ICookStorage cookStorage)
{
_logger = logger;
_cookStorage = cookStorage;
}
public bool Create(CookBindingModel model)
{
CheckModel(model);
if (_cookStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(CookBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_cookStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public CookViewModel? ReadElement(CookSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. FIO: {FIO}. Id: {Id}", model.FIO, model.Id);
var element = _cookStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id: {Id}", element.Id);
return element;
}
public List<CookViewModel>? ReadList(CookSearchModel? model)
{
_logger.LogInformation("ReadList. FIO: {FIO}. Id: {Id}", model?.FIO, model?.Id);
var list = model == null ? _cookStorage.GetFullList() : _cookStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public bool Update(CookBindingModel model)
{
CheckModel(model);
if (_cookStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(CookBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.FIO))
{
throw new ArgumentNullException("Нет ФИО у повара", nameof(model.FIO));
}
if (model.ManagerId <= 0)
{
throw new ArgumentNullException("id менеджера должен дыть больше 0", nameof(model.ManagerId));
}
if (string.IsNullOrEmpty(model.Position))
{
throw new ArgumentNullException("У повара нет должности", nameof(model.Position));
}
_logger.LogInformation("Cook. FIO: {FIO}. ManagerId: {ManagerId}. Position: {Position}. Id: {Id}", model.FIO, model.ManagerId, model.Position, model.Id);
var element = _cookStorage.GetElement(new CookSearchModel { Id = model.Id });
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Такой повар уже есть");
}
}
}
}

View File

@ -0,0 +1,137 @@
using CanteenContracts.BindingModels;
using CanteenContracts.BusinessLogicsContracts;
using CanteenContracts.SearchModel;
using CanteenContracts.StoragesContracts;
using CanteenContracts.View;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CanteenBusinessLogic.BusinessLogics
{
public class DishLogic : IDishLogic
{
private readonly ILogger _logger;
private readonly IDishStorage _dishStorage;
public DishLogic(ILogger<DishLogic> logger, IDishStorage dishStorage)
{
_logger = logger;
_dishStorage = dishStorage;
}
public List<DishViewModel>? ReadList(DishSearchModel? model)
{
_logger.LogInformation("ReadList. DishName: {DishName}. Id: {Id}", model?.DishName, model?.Id);
var list = model == null ? _dishStorage.GetFullList() : _dishStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public DishViewModel? ReadElement(DishSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. DishName: {DishName}. Id: {Id}", model.DishName, model.Id);
var element = _dishStorage.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(DishBindingModel model)
{
CheckModel(model);
if (_dishStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(DishBindingModel model)
{
CheckModel(model);
if (_dishStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(DishBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_dishStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(DishBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.DishName))
{
throw new ArgumentNullException("Нет названия блюда", nameof(model.DishName));
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Цена блюда должна быть больше 0", nameof(model.Price));
}
if (model.ManagerId <= 0)
{
throw new ArgumentNullException("id менеджера должен быть больше 0", nameof(model.ManagerId));
}
_logger.LogInformation("Dish. DishName: {DishName}. Price: {Price}. ManagerId: {ManagerId}. Id: {Id}", model.DishName, model.Price, model.ManagerId, model.Id);
var element = _dishStorage.GetElement(new DishSearchModel { DishName = model.DishName });
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Блюдо с таким названием уже есть");
}
}
}
}

View File

@ -0,0 +1,208 @@
using CanteenContracts.BindingModels;
using CanteenContracts.BusinessLogicsContracts;
using CanteenContracts.SearchModel;
using CanteenContracts.SearchModels;
using CanteenContracts.StoragesContracts;
using CanteenContracts.View;
using CanteenDataModels.Enums;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CanteenBusinessLogic.BusinessLogics
{
public class LunchLogic : ILunchLogic
{
private readonly ILogger _logger;
private readonly ILunchStorage _lunchStorage;
public LunchLogic(ILogger<LunchLogic> logger, ILunchStorage lunchStorage)
{
_logger = logger;
_lunchStorage = lunchStorage;
}
public List<LunchViewModel>? ReadList(LunchSearchModel? model)
{
_logger.LogInformation("ReadList. Id: {Id}", model?.Id);
var list = model == null ? _lunchStorage.GetFullList() : _lunchStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public LunchViewModel? ReadElement(LunchSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id: {Id}", model.Id);
var element = _lunchStorage.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(LunchBindingModel model)
{
CheckModel(model);
if (_lunchStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(LunchBindingModel model)
{
CheckModel(model);
if (_lunchStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(LunchBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_lunchStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(LunchBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.LunchName))
{
throw new ArgumentNullException("Нет названия обеда", nameof(model.LunchName));
}
if (model.VisitorId <= 0)
{
throw new ArgumentNullException("id посетителя должен быть больше 0", nameof(model.VisitorId));
}
_logger.LogInformation("Lunch. LunchName: {LunchName}. Sum: {Sum}. VisitorId: {VisitorId}. Id: {Id}", model.LunchName, model.Sum, model.VisitorId, model.Id);
var element = _lunchStorage.GetElement(new LunchSearchModel { Id = model.Id });
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Обед с таким id уже есть");
}
}
public bool Finish(LunchBindingModel model)
{
CheckModel(model, false);
if (_lunchStorage.GetElement(new LunchSearchModel { Id = model.Id })?.Status != LunchStatus.Создан)
{
_logger.LogWarning("Invalid lunch status");
return false;
}
model.Status = LunchStatus.Окончен;
model.DateImplement = DateTime.Now;
if (_lunchStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
}
return true;
}
public bool AddOrder(LunchOrderBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("AddOrder. LunchId: {LunchId}. OrderId: {OrderId}. CountOrder: {CountOrder}", model.OrderId, model.LunchId, model.OrderCount);
if (!_lunchStorage.AddOrder(model))
{
_logger.LogWarning("AddOrder operation failed");
return false;
}
return true;
}
private void CheckModel(LunchOrderBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.OrderId <= 0)
{
throw new ArgumentNullException("id заказа должен быть больше 0", nameof(model.OrderId));
}
if (model.LunchId <= 0)
{
throw new ArgumentNullException("id обеда должен быть больше 0", nameof(model.LunchId));
}
if (model.VisitorId <= 0)
{
throw new ArgumentNullException("id посетителя должен быть больше 0", nameof(model.VisitorId));
}
if (model.OrderCount <= 0)
{
throw new ArgumentNullException("количество одного заказа должно быть больше 0", nameof(model.OrderCount));
}
_logger.LogInformation("LunchOrder. OrderId: {OrderId}. LunchId: {LunchId}. VisitorId: {VisitorId}. OrderCount: {OrderCount}", model.OrderId, model.LunchId, model.VisitorId, model.OrderCount);
var element = _lunchStorage.GetLunchOrderElement(new LunchOrderSearchModel { Id = model.Id, OrderId = model.OrderId, LunchId = model.LunchId });
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Такая связь уже есть");
}
}
}
}

View File

@ -0,0 +1,141 @@
using CanteenContracts.BindingModels;
using CanteenContracts.BusinessLogicsContracts;
using CanteenContracts.SearchModel;
using CanteenContracts.StoragesContracts;
using CanteenContracts.View;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CanteenBusinessLogic.BusinessLogics
{
public class ManagerLogic : IManagerLogic
{
private readonly ILogger _logger;
private readonly IManagerStorage _managerStorage;
public ManagerLogic(ILogger<ManagerLogic> logger, IManagerStorage managerStorage)
{
_logger = logger;
_managerStorage = managerStorage;
}
public bool Create(ManagerBindingModel model)
{
CheckModel(model);
if (_managerStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(ManagerBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_managerStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public ManagerViewModel? ReadElement(ManagerSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Login: {Login}. Id: {Id}", model.Login, model.Id);
var element = _managerStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id: {Id}", element.Id);
return element;
}
public List<ManagerViewModel>? ReadList(ManagerSearchModel? model)
{
_logger.LogInformation("ReadList. Login: {Login}. Id: {Id}", model.Login, model.Id);
var list = model == null ? _managerStorage.GetFullList() : _managerStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public bool Update(ManagerBindingModel model)
{
CheckModel(model);
if (_managerStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(ManagerBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.FIO))
{
throw new ArgumentNullException("Нет ФИО у менеджера", nameof(model.FIO));
}
if (string.IsNullOrEmpty(model.Login))
{
throw new ArgumentNullException("Нет логина у менеджера", nameof(model.Login));
}
if (string.IsNullOrEmpty(model.Password))
{
throw new ArgumentNullException("Нет пароля у менеджера", nameof(model.Password));
}
if (string.IsNullOrEmpty(model.PhoneNumber))
{
throw new ArgumentNullException("У номер телефона у менеджера", nameof(model.PhoneNumber));
}
_logger.LogInformation("Manager. Login: {Login}. Password: {Password}. PhoneNumber: {PhoneNumber}. Id: {Id}", model.Login, model.Password, model.PhoneNumber, model.Id);
var element = _managerStorage.GetElement(new ManagerSearchModel { Login = model.Login , Password = model.Password});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Такой менеджер уже есть");
}
}
}
}

View File

@ -0,0 +1,238 @@
using CanteenContracts.BindingModels;
using CanteenContracts.BusinessLogicsContracts;
using CanteenContracts.SearchModel;
using CanteenContracts.SearchModels;
using CanteenContracts.StoragesContracts;
using CanteenContracts.View;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CanteenBusinessLogic.BusinessLogics
{
public class OrderLogic : IOrderLogic
{
private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
{
_logger = logger;
_orderStorage = orderStorage;
}
public bool AddCook(OrderCookBindingModel model)
{
CheckModel(model);
_logger.LogInformation("AddOrder. OrderId: {OrderId}. CookId: {CookId}", model.OrderId, model.CookId);
if (!_orderStorage.AddCook(model))
{
_logger.LogWarning("AddOrder operation failed");
return false;
}
return true;
}
public bool AddTableware(OrderTablewareBindingModel model)
{
CheckModel(model);
_logger.LogInformation("AddTableware. OrderId: {OrderId}. TablewareId: {TablewareId}", model.OrderId, model.TablewareId);
if (!_orderStorage.AddTableware(model))
{
_logger.LogWarning("AddTableware operation failed");
return false;
}
return true;
}
public bool Create(OrderBindingModel model)
{
CheckModel(model);
if (_orderStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(OrderBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_orderStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public OrderViewModel? ReadElement(OrderSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id: {Id}", model.Id);
var element = _orderStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id: {Id}", element.Id);
return element;
}
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{
_logger.LogInformation("ReadList. Id: {Id}", model?.Id);
var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public bool Update(OrderBindingModel model)
{
CheckModel(model);
if (_orderStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(OrderBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Description))
{
throw new ArgumentNullException("Нет описание заказа", nameof(model.Description));
}
if (model.VisitorId <= 0)
{
throw new ArgumentNullException("id посетителя должен быть больше 0", nameof(model.VisitorId));
}
if (model.Sum <= 0)
{
throw new ArgumentNullException("Сумма заказа должна быть больше 0", nameof(model.Sum));
}
_logger.LogInformation("Order. Description: {Description}. Sum: {Sum}. VisitorId: {VisitorId}. Id: {Id}", model.Description, model.Sum, model.VisitorId, model.Id);
var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Заказ с таким id уже есть");
}
}
private void CheckModel(OrderCookBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.OrderId <= 0)
{
throw new ArgumentNullException("id заказа должен быть больше 0", nameof(model.OrderId));
}
if (model.CookId <= 0)
{
throw new ArgumentNullException("id повара должен быть больше 0", nameof(model.CookId));
}
if (model.VisitorId <= 0)
{
throw new ArgumentNullException("id посетителя должен быть больше 0", nameof(model.VisitorId));
}
_logger.LogInformation("OrderCook. OrderId: {OrderId}. CookId: {CookId}. VisitorId: {VisitorId}", model.OrderId, model.CookId, model.VisitorId);
var element = _orderStorage.GetOrderCookElement(new OrderCookSearchModel { Id = model.Id, OrderId = model.OrderId, CookId = model.CookId });
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Такая связь уже есть");
}
}
private void CheckModel(OrderTablewareBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.OrderId <= 0)
{
throw new ArgumentNullException("id заказа должен быть больше 0", nameof(model.OrderId));
}
if (model.TablewareId <= 0)
{
throw new ArgumentNullException("id прибора должен быть больше 0", nameof(model.TablewareId));
}
if (model.VisitorId <= 0)
{
throw new ArgumentNullException("id посетителя должен быть больше 0", nameof(model.VisitorId));
}
if (model.CountTablewares <= 0)
{
throw new ArgumentNullException("количество одного прибора должено быть больше 0", nameof(model.CountTablewares));
}
_logger.LogInformation("OrderTableware. OrderId: {OrderId}. TablewareId: {TablewareId}. VisitorId: {VisitorId}. CountTablewares: {CountTablewares}", model.OrderId, model.TablewareId, model.VisitorId, model.CountTablewares);
var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.OrderId});
if (element != null && element.Id == model.OrderId && element.TablewareId == model.TablewareId)
{
throw new InvalidOperationException("Такая связь уже есть");
}
}
}
}

View File

@ -0,0 +1,135 @@
using CanteenContracts.BindingModels;
using CanteenContracts.BusinessLogicsContracts;
using CanteenContracts.SearchModel;
using CanteenContracts.StoragesContracts;
using CanteenContracts.View;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CanteenBusinessLogic.BusinessLogics
{
public class ProductLogic : IProductLogic
{
private readonly ILogger _logger;
private readonly IProductStorage _productStorage;
public ProductLogic(ILogger<ProductLogic> logger, IProductStorage productStorage)
{
_logger = logger;
_productStorage = productStorage;
}
public bool Create(ProductBindingModel model)
{
CheckModel(model);
if (_productStorage.Insert(model) == null)
{
_logger.LogWarning("Insert 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;
}
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 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 bool Update(ProductBindingModel model)
{
CheckModel(model);
if (_productStorage.Update(model) == null)
{
_logger.LogWarning("Update 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));
}
if (model.ManagerId <= 0)
{
throw new ArgumentNullException("id менеджера должен быть больше 0", nameof(model.ManagerId));
}
_logger.LogInformation("Product. ProductName: {ProductName}. Price: {Price}. ManagerId: {ManagerId}. Id: {Id}", model.ProductName, model.Price, model.ManagerId, model.Id);
var element = _productStorage.GetElement(new ProductSearchModel { ProductName = model.ProductName });
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Такое продукт уже есть");
}
}
}
}

View File

@ -0,0 +1,130 @@
using CanteenContracts.BindingModels;
using CanteenContracts.BusinessLogicsContracts;
using CanteenContracts.SearchModel;
using CanteenContracts.StoragesContracts;
using CanteenContracts.View;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CanteenBusinessLogic.BusinessLogics
{
public class TablewareLogic : ITablewareLogic
{
private readonly ILogger _logger;
private readonly ITablewareStorage _tablewareStorage;
public TablewareLogic(ILogger<TablewareLogic> logger, ITablewareStorage tablewareStorage)
{
_logger = logger;
_tablewareStorage = tablewareStorage;
}
public bool Create(TablewareBindingModel model)
{
CheckModel(model);
if (_tablewareStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(TablewareBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_tablewareStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public TablewareViewModel? ReadElement(TablewareSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. TablewareName: {TablewareName}. Id: {Id}", model.TablewareName, model.Id);
var element = _tablewareStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id: {Id}", element.Id);
return element;
}
public List<TablewareViewModel>? ReadList(TablewareSearchModel? model)
{
_logger.LogInformation("ReadList. TablewareName: {TablewareName}. Id: {Id}", model?.TablewareName, model?.Id);
var list = model == null ? _tablewareStorage.GetFullList() : _tablewareStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public bool Update(TablewareBindingModel model)
{
CheckModel(model);
if (_tablewareStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(TablewareBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.TablewareName))
{
throw new ArgumentNullException("Нет названия у прибора", nameof(model.TablewareName));
}
if (model.VisitorId <= 0)
{
throw new ArgumentNullException("id посетителя должен дыть больше 0", nameof(model.VisitorId));
}
_logger.LogInformation("Tableware. TablewareName: {TablewareName}. VisitorId: {VisitorId}. Id: {Id}", model.TablewareName, model.VisitorId, model.Position, model.Id);
var element = _tablewareStorage.GetElement(new TablewareSearchModel { TablewareName = model.TablewareName });
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Такой прибор уже есть");
}
}
}
}

View File

@ -0,0 +1,141 @@
using CanteenContracts.BindingModels;
using CanteenContracts.BusinessLogicsContracts;
using CanteenContracts.SearchModel;
using CanteenContracts.StoragesContracts;
using CanteenContracts.View;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CanteenBusinessLogic.BusinessLogics
{
public class VisitorLogic : IVisitorLogic
{
private readonly ILogger _logger;
private readonly IVisitorStorage _visitorStorage;
public VisitorLogic(ILogger<VisitorLogic> logger, IVisitorStorage visitorStorage)
{
_logger = logger;
_visitorStorage = visitorStorage;
}
public bool Create(VisitorBindingModel model)
{
CheckModel(model);
if (_visitorStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(VisitorBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_visitorStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public VisitorViewModel? ReadElement(VisitorSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Login: {Login}. Id: {Id}", model.Login, model.Id);
var element = _visitorStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id: {Id}", element.Id);
return element;
}
public List<VisitorViewModel>? ReadList(VisitorSearchModel? model)
{
_logger.LogInformation("ReadList. Login: {Login}. Id: {Id}", model.Login, model.Id);
var list = model == null ? _visitorStorage.GetFullList() : _visitorStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public bool Update(VisitorBindingModel model)
{
CheckModel(model);
if (_visitorStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(VisitorBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.FIO))
{
throw new ArgumentNullException("Нет ФИО у посетителя", nameof(model.FIO));
}
if (string.IsNullOrEmpty(model.Login))
{
throw new ArgumentNullException("Нет логина у посетителя", nameof(model.Login));
}
if (string.IsNullOrEmpty(model.Password))
{
throw new ArgumentNullException("Нет пароля у посетителя", nameof(model.Password));
}
if (string.IsNullOrEmpty(model.PhoneNumber))
{
throw new ArgumentNullException("У номер телефона у посетителя", nameof(model.PhoneNumber));
}
_logger.LogInformation("Visitor. Login: {Login}. Password: {Password}. PhoneNumber: {PhoneNumber}. Id: {Id}", model.Login, model.Password, model.PhoneNumber, model.Id);
var element = _visitorStorage.GetElement(new VisitorSearchModel { Login = model.Login, Password = model.Password });
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Такой посетитель уже есть");
}
}
}
}

View File

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CanteenContracts\CanteenContracts.csproj" />
</ItemGroup>
</Project>

View File

@ -13,8 +13,8 @@ namespace CanteenContracts.BindingModels
public int Id { get; set; }
public int VisitorId { get; set; }
public string LunchName { get; set; } = string.Empty;
public double Sum { get; set; }
public LunchStatus Status { get; set; }
public double Sum { get; set; } = 0;
public LunchStatus Status { get; set; } = LunchStatus.Создан;
public DateTime DateCreate { get; set; } = DateTime.Now;
public DateTime? DateImplement { get; set; }
public Dictionary<int, (IProductModel, int)> LunchProducts { get; set; } = new Dictionary<int, (IProductModel, int)>();

View File

@ -8,6 +8,7 @@ namespace CanteenContracts.BindingModels
{
public class LunchOrderBindingModel
{
public int Id { get; set; }
public int LunchId { get; set; }
public int OrderId { get; set; }
public int OrderCount { get; set; }

View File

@ -14,6 +14,8 @@ namespace CanteenContracts.BindingModels
public int VisitorId { get; set; }
public string Description { get; set; } = string.Empty;
public double? Sum { get; set; }
public int? TablewareId { get; set; }
public int? CountTablewares { get; set; }
public Dictionary<int, (IDishModel, int)> OrderDishes { get; set; } = new ();
}

View File

@ -8,9 +8,9 @@ namespace CanteenContracts.BindingModels
{
public class OrderCookBindingModel
{
public int Id { get; set; }
public int OrderId { get; set; }
public int CookId { get; set; }
public int CountOrders { get; set; }
public int VisitorId { get; set; }
}
}

View File

@ -17,6 +17,6 @@ namespace CanteenContracts.BusinessLogicsContracts
bool Update(LunchBindingModel model);
bool Delete(LunchBindingModel model);
bool Finish(LunchBindingModel model);
bool AddOrder(OrderBindingModel model);
bool AddOrder(LunchOrderBindingModel model);
}
}

View File

@ -16,7 +16,7 @@ namespace CanteenContracts.BusinessLogicsContracts
bool Create(OrderBindingModel model);
bool Delete(OrderBindingModel model);
bool Update(OrderBindingModel model);
bool AddCook(CookBindingModel model);
bool AddTableware(TablewareBindingModel model);
bool AddCook(OrderCookBindingModel model);
bool AddTableware(OrderTablewareBindingModel model);
}
}

View File

@ -10,9 +10,4 @@
<ProjectReference Include="..\CanteenDataModels\CanteenDataModels.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="SearchModels\" />
<Folder Include="StoragesContracts\" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CanteenContracts.SearchModels
{
public class LunchOrderSearchModel
{
public int? Id { get; set; }
public int? LunchId { get; set; }
public int? OrderId { get; set; }
public int? CountOrders { get; set; }
}
}

View File

@ -9,6 +9,7 @@ namespace CanteenContracts.SearchModel
public class LunchSearchModel
{
public int? Id { get; set; }
public int? VisitorId { get; set; }
public DateTime? DateFrom { get; set; }
public DateTime? DateTo { get; set; }
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CanteenContracts.SearchModels
{
public class OrderCookSearchModel
{
public int? Id { get; set; }
public int? OrderId { get; set; }
public int? CookId { get; set; }
}
}

View File

@ -1,6 +1,8 @@
using CanteenContracts.BindingModels;
using CanteenContracts.SearchModel;
using CanteenContracts.SearchModels;
using CanteenContracts.View;
using CanteenContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
@ -14,9 +16,10 @@ namespace CanteenContracts.StoragesContracts
List<LunchViewModel> GetFullList();
List<LunchViewModel> GetFilteredList(LunchSearchModel model);
LunchViewModel? GetElement(LunchSearchModel model);
LunchOrderViewModel? GetLunchOrderElement(LunchOrderSearchModel model);
LunchViewModel? Insert(LunchBindingModel model);
LunchViewModel? Update(LunchBindingModel model);
LunchViewModel? Delete(LunchBindingModel model);
void AddOrder(LunchOrderBindingModel model);
bool AddOrder(LunchOrderBindingModel model);
}
}

View File

@ -1,6 +1,8 @@
using CanteenContracts.BindingModels;
using CanteenContracts.SearchModel;
using CanteenContracts.SearchModels;
using CanteenContracts.View;
using CanteenContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
@ -14,6 +16,7 @@ namespace CanteenContracts.StoragesContracts
List<OrderViewModel> GetFullList();
List<OrderViewModel> GetFilteredList(OrderSearchModel model);
OrderViewModel? GetElement(OrderSearchModel model);
OrderCookViewModel? GetOrderCookElement(OrderCookSearchModel model);
OrderViewModel? Insert(OrderBindingModel model);
OrderViewModel? Update(OrderBindingModel model);
OrderViewModel? Delete(OrderBindingModel model);

View File

@ -9,7 +9,7 @@ using System.Threading.Tasks;
namespace CanteenContracts.StoragesContracts
{
public interface ITablewarerStorage
public interface ITablewareStorage
{
List<TablewareViewModel> GetFullList();
List<TablewareViewModel> GetFilteredList(TablewareSearchModel model);

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CanteenContracts.ViewModels
{
public class LunchOrderViewModel
{
public int Id { get; set; }
public int LunchId { get; set; }
public int OrderId { get; set; }
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CanteenContracts.ViewModels
{
public class OrderCookViewModel
{
public int Id { get; set; }
public int OrderId { get; set; }
public int CookId { get; set; }
}
}

View File

@ -20,6 +20,8 @@ namespace CanteenContracts.View
public Dictionary<int, (IDishModel, int)> OrderDishes { get; set; } = new Dictionary<int, (IDishModel, int)>();
[DisplayName("ID заказа")]
public int Id { get; set; }
public int? TablewareId { get; set; }
public int? CountTablewares { get; set; }
}
}

View File

@ -10,8 +10,8 @@ namespace CanteenDataModels.Models
public interface IOrderModel : IId
{
int VisitorId { get; }
int TablewareId { get; }
int CountTablewares { get; }
int? TablewareId { get; }
int? CountTablewares { get; }
string Description { get; }
double? Sum { get; }
Dictionary<int, (IDishModel, int)> OrderDishes { get; }

View File

@ -1,7 +1,9 @@
using CanteenContracts.BindingModels;
using CanteenContracts.SearchModel;
using CanteenContracts.SearchModels;
using CanteenContracts.StoragesContracts;
using CanteenContracts.View;
using CanteenContracts.ViewModels;
using CanteenDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
@ -26,9 +28,20 @@ namespace CanteenDatabaseImplement.Implements
return context.Lunches
.Include(x => x.Products)
.ThenInclude(x => x.Product)
.FirstOrDefault(x => x.DateCreate >= model.DateFrom && x.DateImplement <= model.DateTo)?.GetViewModel;
.FirstOrDefault(x => (x.DateCreate >= model.DateFrom && x.DateImplement <= model.DateTo) || (x.Id == model.Id))?.GetViewModel;
}
public LunchOrderViewModel? GetLunchOrderElement(LunchOrderSearchModel model)
{
if (!model.Id.HasValue && !model.LunchId.HasValue && !model.OrderId.HasValue)
{
return null;
}
using var context = new CanteenDatabase();
return context.LunchOrder
.FirstOrDefault(x => (x.LunchId == model.LunchId && x.OrderId == model.OrderId) || (x.Id == model.Id))?.GetViewModel;
}
public List<LunchViewModel> GetFilteredList(LunchSearchModel model)
{
if (!model.DateFrom.HasValue || !model.DateTo.HasValue)
@ -129,15 +142,15 @@ namespace CanteenDatabaseImplement.Implements
return null;
}
public void AddOrder(LunchOrderBindingModel lunchOrder)
public bool AddOrder(LunchOrderBindingModel lunchOrder)
{
using var context = new CanteenDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
if (context.LunchOrder.FirstOrDefault(rec => rec.LunchId == lunchOrder.LunchId && rec.OrderId == lunchOrder.OrderId) != null)
{
var _lunchOrder = context.LunchOrder.FirstOrDefault(rec => rec.LunchId == lunchOrder.LunchId && rec.OrderId == lunchOrder.OrderId);
if (_lunchOrder != null)
{
_lunchOrder.CountOrders = lunchOrder.OrderCount;
}
else
@ -146,11 +159,12 @@ namespace CanteenDatabaseImplement.Implements
}
context.SaveChanges();
transaction.Commit();
return true;
}
catch
{
transaction.Rollback();
throw;
return false;
}
}
}

View File

@ -23,7 +23,7 @@ namespace CanteenDatabaseImplement.Implements
using var context = new CanteenDatabase();
return context.Managers.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.Login) && x.Login == model.Login) ||
(!string.IsNullOrEmpty(model.Login) && x.Login == model.Login) || (!string.IsNullOrEmpty(model.Password) && x.Login == model.Password) ||
(model.Id.HasValue && x.Id == model.Id)
)?.GetViewModel;
}

View File

@ -1,7 +1,9 @@
using CanteenContracts.BindingModels;
using CanteenContracts.SearchModel;
using CanteenContracts.SearchModels;
using CanteenContracts.StoragesContracts;
using CanteenContracts.View;
using CanteenContracts.ViewModels;
using CanteenDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
@ -14,6 +16,19 @@ namespace CanteenDatabaseImplement.Implements
{
public class OrderStorage : IOrderStorage
{
public OrderCookViewModel? GetOrderCookElement(OrderCookSearchModel model)
{
if (!model.Id.HasValue && (!model.OrderId.HasValue || !model.CookId.HasValue))
{
return null;
}
using var context = new CanteenDatabase();
return context.OrderCook
.FirstOrDefault(x => x.OrderId == model.OrderId && x.CookId == model.CookId)?.GetViewModel;
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)

View File

@ -11,7 +11,7 @@ using System.Threading.Tasks;
namespace CanteenDatabaseImplement.Implements
{
public class TablewareStorage : ITablewarerStorage
public class TablewareStorage : ITablewareStorage
{
public TablewareViewModel? GetElement(TablewareSearchModel model)
{

View File

@ -1,4 +1,5 @@
using System;
using CanteenContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
@ -18,5 +19,11 @@ namespace CanteenDatabaseImplement.Models
public int CountOrders { get; set; }
public virtual Lunch Lunch { get; set; } = new();
public virtual Order Order { get; set; } = new();
public LunchOrderViewModel GetViewModel => new()
{
Id = Id,
LunchId = LunchId,
OrderId = OrderId
};
}
}

View File

@ -1,4 +1,6 @@
using System;
using CanteenContracts.View;
using CanteenContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
@ -16,5 +18,11 @@ namespace CanteenDatabaseImplement.Models
public int OrderId { get; set; }
public virtual Order Order { get; set; } = new();
public virtual Cook Cook { get; set; } = new();
public OrderCookViewModel GetViewModel => new()
{
Id = Id,
CookId = CookId,
OrderId = OrderId
};
}
}