divan/DiwanRazor/Areas/Admin/Pages/Donations.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

195 lines
7.8 KiB
C#

using System;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using DNTPersianUtils.Core;
using DiwanRazor.Utils;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Newtonsoft.Json;
using RMuseum.Models.Accounting.ViewModels;
using RMuseum.Utils;
namespace DiwanRazor.Areas.Admin.Pages
{
public class DonationsModel : PageModel
{
/// <summary>
/// HttpClient instance
/// </summary>
private readonly HttpClient _httpClient;
/// <summary>
/// constructor
/// </summary>
/// <param name="httpClient"></param>
public DonationsModel(HttpClient httpClient)
{
_httpClient = httpClient;
}
/// <summary>
/// last message
/// </summary>
public string LastMessage { get; set; }
/// <summary>
/// email content
/// </summary>
public string EmailContent { get; set; }
[BindProperty]
public DiwanDonationViewModel Donation { get; set; }
/// <summary>
/// donations
/// </summary>
public DiwanDonationViewModel[] Donations { get; set; }
/// <summary>
/// show account info
/// </summary>
public string ShowAccountInfo { get; set; }
private async Task ReadDonations()
{
var response = await _httpClient.GetAsync($"{APIRoot.Url}/api/donations");
if (!response.IsSuccessStatusCode)
{
LastMessage = JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync());
return;
}
Donations = JsonConvert.DeserializeObject<DiwanDonationViewModel[]>(await response.Content.ReadAsStringAsync());
using (HttpClient secureClient = new HttpClient(new DiwanReloginHandler(Request, Response)))
{
if (await DiwanSessionChecker.PrepareClient(secureClient, Request, Response))
{
HttpResponseMessage resAccountInfo = await secureClient.GetAsync($"{APIRoot.Url}/api/donations/accountinfo/visible");
if (!resAccountInfo.IsSuccessStatusCode)
{
LastMessage = JsonConvert.DeserializeObject<string>(await resAccountInfo.Content.ReadAsStringAsync());
return;
}
ShowAccountInfo = JsonConvert.DeserializeObject<bool>(await resAccountInfo.Content.ReadAsStringAsync()) ? "نمایش حساب فعال است." : "نمایش حساب غیرفعال است.";
}
}
}
public async Task<IActionResult> OnGetAsync()
{
if (string.IsNullOrEmpty(Request.Cookies["Token"]))
return Redirect("/");
LastMessage = "";
Donation = new DiwanDonationViewModel()
{
RecordDate = DateTime.Now.Date,
Unit = "تومان"
};
await ReadDonations();
if(!string.IsNullOrEmpty(Request.Query["id"]))
{
var donation = Donations.Where(d => d.Id == int.Parse(Request.Query["id"])).Single();
if(donation != null)
{
int rowNumber = Donations.Length - Array.IndexOf(Donations, donation);
string donorName = DiwanHtmlTools.StripHtmlTags(donation.DonorName);
if(donorName.StartsWith("آقای"))
{
donorName = $"جناب {donorName}";
}
if (donorName.StartsWith("خانم"))
{
donorName = $"سرکار {donorName}";
}
EmailContent = $"با درود به {donorName} عزیز و سپاس از بزرگواری شما{Environment.NewLine}" +
$"کمک دریافتی به شماره ردیف {rowNumber.ToPersianNumbers()} در این نشانی ثبت شد:{Environment.NewLine}" +
$"https://ganjoor.net/donate{Environment.NewLine}" +
$"نحوهٔ هزینه شدن آن متعاقباً در همان ردیف مستند خواهد شد.{Environment.NewLine}" +
$"سرافراز باشید.";
}
}
return Page();
}
public async Task<IActionResult> OnPostAsync(DiwanDonationViewModel Donation)
{
LastMessage = "";
using (HttpClient secureClient = new HttpClient(new DiwanReloginHandler(Request, Response)))
{
if (await DiwanSessionChecker.PrepareClient(secureClient, Request, Response))
{
Donation.ImportedRecord = false;
HttpResponseMessage response = await secureClient.PostAsync($"{APIRoot.Url}/api/donations", new StringContent(JsonConvert.SerializeObject(Donation), Encoding.UTF8, "application/json"));
if (!response.IsSuccessStatusCode)
{
LastMessage = JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync());
}
else
{
await ReadDonations();
EmailContent = $"با درود و سپاس از بزرگواری شما{Environment.NewLine}" +
$"کمک دریافتی به شماره ردیف {Donations.Length.ToPersianNumbers()} در این نشانی ثبت شد:{Environment.NewLine}" +
$"https://ganjoor.net/donate{Environment.NewLine}" +
$"نحوهٔ هزینه شدن آن متعاقباً در همان ردیف مستند خواهد شد.{Environment.NewLine}" +
$"سرافراز باشید.";
}
}
else
{
LastMessage = "لطفاً از دیوان خارج و مجددا به آن وارد شوید.";
}
}
return Page();
}
public async Task<IActionResult> OnPostRebuildPageAsync()
{
using (HttpClient secureClient = new HttpClient(new DiwanReloginHandler(Request, Response)))
{
if (await DiwanSessionChecker.PrepareClient(secureClient, Request, Response))
{
HttpResponseMessage response = await secureClient.PutAsync($"{APIRoot.Url}/api/donations/page", null);
if(!response.IsSuccessStatusCode)
{
return new BadRequestObjectResult(JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync()));
}
return new OkObjectResult(true);
}
}
return new OkObjectResult(false);
}
public async Task<IActionResult> OnDeleteAsync(int id)
{
using (HttpClient secureClient = new HttpClient(new DiwanReloginHandler(Request, Response)))
{
if (await DiwanSessionChecker.PrepareClient(secureClient, Request, Response))
{
var response = await secureClient.DeleteAsync($"{APIRoot.Url}/api/donations/{id}");
if (!response.IsSuccessStatusCode)
{
return new BadRequestObjectResult(JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync()));
}
return new OkObjectResult(true);
}
}
return new OkObjectResult(false);
}
}
}