From d8d68204288cbd08122cc63ef080ac6c667a6682 Mon Sep 17 00:00:00 2001 From: xoy Date: Tue, 4 Mar 2025 22:47:25 +0100 Subject: [PATCH] [Add admin table overview] --- db.go | 33 ++++++++++++++++++++++++ main.go | 27 ++++++++++++++++++++ types.go | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 131 insertions(+), 5 deletions(-) diff --git a/db.go b/db.go index b289d00..799d8fd 100755 --- a/db.go +++ b/db.go @@ -2,6 +2,7 @@ package main import ( "database/sql" + "fmt" "os" _ "github.com/mattn/go-sqlite3" @@ -71,6 +72,36 @@ func (conn *Connection) QueryLocation(id int64) (*Location, error) { return &location, nil } +func (conn *Connection) QueryLocationFullPath(lastChildId int64) (string, error) { + fullPath := "" + + nextId := lastChildId + + isEnd := false + + for !isEnd { + location, err := conn.QueryLocation(nextId) + if err != nil { + return fullPath, err + } + if location.Name.Valid { + fullPath = location.Name.String + "/" + fullPath + } else { + fullPath = fmt.Sprintf("%d/", nextId) + fullPath + } + isEnd = !location.Parent.Valid + if !isEnd { + nextId = location.Parent.Int64 + } + } + + if fullPath[len(fullPath)-1] == '/' { + fullPath = fullPath[0 : len(fullPath)-1] + } + + return "/" + fullPath, nil +} + func (conn *Connection) QueryContainers() ([]*Container, error) { if conn.Error != nil { return nil, conn.Error @@ -155,6 +186,7 @@ func (conn *Connection) QueryParts() ([]*Part, error) { part.Location = *location part.Container = *container + part.Connection = conn parts = append(parts, &part) } @@ -198,6 +230,7 @@ func (conn *Connection) QueryPart(id int64) (*Part, error) { } part.Container = *container } + part.Connection = conn return &part, nil } diff --git a/main.go b/main.go index 7c84c45..015b892 100755 --- a/main.go +++ b/main.go @@ -91,20 +91,47 @@ func main() { }) app.Get("/admin/containers/overview", func(c *fiber.Ctx) error { + containers, err := conn.QueryContainers() + if err != nil { + return err + } + table := ToTable[*Container](containers, TableColumns{ + "ID", + "Name", + }) + return c.Render("admin/overview", fiber.Map{ "Title": "Verwaltung", "Stylenames": NewStyleItemList("colors", "main", "overview"), "NavItems": navItems, "ActivePage": "/admin", + "Table": "locations", + "Columns": table.Collumns, + "Rows": table.Rows, }) }) app.Get("/admin/parts/overview", func(c *fiber.Ctx) error { + parts, err := conn.QueryParts() + if err != nil { + return err + } + table := ToTable[*Part](parts, TableColumns{ + "ID", + "Name", + "Tags", + "Ort", + "Behälter", + }) + return c.Render("admin/overview", fiber.Map{ "Title": "Verwaltung", "Stylenames": NewStyleItemList("colors", "main", "overview"), "NavItems": navItems, "ActivePage": "/admin", + "Table": "locations", + "Columns": table.Collumns, + "Rows": table.Rows, }) }) diff --git a/types.go b/types.go index c0dd8d4..828c36b 100755 --- a/types.go +++ b/types.go @@ -85,12 +85,78 @@ type Container struct { Name sql.NullString } +func (c *Container) ToTableRow() TableRow { + columns := make(TableColumns, 2) + + tr := TableRow{} + + if c.Id.Valid { + columns[0] = fmt.Sprintf("%d", c.Id.Int64) + tr.Id = int(c.Id.Int64) + } else { + columns[0] = "NULL" + tr.Id = -1 + } + if c.Name.Valid { + columns[1] = c.Name.String + } else { + columns[1] = "NULL" + } + + tr.Columns = columns + + return tr +} + type Part struct { - Id sql.NullInt64 - Name sql.NullString - Tags sql.NullString - Location Location - Container Container + Id sql.NullInt64 + Name sql.NullString + Tags sql.NullString + Location Location + Container Container + Connection *Connection +} + +func (p *Part) ToTableRow() TableRow { + columns := make(TableColumns, 5) + + tr := TableRow{} + + if p.Id.Valid { + columns[0] = fmt.Sprintf("%d", p.Id.Int64) + tr.Id = int(p.Id.Int64) + } else { + columns[0] = "NULL" + tr.Id = -1 + } + if p.Name.Valid { + columns[1] = p.Name.String + } else { + columns[1] = "NULL" + } + if p.Tags.Valid { + columns[2] = p.Tags.String + } else { + columns[2] = "NULL" + } + if p.Location.Id.Valid { + fullPath, err := p.Connection.QueryLocationFullPath(p.Location.Id.Int64) + if err != nil { + fmt.Println(err) + } + columns[3] = fullPath + } else { + columns[3] = "NULL" + } + if p.Container.Name.Valid { + columns[4] = p.Container.Name.String + } else { + columns[4] = "NULL" + } + + tr.Columns = columns + + return tr } // Interfaces