может работает, а может и нет, а может не может

This commit is contained in:
Ino 2023-05-03 14:20:42 +04:00
parent 4cf40944f5
commit 7fae4ed8f3
5 changed files with 185 additions and 74 deletions

View File

@ -11,6 +11,8 @@ namespace IceCreamShopClientApp
public static ClientViewModel? Client { get; set; } = null; public static ClientViewModel? Client { get; set; } = null;
public static int CurrentPage { get; set; } = 0;
public static void Connect(IConfiguration configuration) public static void Connect(IConfiguration configuration)
{ {
_client.BaseAddress = new Uri(configuration["IPAddress"]); _client.BaseAddress = new Uri(configuration["IPAddress"]);

View File

@ -3,6 +3,7 @@ using IceCreamShopContracts.ViewModels;
using IceCreamShopClientApp.Models; using IceCreamShopClientApp.Models;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.Diagnostics; using System.Diagnostics;
using System.Text;
namespace IceCreamShopClientApp.Controllers namespace IceCreamShopClientApp.Controllers
{ {
@ -153,5 +154,42 @@ namespace IceCreamShopClientApp.Controllers
} }
return View(APIClient.GetRequest<List<MessageInfoViewModel>>($"api/client/getmessages?clientId={APIClient.Client.Id}")); return View(APIClient.GetRequest<List<MessageInfoViewModel>>($"api/client/getmessages?clientId={APIClient.Client.Id}"));
} }
[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.HasRead ? "Прочитано" : "Непрочитано") + "</td>" +
$"<td>{mail.Reply}</td>" +
"</tr>");
}
return Tuple.Create<string?, string?, bool, bool>(htmlTable.ToString(), APIClient.CurrentPage.ToString(), APIClient.CurrentPage != 1, true);
}
} }
} }

View File

@ -1,7 +1,4 @@
@using IceCreamShopContracts.ViewModels 
@model List<MessageInfoViewModel>
@{ @{
ViewData["Title"] = "Mails"; ViewData["Title"] = "Mails";
} }
@ -10,15 +7,7 @@
<h1 class="display-4">Заказы</h1> <h1 class="display-4">Заказы</h1>
</div> </div>
<div class="text-center"> <div class="text-center">
@{
if (Model == null)
{
<h3 class="display-4">Авторизируйтесь</h3>
return;
}
<table class="table"> <table class="table">
<thead> <thead>
<tr> <tr>
@ -31,24 +20,62 @@
<th> <th>
Текст Текст
</th> </th>
<th>
Статус
</th>
<th>
Ответ
</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody id="mails-table-body">
@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> </tbody>
</table> </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> </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

@ -6,7 +6,7 @@ using IceCreamShopListImplement.Models;
namespace IceCreamShopListImplement.Implements namespace IceCreamShopListImplement.Implements
{ {
public class MessageInfoStorage public class MessageInfoStorage : IMessageInfoStorage
{ {
private readonly DataListSingleton _source; private readonly DataListSingleton _source;
public MessageInfoStorage() public MessageInfoStorage()
@ -34,8 +34,22 @@ namespace IceCreamShopListImplement.Implements
result.Add(item.GetViewModel); result.Add(item.GetViewModel);
} }
} }
if (!(model.Page.HasValue && model.PageSize.HasValue))
{
return result; return result;
} }
if (model.Page * model.PageSize >= result.Count)
{
return null;
}
List<MessageInfoViewModel> filteredResult = new();
for (var i = (model.Page.Value - 1) * model.PageSize.Value; i < model.Page.Value * model.PageSize.Value; i++)
{
filteredResult.Add(result[i]);
}
return filteredResult;
}
public List<MessageInfoViewModel> GetFullList() public List<MessageInfoViewModel> GetFullList()
{ {
@ -57,5 +71,18 @@ namespace IceCreamShopListImplement.Implements
_source.Messages.Add(newMessage); _source.Messages.Add(newMessage);
return newMessage.GetViewModel; return newMessage.GetViewModel;
} }
public MessageInfoViewModel? Update(MessageInfoBindingModel model)
{
foreach (var message in _source.Messages)
{
if (message.MessageId.Equals(model.MessageId))
{
message.Update(model);
return message.GetViewModel;
}
}
return null;
}
} }
} }

View File

@ -18,6 +18,10 @@ namespace IceCreamShopListImplement.Models
public string Body { get; private set; } = string.Empty; public string Body { get; private set; } = string.Empty;
public bool HasRead { get; private set; }
public string? Reply { get; private set; }
public static MessageInfo? Create(MessageInfoBindingModel model) public static MessageInfo? Create(MessageInfoBindingModel model)
{ {
if (model == null) if (model == null)
@ -27,6 +31,8 @@ namespace IceCreamShopListImplement.Models
return new() return new()
{ {
Body = model.Body, Body = model.Body,
Reply = model.Reply,
HasRead = model.HasRead,
Subject = model.Subject, Subject = model.Subject,
ClientId = model.ClientId, ClientId = model.ClientId,
MessageId = model.MessageId, MessageId = model.MessageId,
@ -35,15 +41,26 @@ namespace IceCreamShopListImplement.Models
}; };
} }
public void Update(MessageInfoBindingModel model)
{
if (model == null)
{
return;
}
Reply = model.Reply;
HasRead = model.HasRead;
}
public MessageInfoViewModel GetViewModel => new() public MessageInfoViewModel GetViewModel => new()
{ {
Body = Body, Body = Body,
Reply = Reply,
HasRead = HasRead,
Subject = Subject, Subject = Subject,
ClientId = ClientId, ClientId = ClientId,
MessageId = MessageId, MessageId = MessageId,
SenderName = SenderName, SenderName = SenderName,
DateDelivery = DateDelivery, DateDelivery = DateDelivery,
}; };
} }
} }