From d2e6b8b6f2910e6e65383aa465a75029141780ae Mon Sep 17 00:00:00 2001 From: Programmist73 Date: Sun, 30 Apr 2023 20:35:03 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=BC=D0=B5=D0=B6=D1=83?= =?UTF-8?q?=D1=82=D0=BE=D1=87=D0=BD=D0=BE=D0=B5=20=D1=81=D0=BE=D1=85=D1=80?= =?UTF-8?q?=D0=B0=D0=BD=D0=B5=D0=BD=D0=B8=D0=B5.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/ClientController.cs | 8 +- .../BlacksmithWorkshopClientApp/APIClient.cs | 3 + .../Controllers/HomeController.cs | 51 +++++++- .../Views/Home/Mails.cshtml | 120 +++++++++++------- .../Views/Shared/_Layout.cshtml | 4 +- .../SearchModels/MessageInfoSearchModel.cs | 4 + .../Implements/MessageInfoStorage.cs | 14 +- 7 files changed, 147 insertions(+), 57 deletions(-) diff --git a/BlacksmithWorkshop/BlackmithWorkshopRestApi/Controllers/ClientController.cs b/BlacksmithWorkshop/BlackmithWorkshopRestApi/Controllers/ClientController.cs index c088727..391d5f6 100644 --- a/BlacksmithWorkshop/BlackmithWorkshopRestApi/Controllers/ClientController.cs +++ b/BlacksmithWorkshop/BlackmithWorkshopRestApi/Controllers/ClientController.cs @@ -17,6 +17,8 @@ namespace BlacksmithWorkshopRestApi.Controllers private readonly IMessageInfoLogic _mailLogic; + public int pageSize = 3; + public ClientController(IClientLogic logic, IMessageInfoLogic mailLogic, ILogger logger) { _logger = logger; @@ -75,13 +77,15 @@ namespace BlacksmithWorkshopRestApi.Controllers } [HttpGet] - public List? GetMessages(int clientId) + public List? GetMessages(int clientId, int page) { try { return _mailLogic.ReadList(new MessageInfoSearchModel { - ClientId = clientId + ClientId = clientId, + Page = pageSize, + PageSize = pageSize }); } catch (Exception ex) diff --git a/BlacksmithWorkshop/BlacksmithWorkshopClientApp/APIClient.cs b/BlacksmithWorkshop/BlacksmithWorkshopClientApp/APIClient.cs index f26821b..6800439 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopClientApp/APIClient.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopClientApp/APIClient.cs @@ -13,6 +13,9 @@ namespace BlacksmithWorkshopClientApp //поле, хранящее клиента, которого необходимо авторизовать public static ClientViewModel? Client { get; set; } = null; + //для пагинации + public static int CurrentPage { get; set; } = 0; + public static void Connect(IConfiguration configuration) { _client.BaseAddress = new Uri(configuration["IPAddress"]); diff --git a/BlacksmithWorkshop/BlacksmithWorkshopClientApp/Controllers/HomeController.cs b/BlacksmithWorkshop/BlacksmithWorkshopClientApp/Controllers/HomeController.cs index ac6bb24..7401859 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopClientApp/Controllers/HomeController.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopClientApp/Controllers/HomeController.cs @@ -3,6 +3,7 @@ using BlacksmithWorkshopContracts.BindingModels; using BlacksmithWorkshopContracts.ViewModels; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; +using System.Text; namespace BlacksmithWorkshopClientApp.Controllers { @@ -169,8 +170,7 @@ namespace BlacksmithWorkshopClientApp.Controllers [HttpPost] public double Calc(int count, int manufacture) { - var manuf =APIClient.GetRequest($"api/main/getmanufacture?manufactureId={manufacture}" - ); + var manuf =APIClient.GetRequest($"api/main/getmanufacture?manufactureId={manufacture}"); return count * (manuf?.Price ?? 1); } @@ -184,7 +184,50 @@ namespace BlacksmithWorkshopClientApp.Controllers return Redirect("~/Home/Enter"); } - return View(APIClient.GetRequest>($"api/client/getmessages?clientId={APIClient.Client.Id}")); + return View(); } - } + + //Возвращает кортеж с таблицой в html, текущей страницей писем, выключать ли кнопку пред. страницы, выключать ли кнопку след. страницы + [HttpGet] + public Tuple? SwitchPage(bool isNext) + { + if (isNext) + { + APIClient.CurrentPage++; + } + else + { + if (APIClient.CurrentPage == 1) + { + return null; + } + + APIClient.CurrentPage--; + } + + var res = APIClient.GetRequest>($"api/client/getmessages?clientId={APIClient.Client!.Id}&page={APIClient.CurrentPage}"); + + if (isNext && (res == null || res.Count == 0)) + { + APIClient.CurrentPage--; + + return Tuple.Create(null, null, APIClient.CurrentPage != 1, false); + } + + StringBuilder htmlTable = new(); + + foreach (var mail in res) + { + htmlTable.Append("" + + $"{mail.DateDelivery}" + + $"{mail.Subject}" + + $"{mail.Body}" + + "" + (mail.IsRead ? "Прочитано" : "Непрочитано") + "" + + $"{mail.Answer}" + + ""); + } + + return Tuple.Create(htmlTable.ToString(), APIClient.CurrentPage.ToString(), APIClient.CurrentPage != 1, true); + } + } } \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopClientApp/Views/Home/Mails.cshtml b/BlacksmithWorkshop/BlacksmithWorkshopClientApp/Views/Home/Mails.cshtml index cc6ca5b..b5a371c 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopClientApp/Views/Home/Mails.cshtml +++ b/BlacksmithWorkshop/BlacksmithWorkshopClientApp/Views/Home/Mails.cshtml @@ -1,51 +1,83 @@ -@using BlacksmithWorkshopContracts.ViewModels - -@model List - -@{ +@{ ViewData["Title"] = "Mails"; }
-

Заказы

+

Письма

- @{ - if (Model == null) - { -

Авторизируйтесь

- return; - } - - - - - - - - - - @foreach (var item in Model) - { - - - - - - } - -
- Дата письма - - Заголовок - - Текст -
- @Html.DisplayFor(modelItem => item.DateDelivery) - - @Html.DisplayFor(modelItem => item.Subject) - - @Html.DisplayFor(modelItem => item.Body) -
- } + + + + + + + + + + + + +
+ Дата письма + + Заголовок + + Текст + + Статус + + Ответ +
+
+ + diff --git a/BlacksmithWorkshop/BlacksmithWorkshopClientApp/Views/Shared/_Layout.cshtml b/BlacksmithWorkshop/BlacksmithWorkshopClientApp/Views/Shared/_Layout.cshtml index 709ef5c..3f64196 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopClientApp/Views/Shared/_Layout.cshtml +++ b/BlacksmithWorkshop/BlacksmithWorkshopClientApp/Views/Shared/_Layout.cshtml @@ -8,6 +8,7 @@ +
@@ -53,9 +54,6 @@ © 2023 - BlacksmithWorkshopClientApp - Личные данные - - - @await RenderSectionAsync("Scripts", required: false) diff --git a/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/MessageInfoSearchModel.cs b/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/MessageInfoSearchModel.cs index 47c87aa..662c509 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/MessageInfoSearchModel.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopContracts/SearchModels/MessageInfoSearchModel.cs @@ -11,5 +11,9 @@ namespace BlacksmithWorkshopContracts.SearchModels public string? MessageId { get; set; } public int? ClientId { get; set; } + + public int? Page { get; set; } + + public int? PageSize { get; set; } } } diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/MessageInfoStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/MessageInfoStorage.cs index a591995..09f82de 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/MessageInfoStorage.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/MessageInfoStorage.cs @@ -24,7 +24,7 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements using var context = new BlacksmithWorkshopDatabase(); return context.Messages - .FirstOrDefault(x => (x.MessageId == model.MessageId)) + .FirstOrDefault(x => (x.MessageId.Equals(model.MessageId))) ?.GetViewModel; } @@ -32,10 +32,16 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements { using var context = new BlacksmithWorkshopDatabase(); - return context.Messages + var list = context.Messages .Where(x => x.ClientId.HasValue && x.ClientId == model.ClientId) - .Select(x => x.GetViewModel) - .ToList(); + .Select(x => x.GetViewModel); + + if (!(model.Page.HasValue && model.PageSize.HasValue)) + { + return list.ToList(); + } + + return list.Skip((model.Page.Value - 1) * model.PageSize.Value).Take(model.PageSize.Value).ToList(); } public List GetFullList()