mirror of
https://github.com/JogmansonReal/Radicade.git
synced 2026-10-26 07:29:00 -07:00
Radicade Public Code
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using RadicadeServer.DB;
|
||||
using RadicadeServer.RecNet.Enums;
|
||||
using RadicadeServer.RecNet.Enums.Rooms;
|
||||
using RadicadeServer.RecNet.Rooms;
|
||||
|
||||
namespace RadicadeServer.Lists
|
||||
{
|
||||
[Route("lists/algorithmiclists")]
|
||||
public class AlgorithmicListsAPI : Controller
|
||||
{
|
||||
private const int Rooms = 1;
|
||||
|
||||
private const int RowSize = 20;
|
||||
|
||||
RecDBContext db;
|
||||
|
||||
public AlgorithmicListsAPI(RecDBContext context)
|
||||
{
|
||||
this.db = context;
|
||||
}
|
||||
|
||||
[HttpGet("{list}")]
|
||||
public async Task<IActionResult> GetAlgorithmicList(string list, [FromQuery] int? type)
|
||||
{
|
||||
var echoed = type is >= 0 and <= 255 ? type.Value : Rooms;
|
||||
|
||||
return Ok(new AlgorithmicList
|
||||
{
|
||||
Type = echoed,
|
||||
Entities = (await GetRowIds(list)).Select(roomId => new AlgorithmicListEntity
|
||||
{
|
||||
Id = roomId.ToString(),
|
||||
Context = null,
|
||||
}).ToList(),
|
||||
});
|
||||
}
|
||||
|
||||
private async Task<List<long>> GetRowIds(string? list)
|
||||
{
|
||||
var key = (list ?? string.Empty).Trim().ToLowerInvariant();
|
||||
|
||||
var rooms = await db.Rooms
|
||||
.Where(x => x.Accessibility == Accessibility.Public
|
||||
&& x.State == RoomState.Active
|
||||
&& !x.IsDorm)
|
||||
.ToListAsync();
|
||||
|
||||
if (key == "new")
|
||||
{
|
||||
return rooms
|
||||
.OrderByDescending(x => x.PublishedAt)
|
||||
.ThenBy(x => x.RoomId)
|
||||
.Take(RowSize)
|
||||
.Select(x => x.RoomId)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
var visits = await db.RoomStats
|
||||
.Select(x => new { x.RoomId, x.VisitCount })
|
||||
.ToDictionaryAsync(x => x.RoomId, x => x.VisitCount);
|
||||
|
||||
return rooms
|
||||
.OrderByDescending(x => visits.TryGetValue(x.RoomId, out var count) ? count : 0)
|
||||
.ThenBy(x => x.RoomId)
|
||||
.Take(RowSize)
|
||||
.Select(x => x.RoomId)
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public class AlgorithmicList
|
||||
{
|
||||
public int Type { get; set; }
|
||||
|
||||
public List<AlgorithmicListEntity> Entities { get; set; } = new();
|
||||
}
|
||||
|
||||
public class AlgorithmicListEntity
|
||||
{
|
||||
public string Id { get; set; } = "";
|
||||
|
||||
public string? Context { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user