summaryrefslogtreecommitdiff
path: root/Pages/Index.cshtml.cs
blob: 6e9a5ed44a23586774cef1931dc31bfe8e000e7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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;
    }
}