From 3f05ee34d209fcb994cba50955cd2e301315d9bd Mon Sep 17 00:00:00 2001 From: Ian C Date: Fri, 22 Mar 2024 12:28:07 +0000 Subject: Initial working version. --- Pages/Report.cshtml.cs | 111 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 Pages/Report.cshtml.cs (limited to 'Pages/Report.cshtml.cs') diff --git a/Pages/Report.cshtml.cs b/Pages/Report.cshtml.cs new file mode 100644 index 0000000..37e2c11 --- /dev/null +++ b/Pages/Report.cshtml.cs @@ -0,0 +1,111 @@ +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;} + +} -- cgit v1.2.3