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>
108 lines
3.2 KiB
C#
108 lines
3.2 KiB
C#
using System.Collections.Generic;
|
|
using System.Net.Http;
|
|
using System.Threading.Tasks;
|
|
using DiwanRazor.Utils;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
using Newtonsoft.Json;
|
|
using RMuseum.Models.Diwan.ViewModels;
|
|
|
|
namespace DiwanRazor.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 DiwanPoetViewModel Poet { get; set; }
|
|
|
|
private async Task<bool> PreparePoet()
|
|
{
|
|
var response = await _httpClient.GetAsync($"{APIRoot.Url}/api/diwan/poet/{Request.Query["id"]}");
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
LastResult = JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync());
|
|
return false;
|
|
}
|
|
|
|
var poet = JsonConvert.DeserializeObject<DiwanPoetCompleteViewModel>(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 DiwanReloginHandler(Request, Response)))
|
|
{
|
|
await DiwanSessionChecker.PrepareClient(secureClient, Request, Response);
|
|
|
|
HttpResponseMessage response = await secureClient.DeleteAsync($"{APIRoot.Url}/api/diwan/poet/{Request.Query["id"]}");
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
LastResult = JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync());
|
|
return Page();
|
|
}
|
|
|
|
var cacheKey1 = $"/api/diwan/poets";
|
|
if (_memoryCache.TryGetValue(cacheKey1, out List<DiwanPoetViewModel> poets))
|
|
{
|
|
_memoryCache.Remove(cacheKey1);
|
|
}
|
|
|
|
var cacheKey2 = $"/api/diwan/poet/{Request.Query["id"]}";
|
|
if (_memoryCache.TryGetValue(cacheKey2, out DiwanPoetCompleteViewModel poet))
|
|
{
|
|
_memoryCache.Remove(cacheKey2);
|
|
}
|
|
|
|
LastResult = "عملیات حذف سخنور شروع شد.";
|
|
|
|
return Page();
|
|
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|