diff --git a/Canteen/Canteen.sln b/Canteen/Canteen.sln index e8936fc..ceb5a7d 100644 --- a/Canteen/Canteen.sln +++ b/Canteen/Canteen.sln @@ -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 diff --git a/Canteen/CanteenBusinessLogic/BusinessLogics/CookLogic.cs b/Canteen/CanteenBusinessLogic/BusinessLogics/CookLogic.cs new file mode 100644 index 0000000..c1e4314 --- /dev/null +++ b/Canteen/CanteenBusinessLogic/BusinessLogics/CookLogic.cs @@ -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 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? 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("Такой повар уже есть"); + } + } + } +} diff --git a/Canteen/CanteenBusinessLogic/BusinessLogics/DishLogic.cs b/Canteen/CanteenBusinessLogic/BusinessLogics/DishLogic.cs new file mode 100644 index 0000000..2557bd4 --- /dev/null +++ b/Canteen/CanteenBusinessLogic/BusinessLogics/DishLogic.cs @@ -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 logger, IDishStorage dishStorage) + { + _logger = logger; + _dishStorage = dishStorage; + } + + public List? 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("Блюдо с таким названием уже есть"); + } + } + } +} diff --git a/Canteen/CanteenBusinessLogic/BusinessLogics/LunchLogic.cs b/Canteen/CanteenBusinessLogic/BusinessLogics/LunchLogic.cs new file mode 100644 index 0000000..0c14908 --- /dev/null +++ b/Canteen/CanteenBusinessLogic/BusinessLogics/LunchLogic.cs @@ -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 logger, ILunchStorage lunchStorage) + { + _logger = logger; + _lunchStorage = lunchStorage; + } + + public List? 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("Такая связь уже есть"); + } + } + } +} diff --git a/Canteen/CanteenBusinessLogic/BusinessLogics/ManagerLogic.cs b/Canteen/CanteenBusinessLogic/BusinessLogics/ManagerLogic.cs new file mode 100644 index 0000000..ecd0e82 --- /dev/null +++ b/Canteen/CanteenBusinessLogic/BusinessLogics/ManagerLogic.cs @@ -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 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? 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("Такой менеджер уже есть"); + } + } + } +} diff --git a/Canteen/CanteenBusinessLogic/BusinessLogics/OrderLogic.cs b/Canteen/CanteenBusinessLogic/BusinessLogics/OrderLogic.cs new file mode 100644 index 0000000..df74a98 --- /dev/null +++ b/Canteen/CanteenBusinessLogic/BusinessLogics/OrderLogic.cs @@ -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 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? 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("Такая связь уже есть"); + } + } + } +} diff --git a/Canteen/CanteenBusinessLogic/BusinessLogics/ProductLogic.cs b/Canteen/CanteenBusinessLogic/BusinessLogics/ProductLogic.cs new file mode 100644 index 0000000..d61b213 --- /dev/null +++ b/Canteen/CanteenBusinessLogic/BusinessLogics/ProductLogic.cs @@ -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 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? 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("Такое продукт уже есть"); + } + } + } +} diff --git a/Canteen/CanteenBusinessLogic/BusinessLogics/TablewareLogic.cs b/Canteen/CanteenBusinessLogic/BusinessLogics/TablewareLogic.cs new file mode 100644 index 0000000..0c370d7 --- /dev/null +++ b/Canteen/CanteenBusinessLogic/BusinessLogics/TablewareLogic.cs @@ -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 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? 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("Такой прибор уже есть"); + } + } + } +} diff --git a/Canteen/CanteenBusinessLogic/BusinessLogics/VisitorLogic.cs b/Canteen/CanteenBusinessLogic/BusinessLogics/VisitorLogic.cs new file mode 100644 index 0000000..0ef5dac --- /dev/null +++ b/Canteen/CanteenBusinessLogic/BusinessLogics/VisitorLogic.cs @@ -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 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? 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("Такой посетитель уже есть"); + } + } + } +} diff --git a/Canteen/CanteenBusinessLogic/CanteenBusinessLogic.csproj b/Canteen/CanteenBusinessLogic/CanteenBusinessLogic.csproj new file mode 100644 index 0000000..6645824 --- /dev/null +++ b/Canteen/CanteenBusinessLogic/CanteenBusinessLogic.csproj @@ -0,0 +1,17 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + diff --git a/Canteen/CanteenContracts/BindingModels/LunchBindingModel.cs b/Canteen/CanteenContracts/BindingModels/LunchBindingModel.cs index b179209..8286564 100644 --- a/Canteen/CanteenContracts/BindingModels/LunchBindingModel.cs +++ b/Canteen/CanteenContracts/BindingModels/LunchBindingModel.cs @@ -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 LunchProducts { get; set; } = new Dictionary(); diff --git a/Canteen/CanteenContracts/BindingModels/LunchOrderBindingModel.cs b/Canteen/CanteenContracts/BindingModels/LunchOrderBindingModel.cs index 2b2e5b1..e412e79 100644 --- a/Canteen/CanteenContracts/BindingModels/LunchOrderBindingModel.cs +++ b/Canteen/CanteenContracts/BindingModels/LunchOrderBindingModel.cs @@ -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; } diff --git a/Canteen/CanteenContracts/BindingModels/OrderBindingModel.cs b/Canteen/CanteenContracts/BindingModels/OrderBindingModel.cs index 4d660ce..c990938 100644 --- a/Canteen/CanteenContracts/BindingModels/OrderBindingModel.cs +++ b/Canteen/CanteenContracts/BindingModels/OrderBindingModel.cs @@ -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 OrderDishes { get; set; } = new (); } diff --git a/Canteen/CanteenContracts/BindingModels/OrderCookBindingModel.cs b/Canteen/CanteenContracts/BindingModels/OrderCookBindingModel.cs index 667e10b..46ae6d8 100644 --- a/Canteen/CanteenContracts/BindingModels/OrderCookBindingModel.cs +++ b/Canteen/CanteenContracts/BindingModels/OrderCookBindingModel.cs @@ -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; } } } diff --git a/Canteen/CanteenContracts/BusinessLogicsContracts/ILunchLogic.cs b/Canteen/CanteenContracts/BusinessLogicsContracts/ILunchLogic.cs index 65a870a..65fd433 100644 --- a/Canteen/CanteenContracts/BusinessLogicsContracts/ILunchLogic.cs +++ b/Canteen/CanteenContracts/BusinessLogicsContracts/ILunchLogic.cs @@ -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); } } diff --git a/Canteen/CanteenContracts/BusinessLogicsContracts/IOrderLogic.cs b/Canteen/CanteenContracts/BusinessLogicsContracts/IOrderLogic.cs index 5458576..c69cd5a 100644 --- a/Canteen/CanteenContracts/BusinessLogicsContracts/IOrderLogic.cs +++ b/Canteen/CanteenContracts/BusinessLogicsContracts/IOrderLogic.cs @@ -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); } } diff --git a/Canteen/CanteenContracts/CanteenContracts.csproj b/Canteen/CanteenContracts/CanteenContracts.csproj index 877ccfb..89209ad 100644 --- a/Canteen/CanteenContracts/CanteenContracts.csproj +++ b/Canteen/CanteenContracts/CanteenContracts.csproj @@ -10,9 +10,4 @@ - - - - - diff --git a/Canteen/CanteenContracts/SearchModels/LunchOrderSearchModel.cs b/Canteen/CanteenContracts/SearchModels/LunchOrderSearchModel.cs new file mode 100644 index 0000000..ba2dbbb --- /dev/null +++ b/Canteen/CanteenContracts/SearchModels/LunchOrderSearchModel.cs @@ -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; } + } +} diff --git a/Canteen/CanteenContracts/SearchModels/LunchSearchModel.cs b/Canteen/CanteenContracts/SearchModels/LunchSearchModel.cs index 4bb48d6..ca9eb9c 100644 --- a/Canteen/CanteenContracts/SearchModels/LunchSearchModel.cs +++ b/Canteen/CanteenContracts/SearchModels/LunchSearchModel.cs @@ -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; } } diff --git a/Canteen/CanteenContracts/SearchModels/OrderCookSearchModel.cs b/Canteen/CanteenContracts/SearchModels/OrderCookSearchModel.cs new file mode 100644 index 0000000..3304d85 --- /dev/null +++ b/Canteen/CanteenContracts/SearchModels/OrderCookSearchModel.cs @@ -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; } + } +} diff --git a/Canteen/CanteenContracts/StoragesContracts/ILunchStorage.cs b/Canteen/CanteenContracts/StoragesContracts/ILunchStorage.cs index fcc8070..5859591 100644 --- a/Canteen/CanteenContracts/StoragesContracts/ILunchStorage.cs +++ b/Canteen/CanteenContracts/StoragesContracts/ILunchStorage.cs @@ -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 GetFullList(); List 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); } } diff --git a/Canteen/CanteenContracts/StoragesContracts/IOrderStorage.cs b/Canteen/CanteenContracts/StoragesContracts/IOrderStorage.cs index 91b6e67..7315071 100644 --- a/Canteen/CanteenContracts/StoragesContracts/IOrderStorage.cs +++ b/Canteen/CanteenContracts/StoragesContracts/IOrderStorage.cs @@ -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 GetFullList(); List GetFilteredList(OrderSearchModel model); OrderViewModel? GetElement(OrderSearchModel model); + OrderCookViewModel? GetOrderCookElement(OrderCookSearchModel model); OrderViewModel? Insert(OrderBindingModel model); OrderViewModel? Update(OrderBindingModel model); OrderViewModel? Delete(OrderBindingModel model); diff --git a/Canteen/CanteenContracts/StoragesContracts/ITablewarerStorage.cs b/Canteen/CanteenContracts/StoragesContracts/ITablewareStorage.cs similarity index 94% rename from Canteen/CanteenContracts/StoragesContracts/ITablewarerStorage.cs rename to Canteen/CanteenContracts/StoragesContracts/ITablewareStorage.cs index 82ac23f..6090f44 100644 --- a/Canteen/CanteenContracts/StoragesContracts/ITablewarerStorage.cs +++ b/Canteen/CanteenContracts/StoragesContracts/ITablewareStorage.cs @@ -9,7 +9,7 @@ using System.Threading.Tasks; namespace CanteenContracts.StoragesContracts { - public interface ITablewarerStorage + public interface ITablewareStorage { List GetFullList(); List GetFilteredList(TablewareSearchModel model); diff --git a/Canteen/CanteenContracts/ViewModels/LunchOrderViewModel.cs b/Canteen/CanteenContracts/ViewModels/LunchOrderViewModel.cs new file mode 100644 index 0000000..cfe48e5 --- /dev/null +++ b/Canteen/CanteenContracts/ViewModels/LunchOrderViewModel.cs @@ -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; } + } +} diff --git a/Canteen/CanteenContracts/ViewModels/OrderCookViewModel.cs b/Canteen/CanteenContracts/ViewModels/OrderCookViewModel.cs new file mode 100644 index 0000000..b5749c2 --- /dev/null +++ b/Canteen/CanteenContracts/ViewModels/OrderCookViewModel.cs @@ -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; } + } +} diff --git a/Canteen/CanteenContracts/ViewModels/OrderViewModel.cs b/Canteen/CanteenContracts/ViewModels/OrderViewModel.cs index e866ce3..914ca70 100644 --- a/Canteen/CanteenContracts/ViewModels/OrderViewModel.cs +++ b/Canteen/CanteenContracts/ViewModels/OrderViewModel.cs @@ -20,6 +20,8 @@ namespace CanteenContracts.View public Dictionary OrderDishes { get; set; } = new Dictionary(); [DisplayName("ID заказа")] public int Id { get; set; } + public int? TablewareId { get; set; } + public int? CountTablewares { get; set; } } } diff --git a/Canteen/CanteenDataModels/Models/IOrderModel.cs b/Canteen/CanteenDataModels/Models/IOrderModel.cs index f353bed..65e8832 100644 --- a/Canteen/CanteenDataModels/Models/IOrderModel.cs +++ b/Canteen/CanteenDataModels/Models/IOrderModel.cs @@ -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 OrderDishes { get; } diff --git a/Canteen/CanteenDatabaseImplement/Implements/LunchStorage.cs b/Canteen/CanteenDatabaseImplement/Implements/LunchStorage.cs index c1f5bb7..3d780e4 100644 --- a/Canteen/CanteenDatabaseImplement/Implements/LunchStorage.cs +++ b/Canteen/CanteenDatabaseImplement/Implements/LunchStorage.cs @@ -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 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) { - var _lunchOrder = context.LunchOrder.FirstOrDefault(rec => rec.LunchId == lunchOrder.LunchId && rec.OrderId == lunchOrder.OrderId); _lunchOrder.CountOrders = lunchOrder.OrderCount; } else @@ -146,11 +159,12 @@ namespace CanteenDatabaseImplement.Implements } context.SaveChanges(); transaction.Commit(); + return true; } catch { transaction.Rollback(); - throw; + return false; } } } diff --git a/Canteen/CanteenDatabaseImplement/Implements/ManagerStorage.cs b/Canteen/CanteenDatabaseImplement/Implements/ManagerStorage.cs index 0280fd3..73dd623 100644 --- a/Canteen/CanteenDatabaseImplement/Implements/ManagerStorage.cs +++ b/Canteen/CanteenDatabaseImplement/Implements/ManagerStorage.cs @@ -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; } diff --git a/Canteen/CanteenDatabaseImplement/Implements/OrderStorage.cs b/Canteen/CanteenDatabaseImplement/Implements/OrderStorage.cs index 6861113..3dc63d5 100644 --- a/Canteen/CanteenDatabaseImplement/Implements/OrderStorage.cs +++ b/Canteen/CanteenDatabaseImplement/Implements/OrderStorage.cs @@ -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) diff --git a/Canteen/CanteenDatabaseImplement/Implements/TablewareStorage.cs b/Canteen/CanteenDatabaseImplement/Implements/TablewareStorage.cs index bd650a2..4c2d76f 100644 --- a/Canteen/CanteenDatabaseImplement/Implements/TablewareStorage.cs +++ b/Canteen/CanteenDatabaseImplement/Implements/TablewareStorage.cs @@ -11,7 +11,7 @@ using System.Threading.Tasks; namespace CanteenDatabaseImplement.Implements { - public class TablewareStorage : ITablewarerStorage + public class TablewareStorage : ITablewareStorage { public TablewareViewModel? GetElement(TablewareSearchModel model) { diff --git a/Canteen/CanteenDatabaseImplement/Models/LunchOrder.cs b/Canteen/CanteenDatabaseImplement/Models/LunchOrder.cs index 523305f..8f61d83 100644 --- a/Canteen/CanteenDatabaseImplement/Models/LunchOrder.cs +++ b/Canteen/CanteenDatabaseImplement/Models/LunchOrder.cs @@ -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 + }; } } diff --git a/Canteen/CanteenDatabaseImplement/Models/OrderCook.cs b/Canteen/CanteenDatabaseImplement/Models/OrderCook.cs index 876e89f..1977e03 100644 --- a/Canteen/CanteenDatabaseImplement/Models/OrderCook.cs +++ b/Canteen/CanteenDatabaseImplement/Models/OrderCook.cs @@ -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 + }; } }