using DiwanRazor.Utils; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.OutputCaching; using Microsoft.Extensions.Configuration; using Newtonsoft.Json.Linq; using RMuseum.Models.Diwan.ViewModels; using System.Collections.Generic; using System.Net.Http; using System.Threading.Tasks; namespace DiwanRazor.Pages { [OutputCache(PolicyName = "DiwanPublicPage")] public class QuotesModel : LoginPartialEnabledPageModel { private readonly PoetCacheService _poetCache; public QuotesModel(HttpClient httpClient, IConfiguration configuration, PoetCacheService poetCache) : base(httpClient, configuration) { _poetCache = poetCache; } public string LastError { get; set; } public List Poets { get; set; } public DiwanPoetViewModel Poet { get; set; } public List ClaimedQuotes { get; set; } public List Quotes { get; set; } public async Task OnGetAsync() { var maintenanceResult = TryGetMaintenanceModeResult(); if (maintenanceResult != null) { return maintenanceResult; } InitializeCommonPageState(); var (poetsOk, poets, poetsError) = await _poetCache.GetPoetsAsync(EditorCacheBypass); if (!poetsOk) { LastError = poetsError; return Page(); } Poets = poets; if (!string.IsNullOrEmpty(Request.Query["p"])) { // Fetched by URL slug rather than id, and returns the "complete" poet shape rather // than the list shape - different enough from PoetCacheService's GetPoetAsync(id) // that it stays a direct call here rather than being folded into that service. var response = await _httpClient.GetAsync($"{APIRoot.Url}/api/diwan/poet?url=/{Request.Query["p"]}"); if (!response.IsSuccessStatusCode) { LastError = await ReadErrorMessageAsync(response); return Page(); } Poet = JObject.Parse(await response.Content.ReadAsStringAsync()).ToObject().Poet; Poet.ImageUrl = $"{APIRoot.InternetUrl}{Poet.ImageUrl}"; } var url = $"{APIRoot.Url}/api/diwan/quoted?published=true"; if (Poet != null) { url += $"&poetId={Poet.Id}"; } var responseQuotes = await _httpClient.GetAsync(url + "&claimed=false"); if (!responseQuotes.IsSuccessStatusCode) { LastError = await ReadErrorMessageAsync(responseQuotes); return Page(); } Quotes = JArray.Parse(await responseQuotes.Content.ReadAsStringAsync()).ToObject>(); var responseClaimedQuotes = await _httpClient.GetAsync(url + "&claimed=true"); if (!responseClaimedQuotes.IsSuccessStatusCode) { LastError = await ReadErrorMessageAsync(responseClaimedQuotes); return Page(); } ClaimedQuotes = JArray.Parse(await responseClaimedQuotes.Content.ReadAsStringAsync()).ToObject>(); ViewData["Title"] = Poet == null ? "شعرا کے اقتباسات" : $"اقتباسات اور متعلقہ اشعار {Poet.Nickname}"; return Page(); } } }