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; } }