using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.EntityFrameworkCore; using EonaCat.LogStack.Status.Data; using EonaCat.LogStack.Status.Models; namespace EonaCat.LogStack.Status.Pages; public class LogsModel : PageModel { private readonly DatabaseContext _db; public LogsModel(DatabaseContext db) => _db = db; public List Entries { get; set; } = new(); public List Sources { get; set; } = new(); public int TotalCount { get; set; } public int TotalPages { get; set; } [BindProperty(SupportsGet = true)] public string? Level { get; set; } [BindProperty(SupportsGet = true)] public string? Source { get; set; } [BindProperty(SupportsGet = true)] public string? Search { get; set; } [BindProperty(SupportsGet = true)] public int PageIndex { get; set; } = 1; public async Task OnGetAsync() { if (HttpContext.Session.GetString("IsAdmin") != "true") { return RedirectToPage("/Admin/Login"); } Sources = await _db.Logs.Select(l => l.Source).Distinct().OrderBy(s => s).ToListAsync(); var q = _db.Logs.AsQueryable(); if (!string.IsNullOrWhiteSpace(Level)) { q = q.Where(l => l.Level == Level.ToLower()); } if (!string.IsNullOrWhiteSpace(Source)) { q = q.Where(l => l.Source == Source); } if (!string.IsNullOrWhiteSpace(Search)) { q = q.Where(l => l.Message.Contains(Search) || (l.Exception != null && l.Exception.Contains(Search))); } TotalCount = await q.CountAsync(); TotalPages = (int)Math.Ceiling((double)TotalCount / 100); Entries = await q.OrderByDescending(l => l.Timestamp) .Skip((PageIndex - 1) * 100) .Take(100) .ToListAsync(); return Page(); } }