divan/DiwanRazor/Areas/User/Pages/SuggestedPoetSpecLines.cshtml.cs
Anas Rashid e8f2306422 Rename Ganjoor -> Diwan throughout the code
Projects (GanjooRazor -> DiwanRazor, GanjoorService.sln -> DiwanService.sln), files,
folders, classes, namespaces, settings, API routes (/api/diwan) and DB tables (Diwan*).
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

177 lines
7.2 KiB
C#

using DiwanRazor.Utils;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Newtonsoft.Json;
using RMuseum.Models.Diwan.ViewModels;
using RSecurityBackend.Models.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace DiwanRazor.Areas.User.Pages
{
public class SuggestedPoetSpecLinesModel : PageModel
{
/// <summary>
/// Last Error
/// </summary>
public string LastError { get; set; }
/// <summary>
/// api model
/// </summary>
[BindProperty]
public DiwanPoetSuggestedSpecLineViewModel Suggestion { get; set; }
/// <summary>
/// poet
/// </summary>
public DiwanPoetViewModel Poet { get; set; }
/// <summary>
/// skip
/// </summary>
public int Skip { get; set; }
/// <summary>
/// total count
/// </summary>
public int TotalCount { get; set; }
public async Task<IActionResult> OnGetAsync()
{
if (string.IsNullOrEmpty(Request.Cookies["Token"]))
return Redirect("/");
LastError = "";
TotalCount = 0;
Skip = string.IsNullOrEmpty(Request.Query["skip"]) ? 0 : int.Parse(Request.Query["skip"]);
using (HttpClient secureClient = new HttpClient(new DiwanReloginHandler(Request, Response)))
{
if (await DiwanSessionChecker.PrepareClient(secureClient, Request, Response))
{
if (!string.IsNullOrEmpty(Request.Query["id"]))
{
//modify mode:
int id = int.Parse(Request.Query["id"]);
var suggestionResponse = await secureClient.GetAsync($"{APIRoot.Url}/api/poetspecs/{id}");
if (!suggestionResponse.IsSuccessStatusCode)
{
if (suggestionResponse.StatusCode == System.Net.HttpStatusCode.NotFound)
{
LastError = "پیشنهادی وجود ندارد.";
}
else
{
LastError = JsonConvert.DeserializeObject<string>(await suggestionResponse.Content.ReadAsStringAsync());
}
return Page();
}
else
{
Suggestion = JsonConvert.DeserializeObject<DiwanPoetSuggestedSpecLineViewModel>(await suggestionResponse.Content.ReadAsStringAsync());
}
}
else
{
var suggestionResponse = await secureClient.GetAsync($"{APIRoot.Url}/api/poetspecs/unpublished/next?skip={Skip}");
if (!suggestionResponse.IsSuccessStatusCode)
{
if (suggestionResponse.StatusCode == System.Net.HttpStatusCode.NotFound)
{
LastError = "پیشنهادی وجود ندارد.";
}
else
{
LastError = JsonConvert.DeserializeObject<string>(await suggestionResponse.Content.ReadAsStringAsync());
}
return Page();
}
else
{
string paginnationMetadata = suggestionResponse.Headers.GetValues("paging-headers").FirstOrDefault();
if (!string.IsNullOrEmpty(paginnationMetadata))
{
TotalCount = JsonConvert.DeserializeObject<PaginationMetadata>(paginnationMetadata).totalCount;
}
Suggestion = JsonConvert.DeserializeObject<DiwanPoetSuggestedSpecLineViewModel>(await suggestionResponse.Content.ReadAsStringAsync());
}
}
if (Suggestion != null)
{
var response = await secureClient.GetAsync($"{APIRoot.Url}/api/diwan/poet/{Suggestion.PoetId}");
if (!response.IsSuccessStatusCode)
{
LastError = JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync());
return Page();
}
var poet = JsonConvert.DeserializeObject<DiwanPoetCompleteViewModel>(await response.Content.ReadAsStringAsync());
Poet = poet.Poet;
}
}
else
{
LastError = "لطفاً از دیوان خارج و مجددا به آن وارد شوید.";
}
}
return Page();
}
public async Task<IActionResult> OnPostAsync()
{
Skip = string.IsNullOrEmpty(Request.Query["skip"]) ? 0 : int.Parse(Request.Query["skip"]);
if (Request.Form["next"].Count == 1)
{
return Redirect($"/User/SuggestedPoetSpecLines/?skip={Skip + 1}");
}
Suggestion.Published = Request.Form["approve"].Count == 1;
using (HttpClient secureClient = new HttpClient(new DiwanReloginHandler(Request, Response)))
{
if (await DiwanSessionChecker.PrepareClient(secureClient, Request, Response))
{
if(Suggestion.Published)
{
var putResponse = await secureClient.PutAsync($"{APIRoot.Url}/api/poetspecs", new StringContent(JsonConvert.SerializeObject(Suggestion), Encoding.UTF8, "application/json"));
if (!putResponse.IsSuccessStatusCode)
{
LastError = JsonConvert.DeserializeObject<string>(await putResponse.Content.ReadAsStringAsync());
}
}
else
{
var rejectionResponse = await secureClient.PutAsync($"{APIRoot.Url}/api/poetspecs/reject/{Suggestion.Id}", new StringContent(JsonConvert.SerializeObject(Suggestion.RejectionCause), Encoding.UTF8, "application/json"));
if (!rejectionResponse.IsSuccessStatusCode)
{
LastError = JsonConvert.DeserializeObject<string>(await rejectionResponse.Content.ReadAsStringAsync());
}
}
}
else
{
LastError = "لطفاً از دیوان خارج و مجددا به آن وارد شوید.";
}
}
if (!string.IsNullOrEmpty(LastError))
{
return Page();
}
return Redirect($"/User/SuggestedPoetSpecLines/?skip={Skip}");
}
}
}