summaryrefslogtreecommitdiff
path: root/Pages/Index.cshtml.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Pages/Index.cshtml.cs')
-rw-r--r--Pages/Index.cshtml.cs97
1 files changed, 97 insertions, 0 deletions
diff --git a/Pages/Index.cshtml.cs b/Pages/Index.cshtml.cs
new file mode 100644
index 0000000..6e9a5ed
--- /dev/null
+++ b/Pages/Index.cshtml.cs
@@ -0,0 +1,97 @@
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Mvc.RazorPages;
+using Npgsql;
+using System.Collections.Generic;
+using download_admin;
+
+namespace download_admin.Pages;
+
+public class IndexModel : PageModel
+{
+ private readonly ILogger<IndexModel> _logger;
+
+ public IndexModel(ILogger<IndexModel> logger)
+ {
+ _logger = logger;
+ }
+
+ public void OnGet()
+ {
+ LoadData();
+ }
+
+ public void OnPost()
+ {
+ if (!String.IsNullOrEmpty(Request.Form["add"]))
+ {
+ DoAdd();
+ }
+ else if (!String.IsNullOrEmpty(Request.Form["delete"]))
+ {
+ DoDelete();
+ }
+ else if (!String.IsNullOrEmpty(Request.Form["update"]))
+ {
+ DoUpdate();
+ }
+
+ LoadData();
+ }
+
+
+ public List<FileObject>? Rows {get;private set;}
+
+ public bool HasError {get; private set;}
+
+ public string? ErrorText {get; private set;}
+
+ private void DoAdd()
+ {
+ SetError("DoAdd");
+ }
+
+ private void DoDelete()
+ {
+ SetError("DoDelete");
+ }
+
+ private void DoUpdate()
+ {
+ SetError("DoUpdate");
+ }
+
+ private void LoadData()
+ {
+ try
+ {
+ Rows = new List<FileObject>();
+
+ using var conn = new NpgsqlConnection(Config.Settings["ConnectionStrings:download"]);
+
+ conn.Open();
+
+ using var cmd = new NpgsqlCommand("SELECT key, mime_type, file_name, data FROM file_object ORDER BY key", conn);
+ using var reader = cmd.ExecuteReader();
+
+ while(reader.Read())
+ {
+ string key = (string)reader["key"];
+ string mime_type = (string)reader["mime_type"];
+ string file_name = (string)reader["file_name"];
+ byte[] data = (byte[])reader["data"];
+
+ Rows.Add(new FileObject(key, mime_type, file_name, data));
+ }
+ }
+ catch(Exception e)
+ {
+ SetError(e.Message);
+ }
+ }
+
+ private void SetError(string error_text)
+ {
+ HasError = true;
+ ErrorText = error_text;
+ }
+}