From 0d90b23326b8d5e372061c2b3a74b4c2bbbdb0e9 Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Fri, 31 Dec 2021 10:39:17 +0330 Subject: [PATCH] #268 UI for reviewing probable metres --- .../Areas/Admin/Pages/Probables.cshtml | 73 +++++++++ .../Areas/Admin/Pages/Probables.cshtml.cs | 145 ++++++++++++++++++ GanjooRazor/Pages/Shared/_AdminLayout.cshtml | 7 + 3 files changed, 225 insertions(+) create mode 100644 GanjooRazor/Areas/Admin/Pages/Probables.cshtml create mode 100644 GanjooRazor/Areas/Admin/Pages/Probables.cshtml.cs diff --git a/GanjooRazor/Areas/Admin/Pages/Probables.cshtml b/GanjooRazor/Areas/Admin/Pages/Probables.cshtml new file mode 100644 index 00000000..316079f7 --- /dev/null +++ b/GanjooRazor/Areas/Admin/Pages/Probables.cshtml @@ -0,0 +1,73 @@ +@page +@model GanjooRazor.Areas.Admin.Pages.ProbablesModel +@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers +@{ + Layout = "_AdminLayout"; + ViewData["Title"] = "اوزان محتمل"; +} + +

@ViewData["Title"]

+@if (!string.IsNullOrEmpty(Model.LastMessage)) +{ +

@Model.LastMessage

+} +else +{ +
+ + + + + + + + + + + +
+ + +
+ +
+   +
+
+
+ + + + + +
+ + +
+
+ + + + + + @foreach(var verse in Model.Poem.Verses) + { + + + + + } +
+ @Model.Poem.FullTitle +
+ @verse.Text +
+} + + + diff --git a/GanjooRazor/Areas/Admin/Pages/Probables.cshtml.cs b/GanjooRazor/Areas/Admin/Pages/Probables.cshtml.cs new file mode 100644 index 00000000..f5a8e943 --- /dev/null +++ b/GanjooRazor/Areas/Admin/Pages/Probables.cshtml.cs @@ -0,0 +1,145 @@ +using GanjooRazor.Utils; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.RazorPages; +using Newtonsoft.Json; +using RMuseum.Models.Ganjoor; +using RMuseum.Models.Ganjoor.ViewModels; +using System.Collections.Generic; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; + +namespace GanjooRazor.Areas.Admin.Pages +{ + [IgnoreAntiforgeryToken(Order = 1001)] + public class ProbablesModel : PageModel + { + /// + /// HttpClient instance + /// + private readonly HttpClient _httpClient; + + /// + /// last message + /// + public string LastMessage { get; set; } + + /// + /// rythm + /// + public List Rhythms { get; set; } + + /// + /// constructor + /// + /// + public ProbablesModel(HttpClient httpClient) + { + _httpClient = httpClient; + } + + [BindProperty] + public GanjoorPoemCompleteViewModel Poem { get; set; } + public async Task OnGetAsync() + { + LastMessage = ""; + + var rhythmResponse = await _httpClient.GetAsync($"{APIRoot.Url}/api/ganjoor/rhythms?sortOnVerseCount=true"); + if (!rhythmResponse.IsSuccessStatusCode) + { + LastMessage = JsonConvert.DeserializeObject(await rhythmResponse.Content.ReadAsStringAsync()); + return Page(); + } + Rhythms = JsonConvert.DeserializeObject>(await rhythmResponse.Content.ReadAsStringAsync()); + + var response = await _httpClient.GetAsync($"{APIRoot.Url}/api/ganjoor/probablemetre/next"); + if (!response.IsSuccessStatusCode) + { + LastMessage = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); + } + else + { + Poem = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); + if(string.IsNullOrEmpty(Poem.GanjoorMetre.Rhythm)) + { + Rhythms.Add + ( + new GanjoorMetre() + { + Id = 0, + Rhythm = "" + } + ); + } + } + + return Page(); + } + + + public async Task OnPostAsync(GanjoorPoemCompleteViewModel Poem) + { + LastMessage = ""; + using (HttpClient secureClient = new HttpClient()) + { + if (await GanjoorSessionChecker.PrepareClient(secureClient, Request, Response)) + { + int probableRecordId = Poem.GanjoorMetre.Id; + string metre = Poem.GanjoorMetre.Rhythm; + + if(string.IsNullOrEmpty(metre)) + { + LastMessage = "وزنی انتخاب نشده."; + return Page(); + } + var response = await secureClient.PostAsync($"{APIRoot.Url}/api/ganjoor/probablemetre/save/{probableRecordId}", + new StringContent(JsonConvert.SerializeObject(metre), Encoding.UTF8, "application/json") + ); + + if (!response.IsSuccessStatusCode) + { + LastMessage = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); + } + else + { + return Redirect($"/Admin/Probables"); + } + } + else + { + LastMessage = "لطفا از گنجور خارج و مجددا به آن وارد شوید."; + } + + } + return Page(); + } + + public async Task OnPostDismissAsync(GanjoorPoemCompleteViewModel Poem) + { + LastMessage = ""; + using (HttpClient secureClient = new HttpClient()) + { + if (await GanjoorSessionChecker.PrepareClient(secureClient, Request, Response)) + { + int probableRecordId = Poem.GanjoorMetre.Id; + var response = await secureClient.DeleteAsync($"{APIRoot.Url}/api/ganjoor/probablemetre/dismiss/{probableRecordId}"); + + if (!response.IsSuccessStatusCode) + { + LastMessage = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); + } + else + { + return Redirect($"/Admin/Probables"); + } + } + else + { + LastMessage = "لطفا از گنجور خارج و مجددا به آن وارد شوید."; + } + + } + return Page(); + } + } +} diff --git a/GanjooRazor/Pages/Shared/_AdminLayout.cshtml b/GanjooRazor/Pages/Shared/_AdminLayout.cshtml index 4feeaf3b..c001ef64 100644 --- a/GanjooRazor/Pages/Shared/_AdminLayout.cshtml +++ b/GanjooRazor/Pages/Shared/_AdminLayout.cshtml @@ -435,6 +435,13 @@ +