using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Npgsql; using System.Collections.Generic; using download_admin; namespace download_admin.Pages; public class ReportModel : PageModel { public void OnGet() { ViewData["Title"] = "Download Repsoitory Report"; try { Rows = new List(); using var conn = new NpgsqlConnection(Config.Settings["ConnectionStrings:download"]); conn.Open(); using var cmd = new NpgsqlCommand("SELECT key, ip_address, user_agent, time FROM download ORDER BY time", conn); using var reader = cmd.ExecuteReader(); while(reader.Read()) { string key = (string)reader["key"]; string ip_address = (string)reader["ip_address"]; string user_agent = (string)reader["user_agent"]; DateTime time = (DateTime)reader["time"]; Rows.Add(new Download(key, ip_address, user_agent, time)); } } catch(Exception e) { SetError(e.Message); } } private void SetError(string error_text) { HasError = true; ErrorText = error_text; } public List Keys { get { var keys = new List(); try { using var conn = new NpgsqlConnection(Config.Settings["ConnectionStrings:download"]); conn.Open(); using var cmd = new NpgsqlCommand("SELECT key FROM file_object ORDER BY key", conn); using var reader = cmd.ExecuteReader(); while(reader.Read()) { string key = (string)reader["key"]; keys.Add(key); } } catch(Exception e) { SetError(e.Message); } return keys; } } public long GetCount(string key) { try { using var conn = new NpgsqlConnection(Config.Settings["ConnectionStrings:download"]); conn.Open(); using var cmd = new NpgsqlCommand("SELECT count(*) FROM download where key = @key", conn); cmd.Parameters.AddWithValue("key", NpgsqlTypes.NpgsqlDbType.Varchar, key); object? scalar = cmd.ExecuteScalar(); if (scalar != null) { return (long)scalar; } } catch(Exception e) { SetError(e.Message); } return 0; } public List? Rows {get;private set;} public bool HasError {get; private set;} public string? ErrorText {get; private set;} }