using DiwanRazor.Utils;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Newtonsoft.Json;
using RMuseum.Models.Diwan.ViewModels;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace DiwanRazor.Pages
{
public class ReportCommentModel : PageModel
{
///
/// is logged on
///
public bool LoggedIn { get; set; }
///
/// Last Error
///
public string LastError { get; set; }
///
/// Post Success
///
public bool PostSuccess { get; set; }
///
/// api model
///
[BindProperty]
public DiwanPostReportCommentViewModel Report { get; set; }
public void OnGet()
{
PostSuccess = false;
LastError = "";
LoggedIn = !string.IsNullOrEmpty(Request.Cookies["Token"]);
Report = new DiwanPostReportCommentViewModel()
{
ReasonCode = "bogus",
ReasonText = "",
};
if (!string.IsNullOrEmpty(Request.Query["CommentId"]))
{
Report.CommentId = int.Parse(Request.Query["CommentId"]);
}
else
{
Report.CommentId = 0;
}
}
public async Task OnPostAsync()
{
PostSuccess = false;
LastError = "";
LoggedIn = !string.IsNullOrEmpty(Request.Cookies["Token"]);
using (HttpClient secureClient = new HttpClient(new DiwanReloginHandler(Request, Response)))
{
if (await DiwanSessionChecker.PrepareClient(secureClient, Request, Response))
{
var stringContent = new StringContent(JsonConvert.SerializeObject(Report), Encoding.UTF8, "application/json");
var methodUrl = $"{APIRoot.Url}/api/diwan/comment/report";
var response = await secureClient.PostAsync(methodUrl, stringContent);
if (!response.IsSuccessStatusCode)
{
LastError = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync());
}
else
{
PostSuccess = true;
}
}
else
{
LastError = "براہِ کرم دیوان سے خروج کر کے دوبارہ داخل ہوں.";
}
}
return Page();
}
}
}