fisch/main.go
2025-03-18 21:19:18 +01:00

198 lines
4.6 KiB
Go
Executable file

package main
import (
"log"
"math/rand/v2"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/template/html/v2"
)
const figchProbability = 5
func main() {
engine := html.New("views", ".html")
app := fiber.New(fiber.Config{
Views: engine,
})
app.Static("/", "static")
navItems := []NavItem{
NewNavItem("Suche", "/search"),
NewNavItem("Verwaltung", "/admin"),
}
conn := Connect()
app.Get("/", func(c *fiber.Ctx) error {
figch := rand.IntN(100) <= figchProbability
return c.Render("search", fiber.Map{
"Figch": figch,
"Title": "Suche",
"Stylenames": NewStyleItemList("colors", "main", "search"),
"NavItems": navItems,
"ActivePage": "/search",
"SearchResultCount": -1,
})
})
app.Get("/search", func(c *fiber.Ctx) error {
figch := rand.IntN(100) <= figchProbability
return c.Render("search", fiber.Map{
"Figch": figch,
"Title": "Suche",
"Stylenames": NewStyleItemList("colors", "main", "search"),
"NavItems": navItems,
"ActivePage": "/search",
"SearchResultCount": -1,
})
})
app.Post("/search", func(c *fiber.Ctx) error {
figch := rand.IntN(100) <= figchProbability
search := c.FormValue("search", "")
var parts []*Part
var err error
var table Table
var notification string
var resultCount int = -1
if IsEmpty(search) {
notification = "Sucheingabe darf nicht leer sein!"
search = ""
} else if len(search) < 3 {
notification = "Sucheingabe muss mehr als 2 Zeichen haben!"
search = ""
} else if !IsAlphaNumeric(search) {
notification = "Sucheingabe darf keine Sonderzeichen enthalten (außer Leerzeichen)"
search = ""
} else {
parts, err = conn.SearchPart(search)
if err != nil {
return err
}
table = ToTable[*Part](parts, TableColumns{
"ID",
"Name",
"Tags",
"Ort",
"Behälter",
})
resultCount = len(table.Rows)
}
return c.Render("search", fiber.Map{
"Figch": figch,
"Title": "Suche",
"Stylenames": NewStyleItemList("colors", "main", "search"),
"NavItems": navItems,
"ActivePage": "/search",
"SearchResultCount": resultCount,
"Columns": table.Collumns,
"Rows": table.Rows,
"Notification": notification,
"Search": search,
})
})
app.Get("/admin", func(c *fiber.Ctx) error {
figch := rand.IntN(100) <= figchProbability
return c.Render("admin/tables", fiber.Map{
"Figch": figch,
"Title": "Verwaltung",
"Stylenames": NewStyleItemList("colors", "main", "tables"),
"NavItems": navItems,
"ActivePage": "/admin",
"Tables": []NavItem{
NewNavItem("Orte", "locations"),
NewNavItem("Behälter", "containers"),
NewNavItem("Teile", "parts"),
},
})
})
app.Get("/admin/locations/overview", func(c *fiber.Ctx) error {
figch := rand.IntN(100) <= figchProbability
locations, err := conn.QueryLocations()
if err != nil {
return err
}
table := ToTable[*Location](locations, TableColumns{
"ID",
"Ort",
"Beschreibung",
})
return c.Render("admin/overview", fiber.Map{
"Figch": figch,
"Title": "Verwaltung",
"Stylenames": NewStyleItemList("colors", "main", "overview"),
"NavItems": navItems,
"ActivePage": "/admin",
"Table": "locations",
"Columns": table.Collumns,
"Rows": table.Rows,
})
})
app.Get("/admin/containers/overview", func(c *fiber.Ctx) error {
figch := rand.IntN(100) <= figchProbability
containers, err := conn.QueryContainers()
if err != nil {
return err
}
table := ToTable[*Container](containers, TableColumns{
"ID",
"Name",
})
return c.Render("admin/overview", fiber.Map{
"Figch": figch,
"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 {
figch := rand.IntN(100) <= figchProbability
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{
"Figch": figch,
"Title": "Verwaltung",
"Stylenames": NewStyleItemList("colors", "main", "overview"),
"NavItems": navItems,
"ActivePage": "/admin",
"Table": "locations",
"Columns": table.Collumns,
"Rows": table.Rows,
})
})
log.Fatal(app.Listen(":3000"))
}