switching back to httpclient from db direct access

This commit is contained in:
Hamid Reza Mohammadi 2021-05-17 13:03:54 +04:30
parent d93ae0c89a
commit 848d20f0c7
3 changed files with 93 additions and 48 deletions

View File

@ -12,10 +12,8 @@ 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;
@ -39,6 +37,9 @@ namespace GanjooRazor.Pages
/// </summary>
private readonly HttpClient _httpClient;
/// <summary>
/// memory cache
/// </summary>
private readonly IMemoryCache _memoryCache;
/// <summary>

View File

@ -122,29 +122,51 @@
<div class="spacer">&nbsp;</div>
@foreach (var poem in Model.Poems)
@if (Model.Poems == null)
{
<div class="sitem">
<h2>
<a href="@poem.FullUrl">@poem.FullTitle</a>
</h2>
<div class="spacer">&nbsp;</div>
<div class="sit">
@Html.Raw(poem.PlainText)
<p>
<br>
<a href="@poem.FullUrl">متن کامل شعر را ببینید ...</a>
</p>
</div>
<img src="@($"/image/poets/{poem.Category.Poet.Id}.png")" alt=@("{poem.Category.Poet.Name}") />
<div class="spacer">&nbsp;</div>
</div>
<div class="poem">
<h2>شعری با مشخصات مورد نظر شما پیدا نشد.</h2>
<p>
<strong>توصیه‌هایی برای دستیابی به نتیجه‌ی مطلوبتر در جستجو:</strong>
</p>
<p>
الف) <strong>به جای جستجوی متن کامل یک بیت بهتر است حروف کلیدی آن را وارد کنید.</strong> از آنجا که اشعار به صورتهای مختلف در نسخه‌های متفاوت نقل شده‌اند ممکن است چیزی که شما در ذهن دارید با چیزی که در پایگاه ما ثبت شده متفاوت باشد. به عنوان نمونه مصرع «کشتی شکستگانیم ای باد شرطه برخیز» از حافظ در بعضی از نسخه‌ها به صورت «کشتی نشستگانیم ای باد شرطه برخیز» ضبط شده و شما با تایپ صورت صورت دوم آن را در گنجور نخواهید یافت. بهتر است برای یافتن این شعر کلماتی مانند کشتی و شرطه را جستجو کنیم.
</p>
<p>
ب) کلماتی را که ممکن است به صورت جدا یا سر هم نوشته شوند در گام اول به صورت جدا امتحان کنید (<strong>می شود</strong> و نه <del>میشود</del> یا <del>می‌شود</del>).
</p>
<p>
ج) در صورتی که نام شاعر را می‌دانید بهتر است از طریق لیست بازشوی جلوی جعبه‌ی جستجو نام او را انتخاب کنید. این توصیه برای زمانی مناسب است که جستجوی عبارت مورد نظر شما باعث می‌شود آثار شاعرانی که مورد نظر شما نیستند در صدر نتایج قرار گیرد و به این وسیله شما می‌خواهید به فهرست محدودتری از نتایج دست پیدا کنید.
</p>
<p>
د) در صورتی که با فنون اولیه‌ی جستجو آشنا نیستید یا <strong>معمولاً جستجوهایتان به نتیجه‌ی مطلوب نمی‌رسد</strong> بد نیست نگاهی به <a href="http://hrmoh.ir/1385/04/18/howto-search-better/" title="توصیه هایی برای جستجوی بهتر در اینترنت">این نوشته</a> بیندازید.
</p>
</div>
}
else
foreach (var poem in Model.Poems)
{
<div class="sitem">
<h2>
<a href="@poem.FullUrl">@poem.FullTitle</a>
</h2>
<div class="spacer">&nbsp;</div>
<div class="sit">
@Html.Raw(poem.PlainText)
<p>
<br>
<a href="@poem.FullUrl">متن کامل شعر را ببینید ...</a>
</p>
</div>
<img src="@($"/image/poets/{poem.Category.Poet.Id}.png")" alt=@("{poem.Category.Poet.Name}") />
<div class="spacer">&nbsp;</div>
</div>
}
<div class="spacer">&nbsp;</div>

View File

@ -11,7 +11,9 @@ using GanjooRazor.Utils;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RMuseum.Models.Auth.Memory;
using RMuseum.Models.Ganjoor.ViewModels;
using RMuseum.Services;
@ -25,27 +27,24 @@ namespace GanjooRazor.Pages
public class SearchModel : PageModel
{
/// <summary>
/// ganjoor service
/// </summary>
private readonly IGanjoorService _ganjoorService;
/// <summary>
/// HttpClient instance
/// </summary>
private readonly HttpClient _httpClient;
/// <summary>
/// memory cache
/// </summary>
private readonly IMemoryCache _memoryCache;
/// <summary>
/// constructor
/// </summary>
/// <param name="ganjoorService"></param>
/// <param name="httpClient"></param>
public SearchModel(IGanjoorService ganjoorService, HttpClient httpClient)
public SearchModel(HttpClient httpClient, IMemoryCache memoryCache)
{
_ganjoorService = ganjoorService;
_httpClient = httpClient;
_memoryCache = memoryCache;
}
public List<GanjoorPoetViewModel> Poets { get; set; }
@ -67,12 +66,30 @@ namespace GanjooRazor.Pages
private async Task preparePoets()
{
Poets = new List<GanjoorPoetViewModel>((await _ganjoorService.GetPoets(true, false)).Result);
var cacheKey = $"/api/ganjoor/poets?includeBio=false";
if (!_memoryCache.TryGetValue(cacheKey, out List<GanjoorPoetViewModel> poets))
{
var response = await _httpClient.GetAsync($"{APIRoot.Url}/api/ganjoor/poets?includeBio=false");
response.EnsureSuccessStatusCode();
poets = JArray.Parse(await response.Content.ReadAsStringAsync()).ToObject<List<GanjoorPoetViewModel>>();
_memoryCache.Set(cacheKey, poets);
}
Poets = poets;
}
private async Task preparePoet()
{
Poet = (await _ganjoorService.GetPoetById(PoetId)).Result;
var cacheKey = $"/api/ganjoor/poet/{PoetId}";
if (!_memoryCache.TryGetValue(cacheKey, out GanjoorPoetCompleteViewModel poet))
{
var poetResponse = await _httpClient.GetAsync($"{APIRoot.Url}/api/ganjoor/poet/{PoetId}");
poetResponse.EnsureSuccessStatusCode();
poet = JObject.Parse(await poetResponse.Content.ReadAsStringAsync()).ToObject<GanjoorPoetCompleteViewModel>();
_memoryCache.Set(cacheKey, poet);
}
Poet = poet;
}
public async Task<IActionResult> OnGetAsync()
@ -111,21 +128,18 @@ namespace GanjooRazor.Pages
pageNumber = int.Parse(Request.Query["page"]);
}
var searchRes = await _ganjoorService.Search
(
new PagingParameterModel()
{
PageNumber = pageNumber,
PageSize = 20
},
Query,
PoetId == 0 ? null : PoetId,
CatId == 0 ? null : CatId
);
var searchQueryResponse = await _httpClient.GetAsync($"{APIRoot.Url}/api/ganjoor/poems/search?term={Query}&poetId={PoetId}&catId={CatId}&PageNumber={pageNumber}&PageSize=20");
searchQueryResponse.EnsureSuccessStatusCode();
Poems = searchRes.Result.Items == null ? new List<GanjoorPoemCompleteViewModel>() : new List<GanjoorPoemCompleteViewModel>(searchRes.Result.Items);
if (Poems != null)
Poems = JArray.Parse(await searchQueryResponse.Content.ReadAsStringAsync()).ToObject<List<GanjoorPoemCompleteViewModel>>();
if(Poems != null && Poems.Count == 0)
{
Poems = null;
}
if(Poems != null)
{
// highlight searched word
foreach (var poem in Poems)
@ -161,9 +175,17 @@ namespace GanjooRazor.Pages
}
PagingToolsHtml = GeneratePagingBarHtml(searchRes.Result.PagingMeta, $"/search?s={Query}&author={PoetId}");
string paginationMetadataJsonValue = searchQueryResponse.Headers.GetValues("paging-headers").FirstOrDefault();
if (!string.IsNullOrEmpty(paginationMetadataJsonValue))
{
PaginationMetadata paginationMetadata = JsonConvert.DeserializeObject<PaginationMetadata>(paginationMetadataJsonValue);
PagingToolsHtml = GeneratePagingBarHtml(paginationMetadata, $"/search?s={Query}&author={PoetId}");
}
}
return Page();
}