Projects (GanjooRazor -> DivanRazor, GanjoorService.sln -> DivanService.sln), files, folders, classes, namespaces, settings, API routes (/api/divan) and DB tables (Divan*). External addresses (ganjoor.net, github.com/ganjoor) unchanged. Migrations renamed consistently (no pending model changes); requires a fresh database. Upstream RUNNING_LOCALLY/SEMANTIC_SEARCH docs archived unmodified under docs/. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
158 lines
6.6 KiB
C#
158 lines
6.6 KiB
C#
using DivanRazor.Utils;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.OutputCaching;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using RMuseum.Models.Divan.ViewModels;
|
|
using RMuseum.Models.Generic.ViewModels;
|
|
using RSecurityBackend.Models.Generic;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DivanRazor.Pages
|
|
{
|
|
[OutputCache(PolicyName = "DivanPublicPage")]
|
|
public class ContribsModel : LoginPartialEnabledPageModel
|
|
{
|
|
private readonly PoetCacheService _poetCache;
|
|
|
|
public List<DivanPoetViewModel> Poets { get; set; }
|
|
public int PoetId { get; set; }
|
|
public DivanPoetCompleteViewModel Poet { get; set; }
|
|
public string LastError { get; set; }
|
|
|
|
public async Task<IActionResult> OnGetAsync()
|
|
{
|
|
var maintenanceResult = TryGetMaintenanceModeResult();
|
|
if (maintenanceResult != null)
|
|
{
|
|
return maintenanceResult;
|
|
}
|
|
|
|
InitializeCommonPageState();
|
|
PoetId = string.IsNullOrEmpty(Request.Query["a"]) ? 0 : int.Parse(Request.Query["a"]);
|
|
|
|
var (poetsOk, poets, poetsError) = await _poetCache.GetPoetsAsync(EditorCacheBypass);
|
|
if (!poetsOk)
|
|
{
|
|
LastError = poetsError;
|
|
return Page();
|
|
}
|
|
Poets = poets;
|
|
|
|
if (PoetId != 0)
|
|
{
|
|
var (poetOk, poet, poetError) = await _poetCache.GetPoetAsync(PoetId, EditorCacheBypass);
|
|
if (!poetOk)
|
|
{
|
|
LastError = poetError;
|
|
return Page();
|
|
}
|
|
Poet = poet;
|
|
}
|
|
|
|
return Page();
|
|
}
|
|
|
|
public async Task<IActionResult> OnGetPoetInformationAsync(int id)
|
|
{
|
|
if (id == 0)
|
|
{
|
|
return new OkObjectResult(null);
|
|
}
|
|
var (success, poet, error) = await _poetCache.GetPoetAsync(id, EditorCacheBypass);
|
|
if (!success)
|
|
{
|
|
return BadRequest(error);
|
|
}
|
|
return new OkObjectResult(poet);
|
|
}
|
|
|
|
public async Task<IActionResult> OnGetGroupedByDateAsync(string dataType)
|
|
{
|
|
var responseDays = await _httpClient.GetAsync($"{APIRoot.Url}/api/contributions/{dataType}/daily?PageNumber=1&PageSize=30");
|
|
if (!responseDays.IsSuccessStatusCode)
|
|
{
|
|
return new BadRequestObjectResult(await ReadErrorMessageAsync(responseDays));
|
|
}
|
|
var days = JArray.Parse(await responseDays.Content.ReadAsStringAsync()).ToObject<List<GroupedByDateViewModel>>();
|
|
|
|
List<GroupedByUserViewModel> users = null;
|
|
PaginationMetadata usersPagination = null;
|
|
if (dataType != "users")
|
|
{
|
|
var responseUsers = await _httpClient.GetAsync($"{APIRoot.Url}/api/contributions/{dataType}/by/user?PageNumber=1&PageSize=30");
|
|
if (!responseUsers.IsSuccessStatusCode)
|
|
{
|
|
return new BadRequestObjectResult(await ReadErrorMessageAsync(responseUsers));
|
|
}
|
|
users = JArray.Parse(await responseUsers.Content.ReadAsStringAsync()).ToObject<List<GroupedByUserViewModel>>();
|
|
usersPagination = JsonConvert.DeserializeObject<PaginationMetadata>(responseUsers.Headers.GetValues("paging-headers").Single());
|
|
}
|
|
|
|
var response = await _httpClient.GetAsync($"{APIRoot.Url}/api/contributions/{dataType}/summary");
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
return new BadRequestObjectResult(await ReadErrorMessageAsync(response));
|
|
}
|
|
SummedUpViewModel summary = JsonConvert.DeserializeObject<SummedUpViewModel>(await response.Content.ReadAsStringAsync());
|
|
|
|
return Partial("~/Pages/Partials/Contribs/_GroupedByDateViewPartial.cshtml", new _GroupedByDateViewPartialModel()
|
|
{
|
|
DataType = dataType,
|
|
Days = days.ToArray(),
|
|
DaysPagination = JsonConvert.DeserializeObject<PaginationMetadata>(responseDays.Headers.GetValues("paging-headers").Single()),
|
|
Users = users?.ToArray(),
|
|
UsersPagination = usersPagination,
|
|
Summary = summary
|
|
});
|
|
}
|
|
|
|
public async Task<IActionResult> OnGetGroupedByUsersAsync(string dataType, int pageNumber)
|
|
{
|
|
var responseUsers = await _httpClient.GetAsync($"{APIRoot.Url}/api/contributions/{dataType}/by/user?PageNumber={pageNumber}&PageSize=30");
|
|
if (!responseUsers.IsSuccessStatusCode)
|
|
{
|
|
return new BadRequestObjectResult(await ReadErrorMessageAsync(responseUsers));
|
|
}
|
|
var users = JArray.Parse(await responseUsers.Content.ReadAsStringAsync()).ToObject<List<GroupedByUserViewModel>>();
|
|
var usersPagination = JsonConvert.DeserializeObject<PaginationMetadata>(responseUsers.Headers.GetValues("paging-headers").Single());
|
|
|
|
return Partial("~/Pages/Partials/Contribs/_GroupedByDateViewTablePartial.cshtml", new _GroupedByDateViewTablePartialModel()
|
|
{
|
|
DataType = dataType,
|
|
Users = users.ToArray(),
|
|
UsersPagination = usersPagination,
|
|
});
|
|
}
|
|
|
|
public async Task<IActionResult> OnGetGroupedByDayAsync(string dataType, int pageNumber)
|
|
{
|
|
var responseDays = await _httpClient.GetAsync($"{APIRoot.Url}/api/contributions/{dataType}/daily?PageNumber={pageNumber}&PageSize=30");
|
|
if (!responseDays.IsSuccessStatusCode)
|
|
{
|
|
return new BadRequestObjectResult(await ReadErrorMessageAsync(responseDays));
|
|
}
|
|
var days = JArray.Parse(await responseDays.Content.ReadAsStringAsync()).ToObject<List<GroupedByDateViewModel>>();
|
|
|
|
return Partial("~/Pages/Partials/Contribs/_GroupedByDateViewTablePartial.cshtml", new _GroupedByDateViewTablePartialModel()
|
|
{
|
|
DataType = dataType,
|
|
Days = days.ToArray(),
|
|
DaysPagination = JsonConvert.DeserializeObject<PaginationMetadata>(responseDays.Headers.GetValues("paging-headers").Single()),
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// constructor
|
|
/// </summary>
|
|
public ContribsModel(HttpClient httpClient, IConfiguration configuration, PoetCacheService poetCache) : base(httpClient, configuration)
|
|
{
|
|
_poetCache = poetCache;
|
|
}
|
|
}
|
|
}
|