#112 converting all comments page from httpclient to db query

This commit is contained in:
Hamid Reza Mohammadi 2021-05-08 20:54:35 +04:30
parent 07d2afff49
commit 99e4fa22c9
3 changed files with 60 additions and 56 deletions

View File

@ -4,10 +4,6 @@
<TargetFramework>net5.0-windows</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="Utils\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\RMuseum\RMuseum.csproj" />
<ProjectReference Include="..\RSecurityBackend\RSecurityBackend.csproj" />

View File

@ -12,8 +12,10 @@ using RMuseum.Models.Auth.Memory;
using RMuseum.Models.Ganjoor;
using RMuseum.Models.Ganjoor.ViewModels;
using RMuseum.Models.GanjoorAudio.ViewModels;
using RMuseum.Services;
using RSecurityBackend.Models.Auth.Memory;
using RSecurityBackend.Models.Auth.ViewModels;
using RSecurityBackend.Services;
using System;
using System.Collections.Generic;
using System.Linq;
@ -35,7 +37,14 @@ namespace GanjooRazor.Pages
/// <summary>
/// IMemoryCache
/// </summary>
protected readonly IMemoryCache _memoryCache;
private readonly IMemoryCache _memoryCache;
/// <summary>
/// IAppUserService instance
/// </summary>
private readonly IAppUserService _appUserService;
private readonly IGanjoorService _ganjoorService;
/// <summary>
/// HttpClient instance
@ -47,10 +56,15 @@ namespace GanjooRazor.Pages
/// </summary>
/// <param name="configuration"></param>
/// <param name="memoryCache"></param>
public IndexModel(IConfiguration configuration, IMemoryCache memoryCache, HttpClient httpClient)
/// <param name="appUserService"></param>
/// <param name="ganjoorService"></param>
/// <param name="httpClient"></param>
public IndexModel(IConfiguration configuration, IMemoryCache memoryCache, IAppUserService appUserService, IGanjoorService ganjoorService, HttpClient httpClient)
{
_configuration = configuration;
_memoryCache = memoryCache;
_appUserService = appUserService;
_ganjoorService = ganjoorService;
_httpClient = httpClient;
}

View File

@ -1,14 +1,10 @@
using DNTPersianUtils.Core;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RMuseum.Models.Auth.ViewModel;
using RMuseum.Models.Ganjoor.ViewModels;
using RSecurityBackend.Models.Auth.ViewModels;
using RSecurityBackend.Models.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
namespace GanjooRazor.Pages
@ -27,15 +23,22 @@ namespace GanjooRazor.Pages
pageNumber = int.Parse(Request.Query["page"]);
}
string url = $"{APIRoot.Url}/api/ganjoor/comments?PageNumber={pageNumber}&PageSize=20";
string filterUserId = Request.Query["userid"];
string htmlText = "";
if (!string.IsNullOrEmpty(filterUserId))
{
var responseUserProfile = await _httpClient.GetAsync($"{APIRoot.Url}/api/ganjoor/user/profile/{filterUserId}");
responseUserProfile.EnsureSuccessStatusCode();
RServiceResult<PublicRAppUser> userInfo = await _appUserService.GetUserInformation(Guid.Parse(filterUserId));
GanjoorUserPublicProfile profile = JsonConvert.DeserializeObject<GanjoorUserPublicProfile>(await responseUserProfile.Content.ReadAsStringAsync());
GanjoorUserPublicProfile profile = new GanjoorUserPublicProfile()
{
Id = (Guid)userInfo.Result.Id,
NickName = userInfo.Result.NickName,
Bio = userInfo.Result.Bio,
Website = userInfo.Result.Website,
RImageId = userInfo.Result.RImageId
};
ViewData["Title"] = $"گنجور &raquo; حاشیه‌های {profile.NickName}";
@ -61,24 +64,21 @@ namespace GanjooRazor.Pages
htmlText += $"<hr />{Environment.NewLine}";
url += $"&filterUserId={filterUserId}";
}
var response = await _httpClient.GetAsync(url);
if (!response.IsSuccessStatusCode)
var commentsRes = await _ganjoorService.GetRecentComments(new PagingParameterModel()
{
LastError = await response.Content.ReadAsStringAsync();
return;
}
PageNumber = pageNumber,
PageSize = 20
},
string.IsNullOrEmpty(filterUserId) ? Guid.Empty : Guid.Parse(filterUserId), true);
response.EnsureSuccessStatusCode();
PaginationMetadata paginationMetadata = commentsRes.Result.PagingMeta;
GanjoorCommentFullViewModel[] comments = commentsRes.Result.Items;
foreach (var comment in JArray.Parse(await response.Content.ReadAsStringAsync()).ToObject<List<GanjoorCommentFullViewModel>>())
foreach (var comment in comments)
{
htmlText += $"<blockquote>{comment.HtmlComment}{Environment.NewLine}" +
$"<p>{comment.AuthorName} <small>در {comment.CommentDate.ToFriendlyPersianDateTextify()}</small> دربارهٔ <a href=\"{comment.Poem.UrlSlug}\">{comment.Poem.Title}</a>" +
@ -87,42 +87,36 @@ namespace GanjooRazor.Pages
htmlText += "<p style=\"text-align: center;\">";
string paginnationMetadata = response.Headers.GetValues("paging-headers").FirstOrDefault();
if (!string.IsNullOrEmpty(paginnationMetadata))
string queryFilterUserId = string.IsNullOrEmpty(filterUserId) ? "" : $"&userid={filterUserId}";
if (paginationMetadata.totalPages > 1)
{
PaginationMetadata paginationMetadata = JsonConvert.DeserializeObject<PaginationMetadata>(paginnationMetadata);
GanjoorPage.Title += $" - صفحهٔ {pageNumber.ToPersianNumbers()}";
ViewData["Title"] += $" - صفحهٔ {pageNumber.ToPersianNumbers()}";
string queryFilterUserId = string.IsNullOrEmpty(filterUserId) ? "" : $"&userid={filterUserId}";
if (paginationMetadata.totalPages > 1)
if (paginationMetadata.currentPage > 3)
{
GanjoorPage.Title += $" - صفحهٔ {pageNumber.ToPersianNumbers()}";
ViewData["Title"] += $" - صفحهٔ {pageNumber.ToPersianNumbers()}";
if (paginationMetadata.currentPage > 3)
htmlText += $"[<a href=\"/hashieha/?page=1{queryFilterUserId}\">صفحهٔ اول</a>] …";
}
for (int i = (paginationMetadata.currentPage - 2); i <= (paginationMetadata.currentPage + 2); i++)
{
if (i >= 1 && i <= paginationMetadata.totalPages)
{
htmlText += $"[<a href=\"/hashieha/?page=1{queryFilterUserId}\">صفحهٔ اول</a>] …";
}
for (int i = (paginationMetadata.currentPage - 2); i <= (paginationMetadata.currentPage + 2); i++)
{
if (i >= 1 && i <= paginationMetadata.totalPages)
htmlText += " [";
if (i == paginationMetadata.currentPage)
{
htmlText += " [";
if (i == paginationMetadata.currentPage)
{
htmlText += i.ToPersianNumbers();
}
else
{
htmlText += $"<a href=\"/hashieha/?page={i}{queryFilterUserId}\">{i.ToPersianNumbers()}</a>";
}
htmlText += "] ";
htmlText += i.ToPersianNumbers();
}
else
{
htmlText += $"<a href=\"/hashieha/?page={i}{queryFilterUserId}\">{i.ToPersianNumbers()}</a>";
}
htmlText += "] ";
}
if (paginationMetadata.totalPages > (paginationMetadata.currentPage + 2))
{
htmlText += $"… [<a href=\"/hashieha/?page={paginationMetadata.totalPages}{queryFilterUserId}\">صفحهٔ آخر</a>]";
}
}
if (paginationMetadata.totalPages > (paginationMetadata.currentPage + 2))
{
htmlText += $"… [<a href=\"/hashieha/?page={paginationMetadata.totalPages}{queryFilterUserId}\">صفحهٔ آخر</a>]";
}
}