// download - the admin console and reports // Copyright (C) 2024 Ian Cowburn // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // 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;} }