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 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> 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 Entities { get; set; } = new(); } public class AlgorithmicListEntity { public string Id { get; set; } = ""; public string? Context { get; set; } } }