From 9764af998a4c91196687b8d27693b935a243ab84 Mon Sep 17 00:00:00 2001 From: Ian C Date: Thu, 21 Mar 2024 14:54:11 +0000 Subject: Initial checkin following project regeneration to make post work --- Pages/Index.cshtml.cs | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 Pages/Index.cshtml.cs (limited to 'Pages/Index.cshtml.cs') 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 _logger; + + public IndexModel(ILogger 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? 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(); + + 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; + } +} -- cgit v1.2.3