Add Library Pages

This commit is contained in:
Samuel Lorch 2024-05-05 01:31:45 +02:00
parent 566b9d8667
commit d6fa1fc97a
5 changed files with 235 additions and 0 deletions

80
server/libraries.go Normal file
View file

@ -0,0 +1,80 @@
package server
import (
"bytes"
"fmt"
"log/slog"
"net/http"
"git.lastassault.de/speatzle/morffix/constants"
"github.com/jackc/pgx/v5"
)
type LibrariesData struct {
Libraries []Library
}
type Library struct {
ID string `db:"id"`
Name string `db:"name"`
Path string `db:"path"`
Enable bool `db:"enable"`
}
func handleLibraries(w http.ResponseWriter, r *http.Request) {
data := LibrariesData{}
if r.Method == "POST" {
err := createLibrary(r)
if err != nil {
slog.ErrorContext(r.Context(), "Create Library", "err", err)
http.Error(w, "Error Create Library: "+err.Error(), http.StatusInternalServerError)
return
}
}
rows, err := db.Query(r.Context(), "SELECT id, name, path, enable FROM libraries")
if err != nil {
slog.ErrorContext(r.Context(), "Query Libraries", "err", err)
http.Error(w, "Error Query Libraries: "+err.Error(), http.StatusInternalServerError)
return
}
libraries, err := pgx.CollectRows[Library](rows, pgx.RowToStructByName[Library])
if err != nil {
slog.ErrorContext(r.Context(), "Collect Rows", "err", err)
http.Error(w, "Error Query Libraries: "+err.Error(), http.StatusInternalServerError)
return
}
data.Libraries = libraries
buf := bytes.Buffer{}
err = templates.ExecuteTemplate(&buf, constants.LIBRARIES_TEMPLATE_NAME, data)
if err != nil {
slog.ErrorContext(r.Context(), "Executing Libraries Template", "err", err)
http.Error(w, "Error Executing Template: "+err.Error(), http.StatusInternalServerError)
return
}
_, err = w.Write(buf.Bytes())
if err != nil {
slog.ErrorContext(r.Context(), "Writing http Response", "err", err)
}
}
func createLibrary(r *http.Request) error {
err := r.ParseForm()
if err != nil {
return fmt.Errorf("Parseing Form: %w", err)
}
name := r.FormValue("name")
path := r.FormValue("path")
enable := r.FormValue("enable") == "on"
slog.Info("Got Library Create", "name", name, "path", path, "enable", enable)
_, err = db.Exec(r.Context(), "INSERT INTO Libraries (name, path, enable) VALUES ($1,$2,$3)", name, path, enable)
if err != nil {
return fmt.Errorf("Inserting Library: %w", err)
}
return nil
}

79
server/library.go Normal file
View file

@ -0,0 +1,79 @@
package server
import (
"bytes"
"log/slog"
"net/http"
"git.lastassault.de/speatzle/morffix/constants"
"github.com/jackc/pgx/v5"
)
type LibraryData struct {
Library Library
Files []File
}
type File struct {
ID int `db:"id"`
Path string `db:"path"`
Size int64 `db:"size"`
Missing bool `db:"missing"`
}
func handleLibrary(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
if id == "" {
handleLibraries(w, r)
return
}
data := LibraryData{}
var name string
var path string
var enabled bool
err := db.QueryRow(r.Context(), "SELECT name, path, enable FROM libraries WHERE id = $1", id).Scan(&name, &path, &enabled)
if err != nil {
slog.ErrorContext(r.Context(), "Get Library", "err", err)
http.Error(w, "Error Get Library: "+err.Error(), http.StatusInternalServerError)
return
}
data.Library = Library{
ID: id,
Name: name,
Path: path,
Enable: enabled,
}
if r.Method == "PUT" {
// TODO
}
rows, err := db.Query(r.Context(), "SELECT id, path, size, missing FROM files where library_id = $1", id)
if err != nil {
slog.ErrorContext(r.Context(), "Query Files", "err", err)
http.Error(w, "Error Query Files: "+err.Error(), http.StatusInternalServerError)
return
}
files, err := pgx.CollectRows[File](rows, pgx.RowToStructByName[File])
if err != nil {
slog.ErrorContext(r.Context(), "Collect Rows", "err", err)
http.Error(w, "Error Query Files: "+err.Error(), http.StatusInternalServerError)
return
}
data.Files = files
buf := bytes.Buffer{}
err = templates.ExecuteTemplate(&buf, constants.LIBRARY_TEMPLATE_NAME, data)
if err != nil {
slog.ErrorContext(r.Context(), "Executing Library Template", "err", err)
http.Error(w, "Error Executing Template: "+err.Error(), http.StatusInternalServerError)
return
}
_, err = w.Write(buf.Bytes())
if err != nil {
slog.ErrorContext(r.Context(), "Writing http Response", "err", err)
}
}

View file

@ -71,6 +71,8 @@ func Start(_conf config.Config, tmplFS embed.FS, staticFS embed.FS, migrationsFS
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/worker", handleWorkerWebsocket) mux.HandleFunc("/worker", handleWorkerWebsocket)
mux.Handle("/static/", fs) mux.Handle("/static/", fs)
mux.HandleFunc("/libraries/{id}", handleLibrary)
mux.HandleFunc("/libraries", handleLibraries)
mux.HandleFunc("/", handleIndex) mux.HandleFunc("/", handleIndex)
server := &http.Server{ server := &http.Server{

40
tmpl/libraries.tmpl Normal file
View file

@ -0,0 +1,40 @@
{{template "head"}}
<h2>Libraries</h2>
<div class="library-list">
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Path</th>
<th>Enable</th>
</tr>
{{range $l := .Libraries}}
<tr onclick="window.location='/libraries/{{ $l.ID }}';">
<td>
{{ $l.ID }}
</td>
<td>
{{ $l.Name }}
</td>
<td>
{{ $l.Path }}
</td>
<td>
{{ $l.Enable }}
</td>
</tr>
{{end}}
</table>
</div>
<h2>New Library</h2>
<form method="POST">
<label>Name:</label><br />
<input type="text" name="name"><br />
<label>Path:</label><br />
<input type="text" name="path"><br />
<label>Enable:</label><br />
<input type="checkbox" name="enable"><br />
<input type="submit">
</form>
{{template "tail"}}

34
tmpl/library.tmpl Normal file
View file

@ -0,0 +1,34 @@
{{template "head"}}
<h2>Library {{.Library.Name}}</h2>
<p>Enabled {{.Library.Enable}}
<h2>Files</h2>
<form method="POST" action="/scan/{{.Library.ID}}">
<input value="Scan" type="submit">
</form>
<div class="file-list">
<table>
<tr>
<th>ID</th>
<th>Path</th>
<th>Size</th>
<th>Missing</th>
</tr>
{{range $f := .Files}}
<tr>
<td>
{{ $f.ID }}
</td>
<td>
{{ $f.Path }}
</td>
<td>
{{ $f.Size }}
</td>
<td>
{{ $f.Missing }}
</td>
</tr>
{{end}}
</table>
</div>
{{template "tail"}}