Промежуточное сохранение.

This commit is contained in:
Programmist73 2023-04-30 20:35:03 +04:00
parent a4e6186104
commit d2e6b8b6f2
7 changed files with 147 additions and 57 deletions

View File

@ -17,6 +17,8 @@ namespace BlacksmithWorkshopRestApi.Controllers
private readonly IMessageInfoLogic _mailLogic;
public int pageSize = 3;
public ClientController(IClientLogic logic, IMessageInfoLogic mailLogic, ILogger<ClientController> logger)
{
_logger = logger;
@ -75,13 +77,15 @@ namespace BlacksmithWorkshopRestApi.Controllers
}
[HttpGet]
public List<MessageInfoViewModel>? GetMessages(int clientId)
public List<MessageInfoViewModel>? GetMessages(int clientId, int page)
{
try
{
return _mailLogic.ReadList(new MessageInfoSearchModel
{
ClientId = clientId
ClientId = clientId,
Page = pageSize,
PageSize = pageSize
});
}
catch (Exception ex)

View File

@ -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"]);

View File

@ -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<ManufactureViewModel>($"api/main/getmanufacture?manufactureId={manufacture}"
);
var manuf =APIClient.GetRequest<ManufactureViewModel>($"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<List<MessageInfoViewModel>>($"api/client/getmessages?clientId={APIClient.Client.Id}"));
return View();
}
}
//Возвращает кортеж с таблицой в html, текущей страницей писем, выключать ли кнопку пред. страницы, выключать ли кнопку след. страницы
[HttpGet]
public Tuple<string?, string?, bool, bool>? SwitchPage(bool isNext)
{
if (isNext)
{
APIClient.CurrentPage++;
}
else
{
if (APIClient.CurrentPage == 1)
{
return null;
}
APIClient.CurrentPage--;
}
var res = APIClient.GetRequest<List<MessageInfoViewModel>>($"api/client/getmessages?clientId={APIClient.Client!.Id}&page={APIClient.CurrentPage}");
if (isNext && (res == null || res.Count == 0))
{
APIClient.CurrentPage--;
return Tuple.Create<string?, string?, bool, bool>(null, null, APIClient.CurrentPage != 1, false);
}
StringBuilder htmlTable = new();
foreach (var mail in res)
{
htmlTable.Append("<tr>" +
$"<td>{mail.DateDelivery}</td>" +
$"<td>{mail.Subject}</td>" +
$"<td>{mail.Body}</td>" +
"<td>" + (mail.IsRead ? "Прочитано" : "Непрочитано") + "</td>" +
$"<td>{mail.Answer}</td>" +
"</tr>");
}
return Tuple.Create<string?, string?, bool, bool>(htmlTable.ToString(), APIClient.CurrentPage.ToString(), APIClient.CurrentPage != 1, true);
}
}
}

View File

@ -1,51 +1,83 @@
@using BlacksmithWorkshopContracts.ViewModels
@model List<MessageInfoViewModel>
@{
@{
ViewData["Title"] = "Mails";
}
<div class="text-center">
<h1 class="display-4">Заказы</h1>
<h1 class="display-4">Письма</h1>
</div>
<div class="text-center">
@{
if (Model == null)
{
<h3 class="display-4">Авторизируйтесь</h3>
return;
}
<table class="table">
<thead>
<tr>
<th>
Дата письма
</th>
<th>
Заголовок
</th>
<th>
Текст
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.DateDelivery)
</td>
<td>
@Html.DisplayFor(modelItem => item.Subject)
</td>
<td>
@Html.DisplayFor(modelItem => item.Body)
</td>
</tr>
}
</tbody>
</table>
}
<table class="table">
<thead>
<tr>
<th>
Дата письма
</th>
<th>
Заголовок
</th>
<th>
Текст
</th>
<th>
Статус
</th>
<th>
Ответ
</th>
</tr>
</thead>
<tbody id="mails-table-body">
</tbody>
</table>
<ul class="pagination justify-content-center">
<li id="prev-page" class="page-item">
<a class="page-link" href="#" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
<span class="sr-only">Предыдущая</span>
</a>
</li>
<li class="page-item">
<a id="current-page" class="page-link"></a>
</li>
<li id="next-page" class="page-item">
<a class="page-link" href="#" aria-label="Next">
<span aria-hidden="true">&raquo;</span>
<span class="sr-only">Следующая</span>
</a>
</li>
</ul>
</div>
<script>
function onClicked(isNext) {
$.ajax({
method: "GET",
url: "/Home/SwitchPage",
data: { isNext: isNext },
success: function (result) {
if (result != null) {
if (result.item1 != null && result.item2 != null) {
$("#mails-table-body").html(result.item1);
$("#current-page").text(result.item2);
}
if (result.item3)
$("#prev-page").removeClass("page-item disabled");
else
$("#prev-page").addClass("page-item disabled");
if (result.item4)
$("#next-page").removeClass("page-item disabled");
else
$("#next-page").addClass("page-item disabled");
}
}
});
}
// Чтобы в первый раз загрузить данные и попасть на первую страницу
onClicked(true);
$("#prev-page").on('click', () => onClicked(false));
$("#next-page").on('click', () => onClicked(true));
</script>

View File

@ -8,6 +8,7 @@
<link rel="stylesheet" href="~/css/site.css" />
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
</head>
<body>
<header>
@ -53,9 +54,6 @@
&copy; 2023 - BlacksmithWorkshopClientApp - <a asp-area="" asp-controller="Home" asp-action="Privacy">Личные данные</a>
</div>
</footer>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
@await RenderSectionAsync("Scripts", required: false)
</body>
</html>

View File

@ -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; }
}
}

View File

@ -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<MessageInfoViewModel> GetFullList()