Working on improving the create device page
This commit is contained in:
3
main.go
3
main.go
@@ -36,6 +36,7 @@ func main() {
|
||||
templates, err := template.New("undefined.gohtml").
|
||||
Funcs(template.FuncMap{
|
||||
"statusText": http.StatusText,
|
||||
"createDeviceLink": createDeviceLink,
|
||||
}).
|
||||
ParseFS(templateFS, "templates/*.gohtml")
|
||||
|
||||
@@ -52,7 +53,7 @@ func main() {
|
||||
r.GET("/", app.getIndex)
|
||||
r.GET("/device", app.getDevice)
|
||||
r.GET("/create", app.getCreateDevice)
|
||||
r.POST("/create", app.postCreateDevice)
|
||||
//r.POST("/create", app.postCreateDevice)
|
||||
err = r.Run()
|
||||
if err != nil {
|
||||
log.Fatalf("error serving website: %v", err)
|
||||
|
||||
@@ -2,17 +2,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
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
15
template_funcs.go
Normal 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
|
||||
}
|
||||
@@ -1,23 +1,37 @@
|
||||
{{- /*gotype: main.CreateDeviceVM */}}
|
||||
{{define "create_device"}}
|
||||
{{template "header" "Create Device"}}
|
||||
<form action="/create?id={{.Qr}}" method="post">
|
||||
<label for="qr">QR Code:</label>
|
||||
<input type="text" id="qr" name="qr" value="{{.Qr}}" required disabled>
|
||||
<br>
|
||||
<label for="type">Type:</label>
|
||||
<input type="text" id="type" name="type" required>
|
||||
<br>
|
||||
<label for="brand">Brand:</label>
|
||||
<input type="text" id="brand" name="brand">
|
||||
<br>
|
||||
<label for="name">Name:</label>
|
||||
<input type="text" id="name" name="name">
|
||||
<br>
|
||||
<label for="description">Description:</label>
|
||||
<textarea id="description" name="description"></textarea>
|
||||
<br>
|
||||
<button type="submit">Create</button>
|
||||
</form>
|
||||
|
||||
{{/*<form action="/create?id={{.Qr}}" method="post">*/}}
|
||||
{{/* <label for="qr">QR Code:</label>*/}}
|
||||
{{/* <input type="text" id="qr" name="qr" value="{{.Qr}}" required disabled>*/}}
|
||||
{{/* <br>*/}}
|
||||
{{/* <label for="type">Type:</label>*/}}
|
||||
{{/* <input type="text" id="type" name="type" required>*/}}
|
||||
{{/* <br>*/}}
|
||||
{{/* <label for="brand">Brand:</label>*/}}
|
||||
{{/* <input type="text" id="brand" name="brand">*/}}
|
||||
{{/* <br>*/}}
|
||||
{{/* <label for="name">Name:</label>*/}}
|
||||
{{/* <input type="text" id="name" name="name">*/}}
|
||||
{{/* <br>*/}}
|
||||
{{/* <label for="description">Description:</label>*/}}
|
||||
{{/* <textarea id="description" name="description"></textarea>*/}}
|
||||
{{/* <br>*/}}
|
||||
{{/* <button type="submit">Create</button>*/}}
|
||||
{{/*</form>*/}}
|
||||
|
||||
<ul>
|
||||
{{template "create_device_link" createDeviceLink "ram" "Random Access Memory" .Qr}}
|
||||
</ul>
|
||||
|
||||
{{template "footer"}}
|
||||
{{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}}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{{define "header"}}
|
||||
<html>
|
||||
<head>
|
||||
<title>PC Inventory {{if - {{.}}</title>
|
||||
<title>PC Inventory{{if .}} - {{.}}{{- end}}</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>PC Inventory - <i>{{.}}</i></h1>
|
||||
|
||||
46
views.go
46
views.go
@@ -51,34 +51,38 @@ func (a *App) getDevice(c *gin.Context) {
|
||||
}
|
||||
|
||||
type CreateDeviceVM struct {
|
||||
Qr int
|
||||
Qr *int
|
||||
}
|
||||
|
||||
func (a *App) getCreateDevice(c *gin.Context) {
|
||||
qr, err := strconv.Atoi(c.Query("id"))
|
||||
vm := &CreateDeviceVM{}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func (a *App) postCreateDevice(c *gin.Context) {
|
||||
qr, err := strconv.Atoi(c.Query("id"))
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("invalid qr: %v", err))
|
||||
}
|
||||
|
||||
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")).
|
||||
Exec()
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.Redirect(http.StatusSeeOther, "/")
|
||||
}
|
||||
//func (a *App) postCreateDevice(c *gin.Context) {
|
||||
// qr, err := strconv.Atoi(c.Query("id"))
|
||||
// if err != nil {
|
||||
// c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("invalid qr: %v", err))
|
||||
// }
|
||||
//
|
||||
// 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")).
|
||||
// Exec()
|
||||
// if err != nil {
|
||||
// c.AbortWithError(http.StatusInternalServerError, err)
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// c.Redirect(http.StatusSeeOther, "/")
|
||||
//}
|
||||
|
||||
Reference in New Issue
Block a user