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

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,7 +11,9 @@ namespace IceCreamShopClientApp
public static ClientViewModel? Client { get; set; } = null; public static ClientViewModel? Client { get; set; } = null;
public static void Connect(IConfiguration configuration) public static int CurrentPage { get; set; } = 0;
public static void Connect(IConfiguration configuration)
{ {
_client.BaseAddress = new Uri(configuration["IPAddress"]); _client.BaseAddress = new Uri(configuration["IPAddress"]);
_client.DefaultRequestHeaders.Accept.Clear(); _client.DefaultRequestHeaders.Accept.Clear();

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,45 +7,75 @@
<h1 class="display-4">Заказы</h1> <h1 class="display-4">Заказы</h1>
</div> </div>
<div class="text-center"> <div class="text-center">
@{ <table class="table">
if (Model == null) <thead>
{ <tr>
<h3 class="display-4">Авторизируйтесь</h3> <th>
return; Дата письма
} </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>
<table class="table"> <script>
<thead> function onClicked(isNext) {
<tr> $.ajax({
<th> method: "GET",
Дата письма url: "/Home/SwitchPage",
</th> data: { isNext: isNext },
<th> success: function (result) {
Заголовок if (result != null) {
</th> if (result.item1 != null && result.item2 != null) {
<th> $("#mails-table-body").html(result.item1);
Текст $("#current-page").text(result.item2);
</th> }
</tr> if (result.item3)
</thead> $("#prev-page").removeClass("page-item disabled");
<tbody> else
@foreach (var item in Model) $("#prev-page").addClass("page-item disabled");
{ if (result.item4)
<tr> $("#next-page").removeClass("page-item disabled");
<td> else
@Html.DisplayFor(modelItem => item.DateDelivery) $("#next-page").addClass("page-item disabled");
</td> }
<td> }
@Html.DisplayFor(modelItem => item.Subject) });
</td> }
<td> onClicked(true);
@Html.DisplayFor(modelItem => item.Body) $("#prev-page").on('click', () => onClicked(false));
</td> $("#next-page").on('click', () => onClicked(true));
</tr>
} </script>
</tbody>
</table>
}
</div>

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()
@ -26,16 +26,30 @@ namespace IceCreamShopListImplement.Implements
public List<MessageInfoViewModel> GetFilteredList(MessageInfoSearchModel model) public List<MessageInfoViewModel> GetFilteredList(MessageInfoSearchModel model)
{ {
List<MessageInfoViewModel> result = new(); List<MessageInfoViewModel> result = new();
foreach (var item in _source.Messages) foreach (var item in _source.Messages)
{ {
if (item.ClientId.HasValue && item.ClientId == model.ClientId) if (item.ClientId.HasValue && item.ClientId == model.ClientId)
{ {
result.Add(item.GetViewModel); result.Add(item.GetViewModel);
} }
} }
return result;
} if (!(model.Page.HasValue && model.PageSize.HasValue))
{
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,7 +18,11 @@ namespace IceCreamShopListImplement.Models
public string Body { get; private set; } = string.Empty; public string Body { get; private set; } = string.Empty;
public static MessageInfo? Create(MessageInfoBindingModel model) public bool HasRead { get; private set; }
public string? Reply { get; private set; }
public static MessageInfo? Create(MessageInfoBindingModel model)
{ {
if (model == null) if (model == null)
{ {
@ -26,24 +30,37 @@ namespace IceCreamShopListImplement.Models
} }
return new() return new()
{ {
Body = model.Body, Body = model.Body,
Subject = model.Subject, Reply = model.Reply,
ClientId = model.ClientId, HasRead = model.HasRead,
MessageId = model.MessageId, Subject = model.Subject,
SenderName = model.SenderName, ClientId = model.ClientId,
DateDelivery = model.DateDelivery, MessageId = model.MessageId,
}; SenderName = model.SenderName,
DateDelivery = model.DateDelivery,
};
} }
public MessageInfoViewModel GetViewModel => new() public void Update(MessageInfoBindingModel model)
{ {
Body = Body, if (model == null)
Subject = Subject, {
ClientId = ClientId, return;
MessageId = MessageId, }
SenderName = SenderName, Reply = model.Reply;
DateDelivery = DateDelivery, HasRead = model.HasRead;
}; }
public MessageInfoViewModel GetViewModel => new()
{
Body = Body,
Reply = Reply,
HasRead = HasRead,
Subject = Subject,
ClientId = ClientId,
MessageId = MessageId,
SenderName = SenderName,
DateDelivery = DateDelivery,
};
} }
} }