From ada147f12efe6240e6b00db25621aae9f9877ddd Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Mon, 10 Jan 2022 19:32:02 +0330 Subject: [PATCH] docs: #272 user page added --- GanjooRazor/Areas/User/Pages/History.cshtml | 105 ++++++++++++ .../Areas/User/Pages/History.cshtml.cs | 152 ++++++++++++++++++ GanjooRazor/Pages/Shared/_Layout.cshtml | 4 + GanjooRazor/appsettings.json | 4 +- .../UserVisitsTrackingController.cs | 6 +- RMuseum/Startup.cs | 4 + 6 files changed, 272 insertions(+), 3 deletions(-) create mode 100644 GanjooRazor/Areas/User/Pages/History.cshtml create mode 100644 GanjooRazor/Areas/User/Pages/History.cshtml.cs diff --git a/GanjooRazor/Areas/User/Pages/History.cshtml b/GanjooRazor/Areas/User/Pages/History.cshtml new file mode 100644 index 00000000..bd76a4a9 --- /dev/null +++ b/GanjooRazor/Areas/User/Pages/History.cshtml @@ -0,0 +1,105 @@ +@page +@model GanjooRazor.Areas.User.Pages.HistoryModel +@using DNTPersianUtils.Core +@{ + Layout = "_Layout"; + ViewData["Title"] = "ادامهٔ مطالعه"; + await GanjooRazor.Utils.GanjoorSessionChecker.ApplyPermissionsToViewData(Request, Response, ViewData); + string color = "gray"; +} +

@ViewData["Title"]

+@if (Model.LastError != "") +{ +

@Model.LastError

+} +else +{ + +

+ @foreach (var paginationLink in Model.PaginationLinks) + { + if (!string.IsNullOrEmpty(paginationLink.Url)) + { + @paginationLink.Name + } + else + { + @paginationLink.Name + } + } +

+ + @foreach (var historyItem in Model.HistoryItems) + { + if (color == "gray") + color = "white"; + else + color = "gray"; + +
+
+

+ از @historyItem.PoemFullTitle در @historyItem.DateTime.ToFriendlyPersianDateTextify() +

+ + @historyItem.PoetName + +
+

+ @historyItem.VerseText +

+ + @if (!string.IsNullOrEmpty(historyItem.Verse2Text)) + { +

+ @historyItem.Verse2Text +

+ } + +
+
+

+ 🗑 +

+ +
+ + +
+ } + +

+ @foreach (var paginationLink in Model.PaginationLinks) + { + if (!string.IsNullOrEmpty(paginationLink.Url)) + { + @paginationLink.Name + } + else + { + @paginationLink.Name + } + } +

+ + +} diff --git a/GanjooRazor/Areas/User/Pages/History.cshtml.cs b/GanjooRazor/Areas/User/Pages/History.cshtml.cs new file mode 100644 index 00000000..b4109360 --- /dev/null +++ b/GanjooRazor/Areas/User/Pages/History.cshtml.cs @@ -0,0 +1,152 @@ +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; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Threading.Tasks; + +namespace GanjooRazor.Areas.User.Pages +{ + [IgnoreAntiforgeryToken(Order = 1001)] + public class HistoryModel : PageModel + { + + /// + /// Last Error + /// + public string LastError { get; set; } + + /// + /// pagination links + /// + public List PaginationLinks { get; set; } + + public List HistoryItems { get; set; } + public async Task OnGetAsync() + { + LastError = ""; + using (HttpClient secureClient = new HttpClient()) + if (await GanjoorSessionChecker.PrepareClient(secureClient, Request, Response)) + { + { + int pageNumber = 1; + if (!string.IsNullOrEmpty(Request.Query["page"])) + { + pageNumber = int.Parse(Request.Query["page"]); + } + var response = await secureClient.GetAsync($"{APIRoot.Url}/api/tracking/?PageNumber={pageNumber}&PageSize=20"); + if (!response.IsSuccessStatusCode) + { + LastError = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); + return Page(); + } + + HistoryItems = JArray.Parse(await response.Content.ReadAsStringAsync()).ToObject>(); + + string paginnationMetadata = response.Headers.GetValues("paging-headers").FirstOrDefault(); + if (!string.IsNullOrEmpty(paginnationMetadata)) + { + PaginationMetadata paginationMetadata = JsonConvert.DeserializeObject(paginnationMetadata); + PaginationLinks = new List(); + if (paginationMetadata.totalPages > 1) + { + if (paginationMetadata.currentPage > 3) + { + PaginationLinks.Add + ( + new NameIdUrlImage() + { + Name = "صفحهٔ اول", + Url = "/User/History/?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/History/?page={i}" + } + ); + } + } + } + if (paginationMetadata.totalPages > (paginationMetadata.currentPage + 2)) + { + + PaginationLinks.Add + ( + new NameIdUrlImage() + { + Name = "... ", + } + ); + + PaginationLinks.Add + ( + new NameIdUrlImage() + { + Name = "صفحهٔ آخر", + Url = $"/User/History/?page={paginationMetadata.totalPages}" + } + ); + } + } + } + + } + } + else + { + LastError = "لطفا از گنجور خارج و مجددا به آن وارد شوید."; + } + return Page(); + } + + public async Task OnDeleteHistoryItem(Guid id) + { + using (HttpClient secureClient = new HttpClient()) + { + if (await GanjoorSessionChecker.PrepareClient(secureClient, Request, Response)) + { + var response = await secureClient.DeleteAsync($"{APIRoot.Url}/api/tracking/{id}"); + + if (response.StatusCode != HttpStatusCode.OK) + { + return Redirect($"/login?redirect={Request.Path}&error={JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync())}"); + } + + } + } + return new JsonResult(true); + } + } +} diff --git a/GanjooRazor/Pages/Shared/_Layout.cshtml b/GanjooRazor/Pages/Shared/_Layout.cshtml index 24197e9a..6961063e 100644 --- a/GanjooRazor/Pages/Shared/_Layout.cshtml +++ b/GanjooRazor/Pages/Shared/_Layout.cshtml @@ -360,6 +360,10 @@ خوانش‌های نشان شده + + @if (ViewData.ContainsKey("ganjoor-songrevu")) {