#268 UI for reviewing probable metres

This commit is contained in:
Hamid Reza Mohammadi 2021-12-31 10:39:17 +03:30
parent 0530409aa5
commit 0d90b23326
3 changed files with 225 additions and 0 deletions

View File

@ -0,0 +1,73 @@
@page
@model GanjooRazor.Areas.Admin.Pages.ProbablesModel
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = "_AdminLayout";
ViewData["Title"] = "اوزان محتمل";
}
<h1>@ViewData["Title"]</h1>
@if (!string.IsNullOrEmpty(Model.LastMessage))
{
<p>@Model.LastMessage</p>
}
else
{
<form method="post">
<table>
<tr>
<td>
<select asp-for="Poem.GanjoorMetre.Rhythm" style="width: 500px">
@foreach (var rhythm in Model.Rhythms)
{
<option value="@rhythm.Rhythm">@rhythm.Rhythm</option>
}
</select>
<input type="hidden" asp-for="Poem.GanjoorMetre.Id" />
</td>
</tr>
<tr>
<td>
<button type="submit" style="width:100%;color:white;background-color:green">ذخیره</button>
</td>
</tr>
<tr>
<td>
&nbsp;
</td>
</tr>
</table>
</form>
<form method="post" action="/Admin/Probables/?Handler=Dismiss">
<table>
<tr>
<td>
<input type="hidden" asp-for="Poem.GanjoorMetre.Id" />
<button type="submit" style="width:100%;color:white;background-color:red">نادیده بگیر</button>
</td>
</tr>
<tr>
</table>
</form>
<table>
<tr>
<td>
<a href="@Model.Poem.FullUrl" target="_blank">@Model.Poem.FullTitle</a>
</td>
</tr>
@foreach(var verse in Model.Poem.Verses)
{
<tr>
<td>
@verse.Text
</td>
</tr>
}
</table>
}

View File

@ -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
{
/// <summary>
/// HttpClient instance
/// </summary>
private readonly HttpClient _httpClient;
/// <summary>
/// last message
/// </summary>
public string LastMessage { get; set; }
/// <summary>
/// rythm
/// </summary>
public List<GanjoorMetre> Rhythms { get; set; }
/// <summary>
/// constructor
/// </summary>
/// <param name="httpClient"></param>
public ProbablesModel(HttpClient httpClient)
{
_httpClient = httpClient;
}
[BindProperty]
public GanjoorPoemCompleteViewModel Poem { get; set; }
public async Task<IActionResult> OnGetAsync()
{
LastMessage = "";
var rhythmResponse = await _httpClient.GetAsync($"{APIRoot.Url}/api/ganjoor/rhythms?sortOnVerseCount=true");
if (!rhythmResponse.IsSuccessStatusCode)
{
LastMessage = JsonConvert.DeserializeObject<string>(await rhythmResponse.Content.ReadAsStringAsync());
return Page();
}
Rhythms = JsonConvert.DeserializeObject<List<GanjoorMetre>>(await rhythmResponse.Content.ReadAsStringAsync());
var response = await _httpClient.GetAsync($"{APIRoot.Url}/api/ganjoor/probablemetre/next");
if (!response.IsSuccessStatusCode)
{
LastMessage = JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync());
}
else
{
Poem = JsonConvert.DeserializeObject<GanjoorPoemCompleteViewModel>(await response.Content.ReadAsStringAsync());
if(string.IsNullOrEmpty(Poem.GanjoorMetre.Rhythm))
{
Rhythms.Add
(
new GanjoorMetre()
{
Id = 0,
Rhythm = ""
}
);
}
}
return Page();
}
public async Task<IActionResult> 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<string>(await response.Content.ReadAsStringAsync());
}
else
{
return Redirect($"/Admin/Probables");
}
}
else
{
LastMessage = "لطفا از گنجور خارج و مجددا به آن وارد شوید.";
}
}
return Page();
}
public async Task<IActionResult> 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<string>(await response.Content.ReadAsStringAsync());
}
else
{
return Redirect($"/Admin/Probables");
}
}
else
{
LastMessage = "لطفا از گنجور خارج و مجددا به آن وارد شوید.";
}
}
return Page();
}
}
}

View File

@ -435,6 +435,13 @@
</li>
</ul>
</div>
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="Admin" asp-page="Probables">اوزان محتمل</a>
</li>
</ul>
</div>
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">