From fde6a6b5c44b4c2e19dfc429b23f1a7498d0529a 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, 8 Mar 2023 19:35:48 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BE=20=D0=BE=D1=82=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=B8=20=D0=BF=D1=80=D0=B8=D0=BD=D1=8F=D1=82?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=B8=D0=B7=D0=B4=D0=B5=D0=BB=D0=B8=D0=B9=20?= =?UTF-8?q?=D0=BC=D0=B0=D0=B3=D0=B0=D0=B7=D0=B8=D0=BD=D0=B0=20=D0=BD=D0=B0?= =?UTF-8?q?=20=D0=BA=D0=BB=D0=B8=D0=B5=D0=BD=D1=82=D0=B5=20=D0=B8=20=D1=81?= =?UTF-8?q?=D0=B5=D1=80=D0=B2=D0=B5=D1=80=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Достаточно костыльно но пофиг --- ConfectioneryDatabaseImplement/Pastry.cs | 2 ++ .../Controllers/ShopController.cs | 35 +++++++++++++++++-- .../Controllers/HomeController.cs | 30 ++++++++-------- ConfectioneryShopApp/Views/Home/Index.cshtml | 2 +- ConfectioneryShopApp/Views/Home/Update.cshtml | 9 +++-- .../Views/Shared/_Layout.cshtml | 2 +- 6 files changed, 58 insertions(+), 22 deletions(-) diff --git a/ConfectioneryDatabaseImplement/Pastry.cs b/ConfectioneryDatabaseImplement/Pastry.cs index 85a0129..304d1fa 100644 --- a/ConfectioneryDatabaseImplement/Pastry.cs +++ b/ConfectioneryDatabaseImplement/Pastry.cs @@ -96,5 +96,7 @@ namespace ConfectioneryDatabaseImplement.Models } _pastryComponents = null; } + + public static implicit operator ConfectioneryContracts.ViewModels.PastryViewModel(ConfectioneryDatabaseImplement.Models.Pastry model) => model.GetViewModel; } } diff --git a/ConfectioneryRestApi/Controllers/ShopController.cs b/ConfectioneryRestApi/Controllers/ShopController.cs index ae3a625..0473392 100644 --- a/ConfectioneryRestApi/Controllers/ShopController.cs +++ b/ConfectioneryRestApi/Controllers/ShopController.cs @@ -2,6 +2,7 @@ using ConfectioneryContracts.BusinessLogicsContracts; using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.ViewModels; +using ConfectioneryDatabaseImplement.Models; using ConfectioneryDataModels.Models; using Microsoft.AspNetCore.Mvc; using System.Text.Json; @@ -36,13 +37,41 @@ namespace ConfectioneryRestApi.Controllers } } + /// + /// Получает на вход айди магазина и по нему возвращает магазин + /// + /// The identifier. + /// + /// Кортеж из магазина, итератора с изделиями которые находятся в магазине и итератора с количеством этих изделий. + /// + /// Почему изделия и их количество не находятся в одном кортеже? потому что тогда он их не сериализует + /// и я не знаю почему. + /// Также, к сожалению, приходится явно присваивать каждое поле из IPastyModel в PastyViewModel, поскольку + /// нельзя сериализовать интерфейс, и при это нельзя его неявно кастануть к PastryViewModel, даже если в + /// истинном типе ConfectioneryDatabaseImplement.Pastry такой каст есть. + /// Сделать же каст в PastryViewModel из IPastryModel нельзя, потому что запрещен каст с интерфейсами. + /// Единственный нормальный вариант создать отдельную сущность, где объединить изделия и их количество, + /// и уже ее хранить в магазине и соответственно ее передавать. Но поскольку для этого нужно перелопатить пол-проекта + /// и получить минус баллы на pr я откажусь от подобной идеи. [HttpGet] - public ShopViewModel? GetJsonShop(int id) + public Tuple, IEnumerable>? GetShopWithPastries(int id) { try { - return _logic.ReadElement(new() { Id = id }); + var shop = _logic.ReadElement(new() { Id = id }); + if (shop == null) + { + return null; + } + return Tuple.Create(shop, + shop.Pastries.Select(x => new PastryViewModel () + { + Id = x.Value.Item1.Id, + Price = x.Value.Item1.Price, + PastryName = x.Value.Item1.PastryName, + }), + shop.Pastries.Select(x => x.Value.Item2)); } catch (Exception ex) { @@ -73,7 +102,7 @@ namespace ConfectioneryRestApi.Controllers public void DeleteShop(ShopBindingModel model) => CRUDShop(() => _logic.Delete(model)); [HttpPost] - public void AddPastryInShop(Tuple countPastryForShop) + public void AddPastryInShop(Tuple countPastryForShop) { CRUDShop(() => _logic.AddPastry(countPastryForShop.Item1, countPastryForShop.Item2, countPastryForShop.Item3)); } diff --git a/ConfectioneryShopApp/Controllers/HomeController.cs b/ConfectioneryShopApp/Controllers/HomeController.cs index b2ea4a5..78be541 100644 --- a/ConfectioneryShopApp/Controllers/HomeController.cs +++ b/ConfectioneryShopApp/Controllers/HomeController.cs @@ -98,22 +98,24 @@ namespace ConfectioneryShopApp.Controllers } [HttpGet] - public Tuple GetTablePastriesFromShop(int shop) + public Tuple? GetTablePastriesFromShop(int shop) { - var sh = APIClient.GetRequest($"api/shop/getjsonshop?id={shop}"); - var resultHtml = ""; - if (sh != null) + var result = APIClient.GetRequest, IEnumerable>?>($"api/shop/getshopwithpastries?id={shop}"); + if (result == null) { - foreach (var (item, count) in sh.Pastries.Values) - { - resultHtml += ""; - resultHtml += $"{item?.PastryName ?? string.Empty}"; - resultHtml += $"{item?.Price ?? 0}"; - resultHtml += $"{count}"; - resultHtml += ""; - } + return null; } - return Tuple.Create(resultHtml, sh); + var shopModel = result.Item1; + var resultHtml = ""; + foreach (var (item, count) in result.Item2.Zip(result.Item3)) + { + resultHtml += ""; + resultHtml += $"{item?.PastryName ?? string.Empty}"; + resultHtml += $"{item?.Price ?? 0}"; + resultHtml += $"{count}"; + resultHtml += ""; + } + return Tuple.Create(resultHtml, shopModel); } [HttpGet] @@ -189,7 +191,7 @@ namespace ConfectioneryShopApp.Controllers } APIClient.PostRequest("api/shop/addpastryinshop", Tuple.Create( new ShopSearchModel() { Id = shop }, - new PastryBindingModel() { Id = pastry }, + new PastryViewModel() { Id = pastry }, count )); Response.Redirect("Index"); diff --git a/ConfectioneryShopApp/Views/Home/Index.cshtml b/ConfectioneryShopApp/Views/Home/Index.cshtml index fd14390..6d22535 100644 --- a/ConfectioneryShopApp/Views/Home/Index.cshtml +++ b/ConfectioneryShopApp/Views/Home/Index.cshtml @@ -7,7 +7,7 @@ }
-

Заказы

+

Магазины

diff --git a/ConfectioneryShopApp/Views/Home/Update.cshtml b/ConfectioneryShopApp/Views/Home/Update.cshtml index 2892ef5..bbf8156 100644 --- a/ConfectioneryShopApp/Views/Home/Update.cshtml +++ b/ConfectioneryShopApp/Views/Home/Update.cshtml @@ -58,9 +58,12 @@ url: "/Home/GetTablePastriesFromShop", data: { shop: shop }, success: function (result) { - $('#name').val(result.item2.name); - $('#address').val(result.item2.address); - $('#table-pastries').html(result.item1); + if (result != null) + { + $('#name').val(result.item2.name); + $('#address').val(result.item2.address); + $('#table-pastries').html(result.item1); + } } }); }; diff --git a/ConfectioneryShopApp/Views/Shared/_Layout.cshtml b/ConfectioneryShopApp/Views/Shared/_Layout.cshtml index d5f6e80..75571c1 100644 --- a/ConfectioneryShopApp/Views/Shared/_Layout.cshtml +++ b/ConfectioneryShopApp/Views/Shared/_Layout.cshtml @@ -23,7 +23,7 @@