using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.Extensions.Configuration; using Newtonsoft.Json; using RMuseum.Models.DivanAudio.ViewModels; using System.Linq; using System.Net.Http; using System.Threading.Tasks; namespace DivanRazor.Pages { public class RecitationsOrderModel : PageModel { /// /// HttpClient instance /// private readonly HttpClient _httpClient; /// /// configration file reader (appsettings.json) /// private readonly IConfiguration Configuration; /// /// constructor /// /// /// public RecitationsOrderModel(HttpClient httpClient, IConfiguration configuration) { _httpClient = httpClient; Configuration = configuration; } /// /// Last Error /// public string LastError { get; set; } public RecitationOrderingViewModel[] Scores { get; set; } public async Task OnGetAsync() { if (bool.Parse(Configuration["MaintenanceMode"])) { return StatusCode(503); } LastError = ""; if (!string.IsNullOrEmpty(Request.Query["p"])) { var poemId = int.Parse(Request.Query["p"]); var response = await _httpClient.GetAsync($"{APIRoot.Url}/api/audio/votes/{poemId}/scores"); if (!response.IsSuccessStatusCode) { LastError = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); } else { Scores = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); if(Scores.Length > 0) { response = await _httpClient.GetAsync($"{APIRoot.Url}/api/divan/poem/{poemId}/recitations"); if(!response.IsSuccessStatusCode) { LastError = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); } var recitations = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); foreach (var score in Scores) { score.Recitation = recitations.Where(r => r.Id == score.RecitationId).Single(); } } } } else { LastError = "کوئی شعر منتخب نہیں."; } return Page(); } } }