using System.Net.Http; using System.Text; using System.Threading.Tasks; using GanjooRazor.Utils; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Newtonsoft.Json; using RMuseum.Models.Ganjoor; namespace GanjooRazor.Areas.User.Pages { public class AllPoemSectionsModel : PageModel { /// /// fatal error /// public string FatalError { get; set; } public GanjoorPoemSection[] PoemSections { get; set; } /// /// can edit /// public bool CanEdit { get; set; } public async Task OnGetAsync() { if (string.IsNullOrEmpty(Request.Cookies["Token"])) return Redirect("/"); CanEdit = Request.Cookies["CanEdit"] == "True"; using (HttpClient secureClient = new HttpClient(new GanjoorReloginHandler(Request, Response))) { if (await GanjoorSessionChecker.PrepareClient(secureClient, Request, Response)) { var sectionsResponse = await secureClient.GetAsync($"{APIRoot.Url}/api/ganjoor/sections/{Request.Query["id"]}"); if (!sectionsResponse.IsSuccessStatusCode) { FatalError = JsonConvert.DeserializeObject(await sectionsResponse.Content.ReadAsStringAsync()); return Page(); } PoemSections = JsonConvert.DeserializeObject(await sectionsResponse.Content.ReadAsStringAsync()); } } return Page(); } public async Task OnPostRebuildRelatedSectionsAsync(int meterId, string rhyming) { using (HttpClient secureClient = new HttpClient(new GanjoorReloginHandler(Request, Response))) { if (await GanjoorSessionChecker.PrepareClient(secureClient, Request, Response)) { HttpResponseMessage response = await secureClient.PostAsync( $"{APIRoot.Url}/api/ganjoor/sections/updaterelated?metreId={meterId}&rhyme={rhyming}", null ); if (!response.IsSuccessStatusCode) { return new BadRequestObjectResult(JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync())); } return new OkResult(); } else { return new BadRequestObjectResult("لطفاً از دیوان خارج و مجددا به آن وارد شوید."); } } } } }