using DivanRazor.Utils; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Newtonsoft.Json.Linq; using Newtonsoft.Json; using RMuseum.Models.Divan.ViewModels; using System.Net.Http; using System.Text; using System.Threading.Tasks; using System; using RMuseum.Utils; namespace DivanRazor.Areas.User.Pages { public class CatEditorModel : PageModel { /// /// my last edit /// public DivanCatCorrectionViewModel MyLastEdit { get; set; } /// /// page /// public DivanPageCompleteViewModel PageInformation { get; set; } /// /// fatal error /// public string FatalError { get; set; } /// /// cat id /// public int CatId { get; set; } [BindProperty] public DivanCatCorrectionViewModel Correction { get; set; } /// /// get /// /// public async Task OnGetAsync() { if (string.IsNullOrEmpty(Request.Cookies["Token"])) return Redirect("/"); FatalError = Request.Query["FatalError"]; using (HttpClient secureClient = new HttpClient(new DivanReloginHandler(Request, Response))) { if (await DivanSessionChecker.PrepareClient(secureClient, Request, Response)) { var pageUrlResponse = await secureClient.GetAsync($"{APIRoot.Url}/api/divan/pageurl?id={Request.Query["id"]}"); if (!pageUrlResponse.IsSuccessStatusCode) { FatalError = JsonConvert.DeserializeObject(await pageUrlResponse.Content.ReadAsStringAsync()); return Page(); } var pageUrl = JsonConvert.DeserializeObject(await pageUrlResponse.Content.ReadAsStringAsync()); var pageQuery = await secureClient.GetAsync($"{APIRoot.Url}/api/divan/page?url={pageUrl}"); if (!pageQuery.IsSuccessStatusCode) { FatalError = JsonConvert.DeserializeObject(await pageQuery.Content.ReadAsStringAsync()); return Page(); } PageInformation = JObject.Parse(await pageQuery.Content.ReadAsStringAsync()).ToObject(); CatId = PageInformation.PoetOrCat.Cat.Id; var editResponse = await secureClient.GetAsync($"{APIRoot.Url}/api/divan/cat/correction/last/{CatId}"); if (!editResponse.IsSuccessStatusCode) { FatalError = JsonConvert.DeserializeObject(await editResponse.Content.ReadAsStringAsync()); return Page(); } MyLastEdit = JsonConvert.DeserializeObject(await editResponse.Content.ReadAsStringAsync()); if (MyLastEdit != null) { Correction = JsonConvert.DeserializeObject(await editResponse.Content.ReadAsStringAsync()); } else { Correction = new DivanCatCorrectionViewModel() { CatId = CatId, DescriptionHtml = PageInformation.PoetOrCat.Cat.DescriptionHtml, }; } } else { FatalError = "لطفاً از دیوان خارج و مجددا به آن وارد شوید."; } } return Page(); } public async Task OnPostDeleteCatCorrectionAsync(int catid) { using (HttpClient secureClient = new HttpClient(new DivanReloginHandler(Request, Response))) { if (await DivanSessionChecker.PrepareClient(secureClient, Request, Response)) { HttpResponseMessage response = await secureClient.DeleteAsync( $"{APIRoot.Url}/api/divan/cat/correction/{catid}"); if (!response.IsSuccessStatusCode) { return BadRequest(JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync())); } return new OkObjectResult(true); } } return new BadRequestObjectResult("لطفاً از دیوان خارج و مجددا به آن وارد شوید."); } public async Task OnPostAsync() { try { Correction.Description = DivanHtmlTools.StripHtmlTags(Correction.DescriptionHtml); using (HttpClient secureClient = new HttpClient(new DivanReloginHandler(Request, Response))) { if (await DivanSessionChecker.PrepareClient(secureClient, Request, Response)) { HttpResponseMessage response = await secureClient.PostAsync( $"{APIRoot.Url}/api/divan/cat/correction", new StringContent(JsonConvert.SerializeObject(Correction), Encoding.UTF8, "application/json")); if (!response.IsSuccessStatusCode) { FatalError = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); } } else { FatalError = "لطفاً از دیوان خارج و مجددا به آن وارد شوید."; } } } catch (Exception exp) { FatalError = exp.ToString(); } return Redirect($"/User/CatEditor?id={Request.Query["id"]}&FatalError={FatalError}"); } } }