divan/DivanRazor/Areas/Admin/Pages/EditQuoted.cshtml.cs
Anas Rashid 532d005cdb Rename Ganjoor -> Divan throughout the code
Projects (GanjooRazor -> DivanRazor, GanjoorService.sln -> DivanService.sln), files,
folders, classes, namespaces, settings, API routes (/api/divan) and DB tables (Divan*).
External addresses (ganjoor.net, github.com/ganjoor) unchanged. Migrations renamed
consistently (no pending model changes); requires a fresh database.
Upstream RUNNING_LOCALLY/SEMANTIC_SEARCH docs archived unmodified under docs/.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-04 23:49:01 +02:00

435 lines
20 KiB
C#

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.Threading.Tasks;
using System;
using System.Collections.Generic;
using RMuseum.Models.Divan;
using DivanRazor.Utils;
using System.Text;
using System.Linq;
using System.Net;
namespace DivanRazor.Areas.Admin.Pages
{
public class EditQuotedModel : PageModel
{
public string LastMessage { get; set; }
/// <summary>
/// HttpClient instance
/// </summary>
private readonly HttpClient _httpClient;
/// <summary>
/// constructor
/// </summary>
/// <param name="httpClient"></param>
public EditQuotedModel(HttpClient httpClient)
{
_httpClient = httpClient;
}
public DivanPoemCompleteViewModel Poem { get; set; }
public DivanPoemCompleteViewModel RelatedPoem { get; set; }
public Tuple<int, string>[] Couplets { get; set; }
public Tuple<int, string>[] RelatedCouplets { get; set; }
[BindProperty]
public DivanQuotedPoemViewModel DivanQuotedPoem { get; set; }
public DivanQuotedPoemViewModel[] AllPoemQuoteds { get; set; }
public Guid? ReverseId { get; set; }
public bool DisplayAll { get; set; }
private Tuple<int, string>[] GetCouplets(DivanVerseViewModel[] verses)
{
int coupetIndex = -1;
string coupletText = "";
List<Tuple<int, string>> couplets = new List<Tuple<int, string>>();
int verseIndex = 0;
bool incompleteCouplet = false;
while (verseIndex < verses.Length)
{
switch (verses[verseIndex].VersePosition)
{
case VersePosition.Comment:
incompleteCouplet = false;
if (!string.IsNullOrEmpty(coupletText))
{
couplets.Add(new Tuple<int, string>(coupetIndex, coupletText));
coupletText = "";
}
break;
case VersePosition.Paragraph:
case VersePosition.Single:
incompleteCouplet = false;
if (!string.IsNullOrEmpty(coupletText))
{
couplets.Add(new Tuple<int, string>(coupetIndex, coupletText));
coupletText = "";
}
coupetIndex++;
couplets.Add(new Tuple<int, string>(coupetIndex, verses[verseIndex].Text));
break;
case VersePosition.Right:
case VersePosition.CenteredVerse1:
incompleteCouplet = false;
if (!string.IsNullOrEmpty(coupletText))
{
couplets.Add(new Tuple<int, string>(coupetIndex, coupletText));
}
coupetIndex++;
coupletText = verses[verseIndex].Text;
break;
case VersePosition.Left:
case VersePosition.CenteredVerse2:
incompleteCouplet = true;
coupletText += $" - {verses[verseIndex].Text}";
break;
}
verseIndex++;
}
if (incompleteCouplet && !string.IsNullOrEmpty(coupletText))
couplets.Add(new Tuple<int, string>(coupetIndex, coupletText));
return couplets.ToArray();
}
private async Task<string> Prepare(int poemId, string id)
{
LastMessage = "";
var poemQuery = await _httpClient.GetAsync($"{APIRoot.Url}/api/divan/poem/{poemId}");
if (!poemQuery.IsSuccessStatusCode)
{
LastMessage = JsonConvert.DeserializeObject<string>(await poemQuery.Content.ReadAsStringAsync());
return LastMessage;
}
Poem = JObject.Parse(await poemQuery.Content.ReadAsStringAsync()).ToObject<DivanPoemCompleteViewModel>();
Couplets = GetCouplets(Poem.Verses);
RelatedCouplets = [];
if (!string.IsNullOrEmpty(id))
{
var quoteQuery = await _httpClient.GetAsync($"{APIRoot.Url}/api/divan/quoted/{id}");
if (!quoteQuery.IsSuccessStatusCode)
{
LastMessage = JsonConvert.DeserializeObject<string>(await quoteQuery.Content.ReadAsStringAsync());
return LastMessage;
}
DivanQuotedPoem = JObject.Parse(await quoteQuery.Content.ReadAsStringAsync()).ToObject<DivanQuotedPoemViewModel>();
if (DivanQuotedPoem.RelatedPoemId != null)
{
var relPoemQuery = await _httpClient.GetAsync($"{APIRoot.Url}/api/divan/poem/{DivanQuotedPoem.RelatedPoemId}");
if (!relPoemQuery.IsSuccessStatusCode)
{
LastMessage = JsonConvert.DeserializeObject<string>(await relPoemQuery.Content.ReadAsStringAsync());
return LastMessage;
}
RelatedPoem = JObject.Parse(await relPoemQuery.Content.ReadAsStringAsync()).ToObject<DivanPoemCompleteViewModel>();
RelatedCouplets = GetCouplets(RelatedPoem.Verses);
var revQuery = await _httpClient.GetAsync($"{APIRoot.Url}/api/divan/poem/{DivanQuotedPoem.RelatedPoemId}/quoteds/{DivanQuotedPoem.PoemId}");
if (!revQuery.IsSuccessStatusCode)
{
LastMessage = JsonConvert.DeserializeObject<string>(await revQuery.Content.ReadAsStringAsync());
return LastMessage;
}
var revs = JsonConvert.DeserializeObject<DivanQuotedPoemViewModel[]>(await revQuery.Content.ReadAsStringAsync());
var rev = revs.Where(r => r.CoupletIndex == DivanQuotedPoem.RelatedCoupletIndex && r.RelatedCoupletIndex == DivanQuotedPoem.CoupletIndex).SingleOrDefault();
if (rev != null)
{
ReverseId = rev.Id;
}
}
}
else
{
DivanQuotedPoem = new DivanQuotedPoemViewModel()
{
PoemId = poemId,
PoetId = Poem.Category.Poet.Id,
RelatedPoetId = null,
RelatedPoemId = null,
IsPriorToRelated = false,
ChosenForMainList = true,
CachedRelatedPoemPoetDeathYearInLHijri = 0,
CachedRelatedPoemPoetName = null,
CachedRelatedPoemPoetUrl = null,
CachedRelatedPoemPoetImage = null,
CachedRelatedPoemFullTitle = null,
CachedRelatedPoemFullUrl = null,
SortOrder = 1000,
Note = "",
Published = false,
ClaimedByBothPoets = false,
IndirectQuotation = false,
SamePoemsQuotedCount = 0,
RelatedCoupletVerse1 = null,
RelatedCoupletVerse1ShouldBeEmphasized = false,
RelatedCoupletVerse2 = null,
RelatedCoupletVerse2ShouldBeEmphasized = false,
RelatedCoupletIndex = null,
CoupletVerse1 = Poem.Verses[0].Text,
CoupletVerse1ShouldBeEmphasized = false,
CoupletVerse2 = Poem.Verses[1].Text,
CoupletVerse2ShouldBeEmphasized = false,
CoupletIndex = 0,
};
}
var allQuery = await _httpClient.GetAsync($"{APIRoot.Url}/api/divan/poem/{poemId}/quoteds");
if (!allQuery.IsSuccessStatusCode)
{
LastMessage = JsonConvert.DeserializeObject<string>(await allQuery.Content.ReadAsStringAsync());
return LastMessage;
}
AllPoemQuoteds = JsonConvert.DeserializeObject<DivanQuotedPoemViewModel[]>(await allQuery.Content.ReadAsStringAsync());
return LastMessage;
}
public async Task<IActionResult> OnGetAsync()
{
if (string.IsNullOrEmpty(Request.Cookies["Token"]))
return Redirect("/");
DisplayAll = false;
if (!string.IsNullOrEmpty(Request.Query["all"]))
{
DisplayAll = true;
string url = $"{APIRoot.Url}/api/divan/quoted?published=false";
if (!string.IsNullOrEmpty(Request.Query["p"]))
{
url += $"&poetId={Request.Query["p"]}";
}
if (!string.IsNullOrEmpty(Request.Query["r"]))
{
url += $"&relatedPoetId={Request.Query["r"]}";
}
var allQuery = await _httpClient.GetAsync(url);
if (!allQuery.IsSuccessStatusCode)
{
LastMessage = JsonConvert.DeserializeObject<string>(await allQuery.Content.ReadAsStringAsync());
}
AllPoemQuoteds = JsonConvert.DeserializeObject<DivanQuotedPoemViewModel[]>(await allQuery.Content.ReadAsStringAsync());
return Page();
}
string poemIdString = Request.Query["p"];
if (string.IsNullOrEmpty(poemIdString))
{
if (!string.IsNullOrEmpty(Request.Query["id"]))
{
var quoteQuery = await _httpClient.GetAsync($"{APIRoot.Url}/api/divan/quoted/{Request.Query["id"]}");
if (!quoteQuery.IsSuccessStatusCode)
{
LastMessage = JsonConvert.DeserializeObject<string>(await quoteQuery.Content.ReadAsStringAsync());
return Page();
}
DivanQuotedPoem = JObject.Parse(await quoteQuery.Content.ReadAsStringAsync()).ToObject<DivanQuotedPoemViewModel>();
poemIdString = DivanQuotedPoem.PoemId.ToString();
}
else
{
LastMessage = "شناسهٔ شعر مشخص نیست.";
return Page();
}
}
await Prepare(int.Parse(poemIdString), Request.Query["id"]);
return Page();
}
public async Task<IActionResult> OnPostAsync(DivanQuotedPoemViewModel DivanQuotedPoem)
{
try
{
await Prepare(DivanQuotedPoem.PoemId, DivanQuotedPoem.Id == Guid.Empty ? null : DivanQuotedPoem.Id.ToString());
DivanQuotedPoem.CoupletVerse1 = Poem.Verses.Where(v => v.CoupletIndex == DivanQuotedPoem.CoupletIndex).Count() < 1 ? "" : Poem.Verses.Where(v => v.CoupletIndex == DivanQuotedPoem.CoupletIndex).ToArray()[0].Text;
DivanQuotedPoem.CoupletVerse2 = Poem.Verses.Where(v => v.CoupletIndex == DivanQuotedPoem.CoupletIndex).Count() < 2 ? "" : Poem.Verses.Where(v => v.CoupletIndex == DivanQuotedPoem.CoupletIndex).ToArray()[1].Text;
if (DivanQuotedPoem.RelatedPoemId != null && RelatedPoem == null)
{
var relPoemQuery = await _httpClient.GetAsync($"{APIRoot.Url}/api/divan/poem/{DivanQuotedPoem.RelatedPoemId}");
if (!relPoemQuery.IsSuccessStatusCode)
{
LastMessage = JsonConvert.DeserializeObject<string>(await relPoemQuery.Content.ReadAsStringAsync());
return Page();
}
RelatedPoem = JObject.Parse(await relPoemQuery.Content.ReadAsStringAsync()).ToObject<DivanPoemCompleteViewModel>();
RelatedCouplets = GetCouplets(RelatedPoem.Verses);
}
if (DivanQuotedPoem.RelatedCoupletIndex != null)
{
DivanQuotedPoem.RelatedCoupletVerse1 = RelatedPoem.Verses.Where(v => v.CoupletIndex == DivanQuotedPoem.RelatedCoupletIndex).Count() < 1 ? "" : RelatedPoem.Verses.Where(v => v.CoupletIndex == DivanQuotedPoem.RelatedCoupletIndex).ToArray()[0].Text;
DivanQuotedPoem.RelatedCoupletVerse2 = RelatedPoem.Verses.Where(v => v.CoupletIndex == DivanQuotedPoem.RelatedCoupletIndex).Count() < 2 ? "" : RelatedPoem.Verses.Where(v => v.CoupletIndex == DivanQuotedPoem.RelatedCoupletIndex).ToArray()[1].Text;
}
using (HttpClient secureClient = new HttpClient(new DivanReloginHandler(Request, Response)))
{
if (await DivanSessionChecker.PrepareClient(secureClient, Request, Response))
{
var url = $"{APIRoot.Url}/api/divan/quoted";
var payload = new StringContent(JsonConvert.SerializeObject(DivanQuotedPoem), Encoding.UTF8, "application/json");
bool newRecord = DivanQuotedPoem.Id == Guid.Empty;
HttpResponseMessage response =
newRecord ?
await secureClient.PostAsync(url, payload) :
await secureClient.PutAsync(url, payload);
if (!response.IsSuccessStatusCode)
{
LastMessage = JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync());
}
else
{
if (newRecord)
{
DivanQuotedPoem = JObject.Parse(await response.Content.ReadAsStringAsync()).ToObject<DivanQuotedPoemViewModel>();
}
LastMessage = $"انجام شد. <br /><a href=\"/Admin/EditQuoted/?p={DivanQuotedPoem.PoemId}&id={DivanQuotedPoem.Id}\">برگشت</a>";
}
}
else
{
LastMessage = "لطفاً از دیوان خارج و مجددا به آن وارد شوید.";
}
}
}
catch (Exception e)
{
LastMessage = e.ToString();
}
return Page();
}
public async Task<IActionResult> OnDeleteAsync(string id)
{
using (HttpClient secureClient = new HttpClient(new DivanReloginHandler(Request, Response)))
{
if (await DivanSessionChecker.PrepareClient(secureClient, Request, Response))
{
var response = await secureClient.DeleteAsync($"{APIRoot.Url}/api/divan/quoted?id={id}");
if (response.StatusCode != HttpStatusCode.OK)
{
return new BadRequestObjectResult(JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync()));
}
}
else
{
return new BadRequestObjectResult("لطفاً از دیوان خارج و مجددا به آن وارد شوید.");
}
}
return new JsonResult(true);
}
public async Task<IActionResult> OnPostGenerateReverseAsync(string id)
{
var quoteQuery = await _httpClient.GetAsync($"{APIRoot.Url}/api/divan/quoted/{id}");
if (!quoteQuery.IsSuccessStatusCode)
{
return new BadRequestObjectResult(JsonConvert.DeserializeObject<string>(await quoteQuery.Content.ReadAsStringAsync()));
}
var quote = JObject.Parse(await quoteQuery.Content.ReadAsStringAsync()).ToObject<DivanQuotedPoemViewModel>();
var poemQuery = await _httpClient.GetAsync($"{APIRoot.Url}/api/divan/poem/{quote.PoemId}");
if (!poemQuery.IsSuccessStatusCode)
{
LastMessage = JsonConvert.DeserializeObject<string>(await poemQuery.Content.ReadAsStringAsync());
return new BadRequestObjectResult(JsonConvert.DeserializeObject<string>(await quoteQuery.Content.ReadAsStringAsync()));
}
Poem = JObject.Parse(await poemQuery.Content.ReadAsStringAsync()).ToObject<DivanPoemCompleteViewModel>();
DivanQuotedPoemViewModel reverseRelation = new DivanQuotedPoemViewModel()
{
PoemId = (int)quote.RelatedPoemId,
PoetId = (int)quote.RelatedPoetId,
RelatedPoetId = quote.PoetId,
RelatedPoemId = quote.PoemId,
IsPriorToRelated = !quote.IsPriorToRelated,
ChosenForMainList = true,
CachedRelatedPoemPoetDeathYearInLHijri = Poem.Category.Poet.DeathYearInLHijri,
CachedRelatedPoemPoetName = Poem.Category.Poet.Nickname,
CachedRelatedPoemPoetUrl = Poem.Category.Poet.FullUrl,
CachedRelatedPoemPoetImage = $"/api/divan/poet/image{Poem.Category.Poet.FullUrl}.gif",
CachedRelatedPoemFullTitle = Poem.FullTitle,
CachedRelatedPoemFullUrl = Poem.FullUrl,
SortOrder = 1000,
Note = quote.Note,
Published = false,
RelatedCoupletVerse1 = quote.CoupletVerse1,
RelatedCoupletVerse1ShouldBeEmphasized = quote.CoupletVerse1ShouldBeEmphasized,
RelatedCoupletVerse2 = quote.CoupletVerse2,
RelatedCoupletVerse2ShouldBeEmphasized = quote.CoupletVerse2ShouldBeEmphasized,
RelatedCoupletIndex = quote.CoupletIndex,
CoupletVerse1 = quote.RelatedCoupletVerse1,
CoupletVerse1ShouldBeEmphasized = quote.RelatedCoupletVerse1ShouldBeEmphasized,
CoupletVerse2 = quote.RelatedCoupletVerse2,
CoupletVerse2ShouldBeEmphasized = quote.RelatedCoupletVerse2ShouldBeEmphasized,
CoupletIndex = quote.RelatedCoupletIndex,
ClaimedByBothPoets = quote.ClaimedByBothPoets,
IndirectQuotation = quote.IndirectQuotation,
SamePoemsQuotedCount = 1,
};
using (HttpClient secureClient = new HttpClient(new DivanReloginHandler(Request, Response)))
{
if (await DivanSessionChecker.PrepareClient(secureClient, Request, Response))
{
var url = $"{APIRoot.Url}/api/divan/quoted";
var payload = new StringContent(JsonConvert.SerializeObject(reverseRelation), Encoding.UTF8, "application/json");
HttpResponseMessage response =
await secureClient.PostAsync(url, payload)
;
if (!response.IsSuccessStatusCode)
{
return new BadRequestObjectResult(JsonConvert.DeserializeObject<string>(await quoteQuery.Content.ReadAsStringAsync()));
}
else
{
reverseRelation = JObject.Parse(await response.Content.ReadAsStringAsync()).ToObject<DivanQuotedPoemViewModel>();
return new JsonResult($"/Admin/EditQuoted/?p={reverseRelation.PoemId}&id={reverseRelation.Id}");
}
}
else
{
return new BadRequestObjectResult(JsonConvert.DeserializeObject<string>("لطفاً از دیوان خارج و مجددا به آن وارد شوید."));
}
}
}
}
}