Working on improving the create device page

This commit is contained in:
2025-03-24 07:00:07 +01:00
parent 5232e47b1d
commit 6405e7d692
6 changed files with 78 additions and 54 deletions

View File

@@ -35,7 +35,8 @@ func main() {
templates, err := template.New("undefined.gohtml"). templates, err := template.New("undefined.gohtml").
Funcs(template.FuncMap{ Funcs(template.FuncMap{
"statusText": http.StatusText, "statusText": http.StatusText,
"createDeviceLink": createDeviceLink,
}). }).
ParseFS(templateFS, "templates/*.gohtml") ParseFS(templateFS, "templates/*.gohtml")
@@ -52,7 +53,7 @@ func main() {
r.GET("/", app.getIndex) r.GET("/", app.getIndex)
r.GET("/device", app.getDevice) r.GET("/device", app.getDevice)
r.GET("/create", app.getCreateDevice) r.GET("/create", app.getCreateDevice)
r.POST("/create", app.postCreateDevice) //r.POST("/create", app.postCreateDevice)
err = r.Run() err = r.Run()
if err != nil { if err != nil {
log.Fatalf("error serving website: %v", err) log.Fatalf("error serving website: %v", err)

View File

@@ -2,17 +2,7 @@
package main package main
import ( import (
"html/template"
"io"
"net/http" "net/http"
) )
const staticFiles = http.Dir("./static") const staticFiles = http.Dir("./static")
func renderTemplate(wr io.Writer) error {
templates, err := template.ParseGlob("web/*.gohtml")
if err != nil {
return err
}
return templates.Execute(wr, nil)
}

15
template_funcs.go Normal file
View File

@@ -0,0 +1,15 @@
package main
func createDeviceLink(deviceType, name string, qr *int) CreateDeviceLink {
return CreateDeviceLink{
Type: deviceType,
Name: name,
Qr: qr,
}
}
type CreateDeviceLink struct {
Type string
Name string
Qr *int
}

View File

@@ -1,23 +1,37 @@
{{- /*gotype: main.CreateDeviceVM */}} {{- /*gotype: main.CreateDeviceVM */}}
{{define "create_device"}} {{define "create_device"}}
{{template "header" "Create Device"}} {{template "header" "Create Device"}}
<form action="/create?id={{.Qr}}" method="post">
<label for="qr">QR Code:</label> {{/*<form action="/create?id={{.Qr}}" method="post">*/}}
<input type="text" id="qr" name="qr" value="{{.Qr}}" required disabled> {{/* <label for="qr">QR Code:</label>*/}}
<br> {{/* <input type="text" id="qr" name="qr" value="{{.Qr}}" required disabled>*/}}
<label for="type">Type:</label> {{/* <br>*/}}
<input type="text" id="type" name="type" required> {{/* <label for="type">Type:</label>*/}}
<br> {{/* <input type="text" id="type" name="type" required>*/}}
<label for="brand">Brand:</label> {{/* <br>*/}}
<input type="text" id="brand" name="brand"> {{/* <label for="brand">Brand:</label>*/}}
<br> {{/* <input type="text" id="brand" name="brand">*/}}
<label for="name">Name:</label> {{/* <br>*/}}
<input type="text" id="name" name="name"> {{/* <label for="name">Name:</label>*/}}
<br> {{/* <input type="text" id="name" name="name">*/}}
<label for="description">Description:</label> {{/* <br>*/}}
<textarea id="description" name="description"></textarea> {{/* <label for="description">Description:</label>*/}}
<br> {{/* <textarea id="description" name="description"></textarea>*/}}
<button type="submit">Create</button> {{/* <br>*/}}
</form> {{/* <button type="submit">Create</button>*/}}
{{/*</form>*/}}
<ul>
{{template "create_device_link" createDeviceLink "ram" "Random Access Memory" .Qr}}
</ul>
{{template "footer"}} {{template "footer"}}
{{end}} {{end}}
{{define "create_device_link"}}
{{if .Qr}}
<li><a href="/create?type=ram&qr={{.Qr}}">Random Access Memory</a></li>
{{- else}}
<li><a href="/create?type=ram">Random Access Memory</a></li>
{{- end}}
{{end}}

View File

@@ -1,7 +1,7 @@
{{define "header"}} {{define "header"}}
<html> <html>
<head> <head>
<title>PC Inventory {{if - {{.}}</title> <title>PC Inventory{{if .}} - {{.}}{{- end}}</title>
</head> </head>
<body> <body>
<h1>PC Inventory - <i>{{.}}</i></h1> <h1>PC Inventory - <i>{{.}}</i></h1>

View File

@@ -51,34 +51,38 @@ func (a *App) getDevice(c *gin.Context) {
} }
type CreateDeviceVM struct { type CreateDeviceVM struct {
Qr int Qr *int
} }
func (a *App) getCreateDevice(c *gin.Context) { func (a *App) getCreateDevice(c *gin.Context) {
qr, err := strconv.Atoi(c.Query("id")) vm := &CreateDeviceVM{}
if err != nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("invalid qr: %v", err)) qr := c.Query("id")
if qr != "" {
qrInt, err := strconv.Atoi(qr)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("invalid qr: %v", err))
return
}
vm.Qr = &qrInt
} }
vm := &CreateDeviceVM{
Qr: qr,
}
c.HTML(http.StatusOK, "create_device", vm) c.HTML(http.StatusOK, "create_device", vm)
} }
func (a *App) postCreateDevice(c *gin.Context) { //func (a *App) postCreateDevice(c *gin.Context) {
qr, err := strconv.Atoi(c.Query("id")) // qr, err := strconv.Atoi(c.Query("id"))
if err != nil { // if err != nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("invalid qr: %v", err)) // c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("invalid qr: %v", err))
} // }
//
err = a.db.Query("INSERT INTO assets (qr, type, brand, name, description) VALUES (?, ?, ?, ?, ?)"). // err = a.db.Query("INSERT INTO assets (qr, type, brand, name, description) VALUES (?, ?, ?, ?, ?)").
Bind(qr, c.PostForm("type"), c.PostForm("brand"), c.PostForm("name"), c.PostForm("description")). // Bind(qr, c.PostForm("type"), c.PostForm("brand"), c.PostForm("name"), c.PostForm("description")).
Exec() // Exec()
if err != nil { // if err != nil {
c.AbortWithError(http.StatusInternalServerError, err) // c.AbortWithError(http.StatusInternalServerError, err)
return // return
} // }
//
c.Redirect(http.StatusSeeOther, "/") // c.Redirect(http.StatusSeeOther, "/")
} //}