From d747d797fc8b66a16556a0ffaeb5bbf7c276f27c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sun, 5 Feb 2023 14:44:03 +0400 Subject: [PATCH 01/29] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=81=D1=83=D1=89=D0=BD=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D1=8C=20"=D0=9C=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BindingModels/ShopBindingModel.cs | 12 ++++++++++++ .../SearchModels/ShopSearchModel.cs | 9 +++++++++ .../StoragesContract/IShopStorage.cs | 12 ++++++++++++ ConfectioneryContracts/ViewModels/ShopViewModel.cs | 12 ++++++++++++ ConfectioneryDataModels/IShopModel.cs | 12 ++++++++++++ 5 files changed, 57 insertions(+) create mode 100644 ConfectioneryContracts/BindingModels/ShopBindingModel.cs create mode 100644 ConfectioneryContracts/SearchModels/ShopSearchModel.cs create mode 100644 ConfectioneryContracts/StoragesContract/IShopStorage.cs create mode 100644 ConfectioneryContracts/ViewModels/ShopViewModel.cs create mode 100644 ConfectioneryDataModels/IShopModel.cs diff --git a/ConfectioneryContracts/BindingModels/ShopBindingModel.cs b/ConfectioneryContracts/BindingModels/ShopBindingModel.cs new file mode 100644 index 0000000..764dc63 --- /dev/null +++ b/ConfectioneryContracts/BindingModels/ShopBindingModel.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryContracts.BindingModels +{ + internal interface ShopBindingModel + { + } +} diff --git a/ConfectioneryContracts/SearchModels/ShopSearchModel.cs b/ConfectioneryContracts/SearchModels/ShopSearchModel.cs new file mode 100644 index 0000000..d3c5547 --- /dev/null +++ b/ConfectioneryContracts/SearchModels/ShopSearchModel.cs @@ -0,0 +1,9 @@ + +namespace ConfectioneryContracts.SearchModels +{ + public class ShopSearchModel + { + public int? Id { get; set; } + public string? Name { get; set; } + } +} diff --git a/ConfectioneryContracts/StoragesContract/IShopStorage.cs b/ConfectioneryContracts/StoragesContract/IShopStorage.cs new file mode 100644 index 0000000..27ad4f6 --- /dev/null +++ b/ConfectioneryContracts/StoragesContract/IShopStorage.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryContracts.StoragesContract +{ + internal interface IShopStorage + { + } +} diff --git a/ConfectioneryContracts/ViewModels/ShopViewModel.cs b/ConfectioneryContracts/ViewModels/ShopViewModel.cs new file mode 100644 index 0000000..dbfc7c6 --- /dev/null +++ b/ConfectioneryContracts/ViewModels/ShopViewModel.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryContracts.ViewModels +{ + internal class ShopViewModel + { + } +} diff --git a/ConfectioneryDataModels/IShopModel.cs b/ConfectioneryDataModels/IShopModel.cs new file mode 100644 index 0000000..7315e2f --- /dev/null +++ b/ConfectioneryDataModels/IShopModel.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDataModels +{ + internal interface IShopModel + { + } +} From 2aa903dd99bc82c707817f60891dd2adf560e768 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sun, 5 Feb 2023 14:55:50 +0400 Subject: [PATCH 02/29] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=B0=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA?= =?UTF-8?q?=D0=B0=20=D1=81=D1=83=D1=89=D0=BD=D0=BE=D1=81=D1=82=D0=B8=20"?= =?UTF-8?q?=D0=9C=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryBusinessLogic/ShopLogic.cs | 12 ++++++++++++ .../BusinessLogicsContracts/IShopLogic.cs | 12 ++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 ConfectionaryBusinessLogic/ShopLogic.cs create mode 100644 ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs diff --git a/ConfectionaryBusinessLogic/ShopLogic.cs b/ConfectionaryBusinessLogic/ShopLogic.cs new file mode 100644 index 0000000..235b2b4 --- /dev/null +++ b/ConfectionaryBusinessLogic/ShopLogic.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryBusinessLogic +{ + internal class ShopLogic + { + } +} diff --git a/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs b/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs new file mode 100644 index 0000000..41a4e0e --- /dev/null +++ b/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryContracts.BusinessLogicsContracts +{ + internal interface IShopLogic + { + } +} From 873df1007e9248ea3ca74aa70c8f2c0d67be3189 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sun, 5 Feb 2023 15:14:18 +0400 Subject: [PATCH 03/29] fix --- .../BindingModels/ShopBindingModel.cs | 22 +++++++++++----- .../ViewModels/ShopViewModel.cs | 26 ++++++++++++++----- ConfectioneryDataModels/IShopModel.cs | 12 ++++----- 3 files changed, 42 insertions(+), 18 deletions(-) diff --git a/ConfectioneryContracts/BindingModels/ShopBindingModel.cs b/ConfectioneryContracts/BindingModels/ShopBindingModel.cs index 764dc63..991d04b 100644 --- a/ConfectioneryContracts/BindingModels/ShopBindingModel.cs +++ b/ConfectioneryContracts/BindingModels/ShopBindingModel.cs @@ -1,12 +1,22 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using ConfectioneryDataModels; +using ConfectioneryDataModels.Models; namespace ConfectioneryContracts.BindingModels { - internal interface ShopBindingModel + public class ShopBindingModel : IShopModel { + public string Name { get; set; } = string.Empty; + + public string Address { get; set; } = string.Empty; + + public DateTime DateOpening { get; set; } = DateTime.Now; + + public Dictionary Pastries + { + get; + set; + } = new(); + + public int Id { get; set; } } } diff --git a/ConfectioneryContracts/ViewModels/ShopViewModel.cs b/ConfectioneryContracts/ViewModels/ShopViewModel.cs index dbfc7c6..dd01f43 100644 --- a/ConfectioneryContracts/ViewModels/ShopViewModel.cs +++ b/ConfectioneryContracts/ViewModels/ShopViewModel.cs @@ -1,12 +1,26 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using ConfectioneryDataModels; +using ConfectioneryDataModels.Models; +using System.ComponentModel; namespace ConfectioneryContracts.ViewModels { - internal class ShopViewModel + public class ShopViewModel : IShopModel { + [DisplayName("Название магазина")] + public string Name { get; set; } = string.Empty; + + [DisplayName("Адрес магазина")] + public string Address { get; set; } = string.Empty; + + [DisplayName("Время открытия")] + public DateTime DateOpening { get; set; } = DateTime.Now; + + public Dictionary Pastries + { + get; + set; + } = new(); + + public int Id { get; set; } } } diff --git a/ConfectioneryDataModels/IShopModel.cs b/ConfectioneryDataModels/IShopModel.cs index 7315e2f..1f1e07f 100644 --- a/ConfectioneryDataModels/IShopModel.cs +++ b/ConfectioneryDataModels/IShopModel.cs @@ -1,12 +1,12 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using ConfectioneryDataModels.Models; namespace ConfectioneryDataModels { - internal interface IShopModel + public interface IShopModel : IId { + string Name { get; } + string Address { get; } + DateTime DateOpening { get; } + Dictionary Pastries { get; } } } From ed54dc6fbe58f5646476284689a061ba10df086d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sun, 5 Feb 2023 15:15:38 +0400 Subject: [PATCH 04/29] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=B0=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA?= =?UTF-8?q?=D0=B0=20=D1=81=D1=83=D1=89=D0=BD=D0=BE=D1=81=D1=82=D0=B8=20"?= =?UTF-8?q?=D0=9C=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryBusinessLogic/ShopLogic.cs | 131 +++++++++++++++++- .../DataListSingleton.cs | 2 + ConfectionaryListImplement/Shop.cs | 58 ++++++++ ConfectionaryListImplement/ShopStorage.cs | 46 ++++++ .../BindingModels/ShopBindingModel.cs | 2 +- .../BusinessLogicsContracts/IShopLogic.cs | 17 ++- .../StoragesContract/IShopStorage.cs | 16 ++- .../ViewModels/ShopViewModel.cs | 2 +- ConfectioneryDataModels/IShopModel.cs | 2 +- 9 files changed, 254 insertions(+), 22 deletions(-) create mode 100644 ConfectionaryListImplement/Shop.cs create mode 100644 ConfectionaryListImplement/ShopStorage.cs diff --git a/ConfectionaryBusinessLogic/ShopLogic.cs b/ConfectionaryBusinessLogic/ShopLogic.cs index 235b2b4..c32a715 100644 --- a/ConfectionaryBusinessLogic/ShopLogic.cs +++ b/ConfectionaryBusinessLogic/ShopLogic.cs @@ -1,12 +1,129 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using ConfectioneryBusinessLogic.BusinessLogics; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.BusinessLogicsContracts; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContract; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; +using Microsoft.Extensions.Logging; namespace ConfectioneryBusinessLogic { - internal class ShopLogic - { + public class ShopLogic : IShopLogic + { + private readonly ILogger _logger; + private readonly IShopStorage _shopStorage; + public ShopLogic(ILogger logger, IShopStorage shopStorage) + { + _logger = logger; + _shopStorage = shopStorage; + } + public List? ReadList(ShopSearchModel? model) + { + _logger.LogInformation("ReadList. ShopName:{ShopName}.Id:{ Id} ", + model?.Name, model?.Id); + var list = (model == null) ? _shopStorage.GetFullList() : + _shopStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public ShopViewModel? ReadElement(ShopSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. ShopName:{ShopName}.Id:{ Id}", + model.Name, model.Id); + var element = _shopStorage.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(ShopBindingModel model) + { + CheckModel(model); + if (_shopStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(ShopBindingModel model) + { + CheckModel(model); + if (_shopStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(ShopBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_shopStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(ShopBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.Name)) + { + throw new ArgumentNullException("Нет названия магазина", + nameof(model.Name)); + } + _logger.LogInformation("Shop. ShopName:{0}.Address:{1}. Id: {2}", + model.Name, model.Address, model.Id); + var element = _shopStorage.GetElement(new ShopSearchModel + { + Name = model.Name + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Магазин с таким названием уже есть"); + } + } + + public bool AddPastry(ShopSearchModel model, IPastryModel pastry, int count) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("AddPastryInShop. ShopName:{ShopName}.Id:{ Id}", + model.Name, model.Id); + var element = _shopStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("AddPastryInShop element not found"); + return false; + } + _logger.LogInformation("AddPastryInShop find. Id:{Id}", element.Id); + + return element.Pastries.TryAdd(pastry.Id, (pastry, count)); + } } } diff --git a/ConfectionaryListImplement/DataListSingleton.cs b/ConfectionaryListImplement/DataListSingleton.cs index 24ae616..dab3ef1 100644 --- a/ConfectionaryListImplement/DataListSingleton.cs +++ b/ConfectionaryListImplement/DataListSingleton.cs @@ -8,11 +8,13 @@ namespace ConfectioneryListImplement public List Components { get; set; } public List Orders { get; set; } public List Pastry { get; set; } + public List Shop { get; set; } private DataListSingleton() { Components = new List(); Orders = new List(); Pastry = new List(); + Shop = new List(); } public static DataListSingleton GetInstance() { diff --git a/ConfectionaryListImplement/Shop.cs b/ConfectionaryListImplement/Shop.cs new file mode 100644 index 0000000..c1a7630 --- /dev/null +++ b/ConfectionaryListImplement/Shop.cs @@ -0,0 +1,58 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels; +using ConfectioneryDataModels.Models; + +namespace ConfectioneryListImplement +{ + public class Shop : IShopModel + { + public string Name { get; private set; } = string.Empty; + + public string Address { get; private set; } = string.Empty; + + public DateTime DateOpening { get; private set; } + + public Dictionary Pastries + { + get; + private set; + } = new(); + + public int Id { get; private set; } + + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + Name = model.Name, + Address = model.Address, + DateOpening = model.DateOpening, + Pastries = model.Pastries + }; + } + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + Name = model.Name; + Address = model.Address; + DateOpening = model.DateOpening; + Pastries = model.Pastries; + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Address = Address, + Pastries = Pastries + }; + } +} diff --git a/ConfectionaryListImplement/ShopStorage.cs b/ConfectionaryListImplement/ShopStorage.cs new file mode 100644 index 0000000..d7c3547 --- /dev/null +++ b/ConfectionaryListImplement/ShopStorage.cs @@ -0,0 +1,46 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContract; +using ConfectioneryContracts.ViewModels; + +namespace ConfectioneryListImplement +{ + public class ShopStorage : IShopStorage + { + private readonly DataListSingleton _source; + public ShopStorage() + { + _source = DataListSingleton.GetInstance(); + } + + public ShopViewModel? Delete(ShopBindingModel model) + { + throw new NotImplementedException(); + } + + public ShopViewModel? GetElement(ShopSearchModel model) + { + throw new NotImplementedException(); + } + + public List GetFilteredList(ShopSearchModel model) + { + throw new NotImplementedException(); + } + + public List GetFullList() + { + throw new NotImplementedException(); + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + throw new NotImplementedException(); + } + + public ShopViewModel? Update(ShopBindingModel model) + { + throw new NotImplementedException(); + } + } +} diff --git a/ConfectioneryContracts/BindingModels/ShopBindingModel.cs b/ConfectioneryContracts/BindingModels/ShopBindingModel.cs index 991d04b..38d8750 100644 --- a/ConfectioneryContracts/BindingModels/ShopBindingModel.cs +++ b/ConfectioneryContracts/BindingModels/ShopBindingModel.cs @@ -11,7 +11,7 @@ namespace ConfectioneryContracts.BindingModels public DateTime DateOpening { get; set; } = DateTime.Now; - public Dictionary Pastries + public Dictionary Pastries { get; set; diff --git a/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs b/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs index 41a4e0e..3ba0749 100644 --- a/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs @@ -1,12 +1,17 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; namespace ConfectioneryContracts.BusinessLogicsContracts { - internal interface IShopLogic + public interface IShopLogic { + List? ReadList(ShopSearchModel? model); + ShopViewModel? ReadElement(ShopSearchModel model); + bool Create(ShopBindingModel model); + bool Update(ShopBindingModel model); + bool Delete(ShopBindingModel model); + bool AddPastry(ShopSearchModel model, IPastryModel pastry, int count); } } diff --git a/ConfectioneryContracts/StoragesContract/IShopStorage.cs b/ConfectioneryContracts/StoragesContract/IShopStorage.cs index 27ad4f6..4276f2d 100644 --- a/ConfectioneryContracts/StoragesContract/IShopStorage.cs +++ b/ConfectioneryContracts/StoragesContract/IShopStorage.cs @@ -1,12 +1,16 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.ViewModels; namespace ConfectioneryContracts.StoragesContract { - internal interface IShopStorage + public interface IShopStorage { + List GetFullList(); + List GetFilteredList(ShopSearchModel model); + ShopViewModel? GetElement(ShopSearchModel model); + ShopViewModel? Insert(ShopBindingModel model); + ShopViewModel? Update(ShopBindingModel model); + ShopViewModel? Delete(ShopBindingModel model); } } diff --git a/ConfectioneryContracts/ViewModels/ShopViewModel.cs b/ConfectioneryContracts/ViewModels/ShopViewModel.cs index dd01f43..a87ca8d 100644 --- a/ConfectioneryContracts/ViewModels/ShopViewModel.cs +++ b/ConfectioneryContracts/ViewModels/ShopViewModel.cs @@ -15,7 +15,7 @@ namespace ConfectioneryContracts.ViewModels [DisplayName("Время открытия")] public DateTime DateOpening { get; set; } = DateTime.Now; - public Dictionary Pastries + public Dictionary Pastries { get; set; diff --git a/ConfectioneryDataModels/IShopModel.cs b/ConfectioneryDataModels/IShopModel.cs index 1f1e07f..c0883e9 100644 --- a/ConfectioneryDataModels/IShopModel.cs +++ b/ConfectioneryDataModels/IShopModel.cs @@ -7,6 +7,6 @@ namespace ConfectioneryDataModels string Name { get; } string Address { get; } DateTime DateOpening { get; } - Dictionary Pastries { get; } + Dictionary Pastries { get; } } } From d86f89eca47f3b9eadae62ec9c3248f07a2dd285 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sun, 5 Feb 2023 17:05:49 +0400 Subject: [PATCH 05/29] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA=D0=B0?= =?UTF-8?q?=20=D1=81=D1=83=D1=89=D0=BD=D0=BE=D1=81=D1=82=D0=B8=20"=D0=9C?= =?UTF-8?q?=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryBusinessLogic/ShopLogic.cs | 2 + .../DataListSingleton.cs | 4 +- ConfectionaryListImplement/ShopStorage.cs | 73 +++++++++++++++++-- 3 files changed, 71 insertions(+), 8 deletions(-) diff --git a/ConfectionaryBusinessLogic/ShopLogic.cs b/ConfectionaryBusinessLogic/ShopLogic.cs index c32a715..920f493 100644 --- a/ConfectionaryBusinessLogic/ShopLogic.cs +++ b/ConfectionaryBusinessLogic/ShopLogic.cs @@ -52,6 +52,7 @@ namespace ConfectioneryBusinessLogic public bool Create(ShopBindingModel model) { CheckModel(model); + model.Pastries = new(); if (_shopStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); @@ -62,6 +63,7 @@ namespace ConfectioneryBusinessLogic public bool Update(ShopBindingModel model) { CheckModel(model); + model.Pastries = new(); if (_shopStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); diff --git a/ConfectionaryListImplement/DataListSingleton.cs b/ConfectionaryListImplement/DataListSingleton.cs index dab3ef1..b34de5e 100644 --- a/ConfectionaryListImplement/DataListSingleton.cs +++ b/ConfectionaryListImplement/DataListSingleton.cs @@ -8,13 +8,13 @@ namespace ConfectioneryListImplement public List Components { get; set; } public List Orders { get; set; } public List Pastry { get; set; } - public List Shop { get; set; } + public List Shops { get; set; } private DataListSingleton() { Components = new List(); Orders = new List(); Pastry = new List(); - Shop = new List(); + Shops = new List(); } public static DataListSingleton GetInstance() { diff --git a/ConfectionaryListImplement/ShopStorage.cs b/ConfectionaryListImplement/ShopStorage.cs index d7c3547..893b6ad 100644 --- a/ConfectionaryListImplement/ShopStorage.cs +++ b/ConfectionaryListImplement/ShopStorage.cs @@ -15,32 +15,93 @@ namespace ConfectioneryListImplement public ShopViewModel? Delete(ShopBindingModel model) { - throw new NotImplementedException(); + for (int i = 0; i < _source.Shops.Count; ++i) + { + if (_source.Shops[i].Id == model.Id) + { + var element = _source.Shops[i]; + _source.Shops.RemoveAt(i); + return element.GetViewModel; + } + } + return null; } public ShopViewModel? GetElement(ShopSearchModel model) { - throw new NotImplementedException(); + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + foreach (var shop in _source.Shops) + { + if ((!string.IsNullOrEmpty(model.Name) && + shop.Name == model.Name) || + (model.Id.HasValue && shop.Id == model.Id)) + { + return shop.GetViewModel; + } + } + return null; } public List GetFilteredList(ShopSearchModel model) { - throw new NotImplementedException(); + var result = new List(); + if (string.IsNullOrEmpty(model.Name)) + { + return result; + } + foreach (var shop in _source.Shops) + { + if (shop.Name.Contains(model.Name ?? string.Empty)) + { + result.Add(shop.GetViewModel); + } + } + return result; } public List GetFullList() { - throw new NotImplementedException(); + var result = new List(); + foreach (var shop in _source.Shops) + { + result.Add(shop.GetViewModel); + } + return result; } public ShopViewModel? Insert(ShopBindingModel model) { - throw new NotImplementedException(); + model.Id = 1; + foreach (var shop in _source.Shops) + { + if (model.Id <= shop.Id) + { + model.Id = shop.Id + 1; + } + } + var newShop = Shop.Create(model); + if (newShop == null) + { + return null; + } + _source.Shops.Add(newShop); + return newShop.GetViewModel; } public ShopViewModel? Update(ShopBindingModel model) { - throw new NotImplementedException(); + foreach (var shop in _source.Shops) + { + if (shop.Id == model.Id) + { + shop.Update(model); + return shop.GetViewModel; + } + } + return null; } } } From b4673cefc0308faada2d8779d5315cb8709c34c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sun, 5 Feb 2023 17:13:36 +0400 Subject: [PATCH 06/29] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=84=D0=BE=D1=80=D0=BC=D0=B0=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D0=BE=D1=82=D0=BE=D0=B1=D1=80=D0=B0=D0=B6=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D1=81=D0=BF=D0=B8=D1=81=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=BC=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Confectionery/FormComponents.cs | 19 +--- Confectionery/FormViewShops.Designer.cs | 120 ++++++++++++++++++++++++ Confectionery/FormViewShops.cs | 116 +++++++++++++++++++++++ Confectionery/FormViewShops.resx | 60 ++++++++++++ 4 files changed, 301 insertions(+), 14 deletions(-) create mode 100644 Confectionery/FormViewShops.Designer.cs create mode 100644 Confectionery/FormViewShops.cs create mode 100644 Confectionery/FormViewShops.resx diff --git a/Confectionery/FormComponents.cs b/Confectionery/FormComponents.cs index c93c9ed..b869d81 100644 --- a/Confectionery/FormComponents.cs +++ b/Confectionery/FormComponents.cs @@ -1,15 +1,6 @@ using ConfectioneryContracts.BindingModels; using ConfectioneryContracts.BusinessLogicsContracts; using Microsoft.Extensions.Logging; -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; namespace ConfectioneryView { @@ -36,14 +27,14 @@ namespace ConfectioneryView { dataGridView.DataSource = list; dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["ComponentName"].AutoSizeMode = + dataGridView.Columns["Name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } - _logger.LogInformation("Загрузка компонентов"); + _logger.LogInformation("Загрузка магазинов"); } catch (Exception ex) { - _logger.LogError(ex, "Ошибка загрузки компонентов"); + _logger.LogError(ex, "Ошибка загрузки магазинов"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } @@ -87,7 +78,7 @@ namespace ConfectioneryView { int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - _logger.LogInformation("Удаление компонента"); + _logger.LogInformation("Удаление магазина"); try { if (!_logic.Delete(new ComponentBindingModel @@ -101,7 +92,7 @@ namespace ConfectioneryView } catch (Exception ex) { - _logger.LogError(ex, "Ошибка удаления компонента"); + _logger.LogError(ex, "Ошибка удаления магазина"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } diff --git a/Confectionery/FormViewShops.Designer.cs b/Confectionery/FormViewShops.Designer.cs new file mode 100644 index 0000000..82f254a --- /dev/null +++ b/Confectionery/FormViewShops.Designer.cs @@ -0,0 +1,120 @@ +namespace ConfectioneryView +{ + partial class FormViewShops + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.buttonRef = new System.Windows.Forms.Button(); + this.buttonDel = new System.Windows.Forms.Button(); + this.buttonUpd = new System.Windows.Forms.Button(); + this.buttonAdd = new System.Windows.Forms.Button(); + this.dataGridView = new System.Windows.Forms.DataGridView(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.SuspendLayout(); + // + // buttonRef + // + this.buttonRef.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.buttonRef.Location = new System.Drawing.Point(685, 202); + this.buttonRef.Name = "buttonRef"; + this.buttonRef.Size = new System.Drawing.Size(90, 37); + this.buttonRef.TabIndex = 14; + this.buttonRef.Text = "Обновить"; + this.buttonRef.UseVisualStyleBackColor = true; + this.buttonRef.Click += new System.EventHandler(this.ButtonRef_Click); + // + // buttonDel + // + this.buttonDel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.buttonDel.Location = new System.Drawing.Point(685, 151); + this.buttonDel.Name = "buttonDel"; + this.buttonDel.Size = new System.Drawing.Size(90, 33); + this.buttonDel.TabIndex = 13; + this.buttonDel.Text = "Удалить"; + this.buttonDel.UseVisualStyleBackColor = true; + this.buttonDel.Click += new System.EventHandler(this.ButtonDel_Click); + // + // buttonUpd + // + this.buttonUpd.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.buttonUpd.Location = new System.Drawing.Point(685, 102); + this.buttonUpd.Name = "buttonUpd"; + this.buttonUpd.Size = new System.Drawing.Size(90, 34); + this.buttonUpd.TabIndex = 12; + this.buttonUpd.Text = "Изменить"; + this.buttonUpd.UseVisualStyleBackColor = true; + this.buttonUpd.Click += new System.EventHandler(this.ButtonUpd_Click); + // + // buttonAdd + // + this.buttonAdd.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.buttonAdd.Location = new System.Drawing.Point(685, 57); + this.buttonAdd.Name = "buttonAdd"; + this.buttonAdd.Size = new System.Drawing.Size(90, 30); + this.buttonAdd.TabIndex = 11; + this.buttonAdd.Text = "Добавить"; + this.buttonAdd.UseVisualStyleBackColor = true; + this.buttonAdd.Click += new System.EventHandler(this.ButtonAdd_Click); + // + // dataGridView + // + this.dataGridView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Location = new System.Drawing.Point(12, 12); + this.dataGridView.Name = "dataGridView"; + this.dataGridView.RowTemplate.Height = 25; + this.dataGridView.Size = new System.Drawing.Size(540, 379); + this.dataGridView.TabIndex = 10; + // + // FormViewShops + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(787, 403); + this.Controls.Add(this.buttonRef); + this.Controls.Add(this.buttonDel); + this.Controls.Add(this.buttonUpd); + this.Controls.Add(this.buttonAdd); + this.Controls.Add(this.dataGridView); + this.Name = "FormViewShops"; + this.Text = "Просмотр магазинов"; + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private Button buttonRef; + private Button buttonDel; + private Button buttonUpd; + private Button buttonAdd; + private DataGridView dataGridView; + } +} \ No newline at end of file diff --git a/Confectionery/FormViewShops.cs b/Confectionery/FormViewShops.cs new file mode 100644 index 0000000..6c443d2 --- /dev/null +++ b/Confectionery/FormViewShops.cs @@ -0,0 +1,116 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace ConfectioneryView +{ + public partial class FormViewShops : Form + { + private readonly ILogger _logger; + private readonly IShopLogic _logic; + public FormViewShops(ILogger logger, IShopLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + private void FormShops_Load(object sender, EventArgs e) + { + LoadData(); + } + private void LoadData() + { + try + { + var list = _logic.ReadList(null); + if (list != null) + { + dataGridView.DataSource = list; + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["Name"].AutoSizeMode = + DataGridViewAutoSizeColumnMode.Fill; + } + _logger.LogInformation("Загрузка компонентов"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки компонентов"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + private void ButtonAdd_Click(object sender, EventArgs e) + { + var service = + Program.ServiceProvider?.GetService(typeof(FormComponent)); + if (service is FormComponent form) + { + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + private void ButtonUpd_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + var service = + Program.ServiceProvider?.GetService(typeof(FormComponent)); + if (service is FormComponent form) + { + form.Id = + Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + if (form.ShowDialog() == DialogResult.OK) + { + + } + } + LoadData(); + } + } + private void ButtonDel_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", + MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + int id = + Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + _logger.LogInformation("Удаление компонента"); + try + { + if (!_logic.Delete(new ShopBindingModel + { + Id = id + })) + { + throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления компонента"); + MessageBox.Show(ex.Message, "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + private void ButtonRef_Click(object sender, EventArgs e) + { + LoadData(); + } + } +} diff --git a/Confectionery/FormViewShops.resx b/Confectionery/FormViewShops.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/Confectionery/FormViewShops.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file From fccf69ebd23e733ff6ccec4597cd79923a0ec4e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sun, 5 Feb 2023 17:33:20 +0400 Subject: [PATCH 07/29] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=84=D0=BE=D1=80=D0=BC=D0=B0=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D1=81=D0=BC=D0=BE=D1=82=D1=80=D0=B0=20=D0=BA=D0=BE?= =?UTF-8?q?=D0=BD=D0=BA=D1=80=D0=B5=D1=82=D0=BD=D0=BE=D0=B3=D0=BE=20=D0=BC?= =?UTF-8?q?=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Confectionery/FormShop.Designer.cs | 89 ++++++++++++++++++++++++++++++ Confectionery/FormShop.cs | 65 ++++++++++++++++++++++ Confectionery/FormShop.resx | 60 ++++++++++++++++++++ 3 files changed, 214 insertions(+) create mode 100644 Confectionery/FormShop.Designer.cs create mode 100644 Confectionery/FormShop.cs create mode 100644 Confectionery/FormShop.resx diff --git a/Confectionery/FormShop.Designer.cs b/Confectionery/FormShop.Designer.cs new file mode 100644 index 0000000..da10c27 --- /dev/null +++ b/Confectionery/FormShop.Designer.cs @@ -0,0 +1,89 @@ +namespace ConfectioneryView +{ + partial class FormShop + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.label1 = new System.Windows.Forms.Label(); + this.comboBoxShop = new System.Windows.Forms.ComboBox(); + this.dataGridView = new System.Windows.Forms.DataGridView(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.SuspendLayout(); + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(12, 9); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(124, 15); + this.label1.TabIndex = 0; + this.label1.Text = "Выбранный магазин:"; + // + // comboBoxShop + // + this.comboBoxShop.FormattingEnabled = true; + this.comboBoxShop.Location = new System.Drawing.Point(142, 9); + this.comboBoxShop.Name = "comboBoxShop"; + this.comboBoxShop.Size = new System.Drawing.Size(121, 23); + this.comboBoxShop.TabIndex = 1; + this.comboBoxShop.SelectedIndexChanged += new System.EventHandler(this.ComboBoxShop_SelectedIndexChanged); + // + // dataGridView + // + this.dataGridView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Location = new System.Drawing.Point(12, 38); + this.dataGridView.Name = "dataGridView"; + this.dataGridView.RowTemplate.Height = 25; + this.dataGridView.Size = new System.Drawing.Size(583, 242); + this.dataGridView.TabIndex = 2; + // + // FormShop + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(607, 292); + this.Controls.Add(this.dataGridView); + this.Controls.Add(this.comboBoxShop); + this.Controls.Add(this.label1); + this.Name = "FormShop"; + this.Text = "Просмотр изделий магазина"; + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private Label label1; + private ComboBox comboBoxShop; + private DataGridView dataGridView; + } +} \ No newline at end of file diff --git a/Confectionery/FormShop.cs b/Confectionery/FormShop.cs new file mode 100644 index 0000000..f677a26 --- /dev/null +++ b/Confectionery/FormShop.cs @@ -0,0 +1,65 @@ +using ConfectioneryContracts.BusinessLogicsContracts; +using ConfectioneryContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace ConfectioneryView +{ + public partial class FormShop : Form + { + private readonly List? _list; + private readonly IShopLogic _logic; + private readonly ILogger _logger; + + public FormShop(ILogger logger, IShopLogic logic) + { + InitializeComponent(); + _logger = logger; + _list = logic.ReadList(null); + _logic = logic; + if (_list != null) + { + comboBoxShop.DisplayMember = "ComponentName"; + comboBoxShop.ValueMember = "Id"; + comboBoxShop.DataSource = _list; + comboBoxShop.SelectedItem = null; + } + } + + private void LoadData() + { + try + { + var list = _logic.ReadList(null); + if (list != null) + { + dataGridView.DataSource = list; + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["Name"].AutoSizeMode = + DataGridViewAutoSizeColumnMode.Fill; + } + _logger.LogInformation("Загрузка магазинов"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки магазинов"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + + private void ComboBoxShop_SelectedIndexChanged(object sender, EventArgs e) + { + LoadData(); + } + } +} diff --git a/Confectionery/FormShop.resx b/Confectionery/FormShop.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/Confectionery/FormShop.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file From 475face56324153f962da7753a0ce7ae9baf7066 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sun, 5 Feb 2023 17:44:00 +0400 Subject: [PATCH 08/29] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD?= =?UTF-8?q?=D0=BE=D1=81=D1=82=D1=8C=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D1=8F=D1=82=D1=8C=20=D0=B8=D0=B7=D0=B4=D0=B5=D0=BB=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=B2=20=D0=BC=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryBusinessLogic/ShopLogic.cs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/ConfectionaryBusinessLogic/ShopLogic.cs b/ConfectionaryBusinessLogic/ShopLogic.cs index 920f493..cf9663c 100644 --- a/ConfectionaryBusinessLogic/ShopLogic.cs +++ b/ConfectionaryBusinessLogic/ShopLogic.cs @@ -115,6 +115,10 @@ namespace ConfectioneryBusinessLogic { throw new ArgumentNullException(nameof(model)); } + if (count <= 0) + { + throw new ArgumentException("Количество добавляемого изделия должно быть больше 0", nameof(count)); + } _logger.LogInformation("AddPastryInShop. ShopName:{ShopName}.Id:{ Id}", model.Name, model.Id); var element = _shopStorage.GetElement(model); @@ -125,7 +129,22 @@ namespace ConfectioneryBusinessLogic } _logger.LogInformation("AddPastryInShop find. Id:{Id}", element.Id); - return element.Pastries.TryAdd(pastry.Id, (pastry, count)); + + if (element.Pastries.TryGetValue(pastry.Id, out var pair)) + { + pair.Item2 += count; + _logger.LogInformation( + "AddPastryInShop. Has been added {count} {pastry} in {ShopName}", + count, pastry.PastryName, element.Name); + } + else + { + element.Pastries[pastry.Id] = (pastry, count); + _logger.LogInformation( + "AddPastryInShop. Has been added {count} new Pastry {pastry} in {ShopName}", + count, pastry.PastryName, element.Name); + } + return true; } } } From 6ea2ac3a753ab0e696b378edd1c9bd075c1858fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sun, 5 Feb 2023 19:37:08 +0400 Subject: [PATCH 09/29] =?UTF-8?q?=D0=A1=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD?= =?UTF-8?q?=D0=B0=20=D1=84=D0=BE=D1=80=D0=BC=D0=B0=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=B8=D0=B7=D0=B4=D0=B5=D0=BB=D0=B8=D0=B9=20=D0=B2=20=D0=BC?= =?UTF-8?q?=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Confectionery/FormAddPastryInShop.Designer.cs | 151 ++++++++++++++++++ Confectionery/FormAddPastryInShop.cs | 49 ++++++ Confectionery/FormAddPastryInShop.resx | 60 +++++++ Confectionery/FormMain.Designer.cs | 16 +- Confectionery/FormMain.cs | 13 +- Confectionery/FormShop.cs | 2 +- 6 files changed, 285 insertions(+), 6 deletions(-) create mode 100644 Confectionery/FormAddPastryInShop.Designer.cs create mode 100644 Confectionery/FormAddPastryInShop.cs create mode 100644 Confectionery/FormAddPastryInShop.resx diff --git a/Confectionery/FormAddPastryInShop.Designer.cs b/Confectionery/FormAddPastryInShop.Designer.cs new file mode 100644 index 0000000..76738b9 --- /dev/null +++ b/Confectionery/FormAddPastryInShop.Designer.cs @@ -0,0 +1,151 @@ +namespace ConfectioneryView +{ + partial class FormAddPastryInShop + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.comboBoxShop = new System.Windows.Forms.ComboBox(); + this.label1 = new System.Windows.Forms.Label(); + this.label2 = new System.Windows.Forms.Label(); + this.label3 = new System.Windows.Forms.Label(); + this.comboBoxPastry = new System.Windows.Forms.ComboBox(); + this.numericUpDownCount = new System.Windows.Forms.NumericUpDown(); + this.buttonSave = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownCount)).BeginInit(); + this.SuspendLayout(); + // + // comboBoxShop + // + this.comboBoxShop.FormattingEnabled = true; + this.comboBoxShop.Location = new System.Drawing.Point(214, 17); + this.comboBoxShop.Name = "comboBoxShop"; + this.comboBoxShop.Size = new System.Drawing.Size(222, 23); + this.comboBoxShop.TabIndex = 3; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(84, 20); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(124, 15); + this.label1.TabIndex = 2; + this.label1.Text = "Выбранный магазин:"; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(12, 47); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(196, 15); + this.label2.TabIndex = 4; + this.label2.Text = "Изделие которое нужно добавить:"; + // + // label3 + // + this.label3.AutoSize = true; + this.label3.Location = new System.Drawing.Point(133, 73); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(75, 15); + this.label3.TabIndex = 5; + this.label3.Text = "Количество:"; + // + // comboBoxPastry + // + this.comboBoxPastry.FormattingEnabled = true; + this.comboBoxPastry.Location = new System.Drawing.Point(214, 44); + this.comboBoxPastry.Name = "comboBoxPastry"; + this.comboBoxPastry.Size = new System.Drawing.Size(222, 23); + this.comboBoxPastry.TabIndex = 6; + // + // numericUpDownCount + // + this.numericUpDownCount.Location = new System.Drawing.Point(215, 71); + this.numericUpDownCount.Maximum = new decimal(new int[] { + 1410065408, + 2, + 0, + 0}); + this.numericUpDownCount.Name = "numericUpDownCount"; + this.numericUpDownCount.Size = new System.Drawing.Size(221, 23); + this.numericUpDownCount.TabIndex = 7; + // + // buttonSave + // + this.buttonSave.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonSave.Location = new System.Drawing.Point(280, 123); + this.buttonSave.Name = "buttonSave"; + this.buttonSave.Size = new System.Drawing.Size(75, 23); + this.buttonSave.TabIndex = 8; + this.buttonSave.Text = "Сохранить"; + this.buttonSave.UseVisualStyleBackColor = true; + this.buttonSave.Click += new System.EventHandler(this.ButtonSave_Click); + // + // buttonCancel + // + this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonCancel.Location = new System.Drawing.Point(361, 123); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(75, 23); + this.buttonCancel.TabIndex = 9; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); + // + // FormAddPastryInShop + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(448, 158); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSave); + this.Controls.Add(this.numericUpDownCount); + this.Controls.Add(this.comboBoxPastry); + this.Controls.Add(this.label3); + this.Controls.Add(this.label2); + this.Controls.Add(this.comboBoxShop); + this.Controls.Add(this.label1); + this.Name = "FormAddPastryInShop"; + this.Text = "FormAddPastryInShop"; + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownCount)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private ComboBox comboBoxShop; + private Label label1; + private Label label2; + private Label label3; + private ComboBox comboBoxPastry; + private NumericUpDown numericUpDownCount; + private Button buttonSave; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/Confectionery/FormAddPastryInShop.cs b/Confectionery/FormAddPastryInShop.cs new file mode 100644 index 0000000..e5c786b --- /dev/null +++ b/Confectionery/FormAddPastryInShop.cs @@ -0,0 +1,49 @@ +using ConfectioneryContracts.BusinessLogicsContracts; +using ConfectioneryContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace ConfectioneryView +{ + public partial class FormAddPastryInShop : Form + { + private readonly ILogger _logger; + private readonly List? _listShops; + private readonly List? _listPastries; + + public FormAddPastryInShop(ILogger logger, IShopLogic shopLogic, IPastryLogic pastryLogic) + { + InitializeComponent(); + _logger = logger; + _listShops = shopLogic.ReadList(null); + if (_listShops != null) + { + comboBoxShop.DisplayMember = "ComponentName"; + comboBoxShop.ValueMember = "Id"; + comboBoxShop.DataSource = _listShops; + comboBoxShop.SelectedItem = null; + } + + _listPastries + } + + private void ButtonSave_Click(object sender, EventArgs e) + { + + } + + private void ButtonCancel_Click(object sender, EventArgs e) + { + + } + } +} diff --git a/Confectionery/FormAddPastryInShop.resx b/Confectionery/FormAddPastryInShop.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/Confectionery/FormAddPastryInShop.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Confectionery/FormMain.Designer.cs b/Confectionery/FormMain.Designer.cs index 3522d86..190dc44 100644 --- a/Confectionery/FormMain.Designer.cs +++ b/Confectionery/FormMain.Designer.cs @@ -38,6 +38,7 @@ this.button2 = new System.Windows.Forms.Button(); this.button3 = new System.Windows.Forms.Button(); this.button4 = new System.Windows.Forms.Button(); + this.ShopsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.menuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.SuspendLayout(); @@ -56,7 +57,8 @@ // this.справочникиToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.pastryToolStripMenuItem, - this.componentToolStripMenuItem}); + this.componentToolStripMenuItem, + this.ShopsToolStripMenuItem}); this.справочникиToolStripMenuItem.Name = "справочникиToolStripMenuItem"; this.справочникиToolStripMenuItem.Size = new System.Drawing.Size(94, 20); this.справочникиToolStripMenuItem.Text = "Справочники"; @@ -64,14 +66,14 @@ // pastryToolStripMenuItem // this.pastryToolStripMenuItem.Name = "pastryToolStripMenuItem"; - this.pastryToolStripMenuItem.Size = new System.Drawing.Size(145, 22); + this.pastryToolStripMenuItem.Size = new System.Drawing.Size(180, 22); this.pastryToolStripMenuItem.Text = "Изделия"; this.pastryToolStripMenuItem.Click += new System.EventHandler(this.PastryToolStripMenuItem_Click); // // componentToolStripMenuItem // this.componentToolStripMenuItem.Name = "componentToolStripMenuItem"; - this.componentToolStripMenuItem.Size = new System.Drawing.Size(145, 22); + this.componentToolStripMenuItem.Size = new System.Drawing.Size(180, 22); this.componentToolStripMenuItem.Text = "Компоненты"; this.componentToolStripMenuItem.Click += new System.EventHandler(this.ComponentsToolStripMenuItem_Click); // @@ -142,6 +144,13 @@ this.button4.UseVisualStyleBackColor = true; this.button4.Click += new System.EventHandler(this.ButtonRef_Click); // + // ShopsToolStripMenuItem + // + this.ShopsToolStripMenuItem.Name = "ShopsToolStripMenuItem"; + this.ShopsToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.ShopsToolStripMenuItem.Text = "Магазины"; + this.ShopsToolStripMenuItem.Click += new System.EventHandler(this.ShopsToolStripMenuItem_Click); + // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -177,5 +186,6 @@ private Button button4; private ToolStripMenuItem pastryToolStripMenuItem; private ToolStripMenuItem componentToolStripMenuItem; + private ToolStripMenuItem ShopsToolStripMenuItem; } } \ No newline at end of file diff --git a/Confectionery/FormMain.cs b/Confectionery/FormMain.cs index 628ea63..489ec5c 100644 --- a/Confectionery/FormMain.cs +++ b/Confectionery/FormMain.cs @@ -62,8 +62,7 @@ namespace ConfectioneryView private void ButtonCreateOrder_Click(object sender, EventArgs e) { - var service = - Program.ServiceProvider?.GetService(typeof(FormCreateOrder)); + var service = Program.ServiceProvider?.GetService(typeof(FormCreateOrder)); if (service is FormCreateOrder form) { form.ShowDialog(); @@ -152,5 +151,15 @@ namespace ConfectioneryView { LoadData(); } + + private void ShopsToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormViewShops)); + if (service is FormViewShops form) + { + form.ShowDialog(); + LoadData(); + } + } } } \ No newline at end of file diff --git a/Confectionery/FormShop.cs b/Confectionery/FormShop.cs index f677a26..5026ff8 100644 --- a/Confectionery/FormShop.cs +++ b/Confectionery/FormShop.cs @@ -28,7 +28,7 @@ namespace ConfectioneryView _logic = logic; if (_list != null) { - comboBoxShop.DisplayMember = "ComponentName"; + comboBoxShop.DisplayMember = "Name"; comboBoxShop.ValueMember = "Id"; comboBoxShop.DataSource = _list; comboBoxShop.SelectedItem = null; From 78a3293bb54dbfadad8d7d62aeb40dfcf0831127 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sun, 5 Feb 2023 19:51:51 +0400 Subject: [PATCH 10/29] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D0=BF=D0=BE=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=BC=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=B2=20=D0=B3=D0=BB=D0=B0=D0=B2=D0=BD=D1=83=D1=8E=20?= =?UTF-8?q?=D1=84=D0=BE=D1=80=D0=BC=D1=83=20=D0=B8=20=D0=B2=D0=BD=D0=B5?= =?UTF-8?q?=D0=B4=D1=80=D0=B5=D0=BD=D1=8B=20=D0=B2=D1=81=D0=B5=20=D0=BD?= =?UTF-8?q?=D0=BE=D0=B2=D1=8B=D0=B5=20=D0=B7=D0=B0=D0=B2=D0=B8=D1=81=D0=B8?= =?UTF-8?q?=D0=BC=D0=BE=D1=81=D1=82=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Confectionery/FormAddPastryInShop.cs | 62 +++++++++++++++++++++++++--- Confectionery/FormMain.Designer.cs | 25 ++++++++--- Confectionery/FormMain.cs | 14 +++++-- Confectionery/Program.cs | 5 +++ 4 files changed, 92 insertions(+), 14 deletions(-) diff --git a/Confectionery/FormAddPastryInShop.cs b/Confectionery/FormAddPastryInShop.cs index e5c786b..362584b 100644 --- a/Confectionery/FormAddPastryInShop.cs +++ b/Confectionery/FormAddPastryInShop.cs @@ -1,4 +1,5 @@ -using ConfectioneryContracts.BusinessLogicsContracts; +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.BusinessLogicsContracts; using ConfectioneryContracts.ViewModels; using Microsoft.Extensions.Logging; using System; @@ -17,33 +18,84 @@ namespace ConfectioneryView public partial class FormAddPastryInShop : Form { private readonly ILogger _logger; + private readonly IShopLogic _shopLogic; + private readonly IPastryLogic _pastryLogic; private readonly List? _listShops; private readonly List? _listPastries; public FormAddPastryInShop(ILogger logger, IShopLogic shopLogic, IPastryLogic pastryLogic) { InitializeComponent(); + _shopLogic = shopLogic; + _pastryLogic = pastryLogic; _logger = logger; _listShops = shopLogic.ReadList(null); if (_listShops != null) { - comboBoxShop.DisplayMember = "ComponentName"; + comboBoxShop.DisplayMember = "Name"; comboBoxShop.ValueMember = "Id"; comboBoxShop.DataSource = _listShops; comboBoxShop.SelectedItem = null; } - _listPastries + _listPastries = pastryLogic.ReadList(null); + if (_listPastries != null) + { + comboBoxPastry.DisplayMember = "PastryName"; + comboBoxPastry.ValueMember= "Id"; + comboBoxPastry.DataSource = _listPastries; + comboBoxPastry.SelectedItem = null; + } } private void ButtonSave_Click(object sender, EventArgs e) { - + if (comboBoxShop.SelectedValue == null) + { + MessageBox.Show("Выберите магазин", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (comboBoxPastry.SelectedValue == null) + { + MessageBox.Show("Выберите изделие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Добавление изделия в магазин"); + try + { + var pastry = _pastryLogic.ReadElement(new() + { + Id = (int)comboBoxPastry.SelectedValue + }); + if (pastry == null) + { + throw new Exception("Не найдено изделие. Дополнительная информация в логах."); + } + var resultOperation = _shopLogic.AddPastry( + model: new() { Id = (int)comboBoxShop.SelectedValue }, + pastry: pastry, + count: (int)numericUpDownCount.Value + ); + if (!resultOperation) + { + throw new Exception("Ошибка при добавлении. Дополнительная информация в логах."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", + MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка сохранения изделия"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } private void ButtonCancel_Click(object sender, EventArgs e) { - + DialogResult = DialogResult.Cancel; + Close(); } } } diff --git a/Confectionery/FormMain.Designer.cs b/Confectionery/FormMain.Designer.cs index 190dc44..ca969bd 100644 --- a/Confectionery/FormMain.Designer.cs +++ b/Confectionery/FormMain.Designer.cs @@ -32,13 +32,14 @@ this.справочникиToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.pastryToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.componentToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.ShopsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.dataGridView = new System.Windows.Forms.DataGridView(); this.buttonCreateOrder = new System.Windows.Forms.Button(); this.buttonTakeOrderInWork = new System.Windows.Forms.Button(); this.button2 = new System.Windows.Forms.Button(); this.button3 = new System.Windows.Forms.Button(); this.button4 = new System.Windows.Forms.Button(); - this.ShopsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); + this.buttonAddPastryInShop = new System.Windows.Forms.Button(); this.menuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.SuspendLayout(); @@ -77,6 +78,13 @@ this.componentToolStripMenuItem.Text = "Компоненты"; this.componentToolStripMenuItem.Click += new System.EventHandler(this.ComponentsToolStripMenuItem_Click); // + // ShopsToolStripMenuItem + // + this.ShopsToolStripMenuItem.Name = "ShopsToolStripMenuItem"; + this.ShopsToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.ShopsToolStripMenuItem.Text = "Магазины"; + this.ShopsToolStripMenuItem.Click += new System.EventHandler(this.ShopsToolStripMenuItem_Click); + // // dataGridView // this.dataGridView.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) @@ -144,18 +152,22 @@ this.button4.UseVisualStyleBackColor = true; this.button4.Click += new System.EventHandler(this.ButtonRef_Click); // - // ShopsToolStripMenuItem + // buttonAddPastryInShop // - this.ShopsToolStripMenuItem.Name = "ShopsToolStripMenuItem"; - this.ShopsToolStripMenuItem.Size = new System.Drawing.Size(180, 22); - this.ShopsToolStripMenuItem.Text = "Магазины"; - this.ShopsToolStripMenuItem.Click += new System.EventHandler(this.ShopsToolStripMenuItem_Click); + this.buttonAddPastryInShop.Location = new System.Drawing.Point(624, 326); + this.buttonAddPastryInShop.Name = "buttonAddPastryInShop"; + this.buttonAddPastryInShop.Size = new System.Drawing.Size(147, 31); + this.buttonAddPastryInShop.TabIndex = 7; + this.buttonAddPastryInShop.Text = "Пополнение магазина"; + this.buttonAddPastryInShop.UseVisualStyleBackColor = true; + this.buttonAddPastryInShop.Click += new System.EventHandler(this.ButtonAddPastryInShop_Click); // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(783, 380); + this.Controls.Add(this.buttonAddPastryInShop); this.Controls.Add(this.button4); this.Controls.Add(this.button3); this.Controls.Add(this.button2); @@ -187,5 +199,6 @@ private ToolStripMenuItem pastryToolStripMenuItem; private ToolStripMenuItem componentToolStripMenuItem; private ToolStripMenuItem ShopsToolStripMenuItem; + private Button buttonAddPastryInShop; } } \ No newline at end of file diff --git a/Confectionery/FormMain.cs b/Confectionery/FormMain.cs index 489ec5c..92e771a 100644 --- a/Confectionery/FormMain.cs +++ b/Confectionery/FormMain.cs @@ -30,8 +30,8 @@ namespace ConfectioneryView { dataGridView.DataSource = list; dataGridView.Columns["Id"].Visible = false; - /*dataGridView.Columns["PastryName"].AutoSizeMode = - DataGridViewAutoSizeColumnMode.Fill;*/ + dataGridView.Columns["PastryName"].AutoSizeMode = + DataGridViewAutoSizeColumnMode.Fill; } _logger.LogInformation(" "); } @@ -158,7 +158,15 @@ namespace ConfectioneryView if (service is FormViewShops form) { form.ShowDialog(); - LoadData(); + } + } + + private void ButtonAddPastryInShop_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormAddPastryInShop)); + if (service is FormAddPastryInShop form) + { + form.ShowDialog(); } } } diff --git a/Confectionery/Program.cs b/Confectionery/Program.cs index d3d5709..645885a 100644 --- a/Confectionery/Program.cs +++ b/Confectionery/Program.cs @@ -1,4 +1,5 @@ using AbstractShopListImplement.Implements; +using ConfectioneryBusinessLogic; using ConfectioneryBusinessLogic.BusinessLogics; using ConfectioneryContracts.BusinessLogicsContracts; using ConfectioneryContracts.StoragesContract; @@ -38,9 +39,11 @@ namespace ConfectioneryView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -48,6 +51,8 @@ namespace ConfectioneryView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file From d43db88851998d36872fabd5d41e374def9e6966 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sun, 5 Feb 2023 19:54:17 +0400 Subject: [PATCH 11/29] fix --- Confectionery/FormComponents.cs | 10 +++++----- Confectionery/FormViewShops.cs | 17 ++++------------- 2 files changed, 9 insertions(+), 18 deletions(-) diff --git a/Confectionery/FormComponents.cs b/Confectionery/FormComponents.cs index b869d81..b9600b5 100644 --- a/Confectionery/FormComponents.cs +++ b/Confectionery/FormComponents.cs @@ -27,14 +27,14 @@ namespace ConfectioneryView { dataGridView.DataSource = list; dataGridView.Columns["Id"].Visible = false; - dataGridView.Columns["Name"].AutoSizeMode = + dataGridView.Columns["ComponentName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } - _logger.LogInformation("Загрузка магазинов"); + _logger.LogInformation("Загрузка изделий"); } catch (Exception ex) { - _logger.LogError(ex, "Ошибка загрузки магазинов"); + _logger.LogError(ex, "Ошибка загрузки изделий"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } @@ -78,7 +78,7 @@ namespace ConfectioneryView { int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - _logger.LogInformation("Удаление магазина"); + _logger.LogInformation("Удаление изделия"); try { if (!_logic.Delete(new ComponentBindingModel @@ -92,7 +92,7 @@ namespace ConfectioneryView } catch (Exception ex) { - _logger.LogError(ex, "Ошибка удаления магазина"); + _logger.LogError(ex, "Ошибка удаления изделия"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } diff --git a/Confectionery/FormViewShops.cs b/Confectionery/FormViewShops.cs index 6c443d2..4ff6fc5 100644 --- a/Confectionery/FormViewShops.cs +++ b/Confectionery/FormViewShops.cs @@ -1,15 +1,6 @@ using ConfectioneryContracts.BindingModels; using ConfectioneryContracts.BusinessLogicsContracts; using Microsoft.Extensions.Logging; -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; namespace ConfectioneryView { @@ -39,11 +30,11 @@ namespace ConfectioneryView dataGridView.Columns["Name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } - _logger.LogInformation("Загрузка компонентов"); + _logger.LogInformation("Загрузка магазинов"); } catch (Exception ex) { - _logger.LogError(ex, "Ошибка загрузки компонентов"); + _logger.LogError(ex, "Ошибка загрузки магазинов"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } @@ -87,7 +78,7 @@ namespace ConfectioneryView { int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - _logger.LogInformation("Удаление компонента"); + _logger.LogInformation("Удаление магазина"); try { if (!_logic.Delete(new ShopBindingModel @@ -101,7 +92,7 @@ namespace ConfectioneryView } catch (Exception ex) { - _logger.LogError(ex, "Ошибка удаления компонента"); + _logger.LogError(ex, "Ошибка удаления магазина"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } From 785d086b3f84d1e7b02faee1661ff53824d1ed79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Mon, 6 Feb 2023 01:31:42 +0400 Subject: [PATCH 12/29] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryListImplement/Shop.cs | 3 +- Confectionery/FormShop.Designer.cs | 1 + Confectionery/FormShop.cs | 67 +++++++++++++----------------- Confectionery/FormViewShops.cs | 18 ++------ 4 files changed, 36 insertions(+), 53 deletions(-) diff --git a/ConfectionaryListImplement/Shop.cs b/ConfectionaryListImplement/Shop.cs index c1a7630..fcce77b 100644 --- a/ConfectionaryListImplement/Shop.cs +++ b/ConfectionaryListImplement/Shop.cs @@ -52,7 +52,8 @@ namespace ConfectioneryListImplement Id = Id, Name = Name, Address = Address, - Pastries = Pastries + Pastries = Pastries, + DateOpening = DateOpening, }; } } diff --git a/Confectionery/FormShop.Designer.cs b/Confectionery/FormShop.Designer.cs index 15049d1..694a22f 100644 --- a/Confectionery/FormShop.Designer.cs +++ b/Confectionery/FormShop.Designer.cs @@ -163,6 +163,7 @@ this.Controls.Add(this.label1); this.Name = "FormShop"; this.Text = "Просмотр изделий магазина"; + this.Load += new System.EventHandler(this.FormShop_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); diff --git a/Confectionery/FormShop.cs b/Confectionery/FormShop.cs index 1b2c9f9..9b640b0 100644 --- a/Confectionery/FormShop.cs +++ b/Confectionery/FormShop.cs @@ -23,34 +23,21 @@ namespace ConfectioneryView private readonly List? _listShops; private readonly IShopLogic _logic; private readonly ILogger _logger; - private IShopModel? _currentShopModel; public int Id { - get - { - return Convert.ToInt32(comboBoxShop.SelectedValue); - } - set - { - comboBoxShop.SelectedValue = value; - } - } - public IShopModel? ShopModel - { - get => _currentShopModel; + get; set; } private IShopModel? GetShop() { - var list = _logic.ReadList(null); - if (list == null) + if (_listShops == null) { return null; } - foreach (var elem in list) + foreach (var elem in _listShops) { - if (elem.Id == this.Id) + if (elem.Id == Id) { return elem; } @@ -77,22 +64,18 @@ namespace ConfectioneryView { try { - var currentShop = _logic.ReadElement(new() { Id = this.Id}); - if (currentShop != null) - { - var vmodel = GetShop(); - if (vmodel != null) - { - comboBoxShop.Text = vmodel.Name; - textBoxAddress.Text = vmodel.Address; - textBoxDateOpening.Text = Convert.ToString(vmodel.DateOpening); - } + var model = GetShop(); + if (model != null) + { + comboBoxShop.Text = model.Name; + textBoxAddress.Text = model.Address; + textBoxDateOpening.Text = Convert.ToString(model.DateOpening); + dataGridView.Rows.Clear(); - foreach (var el in currentShop.Pastries.Values) + foreach (var el in model.Pastries.Values) { dataGridView.Rows.Add(new object[]{el.Item1.PastryName, el.Item1.Price, el.Item2 }); } - } _logger.LogInformation("Загрузка магазинов"); } @@ -126,13 +109,6 @@ namespace ConfectioneryView _logger.LogInformation("Сохранение изделия"); try { - var vmodel = GetShop(); - if (vmodel != null) - { - _currentShopModel = vmodel; - return; - } - // Создаем новый магазин если не нашли такого DateTime.TryParse(textBoxDateOpening.Text, out var dateTime); ShopBindingModel model = new() { @@ -140,8 +116,18 @@ namespace ConfectioneryView Address = textBoxAddress.Text, DateOpening = dateTime }; - var operationResult = _logic.Create(model); - _currentShopModel = model; + var vmodel = GetShop(); + bool operationResult = false; + + if (vmodel != null) + { + model.Id = vmodel.Id; + operationResult = _logic.Update(model); + } + else + { + operationResult = _logic.Create(model); + } if (!operationResult) { throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); @@ -163,5 +149,10 @@ namespace ConfectioneryView DialogResult = DialogResult.Cancel; Close(); } + + private void FormShop_Load(object sender, EventArgs e) + { + LoadData(); + } } } diff --git a/Confectionery/FormViewShops.cs b/Confectionery/FormViewShops.cs index d7fa00a..419130c 100644 --- a/Confectionery/FormViewShops.cs +++ b/Confectionery/FormViewShops.cs @@ -26,7 +26,9 @@ namespace ConfectioneryView if (list != null) { dataGridView.DataSource = list; + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["Pastries"].Visible = false; dataGridView.Columns["Name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } @@ -57,24 +59,12 @@ namespace ConfectioneryView var service = Program.ServiceProvider?.GetService(typeof(FormShop)); if (service is FormShop form) { - var id = form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); if (form.ShowDialog() == DialogResult.OK) { - var model = form.ShopModel; - if (model != null) - { - _logic.Update(new() - { - Id = id, - Address = model.Address, - Name = model.Name, - DateOpening= model.DateOpening, - }); - } - + LoadData(); } } - LoadData(); } } private void ButtonDel_Click(object sender, EventArgs e) From 8bca5845e52a103474030b2312daf99009112e2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Mon, 6 Feb 2023 01:31:42 +0400 Subject: [PATCH 13/29] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryListImplement/Shop.cs | 3 +- Confectionery/FormShop.Designer.cs | 1 + Confectionery/FormShop.cs | 73 +++++++++++-------------- Confectionery/FormViewShops.Designer.cs | 1 + Confectionery/FormViewShops.cs | 18 ++---- 5 files changed, 40 insertions(+), 56 deletions(-) diff --git a/ConfectionaryListImplement/Shop.cs b/ConfectionaryListImplement/Shop.cs index c1a7630..fcce77b 100644 --- a/ConfectionaryListImplement/Shop.cs +++ b/ConfectionaryListImplement/Shop.cs @@ -52,7 +52,8 @@ namespace ConfectioneryListImplement Id = Id, Name = Name, Address = Address, - Pastries = Pastries + Pastries = Pastries, + DateOpening = DateOpening, }; } } diff --git a/Confectionery/FormShop.Designer.cs b/Confectionery/FormShop.Designer.cs index 15049d1..694a22f 100644 --- a/Confectionery/FormShop.Designer.cs +++ b/Confectionery/FormShop.Designer.cs @@ -163,6 +163,7 @@ this.Controls.Add(this.label1); this.Name = "FormShop"; this.Text = "Просмотр изделий магазина"; + this.Load += new System.EventHandler(this.FormShop_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); diff --git a/Confectionery/FormShop.cs b/Confectionery/FormShop.cs index 1b2c9f9..b29d8b4 100644 --- a/Confectionery/FormShop.cs +++ b/Confectionery/FormShop.cs @@ -23,34 +23,21 @@ namespace ConfectioneryView private readonly List? _listShops; private readonly IShopLogic _logic; private readonly ILogger _logger; - private IShopModel? _currentShopModel; public int Id { - get - { - return Convert.ToInt32(comboBoxShop.SelectedValue); - } - set - { - comboBoxShop.SelectedValue = value; - } - } - public IShopModel? ShopModel - { - get => _currentShopModel; + get; set; } - private IShopModel? GetShop() + private IShopModel? GetShop(int id) { - var list = _logic.ReadList(null); - if (list == null) + if (_listShops == null) { return null; } - foreach (var elem in list) + foreach (var elem in _listShops) { - if (elem.Id == this.Id) + if (elem.Id == id) { return elem; } @@ -73,26 +60,22 @@ namespace ConfectioneryView } } - private void LoadData() + private void LoadData(bool extendDate = true) { try { - var currentShop = _logic.ReadElement(new() { Id = this.Id}); - if (currentShop != null) - { - var vmodel = GetShop(); - if (vmodel != null) - { - comboBoxShop.Text = vmodel.Name; - textBoxAddress.Text = vmodel.Address; - textBoxDateOpening.Text = Convert.ToString(vmodel.DateOpening); - } + var model = GetShop(extendDate ? Id : Convert.ToInt32(comboBoxShop.SelectedValue)); + if (model != null) + { + comboBoxShop.Text = model.Name; + textBoxAddress.Text = model.Address; + textBoxDateOpening.Text = Convert.ToString(model.DateOpening); + dataGridView.Rows.Clear(); - foreach (var el in currentShop.Pastries.Values) + foreach (var el in model.Pastries.Values) { dataGridView.Rows.Add(new object[]{el.Item1.PastryName, el.Item1.Price, el.Item2 }); } - } _logger.LogInformation("Загрузка магазинов"); } @@ -106,7 +89,7 @@ namespace ConfectioneryView private void ComboBoxShop_SelectedIndexChanged(object sender, EventArgs e) { - LoadData(); + LoadData(false); } private void ButtonSave_Click(object sender, EventArgs e) @@ -126,13 +109,6 @@ namespace ConfectioneryView _logger.LogInformation("Сохранение изделия"); try { - var vmodel = GetShop(); - if (vmodel != null) - { - _currentShopModel = vmodel; - return; - } - // Создаем новый магазин если не нашли такого DateTime.TryParse(textBoxDateOpening.Text, out var dateTime); ShopBindingModel model = new() { @@ -140,8 +116,18 @@ namespace ConfectioneryView Address = textBoxAddress.Text, DateOpening = dateTime }; - var operationResult = _logic.Create(model); - _currentShopModel = model; + var vmodel = GetShop(Id); + bool operationResult = false; + + if (vmodel != null) + { + model.Id = vmodel.Id; + operationResult = _logic.Update(model); + } + else + { + operationResult = _logic.Create(model); + } if (!operationResult) { throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); @@ -163,5 +149,10 @@ namespace ConfectioneryView DialogResult = DialogResult.Cancel; Close(); } + + private void FormShop_Load(object sender, EventArgs e) + { + LoadData(); + } } } diff --git a/Confectionery/FormViewShops.Designer.cs b/Confectionery/FormViewShops.Designer.cs index 82f254a..894b8f9 100644 --- a/Confectionery/FormViewShops.Designer.cs +++ b/Confectionery/FormViewShops.Designer.cs @@ -104,6 +104,7 @@ this.Controls.Add(this.dataGridView); this.Name = "FormViewShops"; this.Text = "Просмотр магазинов"; + this.Load += new System.EventHandler(this.FormShops_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); this.ResumeLayout(false); diff --git a/Confectionery/FormViewShops.cs b/Confectionery/FormViewShops.cs index d7fa00a..419130c 100644 --- a/Confectionery/FormViewShops.cs +++ b/Confectionery/FormViewShops.cs @@ -26,7 +26,9 @@ namespace ConfectioneryView if (list != null) { dataGridView.DataSource = list; + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["Pastries"].Visible = false; dataGridView.Columns["Name"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } @@ -57,24 +59,12 @@ namespace ConfectioneryView var service = Program.ServiceProvider?.GetService(typeof(FormShop)); if (service is FormShop form) { - var id = form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); if (form.ShowDialog() == DialogResult.OK) { - var model = form.ShopModel; - if (model != null) - { - _logic.Update(new() - { - Id = id, - Address = model.Address, - Name = model.Name, - DateOpening= model.DateOpening, - }); - } - + LoadData(); } } - LoadData(); } } private void ButtonDel_Click(object sender, EventArgs e) From 1262146e7aebef2f766b529676accf92b4cb27ed Mon Sep 17 00:00:00 2001 From: 1yuee Date: Tue, 7 Feb 2023 11:03:47 +0400 Subject: [PATCH 14/29] fix --- ConfectionaryBusinessLogic/ShopLogic.cs | 11 +++++++++-- ConfectionaryListImplement/Shop.cs | 2 +- Confectionery/FormComponents.Designer.cs | 1 + Confectionery/FormMain.Designer.cs | 1 + Confectionery/FormMain.cs | 3 ++- Confectionery/FormPastry.Designer.cs | 1 + Confectionery/FormShop.cs | 7 ------- Confectionery/FormViewPastry.Designer.cs | 1 + 8 files changed, 16 insertions(+), 11 deletions(-) diff --git a/ConfectionaryBusinessLogic/ShopLogic.cs b/ConfectionaryBusinessLogic/ShopLogic.cs index 4c06d09..36d618a 100644 --- a/ConfectionaryBusinessLogic/ShopLogic.cs +++ b/ConfectionaryBusinessLogic/ShopLogic.cs @@ -68,7 +68,6 @@ namespace ConfectioneryBusinessLogic throw new ArgumentNullException("Нет названия магазина", nameof(model.Name)); } - model.Pastries = new(); if (_shopStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); @@ -137,7 +136,7 @@ namespace ConfectioneryBusinessLogic if (element.Pastries.TryGetValue(pastry.Id, out var pair)) { - pair.Item2 += count; + element.Pastries[pastry.Id] = (pastry, count + pair.Item2); _logger.LogInformation( "AddPastryInShop. Has been added {count} {pastry} in {ShopName}", count, pastry.PastryName, element.Name); @@ -149,6 +148,14 @@ namespace ConfectioneryBusinessLogic "AddPastryInShop. Has been added {count} new Pastry {pastry} in {ShopName}", count, pastry.PastryName, element.Name); } + _shopStorage.Update(new() + { + Id = element.Id, + Address = element.Address, + Name = element.Name, + DateOpening = element.DateOpening, + Pastries = element.Pastries + }); return true; } } diff --git a/ConfectionaryListImplement/Shop.cs b/ConfectionaryListImplement/Shop.cs index fcce77b..93e754c 100644 --- a/ConfectionaryListImplement/Shop.cs +++ b/ConfectionaryListImplement/Shop.cs @@ -33,7 +33,7 @@ namespace ConfectioneryListImplement Name = model.Name, Address = model.Address, DateOpening = model.DateOpening, - Pastries = model.Pastries + Pastries = new() }; } public void Update(ShopBindingModel? model) diff --git a/Confectionery/FormComponents.Designer.cs b/Confectionery/FormComponents.Designer.cs index dd31844..967f22a 100644 --- a/Confectionery/FormComponents.Designer.cs +++ b/Confectionery/FormComponents.Designer.cs @@ -104,6 +104,7 @@ this.Controls.Add(this.dataGridView); this.Name = "FormComponents"; this.Text = "Редактирование компонентов"; + this.Load += new System.EventHandler(this.FormComponents_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); this.ResumeLayout(false); diff --git a/Confectionery/FormMain.Designer.cs b/Confectionery/FormMain.Designer.cs index d43b97b..0710300 100644 --- a/Confectionery/FormMain.Designer.cs +++ b/Confectionery/FormMain.Designer.cs @@ -179,6 +179,7 @@ this.MainMenuStrip = this.menuStrip1; this.Name = "FormMain"; this.Text = "Кондитерская"; + this.Load += new System.EventHandler(this.FormMain_Load); this.menuStrip1.ResumeLayout(false); this.menuStrip1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); diff --git a/Confectionery/FormMain.cs b/Confectionery/FormMain.cs index 92e771a..3c6c955 100644 --- a/Confectionery/FormMain.cs +++ b/Confectionery/FormMain.cs @@ -29,7 +29,8 @@ namespace ConfectioneryView if (list != null) { dataGridView.DataSource = list; - dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["Id"].HeaderText = " "; + dataGridView.Columns["PastryId"].Visible = false; dataGridView.Columns["PastryName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } diff --git a/Confectionery/FormPastry.Designer.cs b/Confectionery/FormPastry.Designer.cs index ba07aee..1c6ecaf 100644 --- a/Confectionery/FormPastry.Designer.cs +++ b/Confectionery/FormPastry.Designer.cs @@ -207,6 +207,7 @@ this.Controls.Add(this.label1); this.Name = "FormPastry"; this.Text = "Кондитерское изделие"; + this.Load += new System.EventHandler(this.FormPastry_Load); this.groupBox1.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); this.ResumeLayout(false); diff --git a/Confectionery/FormShop.cs b/Confectionery/FormShop.cs index 4700625..c755270 100644 --- a/Confectionery/FormShop.cs +++ b/Confectionery/FormShop.cs @@ -70,13 +70,6 @@ namespace ConfectioneryView comboBoxShop.Text = model.Name; textBoxAddress.Text = model.Address; textBoxDateOpening.Text = Convert.ToString(model.DateOpening); - - textBoxAddress.Text = vmodel.Address; - textBoxDateOpening.Text = Convert.ToString(vmodel.DateOpening); - } - textBoxAddress.Text = vmodel.Address; - textBoxDateOpening.Text = Convert.ToString(vmodel.DateOpening); - } dataGridView.Rows.Clear(); foreach (var el in model.Pastries.Values) { diff --git a/Confectionery/FormViewPastry.Designer.cs b/Confectionery/FormViewPastry.Designer.cs index 3417617..67d3480 100644 --- a/Confectionery/FormViewPastry.Designer.cs +++ b/Confectionery/FormViewPastry.Designer.cs @@ -104,6 +104,7 @@ this.Controls.Add(this.dataGridView); this.Name = "FormViewPastry"; this.Text = "Редактирование изделий"; + this.Load += new System.EventHandler(this.FormViewPastry_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); this.ResumeLayout(false); From 6bcbf619c0ac7ec82efa6faab0751519b51209c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Thu, 9 Feb 2023 16:13:08 +0400 Subject: [PATCH 15/29] fix --- Confectionery/FormComponents.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Confectionery/FormComponents.cs b/Confectionery/FormComponents.cs index 3368e14..2f20d12 100644 --- a/Confectionery/FormComponents.cs +++ b/Confectionery/FormComponents.cs @@ -30,11 +30,11 @@ namespace ConfectioneryView dataGridView.Columns["ComponentName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } - _logger.LogInformation("Загрузка изделий"); + _logger.LogInformation("Загрузка компонентов"); } catch (Exception ex) { - _logger.LogError(ex, "Ошибка загрузки изделий"); + _logger.LogError(ex, "Ошибка загрузки компонентов"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } @@ -75,7 +75,7 @@ namespace ConfectioneryView { int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); - _logger.LogInformation("Удаление изделия"); + _logger.LogInformation("Удаление компонента"); try { if (!_logic.Delete(new ComponentBindingModel @@ -89,7 +89,7 @@ namespace ConfectioneryView } catch (Exception ex) { - _logger.LogError(ex, "Ошибка удаления изделия"); + _logger.LogError(ex, "Ошибка удаления компонента"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } From 1d77f16d44178a98b9eec78396a21188312f88bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Wed, 15 Feb 2023 05:01:38 +0400 Subject: [PATCH 16/29] =?UTF-8?q?=D0=A1=D0=BE=D1=85=D1=80=D0=B0=D0=BD?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BC=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DataFileSingleton.cs | 4 + ConfectionaryFileImplement/Shop.cs | 103 +++++++++++++++++ ConfectionaryFileImplement/ShopStorage.cs | 107 ++++++++++++++++++ Confectionery/Program.cs | 4 +- 4 files changed, 216 insertions(+), 2 deletions(-) create mode 100644 ConfectionaryFileImplement/Shop.cs create mode 100644 ConfectionaryFileImplement/ShopStorage.cs diff --git a/ConfectionaryFileImplement/DataFileSingleton.cs b/ConfectionaryFileImplement/DataFileSingleton.cs index ff056a2..ac19587 100644 --- a/ConfectionaryFileImplement/DataFileSingleton.cs +++ b/ConfectionaryFileImplement/DataFileSingleton.cs @@ -9,9 +9,11 @@ namespace ConfectioneryFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string PastryFileName = "Pastry.xml"; + private readonly string ShopFileName = "Shop.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Pastries { get; private set; } + public List Shops { get; private set; } public static DataFileSingleton GetInstance() { @@ -24,12 +26,14 @@ namespace ConfectioneryFileImplement public void SaveComponents() => SaveData(Components, ComponentFileName, "Components", x => x.GetXElement); public void SavePastries() => SaveData(Pastries, PastryFileName, "Pastries", x => x.GetXElement); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x => x.GetXElement); + public void SaveShops() => SaveData(Shops, ShopFileName, "Shops", x => x.GetXElement); private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", x => Component.Create(x)!)!; Pastries = LoadData(PastryFileName, "Pastry", x => Pastry.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + Shops = LoadData(ShopFileName, "Shop", x => Shop.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) { diff --git a/ConfectionaryFileImplement/Shop.cs b/ConfectionaryFileImplement/Shop.cs new file mode 100644 index 0000000..39f14be --- /dev/null +++ b/ConfectionaryFileImplement/Shop.cs @@ -0,0 +1,103 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels; +using ConfectioneryDataModels.Models; +using System.Xml.Linq; + +namespace ConfectioneryFileImplement +{ + public class Shop : IShopModel + { + public string Name { get; private set; } = string.Empty; + + public string Address { get; private set; } = string.Empty; + + public DateTime DateOpening { get; private set; } + + public Dictionary CountPastries { get; private set; } = new(); + + private Dictionary? _cachedPastries = null; + public Dictionary Pastries + { + get + { + if (_cachedPastries == null) + { + var source = DataFileSingleton.GetInstance(); + _cachedPastries = CountPastries + .ToDictionary(x => x.Key, x => (source.Pastries + .FirstOrDefault(y => y.Id == x.Key)! as IPastryModel, x.Value)); + } + return _cachedPastries; + } + } + + public int Id { get; private set; } + + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + Name = model.Name, + Address = model.Address, + DateOpening = model.DateOpening, + CountPastries = new() + }; + } + public static Shop? Create(XElement element) + { + if (element == null) + { + return null; + } + return new() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + Name = element.Element("Name")!.Value, + Address = element.Element("Address")!.Value, + DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value), + CountPastries = element.Element("CountPastries")!.Elements("CountPastry") + .ToDictionary( + x => Convert.ToInt32(x.Element("Key")?.Value), + x => Convert.ToInt32(x.Element("Value")?.Value) + ) + }; + } + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + Name = model.Name; + Address = model.Address; + DateOpening = model.DateOpening; + CountPastries = model.Pastries.ToDictionary(x => x.Key, x => x.Value.Item2); + _cachedPastries = null; + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Address = Address, + Pastries = Pastries, + DateOpening = DateOpening, + }; + public XElement GetXElement => new("Order", + new XAttribute("Id", Id), + new XElement("Name", Name), + new XElement("Address", Address), + new XElement("DateOpening", DateOpening), + new XElement("CountPastries", CountPastries + .Select(x => new XElement("CountPastry", + new XElement("Key", x.Key), + new XElement("Value", x.Value)) + )) + ); + } +} diff --git a/ConfectionaryFileImplement/ShopStorage.cs b/ConfectionaryFileImplement/ShopStorage.cs new file mode 100644 index 0000000..6ae843a --- /dev/null +++ b/ConfectionaryFileImplement/ShopStorage.cs @@ -0,0 +1,107 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContract; +using ConfectioneryContracts.ViewModels; + +namespace ConfectioneryFileImplement +{ + public class ShopStorage : IShopStorage + { + private readonly DataFileSingleton _source; + public ShopStorage() + { + _source = DataFileSingleton.GetInstance(); + } + + public ShopViewModel? Delete(ShopBindingModel model) + { + for (int i = 0; i < _source.Shops.Count; ++i) + { + if (_source.Shops[i].Id == model.Id) + { + var element = _source.Shops[i]; + _source.Shops.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + foreach (var shop in _source.Shops) + { + if ((!string.IsNullOrEmpty(model.Name) && + shop.Name == model.Name) || + (model.Id.HasValue && shop.Id == model.Id)) + { + return shop.GetViewModel; + } + } + return null; + } + + public List GetFilteredList(ShopSearchModel model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.Name)) + { + return result; + } + foreach (var shop in _source.Shops) + { + if (shop.Name.Contains(model.Name ?? string.Empty)) + { + result.Add(shop.GetViewModel); + } + } + return result; + } + + public List GetFullList() + { + var result = new List(); + foreach (var shop in _source.Shops) + { + result.Add(shop.GetViewModel); + } + return result; + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + model.Id = 1; + foreach (var shop in _source.Shops) + { + if (model.Id <= shop.Id) + { + model.Id = shop.Id + 1; + } + } + var newShop = Shop.Create(model); + if (newShop == null) + { + return null; + } + _source.Shops.Add(newShop); + return newShop.GetViewModel; + } + + public ShopViewModel? Update(ShopBindingModel model) + { + foreach (var shop in _source.Shops) + { + if (shop.Id == model.Id) + { + shop.Update(model); + return shop.GetViewModel; + } + } + return null; + } + } +} diff --git a/Confectionery/Program.cs b/Confectionery/Program.cs index f9437a4..6f0ea6d 100644 --- a/Confectionery/Program.cs +++ b/Confectionery/Program.cs @@ -1,8 +1,8 @@ -using ConfectioneryListImplement.Implements; +using ConfectioneryFileImplement.Implements; using ConfectioneryBusinessLogic.BusinessLogics; using ConfectioneryContracts.BusinessLogicsContracts; using ConfectioneryContracts.StoragesContract; -using ConfectioneryListImplement; +using ConfectioneryFileImplement; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; From 771985a191c706a80352cefd7f1fe30562c16c4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Wed, 15 Feb 2023 05:47:23 +0400 Subject: [PATCH 17/29] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D0=BF=D0=BE=D0=BB=D0=B5=20=D0=B2=20=D1=81?= =?UTF-8?q?=D1=83=D1=89=D0=BD=D0=BE=D1=81=D1=82=D1=8C=20"=D0=9C=D0=B0?= =?UTF-8?q?=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryBusinessLogic/ShopLogic.cs | 6 ++++++ ConfectionaryFileImplement/Pastry.cs | 1 - ConfectionaryFileImplement/Shop.cs | 17 ++++++++++++----- ConfectionaryListImplement/Shop.cs | 5 +++++ .../BindingModels/ShopBindingModel.cs | 2 ++ .../ViewModels/ShopViewModel.cs | 4 ++++ ConfectioneryDataModels/IShopModel.cs | 1 + 7 files changed, 30 insertions(+), 6 deletions(-) diff --git a/ConfectionaryBusinessLogic/ShopLogic.cs b/ConfectionaryBusinessLogic/ShopLogic.cs index 36d618a..d127dfa 100644 --- a/ConfectionaryBusinessLogic/ShopLogic.cs +++ b/ConfectionaryBusinessLogic/ShopLogic.cs @@ -133,6 +133,12 @@ namespace ConfectioneryBusinessLogic } _logger.LogInformation("AddPastryInShop find. Id:{Id}", element.Id); + if (element.MaxCountPastries < 0) + { + throw new ArgumentException( + "Максимальное количество изделий в магазине не должно быть отрицательным", + nameof(element.MaxCountPastries)); + } if (element.Pastries.TryGetValue(pastry.Id, out var pair)) { diff --git a/ConfectionaryFileImplement/Pastry.cs b/ConfectionaryFileImplement/Pastry.cs index f32067c..6138c0c 100644 --- a/ConfectionaryFileImplement/Pastry.cs +++ b/ConfectionaryFileImplement/Pastry.cs @@ -1,7 +1,6 @@ using ConfectioneryContracts.BindingModels; using ConfectioneryContracts.ViewModels; using ConfectioneryDataModels.Models; -using ConfectioneryDataModels.Models; using System.Xml.Linq; namespace ConfectioneryFileImplement.Models diff --git a/ConfectionaryFileImplement/Shop.cs b/ConfectionaryFileImplement/Shop.cs index 39f14be..fadd63f 100644 --- a/ConfectionaryFileImplement/Shop.cs +++ b/ConfectionaryFileImplement/Shop.cs @@ -12,6 +12,8 @@ namespace ConfectioneryFileImplement public string Address { get; private set; } = string.Empty; + public int MaxCountPastries { get; private set; } + public DateTime DateOpening { get; private set; } public Dictionary CountPastries { get; private set; } = new(); @@ -46,6 +48,7 @@ namespace ConfectioneryFileImplement Name = model.Name, Address = model.Address, DateOpening = model.DateOpening, + MaxCountPastries = model.MaxCountPastries, CountPastries = new() }; } @@ -57,11 +60,12 @@ namespace ConfectioneryFileImplement } return new() { - Id = Convert.ToInt32(element.Attribute("Id")!.Value), - Name = element.Element("Name")!.Value, - Address = element.Element("Address")!.Value, - DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value), - CountPastries = element.Element("CountPastries")!.Elements("CountPastry") + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + Name = element.Element("Name")!.Value, + Address = element.Element("Address")!.Value, + DateOpening = Convert.ToDateTime(element.Element("DateOpening")!.Value), + MaxCountPastries = Convert.ToInt32(element.Element("MaxCountPastries")!.Value), + CountPastries = element.Element("CountPastries")!.Elements("CountPastry") .ToDictionary( x => Convert.ToInt32(x.Element("Key")?.Value), x => Convert.ToInt32(x.Element("Value")?.Value) @@ -77,6 +81,7 @@ namespace ConfectioneryFileImplement Name = model.Name; Address = model.Address; DateOpening = model.DateOpening; + MaxCountPastries = model.MaxCountPastries; CountPastries = model.Pastries.ToDictionary(x => x.Key, x => x.Value.Item2); _cachedPastries = null; } @@ -87,12 +92,14 @@ namespace ConfectioneryFileImplement Address = Address, Pastries = Pastries, DateOpening = DateOpening, + MaxCountPastries = MaxCountPastries, }; public XElement GetXElement => new("Order", new XAttribute("Id", Id), new XElement("Name", Name), new XElement("Address", Address), new XElement("DateOpening", DateOpening), + new XElement("MaxCountPastries", MaxCountPastries), new XElement("CountPastries", CountPastries .Select(x => new XElement("CountPastry", new XElement("Key", x.Key), diff --git a/ConfectionaryListImplement/Shop.cs b/ConfectionaryListImplement/Shop.cs index 93e754c..42b553d 100644 --- a/ConfectionaryListImplement/Shop.cs +++ b/ConfectionaryListImplement/Shop.cs @@ -11,6 +11,8 @@ namespace ConfectioneryListImplement public string Address { get; private set; } = string.Empty; + public int MaxCountPastries { get; private set; } + public DateTime DateOpening { get; private set; } public Dictionary Pastries @@ -33,6 +35,7 @@ namespace ConfectioneryListImplement Name = model.Name, Address = model.Address, DateOpening = model.DateOpening, + MaxCountPastries = model.MaxCountPastries, Pastries = new() }; } @@ -45,6 +48,7 @@ namespace ConfectioneryListImplement Name = model.Name; Address = model.Address; DateOpening = model.DateOpening; + MaxCountPastries = model.MaxCountPastries; Pastries = model.Pastries; } public ShopViewModel GetViewModel => new() @@ -54,6 +58,7 @@ namespace ConfectioneryListImplement Address = Address, Pastries = Pastries, DateOpening = DateOpening, + MaxCountPastries = MaxCountPastries, }; } } diff --git a/ConfectioneryContracts/BindingModels/ShopBindingModel.cs b/ConfectioneryContracts/BindingModels/ShopBindingModel.cs index 38d8750..f627224 100644 --- a/ConfectioneryContracts/BindingModels/ShopBindingModel.cs +++ b/ConfectioneryContracts/BindingModels/ShopBindingModel.cs @@ -9,6 +9,8 @@ namespace ConfectioneryContracts.BindingModels public string Address { get; set; } = string.Empty; + public int MaxCountPastries { get; set; } + public DateTime DateOpening { get; set; } = DateTime.Now; public Dictionary Pastries diff --git a/ConfectioneryContracts/ViewModels/ShopViewModel.cs b/ConfectioneryContracts/ViewModels/ShopViewModel.cs index a87ca8d..f7fce9b 100644 --- a/ConfectioneryContracts/ViewModels/ShopViewModel.cs +++ b/ConfectioneryContracts/ViewModels/ShopViewModel.cs @@ -12,6 +12,9 @@ namespace ConfectioneryContracts.ViewModels [DisplayName("Адрес магазина")] public string Address { get; set; } = string.Empty; + [DisplayName("Максимальное количество изделий в магазине")] + public int MaxCountPastries { get; set; } + [DisplayName("Время открытия")] public DateTime DateOpening { get; set; } = DateTime.Now; @@ -22,5 +25,6 @@ namespace ConfectioneryContracts.ViewModels } = new(); public int Id { get; set; } + } } diff --git a/ConfectioneryDataModels/IShopModel.cs b/ConfectioneryDataModels/IShopModel.cs index c0883e9..31936ea 100644 --- a/ConfectioneryDataModels/IShopModel.cs +++ b/ConfectioneryDataModels/IShopModel.cs @@ -6,6 +6,7 @@ namespace ConfectioneryDataModels { string Name { get; } string Address { get; } + int MaxCountPastries { get; } DateTime DateOpening { get; } Dictionary Pastries { get; } } From 4368d4d6ccec34caff488d082a7201848c470f17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Wed, 15 Feb 2023 06:34:44 +0400 Subject: [PATCH 18/29] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=B0=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA?= =?UTF-8?q?=D0=B0=20=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89=D0=B0?= =?UTF-8?q?=20=D0=BC=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryFileImplement/ShopStorage.cs | 99 ++++++++++--------- ConfectionaryListImplement/ShopStorage.cs | 11 +++ .../StoragesContract/IShopStorage.cs | 4 + 3 files changed, 67 insertions(+), 47 deletions(-) diff --git a/ConfectionaryFileImplement/ShopStorage.cs b/ConfectionaryFileImplement/ShopStorage.cs index 6ae843a..3e9d4da 100644 --- a/ConfectionaryFileImplement/ShopStorage.cs +++ b/ConfectionaryFileImplement/ShopStorage.cs @@ -2,6 +2,7 @@ using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; namespace ConfectioneryFileImplement { @@ -15,93 +16,97 @@ namespace ConfectioneryFileImplement public ShopViewModel? Delete(ShopBindingModel model) { - for (int i = 0; i < _source.Shops.Count; ++i) + var element = _source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (element != null) { - if (_source.Shops[i].Id == model.Id) - { - var element = _source.Shops[i]; - _source.Shops.RemoveAt(i); - return element.GetViewModel; - } + _source.Shops.Remove(element); + _source.SaveShops(); + return element.GetViewModel; } return null; } public ShopViewModel? GetElement(ShopSearchModel model) { - if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + if (!model.Id.HasValue) { return null; } - foreach (var shop in _source.Shops) - { - if ((!string.IsNullOrEmpty(model.Name) && - shop.Name == model.Name) || - (model.Id.HasValue && shop.Id == model.Id)) - { - return shop.GetViewModel; - } - } - return null; + return _source.Shops.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; } public List GetFilteredList(ShopSearchModel model) { - var result = new List(); if (string.IsNullOrEmpty(model.Name)) { - return result; + return new(); } - foreach (var shop in _source.Shops) - { - if (shop.Name.Contains(model.Name ?? string.Empty)) - { - result.Add(shop.GetViewModel); - } - } - return result; + return _source.Shops + .Select(x => x.GetViewModel) + .Where(x => x.Name.Contains(model.Name ?? string.Empty)) + .ToList(); } public List GetFullList() { - var result = new List(); - foreach (var shop in _source.Shops) - { - result.Add(shop.GetViewModel); - } - return result; + return _source.Shops + .Select(shop => shop.GetViewModel) + .ToList(); } public ShopViewModel? Insert(ShopBindingModel model) { - model.Id = 1; - foreach (var shop in _source.Shops) - { - if (model.Id <= shop.Id) - { - model.Id = shop.Id + 1; - } - } + model.Id = _source.Shops.Count > 0 ? _source.Shops.Max(x => x.Id) + 1 : 1; var newShop = Shop.Create(model); if (newShop == null) { return null; } _source.Shops.Add(newShop); + _source.SaveShops(); return newShop.GetViewModel; } public ShopViewModel? Update(ShopBindingModel model) { - foreach (var shop in _source.Shops) + var shop = _source.Shops.FirstOrDefault(x => x.Id == model.Id); + if (shop == null) { - if (shop.Id == model.Id) + return null; + } + shop.Update(model); + _source.SaveShops(); + return shop.GetViewModel; + } + + public bool HasNeedPastries(IPastryModel pastry, int needCount) + { + var resultCount = _source.Shops + .Select(shop => shop.Pastries + .FirstOrDefault(x => x.Key == pastry.Id).Value.Item2) + .Sum(); + return resultCount >= needCount; + } + + public bool SellPastries(IPastryModel pastry, int needCount) + { + if (!HasNeedPastries(pastry, needCount)) + { + return false; + } + foreach (var pair in _source.Shops.Where(shop => shop.Pastries.ContainsKey(pastry.Id))) + { + var tuple = pair.Pastries[pastry.Id]; + var diff = Math.Min(tuple.Item2, needCount); + pair.Pastries[pastry.Id] = (tuple.Item1, tuple.Item2 - diff); + needCount -= diff; + if (needCount <= 0) { - shop.Update(model); - return shop.GetViewModel; + return true; } } - return null; + + return true; } } } diff --git a/ConfectionaryListImplement/ShopStorage.cs b/ConfectionaryListImplement/ShopStorage.cs index 893b6ad..c9ff5c3 100644 --- a/ConfectionaryListImplement/ShopStorage.cs +++ b/ConfectionaryListImplement/ShopStorage.cs @@ -2,6 +2,7 @@ using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; namespace ConfectioneryListImplement { @@ -72,6 +73,11 @@ namespace ConfectioneryListImplement return result; } + public bool HasNeedPastries(IPastryModel pastry, int needCount) + { + throw new NotImplementedException(); + } + public ShopViewModel? Insert(ShopBindingModel model) { model.Id = 1; @@ -91,6 +97,11 @@ namespace ConfectioneryListImplement return newShop.GetViewModel; } + public bool SellPastries(IPastryModel pastry, int needCount) + { + throw new NotImplementedException(); + } + public ShopViewModel? Update(ShopBindingModel model) { foreach (var shop in _source.Shops) diff --git a/ConfectioneryContracts/StoragesContract/IShopStorage.cs b/ConfectioneryContracts/StoragesContract/IShopStorage.cs index 4276f2d..1f53aa5 100644 --- a/ConfectioneryContracts/StoragesContract/IShopStorage.cs +++ b/ConfectioneryContracts/StoragesContract/IShopStorage.cs @@ -1,6 +1,7 @@ using ConfectioneryContracts.BindingModels; using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; namespace ConfectioneryContracts.StoragesContract { @@ -12,5 +13,8 @@ namespace ConfectioneryContracts.StoragesContract ShopViewModel? Insert(ShopBindingModel model); ShopViewModel? Update(ShopBindingModel model); ShopViewModel? Delete(ShopBindingModel model); + + bool HasNeedPastries(IPastryModel pastry, int needCount); + public bool SellPastries(IPastryModel pastry, int needCount); } } From d6bf33d45b2c54b4c1e8c9c48e6b7768f6b9c688 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sat, 18 Feb 2023 16:14:11 +0400 Subject: [PATCH 19/29] fix --- ConfectionaryFileImplement/OrderStorage.cs | 8 ++++---- ConfectionaryFileImplement/Pastry.cs | 1 - 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/ConfectionaryFileImplement/OrderStorage.cs b/ConfectionaryFileImplement/OrderStorage.cs index aae2076..c2dc788 100644 --- a/ConfectionaryFileImplement/OrderStorage.cs +++ b/ConfectionaryFileImplement/OrderStorage.cs @@ -63,14 +63,14 @@ namespace ConfectioneryFileImplement public OrderViewModel? Update(OrderBindingModel model) { - var pastry = _source.Orders.FirstOrDefault(x => x.Id == model.Id); - if (pastry == null) + var order = _source.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) { return null; } - pastry.Update(model); + order.Update(model); _source.SaveOrders(); - return pastry.GetViewModel; + return order.GetViewModel; } } } diff --git a/ConfectionaryFileImplement/Pastry.cs b/ConfectionaryFileImplement/Pastry.cs index f32067c..6138c0c 100644 --- a/ConfectionaryFileImplement/Pastry.cs +++ b/ConfectionaryFileImplement/Pastry.cs @@ -1,7 +1,6 @@ using ConfectioneryContracts.BindingModels; using ConfectioneryContracts.ViewModels; using ConfectioneryDataModels.Models; -using ConfectioneryDataModels.Models; using System.Xml.Linq; namespace ConfectioneryFileImplement.Models From 23be4237609537d31f2e66502294f7db119829a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sat, 18 Feb 2023 17:15:00 +0400 Subject: [PATCH 20/29] =?UTF-8?q?=D0=92=D0=BD=D0=B5=D0=B4=D1=80=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE=20=D0=BC=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=D0=B0=D0=BB?= =?UTF-8?q?=D1=8C=D0=BD=D0=BE=D0=B5=20=D0=BA=D0=BE=D0=BB=D0=B8=D1=87=D0=B5?= =?UTF-8?q?=D1=81=D1=82=D0=B2=D0=BE=20=D0=B8=D0=B7=D0=B4=D0=B5=D0=BB=D0=B8?= =?UTF-8?q?=D0=B9=20=D0=B2=20=D1=81=D1=83=D1=89=D0=BD=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D1=8C=20=D0=9C=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryBusinessLogic/ShopLogic.cs | 26 ++++++++------ Confectionery/FormShop.Designer.cs | 45 ++++++++++++++++++++----- Confectionery/FormShop.cs | 6 ++-- Confectionery/FormShop.resx | 9 +++++ 4 files changed, 65 insertions(+), 21 deletions(-) diff --git a/ConfectionaryBusinessLogic/ShopLogic.cs b/ConfectionaryBusinessLogic/ShopLogic.cs index d127dfa..3974880 100644 --- a/ConfectionaryBusinessLogic/ShopLogic.cs +++ b/ConfectionaryBusinessLogic/ShopLogic.cs @@ -101,6 +101,13 @@ namespace ConfectioneryBusinessLogic throw new ArgumentNullException("Нет названия магазина", nameof(model.Name)); } + if (model.MaxCountPastries < 0) + { + throw new ArgumentException( + "Максимальное количество изделий в магазине не должно быть отрицательным", + nameof(model.MaxCountPastries)); + } + _logger.LogInformation("Shop. ShopName:{0}.Address:{1}. Id: {2}", model.Name, model.Address, model.Id); var element = _shopStorage.GetElement(new ShopSearchModel @@ -133,27 +140,24 @@ namespace ConfectioneryBusinessLogic } _logger.LogInformation("AddPastryInShop find. Id:{Id}", element.Id); - if (element.MaxCountPastries < 0) + var currentCountPasties = element.Pastries.Select(x => x.Value.Item2).Sum(); + if (currentCountPasties + count > element.MaxCountPastries) { - throw new ArgumentException( - "Максимальное количество изделий в магазине не должно быть отрицательным", - nameof(element.MaxCountPastries)); + _logger.LogWarning("AddPastryInShop. The number of pastry {count} exceeds maximum {max}", + currentCountPasties + count, element.MaxCountPastries); + return false; } - if (element.Pastries.TryGetValue(pastry.Id, out var pair)) { element.Pastries[pastry.Id] = (pastry, count + pair.Item2); - _logger.LogInformation( - "AddPastryInShop. Has been added {count} {pastry} in {ShopName}", - count, pastry.PastryName, element.Name); } else { element.Pastries[pastry.Id] = (pastry, count); - _logger.LogInformation( - "AddPastryInShop. Has been added {count} new Pastry {pastry} in {ShopName}", - count, pastry.PastryName, element.Name); } + _logger.LogInformation( + "AddPastryInShop. Has been added {count} {pastry} in {ShopName}", + count, pastry.PastryName, element.Name); _shopStorage.Update(new() { Id = element.Id, diff --git a/Confectionery/FormShop.Designer.cs b/Confectionery/FormShop.Designer.cs index 694a22f..3a9ec82 100644 --- a/Confectionery/FormShop.Designer.cs +++ b/Confectionery/FormShop.Designer.cs @@ -40,7 +40,10 @@ this.textBoxDateOpening = new System.Windows.Forms.TextBox(); this.buttonCancel = new System.Windows.Forms.Button(); this.buttonSave = new System.Windows.Forms.Button(); + this.label4 = new System.Windows.Forms.Label(); + this.numericUpDownMaxPastry = new System.Windows.Forms.NumericUpDown(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownMaxPastry)).BeginInit(); this.SuspendLayout(); // // label1 @@ -74,7 +77,7 @@ this.dataGridView.Location = new System.Drawing.Point(12, 64); this.dataGridView.Name = "dataGridView"; this.dataGridView.RowTemplate.Height = 25; - this.dataGridView.Size = new System.Drawing.Size(583, 213); + this.dataGridView.Size = new System.Drawing.Size(640, 213); this.dataGridView.TabIndex = 2; // // PastryName @@ -105,7 +108,7 @@ // label3 // this.label3.AutoSize = true; - this.label3.Location = new System.Drawing.Point(386, 9); + this.label3.Location = new System.Drawing.Point(366, 9); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(100, 15); this.label3.TabIndex = 4; @@ -115,20 +118,20 @@ // this.textBoxAddress.Location = new System.Drawing.Point(159, 27); this.textBoxAddress.Name = "textBoxAddress"; - this.textBoxAddress.Size = new System.Drawing.Size(221, 23); + this.textBoxAddress.Size = new System.Drawing.Size(201, 23); this.textBoxAddress.TabIndex = 5; // // textBoxDateOpening // - this.textBoxDateOpening.Location = new System.Drawing.Point(386, 27); + this.textBoxDateOpening.Location = new System.Drawing.Point(366, 27); this.textBoxDateOpening.Name = "textBoxDateOpening"; - this.textBoxDateOpening.Size = new System.Drawing.Size(209, 23); + this.textBoxDateOpening.Size = new System.Drawing.Size(160, 23); this.textBoxDateOpening.TabIndex = 6; // // buttonCancel // this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonCancel.Location = new System.Drawing.Point(492, 283); + this.buttonCancel.Location = new System.Drawing.Point(549, 283); this.buttonCancel.Name = "buttonCancel"; this.buttonCancel.Size = new System.Drawing.Size(103, 23); this.buttonCancel.TabIndex = 7; @@ -139,7 +142,7 @@ // buttonSave // this.buttonSave.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonSave.Location = new System.Drawing.Point(366, 284); + this.buttonSave.Location = new System.Drawing.Point(423, 284); this.buttonSave.Name = "buttonSave"; this.buttonSave.Size = new System.Drawing.Size(120, 22); this.buttonSave.TabIndex = 8; @@ -147,11 +150,34 @@ this.buttonSave.UseVisualStyleBackColor = true; this.buttonSave.Click += new System.EventHandler(this.ButtonSave_Click); // + // label4 + // + this.label4.AutoSize = true; + this.label4.Location = new System.Drawing.Point(530, 9); + this.label4.Name = "label4"; + this.label4.Size = new System.Drawing.Size(118, 15); + this.label4.TabIndex = 9; + this.label4.Text = "Максимум изделий:"; + // + // numericUpDownMaxPastry + // + this.numericUpDownMaxPastry.Location = new System.Drawing.Point(532, 27); + this.numericUpDownMaxPastry.Maximum = new decimal(new int[] { + 10000000, + 0, + 0, + 0}); + this.numericUpDownMaxPastry.Name = "numericUpDownMaxPastry"; + this.numericUpDownMaxPastry.Size = new System.Drawing.Size(120, 23); + this.numericUpDownMaxPastry.TabIndex = 10; + // // FormShop // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(607, 317); + this.ClientSize = new System.Drawing.Size(664, 317); + this.Controls.Add(this.numericUpDownMaxPastry); + this.Controls.Add(this.label4); this.Controls.Add(this.buttonSave); this.Controls.Add(this.buttonCancel); this.Controls.Add(this.textBoxDateOpening); @@ -165,6 +191,7 @@ this.Text = "Просмотр изделий магазина"; this.Load += new System.EventHandler(this.FormShop_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownMaxPastry)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); @@ -184,5 +211,7 @@ private DataGridViewTextBoxColumn PastryName; private DataGridViewTextBoxColumn Price; private DataGridViewTextBoxColumn Count; + private Label label4; + private NumericUpDown numericUpDownMaxPastry; } } \ No newline at end of file diff --git a/Confectionery/FormShop.cs b/Confectionery/FormShop.cs index c755270..fa5c0f5 100644 --- a/Confectionery/FormShop.cs +++ b/Confectionery/FormShop.cs @@ -66,7 +66,8 @@ namespace ConfectioneryView { var model = GetShop(extendDate ? Id : Convert.ToInt32(comboBoxShop.SelectedValue)); if (model != null) - { + { + numericUpDownMaxPastry.Value = model.MaxCountPastries; comboBoxShop.Text = model.Name; textBoxAddress.Text = model.Address; textBoxDateOpening.Text = Convert.ToString(model.DateOpening); @@ -113,7 +114,8 @@ namespace ConfectioneryView { Name = comboBoxShop.Text, Address = textBoxAddress.Text, - DateOpening = dateTime + DateOpening = dateTime, + MaxCountPastries = (int)numericUpDownMaxPastry.Value, }; var vmodel = GetShop(Id); bool operationResult = false; diff --git a/Confectionery/FormShop.resx b/Confectionery/FormShop.resx index c4ce247..5191e62 100644 --- a/Confectionery/FormShop.resx +++ b/Confectionery/FormShop.resx @@ -66,4 +66,13 @@ True + + True + + + True + + + True + \ No newline at end of file From 977d357ea189a8b349198075bdaaa4cf83df542f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sat, 18 Feb 2023 17:19:29 +0400 Subject: [PATCH 21/29] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B0?= =?UTF-8?q?=20=D0=B7=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=BA=D0=B8=20=D0=BC?= =?UTF-8?q?=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD=D0=B0=20=D0=B8=D0=B7=20?= =?UTF-8?q?=D1=84=D0=B0=D0=B9=D0=BB=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryFileImplement/Shop.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ConfectionaryFileImplement/Shop.cs b/ConfectionaryFileImplement/Shop.cs index fadd63f..a873e7d 100644 --- a/ConfectionaryFileImplement/Shop.cs +++ b/ConfectionaryFileImplement/Shop.cs @@ -94,7 +94,7 @@ namespace ConfectioneryFileImplement DateOpening = DateOpening, MaxCountPastries = MaxCountPastries, }; - public XElement GetXElement => new("Order", + public XElement GetXElement => new("Shop", new XAttribute("Id", Id), new XElement("Name", Name), new XElement("Address", Address), From 64b59948b7717914077b22d6dfb1cedfc027e900 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sat, 18 Feb 2023 18:21:15 +0400 Subject: [PATCH 22/29] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D1=8B=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4=D1=8B=20?= =?UTF-8?q?=D0=B2=20=D0=B1=D0=B8=D0=B7=D0=BD=D0=B5=D1=81-=D0=BB=D0=BE?= =?UTF-8?q?=D0=B3=D0=B8=D0=BA=D0=B5=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D0=B8=20=D0=B8=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=BC=D0=B0?= =?UTF-8?q?=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD=D0=BE=D0=B2=20=D0=B8=D0=B7=D0=B4?= =?UTF-8?q?=D0=B5=D0=BB=D0=B8=D1=8F=D0=BC=D0=B8=20=D0=BF=D1=80=D0=B8=20?= =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B5=D0=B2=D0=BE=D0=B4=D0=B5=20=D0=B7=D0=B0?= =?UTF-8?q?=D0=BA=D0=B0=D0=B7=D0=B0=20=D0=B2=20=D1=81=D1=82=D0=B0=D1=82?= =?UTF-8?q?=D1=83=D1=81=20=D0=93=D0=BE=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryBusinessLogic/OrderLogic.cs | 15 +++++++- ConfectionaryBusinessLogic/ShopLogic.cs | 36 +++++++++++++++++++ ConfectionaryFileImplement/Shop.cs | 1 - .../BusinessLogicsContracts/IShopLogic.cs | 2 ++ 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/ConfectionaryBusinessLogic/OrderLogic.cs b/ConfectionaryBusinessLogic/OrderLogic.cs index 1471185..3a57520 100644 --- a/ConfectionaryBusinessLogic/OrderLogic.cs +++ b/ConfectionaryBusinessLogic/OrderLogic.cs @@ -12,10 +12,14 @@ namespace ConfectioneryBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; + private readonly IPastryStorage _pastryStorage; + private readonly IShopLogic _shopLogic; - public OrderLogic(ILogger logger, IOrderStorage orderStorage) + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IPastryStorage pastryStorage, IShopLogic shopLogic) { _logger = logger; + _shopLogic = shopLogic; + _pastryStorage = pastryStorage; _orderStorage = orderStorage; } @@ -96,6 +100,15 @@ namespace ConfectioneryBusinessLogic.BusinessLogics $"Доступный статус: {(OrderStatus)((int)vmodel.Status + 1)}", nameof(vmodel)); } + if (orderStatus == OrderStatus.Готов) + { + var vpastry = _pastryStorage.GetElement(new() { Id = vmodel.PastryId }); + + if (vpastry == null || !_shopLogic.AddPastriesInShops(vpastry, vmodel.Count)) + { + throw new Exception($"Не удалось заполнить магазины изделием '{vpastry?.PastryName ?? string.Empty}' из заказа {vmodel.Id}"); + } + } model.Status = orderStatus; model.DateCreate = vmodel.DateCreate; if (model.DateImplement == null) diff --git a/ConfectionaryBusinessLogic/ShopLogic.cs b/ConfectionaryBusinessLogic/ShopLogic.cs index 3974880..5956da2 100644 --- a/ConfectionaryBusinessLogic/ShopLogic.cs +++ b/ConfectionaryBusinessLogic/ShopLogic.cs @@ -168,5 +168,41 @@ namespace ConfectioneryBusinessLogic }); return true; } + + + public int GetFreePlacesWithPastriesInShops(int countPastries) + { + // Сумма разностей между максимальный кол-вом изделий и суммой всех изделий в магазине + return _shopStorage.GetFullList() + .Select(x => x.MaxCountPastries - x.Pastries + .Select(p => p.Value.Item2).Sum()) + .Sum() - countPastries; + } + + public bool AddPastriesInShops(IPastryModel pastry, int count) + { + var freePlaces = GetFreePlacesWithPastriesInShops(count); + if (freePlaces < 0) + { + _logger.LogInformation("AddPastriesInShops. Не удалось добавить изделия в магазины, поскольку они переполнены." + + $"Освободите магазины на {-freePlaces} изделий"); + return false; + } + foreach (var shop in _shopStorage.GetFullList()) + { + var cnt = Math.Min(count, shop.MaxCountPastries - shop.Pastries.Select(x => x.Value.Item2).Sum()); + if (!AddPastry(new() { Id = shop.Id }, pastry, cnt)) + { + _logger.LogWarning("При добавления изделий во все магазины произошла ошибка"); + return false; + } + count -= cnt; + if (count == 0) + { + return true; + } + } + return true; + } } } diff --git a/ConfectionaryFileImplement/Shop.cs b/ConfectionaryFileImplement/Shop.cs index a873e7d..928faf6 100644 --- a/ConfectionaryFileImplement/Shop.cs +++ b/ConfectionaryFileImplement/Shop.cs @@ -81,7 +81,6 @@ namespace ConfectioneryFileImplement Name = model.Name; Address = model.Address; DateOpening = model.DateOpening; - MaxCountPastries = model.MaxCountPastries; CountPastries = model.Pastries.ToDictionary(x => x.Key, x => x.Value.Item2); _cachedPastries = null; } diff --git a/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs b/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs index 3ba0749..cd2e634 100644 --- a/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs @@ -13,5 +13,7 @@ namespace ConfectioneryContracts.BusinessLogicsContracts bool Update(ShopBindingModel model); bool Delete(ShopBindingModel model); bool AddPastry(ShopSearchModel model, IPastryModel pastry, int count); + int GetFreePlacesWithPastriesInShops(int countPastries); + bool AddPastriesInShops(IPastryModel pastry, int count); } } From 39bf5685390412c1c2cb42cfae2772c8f63015d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sat, 18 Feb 2023 19:15:28 +0400 Subject: [PATCH 23/29] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=BE=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B8=D0=B7=D0=B4=D0=B5=D0=BB=D0=B8?= =?UTF-8?q?=D0=B9=20=D0=B2=20=D0=BC=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD?= =?UTF-8?q?=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryBusinessLogic/ShopLogic.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/ConfectionaryBusinessLogic/ShopLogic.cs b/ConfectionaryBusinessLogic/ShopLogic.cs index 5956da2..bf255f6 100644 --- a/ConfectionaryBusinessLogic/ShopLogic.cs +++ b/ConfectionaryBusinessLogic/ShopLogic.cs @@ -181,16 +181,25 @@ namespace ConfectioneryBusinessLogic public bool AddPastriesInShops(IPastryModel pastry, int count) { + if (count <= 0) + { + _logger.LogWarning("AddPastriesInShops. Количество добавляемых изделий должно быть больше 0. Количество - {count}", count); + return false; + } var freePlaces = GetFreePlacesWithPastriesInShops(count); if (freePlaces < 0) { _logger.LogInformation("AddPastriesInShops. Не удалось добавить изделия в магазины, поскольку они переполнены." + - $"Освободите магазины на {-freePlaces} изделий"); + "Освободите магазины на {places} изделий", -freePlaces); return false; } foreach (var shop in _shopStorage.GetFullList()) { var cnt = Math.Min(count, shop.MaxCountPastries - shop.Pastries.Select(x => x.Value.Item2).Sum()); + if (cnt <= 0) + { + continue; + } if (!AddPastry(new() { Id = shop.Id }, pastry, cnt)) { _logger.LogWarning("При добавления изделий во все магазины произошла ошибка"); From b2a18b6a98a64c6cbcc821ed7ba0dab56cefc969 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sat, 18 Feb 2023 20:10:51 +0400 Subject: [PATCH 24/29] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD?= =?UTF-8?q?=D0=BE=D1=81=D1=8C=20=D0=BF=D1=80=D0=BE=D0=B4=D0=B0=D0=B2=D0=B0?= =?UTF-8?q?=D1=82=D1=8C=20=D0=B8=D0=B7=D0=B4=D0=B5=D0=BB=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryBusinessLogic/ShopLogic.cs | 9 ++ ConfectionaryFileImplement/ShopStorage.cs | 8 +- Confectionery/FormMain.Designer.cs | 20 ++- Confectionery/FormMain.cs | 9 ++ Confectionery/FormSellPastry.Designer.cs | 133 ++++++++++++++++++ Confectionery/FormSellPastry.cs | 67 +++++++++ Confectionery/FormSellPastry.resx | 60 ++++++++ Confectionery/Program.cs | 1 + .../BusinessLogicsContracts/IShopLogic.cs | 2 + 9 files changed, 303 insertions(+), 6 deletions(-) create mode 100644 Confectionery/FormSellPastry.Designer.cs create mode 100644 Confectionery/FormSellPastry.cs create mode 100644 Confectionery/FormSellPastry.resx diff --git a/ConfectionaryBusinessLogic/ShopLogic.cs b/ConfectionaryBusinessLogic/ShopLogic.cs index bf255f6..fe157d8 100644 --- a/ConfectionaryBusinessLogic/ShopLogic.cs +++ b/ConfectionaryBusinessLogic/ShopLogic.cs @@ -213,5 +213,14 @@ namespace ConfectioneryBusinessLogic } return true; } + + public bool HasNeedPastries(IPastryModel pastry, int needCount) + { + return _shopStorage.HasNeedPastries(pastry, needCount); + } + public bool SellPastries(IPastryModel pastry, int needCount) + { + return _shopStorage.SellPastries(pastry, needCount); + } } } diff --git a/ConfectionaryFileImplement/ShopStorage.cs b/ConfectionaryFileImplement/ShopStorage.cs index 3e9d4da..e155218 100644 --- a/ConfectionaryFileImplement/ShopStorage.cs +++ b/ConfectionaryFileImplement/ShopStorage.cs @@ -2,6 +2,7 @@ using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels; using ConfectioneryDataModels.Models; namespace ConfectioneryFileImplement @@ -94,11 +95,12 @@ namespace ConfectioneryFileImplement { return false; } - foreach (var pair in _source.Shops.Where(shop => shop.Pastries.ContainsKey(pastry.Id))) + foreach (var shop in _source.Shops.Where(shop => shop.Pastries.ContainsKey(pastry.Id))) { - var tuple = pair.Pastries[pastry.Id]; + var tuple = shop.Pastries[pastry.Id]; var diff = Math.Min(tuple.Item2, needCount); - pair.Pastries[pastry.Id] = (tuple.Item1, tuple.Item2 - diff); + shop.Pastries[pastry.Id] = (tuple.Item1, tuple.Item2 - diff); + needCount -= diff; if (needCount <= 0) { diff --git a/Confectionery/FormMain.Designer.cs b/Confectionery/FormMain.Designer.cs index 0710300..35f58fe 100644 --- a/Confectionery/FormMain.Designer.cs +++ b/Confectionery/FormMain.Designer.cs @@ -40,6 +40,7 @@ this.button3 = new System.Windows.Forms.Button(); this.button4 = new System.Windows.Forms.Button(); this.buttonAddPastryInShop = new System.Windows.Forms.Button(); + this.buttonSellPastry = new System.Windows.Forms.Button(); this.menuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.SuspendLayout(); @@ -94,7 +95,7 @@ this.dataGridView.Location = new System.Drawing.Point(12, 27); this.dataGridView.Name = "dataGridView"; this.dataGridView.RowTemplate.Height = 25; - this.dataGridView.Size = new System.Drawing.Size(606, 341); + this.dataGridView.Size = new System.Drawing.Size(606, 399); this.dataGridView.TabIndex = 1; // // buttonCreateOrder @@ -155,7 +156,7 @@ // buttonAddPastryInShop // this.buttonAddPastryInShop.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonAddPastryInShop.Location = new System.Drawing.Point(624, 326); + this.buttonAddPastryInShop.Location = new System.Drawing.Point(624, 384); this.buttonAddPastryInShop.Name = "buttonAddPastryInShop"; this.buttonAddPastryInShop.Size = new System.Drawing.Size(147, 31); this.buttonAddPastryInShop.TabIndex = 7; @@ -163,11 +164,23 @@ this.buttonAddPastryInShop.UseVisualStyleBackColor = true; this.buttonAddPastryInShop.Click += new System.EventHandler(this.ButtonAddPastryInShop_Click); // + // buttonSellPastry + // + this.buttonSellPastry.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.buttonSellPastry.Location = new System.Drawing.Point(624, 331); + this.buttonSellPastry.Name = "buttonSellPastry"; + this.buttonSellPastry.Size = new System.Drawing.Size(147, 31); + this.buttonSellPastry.TabIndex = 8; + this.buttonSellPastry.Text = "Продать изделие"; + this.buttonSellPastry.UseVisualStyleBackColor = true; + this.buttonSellPastry.Click += new System.EventHandler(this.ButtonSellPastry_Click); + // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(783, 380); + this.ClientSize = new System.Drawing.Size(783, 438); + this.Controls.Add(this.buttonSellPastry); this.Controls.Add(this.buttonAddPastryInShop); this.Controls.Add(this.button4); this.Controls.Add(this.button3); @@ -202,5 +215,6 @@ private ToolStripMenuItem componentToolStripMenuItem; private ToolStripMenuItem ShopsToolStripMenuItem; private Button buttonAddPastryInShop; + private Button buttonSellPastry; } } \ No newline at end of file diff --git a/Confectionery/FormMain.cs b/Confectionery/FormMain.cs index 3c6c955..6dcaf07 100644 --- a/Confectionery/FormMain.cs +++ b/Confectionery/FormMain.cs @@ -170,5 +170,14 @@ namespace ConfectioneryView form.ShowDialog(); } } + + private void ButtonSellPastry_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormSellPastry)); + if (service is FormSellPastry form) + { + form.ShowDialog(); + } + } } } \ No newline at end of file diff --git a/Confectionery/FormSellPastry.Designer.cs b/Confectionery/FormSellPastry.Designer.cs new file mode 100644 index 0000000..46f5949 --- /dev/null +++ b/Confectionery/FormSellPastry.Designer.cs @@ -0,0 +1,133 @@ +namespace ConfectioneryView +{ + partial class FormSellPastry + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.comboBoxPastry = new System.Windows.Forms.ComboBox(); + this.label1 = new System.Windows.Forms.Label(); + this.label2 = new System.Windows.Forms.Label(); + this.numericUpDownCount = new System.Windows.Forms.NumericUpDown(); + this.buttonSell = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownCount)).BeginInit(); + this.SuspendLayout(); + // + // comboBoxPastry + // + this.comboBoxPastry.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.comboBoxPastry.FormattingEnabled = true; + this.comboBoxPastry.Location = new System.Drawing.Point(141, 12); + this.comboBoxPastry.Name = "comboBoxPastry"; + this.comboBoxPastry.Size = new System.Drawing.Size(121, 23); + this.comboBoxPastry.TabIndex = 0; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(12, 15); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(123, 15); + this.label1.TabIndex = 1; + this.label1.Text = "Изделие на продажу:"; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(60, 46); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(75, 15); + this.label2.TabIndex = 2; + this.label2.Text = "Количество:"; + // + // numericUpDownCount + // + this.numericUpDownCount.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.numericUpDownCount.Location = new System.Drawing.Point(142, 46); + this.numericUpDownCount.Maximum = new decimal(new int[] { + 10000000, + 0, + 0, + 0}); + this.numericUpDownCount.Name = "numericUpDownCount"; + this.numericUpDownCount.RightToLeft = System.Windows.Forms.RightToLeft.No; + this.numericUpDownCount.Size = new System.Drawing.Size(120, 23); + this.numericUpDownCount.TabIndex = 3; + // + // buttonSell + // + this.buttonSell.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonSell.Location = new System.Drawing.Point(12, 90); + this.buttonSell.Name = "buttonSell"; + this.buttonSell.Size = new System.Drawing.Size(123, 23); + this.buttonSell.TabIndex = 4; + this.buttonSell.Text = "Продать"; + this.buttonSell.UseVisualStyleBackColor = true; + this.buttonSell.Click += new System.EventHandler(this.ButtonSell_Click); + // + // buttonCancel + // + this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonCancel.Location = new System.Drawing.Point(142, 90); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(123, 23); + this.buttonCancel.TabIndex = 5; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); + // + // FormSellPastry + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(277, 122); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSell); + this.Controls.Add(this.numericUpDownCount); + this.Controls.Add(this.label2); + this.Controls.Add(this.label1); + this.Controls.Add(this.comboBoxPastry); + this.Name = "FormSellPastry"; + this.Text = "Продажа изделия"; + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownCount)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private ComboBox comboBoxPastry; + private Label label1; + private Label label2; + private NumericUpDown numericUpDownCount; + private Button buttonSell; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/Confectionery/FormSellPastry.cs b/Confectionery/FormSellPastry.cs new file mode 100644 index 0000000..17731cd --- /dev/null +++ b/Confectionery/FormSellPastry.cs @@ -0,0 +1,67 @@ +using ConfectioneryContracts.BusinessLogicsContracts; +using System; +using System.Collections; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace ConfectioneryView +{ + public partial class FormSellPastry : Form + { + private readonly IShopLogic _shopLogic; + private readonly IPastryLogic _pastryLogic; + + public FormSellPastry(IPastryLogic logic, IShopLogic shopLogic) + { + InitializeComponent(); + _pastryLogic = logic; + _shopLogic = shopLogic; + var list = logic.ReadList(null); + if (list != null) + { + comboBoxPastry.DisplayMember = "PastryName"; + comboBoxPastry.ValueMember = "Id"; + comboBoxPastry.DataSource = list; + comboBoxPastry.SelectedItem = null; + } + } + + private void ButtonSell_Click(object sender, EventArgs e) + { + if (comboBoxPastry.SelectedValue == null) + { + MessageBox.Show("Выберите изделие", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (numericUpDownCount.Value <= 0) + { + MessageBox.Show("Количество должно быть больше нуля", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + var count = (int)numericUpDownCount.Value; + var pastry = _pastryLogic.ReadElement(new() { Id = (int)comboBoxPastry.SelectedValue }); + if (pastry == null || !_shopLogic.SellPastries(pastry, count)) + { + MessageBox.Show("Не удалось продать изделия. Информацию смотрите в логах", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + DialogResult = DialogResult.OK; + Close(); + } + + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/Confectionery/FormSellPastry.resx b/Confectionery/FormSellPastry.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/Confectionery/FormSellPastry.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Confectionery/Program.cs b/Confectionery/Program.cs index 6f0ea6d..e76fcdb 100644 --- a/Confectionery/Program.cs +++ b/Confectionery/Program.cs @@ -53,6 +53,7 @@ namespace ConfectioneryView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs b/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs index cd2e634..f0947f9 100644 --- a/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs @@ -15,5 +15,7 @@ namespace ConfectioneryContracts.BusinessLogicsContracts bool AddPastry(ShopSearchModel model, IPastryModel pastry, int count); int GetFreePlacesWithPastriesInShops(int countPastries); bool AddPastriesInShops(IPastryModel pastry, int count); + bool HasNeedPastries(IPastryModel pastry, int needCount); + public bool SellPastries(IPastryModel pastry, int needCount); } } From 5d93f4126cc4f3c53a941288bac8c1bce830b688 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Sat, 18 Feb 2023 20:10:51 +0400 Subject: [PATCH 25/29] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=20?= =?UTF-8?q?=D0=BD=D0=B5=D0=B8=D1=81=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D1=83?= =?UTF-8?q?=D0=B5=D0=BC=D1=8B=D0=B9=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryBusinessLogic/ShopLogic.cs | 5 + ConfectionaryFileImplement/ShopStorage.cs | 8 +- Confectionery/FormMain.Designer.cs | 20 ++- Confectionery/FormMain.cs | 9 ++ Confectionery/FormSellPastry.Designer.cs | 133 ++++++++++++++++++ Confectionery/FormSellPastry.cs | 67 +++++++++ Confectionery/FormSellPastry.resx | 60 ++++++++ Confectionery/Program.cs | 1 + .../BusinessLogicsContracts/IShopLogic.cs | 1 + 9 files changed, 298 insertions(+), 6 deletions(-) create mode 100644 Confectionery/FormSellPastry.Designer.cs create mode 100644 Confectionery/FormSellPastry.cs create mode 100644 Confectionery/FormSellPastry.resx diff --git a/ConfectionaryBusinessLogic/ShopLogic.cs b/ConfectionaryBusinessLogic/ShopLogic.cs index bf255f6..045428b 100644 --- a/ConfectionaryBusinessLogic/ShopLogic.cs +++ b/ConfectionaryBusinessLogic/ShopLogic.cs @@ -213,5 +213,10 @@ namespace ConfectioneryBusinessLogic } return true; } + + public bool SellPastries(IPastryModel pastry, int needCount) + { + return _shopStorage.SellPastries(pastry, needCount); + } } } diff --git a/ConfectionaryFileImplement/ShopStorage.cs b/ConfectionaryFileImplement/ShopStorage.cs index 3e9d4da..e155218 100644 --- a/ConfectionaryFileImplement/ShopStorage.cs +++ b/ConfectionaryFileImplement/ShopStorage.cs @@ -2,6 +2,7 @@ using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels; using ConfectioneryDataModels.Models; namespace ConfectioneryFileImplement @@ -94,11 +95,12 @@ namespace ConfectioneryFileImplement { return false; } - foreach (var pair in _source.Shops.Where(shop => shop.Pastries.ContainsKey(pastry.Id))) + foreach (var shop in _source.Shops.Where(shop => shop.Pastries.ContainsKey(pastry.Id))) { - var tuple = pair.Pastries[pastry.Id]; + var tuple = shop.Pastries[pastry.Id]; var diff = Math.Min(tuple.Item2, needCount); - pair.Pastries[pastry.Id] = (tuple.Item1, tuple.Item2 - diff); + shop.Pastries[pastry.Id] = (tuple.Item1, tuple.Item2 - diff); + needCount -= diff; if (needCount <= 0) { diff --git a/Confectionery/FormMain.Designer.cs b/Confectionery/FormMain.Designer.cs index 0710300..35f58fe 100644 --- a/Confectionery/FormMain.Designer.cs +++ b/Confectionery/FormMain.Designer.cs @@ -40,6 +40,7 @@ this.button3 = new System.Windows.Forms.Button(); this.button4 = new System.Windows.Forms.Button(); this.buttonAddPastryInShop = new System.Windows.Forms.Button(); + this.buttonSellPastry = new System.Windows.Forms.Button(); this.menuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.SuspendLayout(); @@ -94,7 +95,7 @@ this.dataGridView.Location = new System.Drawing.Point(12, 27); this.dataGridView.Name = "dataGridView"; this.dataGridView.RowTemplate.Height = 25; - this.dataGridView.Size = new System.Drawing.Size(606, 341); + this.dataGridView.Size = new System.Drawing.Size(606, 399); this.dataGridView.TabIndex = 1; // // buttonCreateOrder @@ -155,7 +156,7 @@ // buttonAddPastryInShop // this.buttonAddPastryInShop.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonAddPastryInShop.Location = new System.Drawing.Point(624, 326); + this.buttonAddPastryInShop.Location = new System.Drawing.Point(624, 384); this.buttonAddPastryInShop.Name = "buttonAddPastryInShop"; this.buttonAddPastryInShop.Size = new System.Drawing.Size(147, 31); this.buttonAddPastryInShop.TabIndex = 7; @@ -163,11 +164,23 @@ this.buttonAddPastryInShop.UseVisualStyleBackColor = true; this.buttonAddPastryInShop.Click += new System.EventHandler(this.ButtonAddPastryInShop_Click); // + // buttonSellPastry + // + this.buttonSellPastry.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.buttonSellPastry.Location = new System.Drawing.Point(624, 331); + this.buttonSellPastry.Name = "buttonSellPastry"; + this.buttonSellPastry.Size = new System.Drawing.Size(147, 31); + this.buttonSellPastry.TabIndex = 8; + this.buttonSellPastry.Text = "Продать изделие"; + this.buttonSellPastry.UseVisualStyleBackColor = true; + this.buttonSellPastry.Click += new System.EventHandler(this.ButtonSellPastry_Click); + // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(783, 380); + this.ClientSize = new System.Drawing.Size(783, 438); + this.Controls.Add(this.buttonSellPastry); this.Controls.Add(this.buttonAddPastryInShop); this.Controls.Add(this.button4); this.Controls.Add(this.button3); @@ -202,5 +215,6 @@ private ToolStripMenuItem componentToolStripMenuItem; private ToolStripMenuItem ShopsToolStripMenuItem; private Button buttonAddPastryInShop; + private Button buttonSellPastry; } } \ No newline at end of file diff --git a/Confectionery/FormMain.cs b/Confectionery/FormMain.cs index 3c6c955..6dcaf07 100644 --- a/Confectionery/FormMain.cs +++ b/Confectionery/FormMain.cs @@ -170,5 +170,14 @@ namespace ConfectioneryView form.ShowDialog(); } } + + private void ButtonSellPastry_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormSellPastry)); + if (service is FormSellPastry form) + { + form.ShowDialog(); + } + } } } \ No newline at end of file diff --git a/Confectionery/FormSellPastry.Designer.cs b/Confectionery/FormSellPastry.Designer.cs new file mode 100644 index 0000000..46f5949 --- /dev/null +++ b/Confectionery/FormSellPastry.Designer.cs @@ -0,0 +1,133 @@ +namespace ConfectioneryView +{ + partial class FormSellPastry + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.comboBoxPastry = new System.Windows.Forms.ComboBox(); + this.label1 = new System.Windows.Forms.Label(); + this.label2 = new System.Windows.Forms.Label(); + this.numericUpDownCount = new System.Windows.Forms.NumericUpDown(); + this.buttonSell = new System.Windows.Forms.Button(); + this.buttonCancel = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownCount)).BeginInit(); + this.SuspendLayout(); + // + // comboBoxPastry + // + this.comboBoxPastry.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.comboBoxPastry.FormattingEnabled = true; + this.comboBoxPastry.Location = new System.Drawing.Point(141, 12); + this.comboBoxPastry.Name = "comboBoxPastry"; + this.comboBoxPastry.Size = new System.Drawing.Size(121, 23); + this.comboBoxPastry.TabIndex = 0; + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(12, 15); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(123, 15); + this.label1.TabIndex = 1; + this.label1.Text = "Изделие на продажу:"; + // + // label2 + // + this.label2.AutoSize = true; + this.label2.Location = new System.Drawing.Point(60, 46); + this.label2.Name = "label2"; + this.label2.Size = new System.Drawing.Size(75, 15); + this.label2.TabIndex = 2; + this.label2.Text = "Количество:"; + // + // numericUpDownCount + // + this.numericUpDownCount.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.numericUpDownCount.Location = new System.Drawing.Point(142, 46); + this.numericUpDownCount.Maximum = new decimal(new int[] { + 10000000, + 0, + 0, + 0}); + this.numericUpDownCount.Name = "numericUpDownCount"; + this.numericUpDownCount.RightToLeft = System.Windows.Forms.RightToLeft.No; + this.numericUpDownCount.Size = new System.Drawing.Size(120, 23); + this.numericUpDownCount.TabIndex = 3; + // + // buttonSell + // + this.buttonSell.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonSell.Location = new System.Drawing.Point(12, 90); + this.buttonSell.Name = "buttonSell"; + this.buttonSell.Size = new System.Drawing.Size(123, 23); + this.buttonSell.TabIndex = 4; + this.buttonSell.Text = "Продать"; + this.buttonSell.UseVisualStyleBackColor = true; + this.buttonSell.Click += new System.EventHandler(this.ButtonSell_Click); + // + // buttonCancel + // + this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.buttonCancel.Location = new System.Drawing.Point(142, 90); + this.buttonCancel.Name = "buttonCancel"; + this.buttonCancel.Size = new System.Drawing.Size(123, 23); + this.buttonCancel.TabIndex = 5; + this.buttonCancel.Text = "Отмена"; + this.buttonCancel.UseVisualStyleBackColor = true; + this.buttonCancel.Click += new System.EventHandler(this.ButtonCancel_Click); + // + // FormSellPastry + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(277, 122); + this.Controls.Add(this.buttonCancel); + this.Controls.Add(this.buttonSell); + this.Controls.Add(this.numericUpDownCount); + this.Controls.Add(this.label2); + this.Controls.Add(this.label1); + this.Controls.Add(this.comboBoxPastry); + this.Name = "FormSellPastry"; + this.Text = "Продажа изделия"; + ((System.ComponentModel.ISupportInitialize)(this.numericUpDownCount)).EndInit(); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private ComboBox comboBoxPastry; + private Label label1; + private Label label2; + private NumericUpDown numericUpDownCount; + private Button buttonSell; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/Confectionery/FormSellPastry.cs b/Confectionery/FormSellPastry.cs new file mode 100644 index 0000000..17731cd --- /dev/null +++ b/Confectionery/FormSellPastry.cs @@ -0,0 +1,67 @@ +using ConfectioneryContracts.BusinessLogicsContracts; +using System; +using System.Collections; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace ConfectioneryView +{ + public partial class FormSellPastry : Form + { + private readonly IShopLogic _shopLogic; + private readonly IPastryLogic _pastryLogic; + + public FormSellPastry(IPastryLogic logic, IShopLogic shopLogic) + { + InitializeComponent(); + _pastryLogic = logic; + _shopLogic = shopLogic; + var list = logic.ReadList(null); + if (list != null) + { + comboBoxPastry.DisplayMember = "PastryName"; + comboBoxPastry.ValueMember = "Id"; + comboBoxPastry.DataSource = list; + comboBoxPastry.SelectedItem = null; + } + } + + private void ButtonSell_Click(object sender, EventArgs e) + { + if (comboBoxPastry.SelectedValue == null) + { + MessageBox.Show("Выберите изделие", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (numericUpDownCount.Value <= 0) + { + MessageBox.Show("Количество должно быть больше нуля", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + var count = (int)numericUpDownCount.Value; + var pastry = _pastryLogic.ReadElement(new() { Id = (int)comboBoxPastry.SelectedValue }); + if (pastry == null || !_shopLogic.SellPastries(pastry, count)) + { + MessageBox.Show("Не удалось продать изделия. Информацию смотрите в логах", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + DialogResult = DialogResult.OK; + Close(); + } + + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/Confectionery/FormSellPastry.resx b/Confectionery/FormSellPastry.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/Confectionery/FormSellPastry.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Confectionery/Program.cs b/Confectionery/Program.cs index 6f0ea6d..e76fcdb 100644 --- a/Confectionery/Program.cs +++ b/Confectionery/Program.cs @@ -53,6 +53,7 @@ namespace ConfectioneryView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs b/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs index cd2e634..6390a91 100644 --- a/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs +++ b/ConfectioneryContracts/BusinessLogicsContracts/IShopLogic.cs @@ -15,5 +15,6 @@ namespace ConfectioneryContracts.BusinessLogicsContracts bool AddPastry(ShopSearchModel model, IPastryModel pastry, int count); int GetFreePlacesWithPastriesInShops(int countPastries); bool AddPastriesInShops(IPastryModel pastry, int count); + public bool SellPastries(IPastryModel pastry, int needCount); } } From ddd14fe06474614f7d66da6434a866410c3166c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Mon, 20 Feb 2023 20:37:47 +0400 Subject: [PATCH 26/29] =?UTF-8?q?=D0=9E=D1=82=D1=80=D0=B5=D1=84=D0=B0?= =?UTF-8?q?=D0=BA=D1=82=D0=BE=D1=80=D0=B5=D0=BD=D0=BE=20=D0=BF=D0=BE=D0=BF?= =?UTF-8?q?=D0=BE=D0=BB=D0=BD=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BC=D0=B0=D0=B3?= =?UTF-8?q?=D0=B0=D0=B7=D0=B8=D0=BD=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectionaryBusinessLogic/ShopLogic.cs | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/ConfectionaryBusinessLogic/ShopLogic.cs b/ConfectionaryBusinessLogic/ShopLogic.cs index 36d618a..b6fb992 100644 --- a/ConfectionaryBusinessLogic/ShopLogic.cs +++ b/ConfectionaryBusinessLogic/ShopLogic.cs @@ -133,21 +133,13 @@ namespace ConfectioneryBusinessLogic } _logger.LogInformation("AddPastryInShop find. Id:{Id}", element.Id); - - if (element.Pastries.TryGetValue(pastry.Id, out var pair)) - { - element.Pastries[pastry.Id] = (pastry, count + pair.Item2); - _logger.LogInformation( - "AddPastryInShop. Has been added {count} {pastry} in {ShopName}", + + var prevCount = element.Pastries.GetValueOrDefault(pastry.Id, (pastry, 0)).Item2; + element.Pastries[pastry.Id] = (pastry, prevCount + count); + _logger.LogInformation( + "AddPastryInShop. Has been added {count} {pastry} in {ShopName}", count, pastry.PastryName, element.Name); - } - else - { - element.Pastries[pastry.Id] = (pastry, count); - _logger.LogInformation( - "AddPastryInShop. Has been added {count} new Pastry {pastry} in {ShopName}", - count, pastry.PastryName, element.Name); - } + _shopStorage.Update(new() { Id = element.Id, From c346aa79df11bd786e0e3c4213b3d575f3e25054 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Wed, 1 Mar 2023 00:04:15 +0400 Subject: [PATCH 27/29] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D1=81=D1=83=D1=89=D0=BD=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D1=8C=20=D0=BC=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD=20=D0=B8=20?= =?UTF-8?q?=D0=B5=D0=B5=20=D1=81=D0=B2=D1=8F=D0=B7=D1=8C=20=D1=81=20=D0=B8?= =?UTF-8?q?=D0=B7=D0=B4=D0=B5=D0=BB=D0=B8=D0=B5=D0=BC=20=D0=BC=D0=BD=D0=BE?= =?UTF-8?q?=D0=B3=D0=B8=D0=B5=20=D0=BA=D0=BE=20=D0=BC=D0=BD=D0=BE=D0=B3?= =?UTF-8?q?=D0=B8=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ConfectioneryDatabase.cs | 1 + .../20230228200344_create_shop.Designer.cs | 262 ++++++++++++++++++ .../Migrations/20230228200344_create_shop.cs | 89 ++++++ .../ConfectioneryDatabaseModelSnapshot.cs | 91 ++++++ ConfectioneryDatabaseImplement/Pastry.cs | 2 + ConfectioneryDatabaseImplement/Shop.cs | 84 ++++++ ConfectioneryDatabaseImplement/ShopPastry.cs | 24 ++ ConfectioneryDatabaseImplement/ShopStorage.cs | 116 ++++++++ 8 files changed, 669 insertions(+) create mode 100644 ConfectioneryDatabaseImplement/Migrations/20230228200344_create_shop.Designer.cs create mode 100644 ConfectioneryDatabaseImplement/Migrations/20230228200344_create_shop.cs create mode 100644 ConfectioneryDatabaseImplement/Shop.cs create mode 100644 ConfectioneryDatabaseImplement/ShopPastry.cs create mode 100644 ConfectioneryDatabaseImplement/ShopStorage.cs diff --git a/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs b/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs index 08095db..7164d50 100644 --- a/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs +++ b/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs @@ -24,5 +24,6 @@ namespace ConfectioneryDatabaseImplement public virtual DbSet Pastries { set; get; } public virtual DbSet PastryComponents { set; get; } public virtual DbSet Orders { set; get; } + public virtual DbSet Shops { set; get; } } } diff --git a/ConfectioneryDatabaseImplement/Migrations/20230228200344_create_shop.Designer.cs b/ConfectioneryDatabaseImplement/Migrations/20230228200344_create_shop.Designer.cs new file mode 100644 index 0000000..256b50a --- /dev/null +++ b/ConfectioneryDatabaseImplement/Migrations/20230228200344_create_shop.Designer.cs @@ -0,0 +1,262 @@ +// +using System; +using ConfectioneryDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace ConfectioneryDatabaseImplement.Migrations +{ + [DbContext(typeof(ConfectioneryDatabase))] + [Migration("20230228200344_create_shop")] + partial class create_shop + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("PastryName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Pastries"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("PastryId"); + + b.ToTable("PastryComponents"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DateOpening") + .HasColumnType("datetime2"); + + b.Property("MaxCountPastries") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.ShopPastry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopPastry"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany("Orders") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pastry"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Component", "Component") + .WithMany("PastryComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany("Components") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Pastry"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", null) + .WithMany("Shops") + .HasForeignKey("PastryId"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.ShopPastry", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany() + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ConfectioneryDatabaseImplement.Models.Shop", "Shop") + .WithMany("ShopPastries") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pastry"); + + b.Navigation("Shop"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Component", b => + { + b.Navigation("PastryComponents"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + + b.Navigation("Shops"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b => + { + b.Navigation("ShopPastries"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/ConfectioneryDatabaseImplement/Migrations/20230228200344_create_shop.cs b/ConfectioneryDatabaseImplement/Migrations/20230228200344_create_shop.cs new file mode 100644 index 0000000..1546c98 --- /dev/null +++ b/ConfectioneryDatabaseImplement/Migrations/20230228200344_create_shop.cs @@ -0,0 +1,89 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace ConfectioneryDatabaseImplement.Migrations +{ + /// + public partial class create_shop : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Shops", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Name = table.Column(type: "nvarchar(max)", nullable: false), + Address = table.Column(type: "nvarchar(max)", nullable: false), + MaxCountPastries = table.Column(type: "int", nullable: false), + DateOpening = table.Column(type: "datetime2", nullable: false), + PastryId = table.Column(type: "int", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + table.ForeignKey( + name: "FK_Shops_Pastries_PastryId", + column: x => x.PastryId, + principalTable: "Pastries", + principalColumn: "Id"); + }); + + migrationBuilder.CreateTable( + name: "ShopPastry", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + PastryId = table.Column(type: "int", nullable: false), + ShopId = table.Column(type: "int", nullable: false), + Count = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ShopPastry", x => x.Id); + table.ForeignKey( + name: "FK_ShopPastry_Pastries_PastryId", + column: x => x.PastryId, + principalTable: "Pastries", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopPastry_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_ShopPastry_PastryId", + table: "ShopPastry", + column: "PastryId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopPastry_ShopId", + table: "ShopPastry", + column: "ShopId"); + + migrationBuilder.CreateIndex( + name: "IX_Shops_PastryId", + table: "Shops", + column: "PastryId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ShopPastry"); + + migrationBuilder.DropTable( + name: "Shops"); + } + } +} diff --git a/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs b/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs index a67eb4a..609d1d8 100644 --- a/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs +++ b/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs @@ -121,6 +121,64 @@ namespace ConfectioneryDatabaseImplement.Migrations b.ToTable("PastryComponents"); }); + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DateOpening") + .HasColumnType("datetime2"); + + b.Property("MaxCountPastries") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.ShopPastry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopPastry"); + }); + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b => { b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") @@ -151,6 +209,32 @@ namespace ConfectioneryDatabaseImplement.Migrations b.Navigation("Pastry"); }); + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", null) + .WithMany("Shops") + .HasForeignKey("PastryId"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.ShopPastry", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany() + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ConfectioneryDatabaseImplement.Models.Shop", "Shop") + .WithMany("ShopPastries") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pastry"); + + b.Navigation("Shop"); + }); + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Component", b => { b.Navigation("PastryComponents"); @@ -161,6 +245,13 @@ namespace ConfectioneryDatabaseImplement.Migrations b.Navigation("Components"); b.Navigation("Orders"); + + b.Navigation("Shops"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b => + { + b.Navigation("ShopPastries"); }); #pragma warning restore 612, 618 } diff --git a/ConfectioneryDatabaseImplement/Pastry.cs b/ConfectioneryDatabaseImplement/Pastry.cs index 8f69a58..a9c7fdf 100644 --- a/ConfectioneryDatabaseImplement/Pastry.cs +++ b/ConfectioneryDatabaseImplement/Pastry.cs @@ -39,6 +39,8 @@ namespace ConfectioneryDatabaseImplement.Models public virtual List Components { get; set; } = new(); [ForeignKey("PastryId")] public virtual List Orders { get; set; } = new(); + [ForeignKey("PastryId")] + public virtual List Shops { get; set; } = new(); public static Pastry Create(ConfectioneryDatabase context, PastryBindingModel model) { diff --git a/ConfectioneryDatabaseImplement/Shop.cs b/ConfectioneryDatabaseImplement/Shop.cs new file mode 100644 index 0000000..3f1c726 --- /dev/null +++ b/ConfectioneryDatabaseImplement/Shop.cs @@ -0,0 +1,84 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDatabaseImplement; +using ConfectioneryDataModels; +using ConfectioneryDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Xml.Linq; + +namespace ConfectioneryDatabaseImplement.Models +{ + public class Shop : IShopModel + { + [Required] + public string Name { get; private set; } = string.Empty; + + [Required] + public string Address { get; private set; } = string.Empty; + + [Required] + public int MaxCountPastries { get; private set; } + + public DateTime DateOpening { get; private set; } + + private Dictionary? _cachedPastries = null; + [NotMapped] + public Dictionary Pastries + { + get + { + if (_cachedPastries == null) + { + using var context = new ConfectioneryDatabase(); + _cachedPastries = ShopPastries + .ToDictionary(x => x.Id, x => (context.Pastries + .FirstOrDefault(y => y.Id == x.Id)! as IPastryModel, x.Count)); + } + return _cachedPastries; + } + } + + public int Id { get; private set; } + + [ForeignKey("ShopId")] + public virtual List ShopPastries { get; set; } = new(); + + public static Shop? Create(ShopBindingModel? model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + Name = model.Name, + Address = model.Address, + DateOpening = model.DateOpening, + MaxCountPastries = model.MaxCountPastries, + }; + } + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + Name = model.Name; + Address = model.Address; + DateOpening = model.DateOpening; + // TODO update pastries + _cachedPastries = null; + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Address = Address, + Pastries = Pastries, + DateOpening = DateOpening, + MaxCountPastries = MaxCountPastries, + }; + } +} diff --git a/ConfectioneryDatabaseImplement/ShopPastry.cs b/ConfectioneryDatabaseImplement/ShopPastry.cs new file mode 100644 index 0000000..90a4475 --- /dev/null +++ b/ConfectioneryDatabaseImplement/ShopPastry.cs @@ -0,0 +1,24 @@ +using ConfectioneryDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConfectioneryDatabaseImplement.Models +{ + public class ShopPastry + { + public int Id { get; set; } + [Required] + public int PastryId { get; set; } + [Required] + public int ShopId { get; set; } + [Required] + public int Count { get; set; } + + public virtual Shop Shop { get; set; } = new(); + public virtual Pastry Pastry { get; set; } = new(); + } +} diff --git a/ConfectioneryDatabaseImplement/ShopStorage.cs b/ConfectioneryDatabaseImplement/ShopStorage.cs new file mode 100644 index 0000000..1b7609d --- /dev/null +++ b/ConfectioneryDatabaseImplement/ShopStorage.cs @@ -0,0 +1,116 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContract; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDatabaseImplement.Models; +using ConfectioneryDataModels; +using ConfectioneryDataModels.Models; + +namespace ConfectioneryDatabaseImplement +{ + public class ShopStorage : IShopStorage + { + + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var element = context.Shops.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + context.Shops.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new ConfectioneryDatabase(); + return context.Shops.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + using var context = new ConfectioneryDatabase(); + return context.Shops + .Select(x => x.GetViewModel) + .Where(x => x.Name.Contains(model.Name ?? string.Empty)) + .ToList(); + } + + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.Shops + .Select(shop => shop.GetViewModel) + .ToList(); + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + var newShop = Shop.Create(model); + if (newShop == null) + { + return null; + } + using var context = new ConfectioneryDatabase(); + context.Shops.Add(newShop); + context.SaveChanges(); + return newShop.GetViewModel; + } + + public ShopViewModel? Update(ShopBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var shop = context.Shops.FirstOrDefault(x => x.Id == model.Id); + if (shop == null) + { + return null; + } + shop.Update(model); + context.SaveChanges(); + return shop.GetViewModel; + } + + public bool HasNeedPastries(IPastryModel pastry, int needCount) + { + using var context = new ConfectioneryDatabase(); + var resultCount = context.Shops + .Select(shop => shop.Pastries + .FirstOrDefault(x => x.Key == pastry.Id).Value.Item2) + .Sum(); + return resultCount >= needCount; + } + + public bool SellPastries(IPastryModel pastry, int needCount) + { + if (!HasNeedPastries(pastry, needCount)) + { + return false; + } + using var context = new ConfectioneryDatabase(); + foreach (var shop in context.Shops.Where(shop => shop.Pastries.ContainsKey(pastry.Id))) + { + var tuple = shop.Pastries[pastry.Id]; + var diff = Math.Min(tuple.Item2, needCount); + shop.Pastries[pastry.Id] = (tuple.Item1, tuple.Item2 - diff); + + needCount -= diff; + if (needCount <= 0) + { + return true; + } + } + return true; + } + } +} From 369025f7a8d8bb355785517f6ba0fbc35c45324d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Wed, 1 Mar 2023 01:51:24 +0400 Subject: [PATCH 28/29] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=BE=20=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BC=D0=B0=D0=B3=D0=B0=D0=B7?= =?UTF-8?q?=D0=B8=D0=BD=D0=B0=20=D0=B2=D0=BC=D0=B5=D1=81=D1=82=D0=B5=20?= =?UTF-8?q?=D1=81=D0=BE=20=D1=81=D0=B2=D1=8F=D0=B7=D0=B0=D0=BD=D0=BD=D1=8B?= =?UTF-8?q?=D0=BC=D0=B8=20=D0=B8=D0=B7=D0=B4=D0=B5=D0=BB=D0=B8=D1=8F=D0=BC?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ConfectioneryDatabase.cs | 2 + .../20230228200344_create_shop.Designer.cs | 4 +- .../20230228204422_create_shop1.Designer.cs | 262 ++++++++++++++++++ .../Migrations/20230228204422_create_shop1.cs | 112 ++++++++ .../ConfectioneryDatabaseModelSnapshot.cs | 2 +- ConfectioneryDatabaseImplement/Shop.cs | 44 ++- ConfectioneryDatabaseImplement/ShopStorage.cs | 42 ++- 7 files changed, 451 insertions(+), 17 deletions(-) create mode 100644 ConfectioneryDatabaseImplement/Migrations/20230228204422_create_shop1.Designer.cs create mode 100644 ConfectioneryDatabaseImplement/Migrations/20230228204422_create_shop1.cs diff --git a/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs b/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs index 7164d50..d1ce8be 100644 --- a/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs +++ b/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs @@ -25,5 +25,7 @@ namespace ConfectioneryDatabaseImplement public virtual DbSet PastryComponents { set; get; } public virtual DbSet Orders { set; get; } public virtual DbSet Shops { set; get; } + public virtual DbSet ShopPastries { set; get; } + } } diff --git a/ConfectioneryDatabaseImplement/Migrations/20230228200344_create_shop.Designer.cs b/ConfectioneryDatabaseImplement/Migrations/20230228200344_create_shop.Designer.cs index 256b50a..ab30b13 100644 --- a/ConfectioneryDatabaseImplement/Migrations/20230228200344_create_shop.Designer.cs +++ b/ConfectioneryDatabaseImplement/Migrations/20230228200344_create_shop.Designer.cs @@ -228,7 +228,7 @@ namespace ConfectioneryDatabaseImplement.Migrations .IsRequired(); b.HasOne("ConfectioneryDatabaseImplement.Models.Shop", "Shop") - .WithMany("ShopPastries") + .WithMany("ShopPastry") .HasForeignKey("ShopId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); @@ -254,7 +254,7 @@ namespace ConfectioneryDatabaseImplement.Migrations modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b => { - b.Navigation("ShopPastries"); + b.Navigation("ShopPastry"); }); #pragma warning restore 612, 618 } diff --git a/ConfectioneryDatabaseImplement/Migrations/20230228204422_create_shop1.Designer.cs b/ConfectioneryDatabaseImplement/Migrations/20230228204422_create_shop1.Designer.cs new file mode 100644 index 0000000..d47f4b1 --- /dev/null +++ b/ConfectioneryDatabaseImplement/Migrations/20230228204422_create_shop1.Designer.cs @@ -0,0 +1,262 @@ +// +using System; +using ConfectioneryDatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace ConfectioneryDatabaseImplement.Migrations +{ + [DbContext(typeof(ConfectioneryDatabase))] + [Migration("20230228204422_create_shop1")] + partial class create_shop1 + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("PastryName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Price") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Pastries"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("PastryId"); + + b.ToTable("PastryComponents"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Address") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("DateOpening") + .HasColumnType("datetime2"); + + b.Property("MaxCountPastries") + .HasColumnType("int"); + + b.Property("Name") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.ShopPastry", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("PastryId") + .HasColumnType("int"); + + b.Property("ShopId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("PastryId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopPastries"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany("Orders") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pastry"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.PastryComponent", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Component", "Component") + .WithMany("PastryComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany("Components") + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Pastry"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", null) + .WithMany("Shops") + .HasForeignKey("PastryId"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.ShopPastry", b => + { + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastry") + .WithMany() + .HasForeignKey("PastryId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ConfectioneryDatabaseImplement.Models.Shop", "Shop") + .WithMany("ShopPastries") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Pastry"); + + b.Navigation("Shop"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Component", b => + { + b.Navigation("PastryComponents"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + + b.Navigation("Shops"); + }); + + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Shop", b => + { + b.Navigation("ShopPastries"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/ConfectioneryDatabaseImplement/Migrations/20230228204422_create_shop1.cs b/ConfectioneryDatabaseImplement/Migrations/20230228204422_create_shop1.cs new file mode 100644 index 0000000..107508b --- /dev/null +++ b/ConfectioneryDatabaseImplement/Migrations/20230228204422_create_shop1.cs @@ -0,0 +1,112 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace ConfectioneryDatabaseImplement.Migrations +{ + /// + public partial class create_shop1 : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_ShopPastry_Pastries_PastryId", + table: "ShopPastry"); + + migrationBuilder.DropForeignKey( + name: "FK_ShopPastry_Shops_ShopId", + table: "ShopPastry"); + + migrationBuilder.DropPrimaryKey( + name: "PK_ShopPastry", + table: "ShopPastry"); + + migrationBuilder.RenameTable( + name: "ShopPastry", + newName: "ShopPastries"); + + migrationBuilder.RenameIndex( + name: "IX_ShopPastry_ShopId", + table: "ShopPastries", + newName: "IX_ShopPastries_ShopId"); + + migrationBuilder.RenameIndex( + name: "IX_ShopPastry_PastryId", + table: "ShopPastries", + newName: "IX_ShopPastries_PastryId"); + + migrationBuilder.AddPrimaryKey( + name: "PK_ShopPastries", + table: "ShopPastries", + column: "Id"); + + migrationBuilder.AddForeignKey( + name: "FK_ShopPastries_Pastries_PastryId", + table: "ShopPastries", + column: "PastryId", + principalTable: "Pastries", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_ShopPastries_Shops_ShopId", + table: "ShopPastries", + column: "ShopId", + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_ShopPastries_Pastries_PastryId", + table: "ShopPastries"); + + migrationBuilder.DropForeignKey( + name: "FK_ShopPastries_Shops_ShopId", + table: "ShopPastries"); + + migrationBuilder.DropPrimaryKey( + name: "PK_ShopPastries", + table: "ShopPastries"); + + migrationBuilder.RenameTable( + name: "ShopPastries", + newName: "ShopPastry"); + + migrationBuilder.RenameIndex( + name: "IX_ShopPastries_ShopId", + table: "ShopPastry", + newName: "IX_ShopPastry_ShopId"); + + migrationBuilder.RenameIndex( + name: "IX_ShopPastries_PastryId", + table: "ShopPastry", + newName: "IX_ShopPastry_PastryId"); + + migrationBuilder.AddPrimaryKey( + name: "PK_ShopPastry", + table: "ShopPastry", + column: "Id"); + + migrationBuilder.AddForeignKey( + name: "FK_ShopPastry_Pastries_PastryId", + table: "ShopPastry", + column: "PastryId", + principalTable: "Pastries", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "FK_ShopPastry_Shops_ShopId", + table: "ShopPastry", + column: "ShopId", + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + } +} diff --git a/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs b/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs index 609d1d8..d006e0b 100644 --- a/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs +++ b/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs @@ -176,7 +176,7 @@ namespace ConfectioneryDatabaseImplement.Migrations b.HasIndex("ShopId"); - b.ToTable("ShopPastry"); + b.ToTable("ShopPastries"); }); modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b => diff --git a/ConfectioneryDatabaseImplement/Shop.cs b/ConfectioneryDatabaseImplement/Shop.cs index 3f1c726..190e58d 100644 --- a/ConfectioneryDatabaseImplement/Shop.cs +++ b/ConfectioneryDatabaseImplement/Shop.cs @@ -3,6 +3,8 @@ using ConfectioneryContracts.ViewModels; using ConfectioneryDatabaseImplement; using ConfectioneryDataModels; using ConfectioneryDataModels.Models; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging.Abstractions; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Xml.Linq; @@ -32,8 +34,8 @@ namespace ConfectioneryDatabaseImplement.Models { using var context = new ConfectioneryDatabase(); _cachedPastries = ShopPastries - .ToDictionary(x => x.Id, x => (context.Pastries - .FirstOrDefault(y => y.Id == x.Id)! as IPastryModel, x.Count)); + .ToDictionary(x => x.PastryId, x => (context.Pastries + .FirstOrDefault(y => y.Id == x.PastryId)! as IPastryModel, x.Count)); } return _cachedPastries; } @@ -44,7 +46,7 @@ namespace ConfectioneryDatabaseImplement.Models [ForeignKey("ShopId")] public virtual List ShopPastries { get; set; } = new(); - public static Shop? Create(ShopBindingModel? model) + public static Shop? Create(ConfectioneryDatabase context, ShopBindingModel? model) { if (model == null) { @@ -57,6 +59,11 @@ namespace ConfectioneryDatabaseImplement.Models Address = model.Address, DateOpening = model.DateOpening, MaxCountPastries = model.MaxCountPastries, + ShopPastries = model.Pastries.Select(x => new ShopPastry + { + Pastry = context.Pastries.FirstOrDefault(y => y.Id == x.Key)!, + Count = x.Value.Item2, + }).ToList() }; } public void Update(ShopBindingModel? model) @@ -68,8 +75,6 @@ namespace ConfectioneryDatabaseImplement.Models Name = model.Name; Address = model.Address; DateOpening = model.DateOpening; - // TODO update pastries - _cachedPastries = null; } public ShopViewModel GetViewModel => new() { @@ -80,5 +85,34 @@ namespace ConfectioneryDatabaseImplement.Models DateOpening = DateOpening, MaxCountPastries = MaxCountPastries, }; + + public void UpdatePastries(ConfectioneryDatabase context, ShopBindingModel model) + { + var shopPastries = context.ShopPastries + .Where(rec => rec.ShopId == model.Id) + .ToList(); + // удалили те, которых нет в модели + if (shopPastries != null && shopPastries.Count > 0) + { + context.ShopPastries + .RemoveRange(shopPastries + .Where(rec => !model.Pastries + .ContainsKey(rec.PastryId))); + // обновили количество у существующих записей + foreach (var updatePastry in shopPastries.Where(x => model.Pastries.ContainsKey(x.PastryId))) + { + updatePastry.Count = model.Pastries[updatePastry.PastryId].Item2; + model.Pastries.Remove(updatePastry.PastryId); + } + } + var shop = context.Shops.First(x => x.Id == model.Id); + shop.ShopPastries.AddRange(model.Pastries.Select(x => new ShopPastry + { + Pastry = context.Pastries.First(y => y.Id == x.Key), + Count = x.Value.Item2, + }).Except(shopPastries ?? new())); + context.SaveChanges(); + _cachedPastries = null; + } } } diff --git a/ConfectioneryDatabaseImplement/ShopStorage.cs b/ConfectioneryDatabaseImplement/ShopStorage.cs index 1b7609d..d541ceb 100644 --- a/ConfectioneryDatabaseImplement/ShopStorage.cs +++ b/ConfectioneryDatabaseImplement/ShopStorage.cs @@ -5,6 +5,7 @@ using ConfectioneryContracts.ViewModels; using ConfectioneryDatabaseImplement.Models; using ConfectioneryDataModels; using ConfectioneryDataModels.Models; +using Microsoft.EntityFrameworkCore; namespace ConfectioneryDatabaseImplement { @@ -31,7 +32,11 @@ namespace ConfectioneryDatabaseImplement return null; } using var context = new ConfectioneryDatabase(); - return context.Shops.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; + return context.Shops + .Include(x => x.ShopPastries) + .ThenInclude(x => x.Pastry) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; } public List GetFilteredList(ShopSearchModel model) @@ -42,6 +47,8 @@ namespace ConfectioneryDatabaseImplement } using var context = new ConfectioneryDatabase(); return context.Shops + .Include(x => x.ShopPastries) + .ThenInclude(x => x.Pastry) .Select(x => x.GetViewModel) .Where(x => x.Name.Contains(model.Name ?? string.Empty)) .ToList(); @@ -51,21 +58,37 @@ namespace ConfectioneryDatabaseImplement { using var context = new ConfectioneryDatabase(); return context.Shops + .Include(x => x.ShopPastries) + .ThenInclude(x => x.Pastry) .Select(shop => shop.GetViewModel) .ToList(); } public ShopViewModel? Insert(ShopBindingModel model) { - var newShop = Shop.Create(model); - if (newShop == null) - { - return null; - } using var context = new ConfectioneryDatabase(); - context.Shops.Add(newShop); - context.SaveChanges(); - return newShop.GetViewModel; + using var transaction = context.Database.BeginTransaction(); + try + { + var newShop = Shop.Create(context, model); + if (newShop == null) + { + return null; + } + if (context.Shops.Any(x => x.Name == newShop.Name)) + { + throw new Exception("Не должно быть два магазина с одним названием"); + } + context.Shops.Add(newShop); + context.SaveChanges(); + transaction.Commit(); + return newShop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } } public ShopViewModel? Update(ShopBindingModel model) @@ -77,6 +100,7 @@ namespace ConfectioneryDatabaseImplement return null; } shop.Update(model); + shop.UpdatePastries(context, model); context.SaveChanges(); return shop.GetViewModel; } From ce944a6803102092d654ea44d53e6f3a4318e8b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D0=BD=D0=B8=D1=8F=D1=80=20=D0=90=D0=B3=D0=BB?= =?UTF-8?q?=D0=B8=D1=83=D0=BB=D0=BB=D0=BE=D0=B2?= Date: Wed, 1 Mar 2023 02:38:30 +0400 Subject: [PATCH 29/29] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=D0=BD=D0=B0=20=D0=BB=D0=BE=D0=B3=D0=B8=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=BF=D1=80=D0=BE=D0=B4=D0=B0=D0=B6=D0=B8=20=D0=B8?= =?UTF-8?q?=D0=B7=D0=B4=D0=B5=D0=BB=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConfectioneryDatabaseImplement/ShopStorage.cs | 41 ++++++++++--------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/ConfectioneryDatabaseImplement/ShopStorage.cs b/ConfectioneryDatabaseImplement/ShopStorage.cs index d541ceb..133c0e4 100644 --- a/ConfectioneryDatabaseImplement/ShopStorage.cs +++ b/ConfectioneryDatabaseImplement/ShopStorage.cs @@ -107,34 +107,37 @@ namespace ConfectioneryDatabaseImplement public bool HasNeedPastries(IPastryModel pastry, int needCount) { - using var context = new ConfectioneryDatabase(); - var resultCount = context.Shops - .Select(shop => shop.Pastries - .FirstOrDefault(x => x.Key == pastry.Id).Value.Item2) - .Sum(); - return resultCount >= needCount; + throw new NotImplementedException(); } public bool SellPastries(IPastryModel pastry, int needCount) { - if (!HasNeedPastries(pastry, needCount)) - { - return false; - } using var context = new ConfectioneryDatabase(); - foreach (var shop in context.Shops.Where(shop => shop.Pastries.ContainsKey(pastry.Id))) + using var transaction = context.Database.BeginTransaction(); + foreach (var sp in context.ShopPastries.Where(x => x.PastryId == pastry.Id)) { - var tuple = shop.Pastries[pastry.Id]; - var diff = Math.Min(tuple.Item2, needCount); - shop.Pastries[pastry.Id] = (tuple.Item1, tuple.Item2 - diff); - - needCount -= diff; - if (needCount <= 0) + var res = Math.Min(needCount, sp.Count); + sp.Count -= res; + needCount -= res; + if (sp.Count == 0) // Изделия больше нет в магазине, значит удаляем его { - return true; + context.ShopPastries.Remove(sp); + } + if (needCount == 0) // Нельзя коммитить изменения в цикле, что использует контекст, поэтому выходим + { + break; } } - return true; + if (needCount == 0) + { + context.SaveChanges(); + transaction.Commit(); + } + else + { + transaction.Rollback(); + } + return needCount == 0; } } }