using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Server.Data; using Server.Data.Entities; namespace VoidScavengers.Services.Admin.Pages.Assets; public class IndexModel : AdminPageModel { private readonly GameDbContext _db; public IndexModel(GameDbContext db) { _db = db; } public List Assets { get; set; } = new(); [FromQuery(Name = "search")] public string? Search { get; set; } [FromQuery(Name = "type")] public ClientAssetType? TypeFilter { get; set; } [FromQuery(Name = "page")] public int CurrentPage { get; set; } = 1; public int PageSize { get; set; } = 24; public int TotalCount { get; set; } public int TotalPages => (int)Math.Ceiling(TotalCount / (double)PageSize); public int TextureCount { get; set; } public int SceneCount { get; set; } public int ScriptCount { get; set; } public string CdnBaseUrl { get; set; } = "https://assets.aeternalisgames.com"; public string GetPageUrl(int page) { var parts = new List { $"page={page}" }; if (!string.IsNullOrEmpty(Search)) parts.Add($"search={Uri.EscapeDataString(Search)}"); if (TypeFilter.HasValue) parts.Add($"type={TypeFilter}"); return "/admin/assets?" + string.Join("&", parts); } public async Task OnGetAsync() { if (CurrentPage < 1) CurrentPage = 1; var cdnUrl = Environment.GetEnvironmentVariable("CDN_BASE_URL"); if (!string.IsNullOrEmpty(cdnUrl)) CdnBaseUrl = cdnUrl; // Stats TextureCount = await _db.ClientAssets.CountAsync(a => a.AssetType == ClientAssetType.Texture); SceneCount = await _db.ClientAssets.CountAsync(a => a.AssetType == ClientAssetType.Scene); ScriptCount = await _db.ClientAssets.CountAsync(a => a.AssetType == ClientAssetType.Script); var query = _db.ClientAssets.AsQueryable(); if (!string.IsNullOrWhiteSpace(Search)) query = query.Where(a => a.FilePath.Contains(Search)); if (TypeFilter.HasValue) query = query.Where(a => a.AssetType == TypeFilter.Value); TotalCount = await query.CountAsync(); Assets = await query .OrderByDescending(a => a.UpdatedAt) .Skip((CurrentPage - 1) * PageSize) .Take(PageSize) .ToListAsync(); } }