232 lines
10 KiB
C#
232 lines
10 KiB
C#
using DNTPersianUtils.Core;
|
|
using GanjooRazor.Utils;
|
|
using GSpotifyProxy.Models;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using RMuseum.Models.Ganjoor.ViewModels;
|
|
using RSecurityBackend.Models.Generic;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace GanjooRazor.Areas.User.Pages
|
|
{
|
|
[IgnoreAntiforgeryToken(Order = 1001)]
|
|
public class AwaitingCommentsModel : PageModel
|
|
{
|
|
/// <summary>
|
|
/// Last Error
|
|
/// </summary>
|
|
public string LastError { get; set; }
|
|
|
|
/// <summary>
|
|
/// comments
|
|
/// </summary>
|
|
public List<GanjoorCommentFullViewModel> Comments { get; set; }
|
|
|
|
/// <summary>
|
|
/// pagination links
|
|
/// </summary>
|
|
public List<NameIdUrlImage> PaginationLinks { get; set; }
|
|
|
|
/// <summary>
|
|
/// keep comments
|
|
/// </summary>
|
|
public bool KeepFirstTimeUsersComments { get; set; }
|
|
public async Task<IActionResult> OnGetAsync()
|
|
{
|
|
LastError = "";
|
|
using (HttpClient secureClient = new HttpClient())
|
|
if (await GanjoorSessionChecker.PrepareClient(secureClient, Request, Response))
|
|
{
|
|
|
|
var responseKeepFirstTimeUsersComments = await secureClient.GetAsync($"{APIRoot.Url}/api/options/global/KeepFirstTimeUsersComments");
|
|
if (!responseKeepFirstTimeUsersComments.IsSuccessStatusCode)
|
|
{
|
|
LastError = JsonConvert.DeserializeObject<string>(await responseKeepFirstTimeUsersComments.Content.ReadAsStringAsync());
|
|
return Page();
|
|
}
|
|
|
|
KeepFirstTimeUsersComments = JsonConvert.DeserializeObject<string>(await responseKeepFirstTimeUsersComments.Content.ReadAsStringAsync()) == true.ToString();
|
|
|
|
int pageNumber = 1;
|
|
if (!string.IsNullOrEmpty(Request.Query["page"]))
|
|
{
|
|
pageNumber = int.Parse(Request.Query["page"]);
|
|
}
|
|
var response = await secureClient.GetAsync($"{APIRoot.Url}/api/ganjoor/comments/awaiting?PageNumber={pageNumber}&PageSize=20");
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
LastError = JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync());
|
|
return Page();
|
|
}
|
|
|
|
Comments = JArray.Parse(await response.Content.ReadAsStringAsync()).ToObject<List<GanjoorCommentFullViewModel>>();
|
|
|
|
string paginnationMetadata = response.Headers.GetValues("paging-headers").FirstOrDefault();
|
|
if (!string.IsNullOrEmpty(paginnationMetadata))
|
|
{
|
|
PaginationMetadata paginationMetadata = JsonConvert.DeserializeObject<PaginationMetadata>(paginnationMetadata);
|
|
PaginationLinks = new List<NameIdUrlImage>();
|
|
if (paginationMetadata.totalPages > 1)
|
|
{
|
|
if (paginationMetadata.currentPage > 3)
|
|
{
|
|
PaginationLinks.Add
|
|
(
|
|
new NameIdUrlImage()
|
|
{
|
|
Name = "صفحهٔ اول",
|
|
Url = "/User/AwaitingComments/?page=1"
|
|
}
|
|
);
|
|
}
|
|
for (int i = (paginationMetadata.currentPage - 2); i <= (paginationMetadata.currentPage + 2); i++)
|
|
{
|
|
if (i >= 1 && i <= paginationMetadata.totalPages)
|
|
{
|
|
if (i == paginationMetadata.currentPage)
|
|
{
|
|
|
|
PaginationLinks.Add
|
|
(
|
|
new NameIdUrlImage()
|
|
{
|
|
Name = i.ToPersianNumbers(),
|
|
}
|
|
);
|
|
}
|
|
else
|
|
{
|
|
|
|
PaginationLinks.Add
|
|
(
|
|
new NameIdUrlImage()
|
|
{
|
|
Name = i.ToPersianNumbers(),
|
|
Url = $"/User/AwaitingComments/?page={i}"
|
|
}
|
|
);
|
|
}
|
|
}
|
|
}
|
|
if (paginationMetadata.totalPages > (paginationMetadata.currentPage + 2))
|
|
{
|
|
|
|
PaginationLinks.Add
|
|
(
|
|
new NameIdUrlImage()
|
|
{
|
|
Name = "... ",
|
|
}
|
|
);
|
|
|
|
PaginationLinks.Add
|
|
(
|
|
new NameIdUrlImage()
|
|
{
|
|
Name = "صفحهٔ آخر",
|
|
Url = $"/User/AwaitingComments/?page={paginationMetadata.totalPages}"
|
|
}
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
LastError = "لطفا از گنجور خارج و مجددا به آن وارد شوید.";
|
|
}
|
|
return Page();
|
|
}
|
|
|
|
public async Task<IActionResult> OnDeleteCommentAsync(int id)
|
|
{
|
|
using (HttpClient secureClient = new HttpClient())
|
|
{
|
|
if (await GanjoorSessionChecker.PrepareClient(secureClient, Request, Response))
|
|
{
|
|
var response = await secureClient.DeleteAsync($"{APIRoot.Url}/api/ganjoor/comment/awaiting/delete?id={id}");
|
|
|
|
if (response.StatusCode != HttpStatusCode.OK)
|
|
{
|
|
return new BadRequestObjectResult(JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync()));
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
return new BadRequestObjectResult("لطفا از گنجور خارج و مجددا به آن وارد شوید.");
|
|
}
|
|
}
|
|
return new JsonResult(true);
|
|
}
|
|
|
|
public async Task<IActionResult> OnPutApproveAsync(int id)
|
|
{
|
|
using (HttpClient secureClient = new HttpClient())
|
|
{
|
|
if (await GanjoorSessionChecker.PrepareClient(secureClient, Request, Response))
|
|
{
|
|
var response = await secureClient.PutAsync($"{APIRoot.Url}/api/ganjoor/comment/awaiting/publish", new StringContent(JsonConvert.SerializeObject(id), Encoding.UTF8, "application/json"));
|
|
if (response.StatusCode != HttpStatusCode.OK)
|
|
{
|
|
return new BadRequestObjectResult(JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync()));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return new BadRequestObjectResult("لطفا از گنجور خارج و مجددا به آن وارد شوید.");
|
|
}
|
|
}
|
|
return new JsonResult(true);
|
|
}
|
|
|
|
public async Task<IActionResult> OnPostStopKeepingAsync()
|
|
{
|
|
using (HttpClient secureClient = new HttpClient())
|
|
{
|
|
if (await GanjoorSessionChecker.PrepareClient(secureClient, Request, Response))
|
|
{
|
|
var response = await secureClient.PutAsync($"{APIRoot.Url}/api/options/global/KeepFirstTimeUsersComments", new StringContent(JsonConvert.SerializeObject(false.ToString()), Encoding.UTF8, "application/json"));
|
|
if (response.StatusCode != HttpStatusCode.OK)
|
|
{
|
|
return new BadRequestObjectResult(JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync()));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return new BadRequestObjectResult("لطفا از گنجور خارج و مجددا به آن وارد شوید.");
|
|
}
|
|
}
|
|
return new JsonResult(true);
|
|
}
|
|
|
|
public async Task<IActionResult> OnPostStartKeepingAsync()
|
|
{
|
|
using (HttpClient secureClient = new HttpClient())
|
|
{
|
|
if (await GanjoorSessionChecker.PrepareClient(secureClient, Request, Response))
|
|
{
|
|
var response = await secureClient.PutAsync($"{APIRoot.Url}/api/options/global/KeepFirstTimeUsersComments", new StringContent(JsonConvert.SerializeObject(true.ToString()), Encoding.UTF8, "application/json"));
|
|
if (response.StatusCode != HttpStatusCode.OK)
|
|
{
|
|
return new BadRequestObjectResult(JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync()));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return new BadRequestObjectResult("لطفا از گنجور خارج و مجددا به آن وارد شوید.");
|
|
}
|
|
}
|
|
return new JsonResult(true);
|
|
}
|
|
}
|
|
}
|