summaryrefslogtreecommitdiff
path: root/Pages/Report.cshtml.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Pages/Report.cshtml.cs')
-rw-r--r--Pages/Report.cshtml.cs111
1 files changed, 111 insertions, 0 deletions
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<Download>();
+
+ 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<string> Keys
+ {
+ get
+ {
+ var keys = new List<String>();
+
+ 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<Download>? Rows {get;private set;}
+
+ public bool HasError {get; private set;}
+
+ public string? ErrorText {get; private set;}
+
+}