[Add admin table overview]

This commit is contained in:
xoy 2025-03-04 22:47:25 +01:00
parent cf15c8dddc
commit d8d6820428
3 changed files with 131 additions and 5 deletions

33
db.go
View file

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

27
main.go
View file

@ -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,
})
})

View file

@ -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