divan/DivanRazor/Areas/Admin/Pages/PoetDel.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

108 lines
3.2 KiB
C#

using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using DivanRazor.Utils;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Caching.Memory;
using Newtonsoft.Json;
using RMuseum.Models.Divan.ViewModels;
namespace DivanRazor.Areas.Admin.Pages
{
public class PoetDelModel : PageModel
{
/// <summary>
/// HttpClient instance
/// </summary>
private readonly HttpClient _httpClient;
/// <summary>
/// memory cache
/// </summary>
private readonly IMemoryCache _memoryCache;
/// <summary>
/// constructor
/// </summary>
/// <param name="httpClient"></param>
/// <param name="memoryCache"></param>
public PoetDelModel(HttpClient httpClient, IMemoryCache memoryCache)
{
_httpClient = httpClient;
_memoryCache = memoryCache;
}
/// <summary>
/// last result
/// </summary>
public string LastResult { get; set; }
public DivanPoetViewModel Poet { get; set; }
private async Task<bool> PreparePoet()
{
var response = await _httpClient.GetAsync($"{APIRoot.Url}/api/divan/poet/{Request.Query["id"]}");
if (!response.IsSuccessStatusCode)
{
LastResult = JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync());
return false;
}
var poet = JsonConvert.DeserializeObject<DivanPoetCompleteViewModel>(await response.Content.ReadAsStringAsync());
Poet = poet.Poet;
return true;
}
public async Task<IActionResult> OnGetAsync()
{
if (string.IsNullOrEmpty(Request.Cookies["Token"]))
return Redirect("/");
LastResult = "";
await PreparePoet();
return Page();
}
public async Task<IActionResult> OnPostAsync()
{
LastResult = "";
using (HttpClient secureClient = new HttpClient(new DivanReloginHandler(Request, Response)))
{
await DivanSessionChecker.PrepareClient(secureClient, Request, Response);
HttpResponseMessage response = await secureClient.DeleteAsync($"{APIRoot.Url}/api/divan/poet/{Request.Query["id"]}");
if (!response.IsSuccessStatusCode)
{
LastResult = JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync());
return Page();
}
var cacheKey1 = $"/api/divan/poets";
if (_memoryCache.TryGetValue(cacheKey1, out List<DivanPoetViewModel> poets))
{
_memoryCache.Remove(cacheKey1);
}
var cacheKey2 = $"/api/divan/poet/{Request.Query["id"]}";
if (_memoryCache.TryGetValue(cacheKey2, out DivanPoetCompleteViewModel poet))
{
_memoryCache.Remove(cacheKey2);
}
LastResult = "عملیات حذف سخنور شروع شد.";
return Page();
}
}
}
}