divan/DivanRazor/Areas/User/Pages/SuggestedPoetPhotos.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

177 lines
7.1 KiB
C#

using DivanRazor.Utils;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Newtonsoft.Json;
using RMuseum.Models.Divan.ViewModels;
using RSecurityBackend.Models.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace DivanRazor.Areas.User.Pages
{
public class SuggestedPoetPhotosModel : PageModel
{
/// <summary>
/// Last Error
/// </summary>
public string LastError { get; set; }
/// <summary>
/// api model
/// </summary>
[BindProperty]
public DivanPoetSuggestedPictureViewModel Suggestion { get; set; }
/// <summary>
/// poet
/// </summary>
public DivanPoetViewModel 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 DivanReloginHandler(Request, Response)))
{
if (await DivanSessionChecker.PrepareClient(secureClient, Request, Response))
{
if(!string.IsNullOrEmpty(Request.Query["id"]))
{
//modify mode:
int id = int.Parse(Request.Query["id"]);
var responsePhoto = await secureClient.GetAsync($"{APIRoot.Url}/api/poetphotos/{id}");
if (!responsePhoto.IsSuccessStatusCode)
{
LastError = JsonConvert.DeserializeObject<string>(await responsePhoto.Content.ReadAsStringAsync());
return Page();
}
Suggestion = JsonConvert.DeserializeObject<DivanPoetSuggestedPictureViewModel>(await responsePhoto.Content.ReadAsStringAsync());
if (Suggestion == null)
{
LastError = "تصویری با شناسهٔ ارسالی یافت نشد.";
return Page();
}
}
else
{
//moderate mode
var suggestionResponse = await secureClient.GetAsync($"{APIRoot.Url}/api/poetphotos/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<DivanPoetSuggestedPictureViewModel>(await suggestionResponse.Content.ReadAsStringAsync());
}
}
if (Suggestion != null)
{
Suggestion.ImageUrl = $"{APIRoot.InternetUrl}/{Suggestion.ImageUrl}";
var response = await secureClient.GetAsync($"{APIRoot.Url}/api/divan/poet/{Suggestion.PoetId}");
if (!response.IsSuccessStatusCode)
{
LastError = JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync());
return Page();
}
var poet = JsonConvert.DeserializeObject<DivanPoetCompleteViewModel>(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/SuggestedPoetPhotos/?skip={Skip + 1}");
}
Suggestion.Published = Request.Form["approve"].Count == 1;
using (HttpClient secureClient = new HttpClient(new DivanReloginHandler(Request, Response)))
{
if (await DivanSessionChecker.PrepareClient(secureClient, Request, Response))
{
if (Suggestion.Published)
{
var putResponse = await secureClient.PutAsync($"{APIRoot.Url}/api/poetphotos", new StringContent(JsonConvert.SerializeObject(Suggestion), Encoding.UTF8, "application/json"));
if (!putResponse.IsSuccessStatusCode)
{
LastError = JsonConvert.DeserializeObject<string>(await putResponse.Content.ReadAsStringAsync());
}
}
else
{
var rejectResponse = await secureClient.PutAsync($"{APIRoot.Url}/api/poetphotos/reject/{Suggestion.Id}", new StringContent(JsonConvert.SerializeObject(Suggestion.RejectionCause), Encoding.UTF8, "application/json"));
if (!rejectResponse.IsSuccessStatusCode)
{
LastError = JsonConvert.DeserializeObject<string>(await rejectResponse.Content.ReadAsStringAsync());
}
}
}
else
{
LastError = "لطفاً از دیوان خارج و مجددا به آن وارد شوید.";
}
}
if (!string.IsNullOrEmpty(LastError))
{
return Page();
}
return Redirect($"/User/SuggestedPoetPhotos/?skip={Skip}");
}
}
}