diff --git a/main.go b/main.go index 295d945..ad196d6 100644 --- a/main.go +++ b/main.go @@ -35,7 +35,8 @@ func main() { templates, err := template.New("undefined.gohtml"). Funcs(template.FuncMap{ - "statusText": http.StatusText, + "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) diff --git a/template_debug.go b/template_debug.go index d169619..f73461b 100644 --- a/template_debug.go +++ b/template_debug.go @@ -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) -} diff --git a/template_funcs.go b/template_funcs.go new file mode 100644 index 0000000..de900cc --- /dev/null +++ b/template_funcs.go @@ -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 +} diff --git a/templates/create_device.gohtml b/templates/create_device.gohtml index 3252809..e3e1151 100644 --- a/templates/create_device.gohtml +++ b/templates/create_device.gohtml @@ -1,23 +1,37 @@ {{- /*gotype: main.CreateDeviceVM */}} {{define "create_device"}} {{template "header" "Create Device"}} -
- - -
- - -
- - -
- - -
- - -
- -
+ +{{/*
*/}} +{{/* */}} +{{/* */}} +{{/*
*/}} +{{/* */}} +{{/* */}} +{{/*
*/}} +{{/* */}} +{{/* */}} +{{/*
*/}} +{{/* */}} +{{/* */}} +{{/*
*/}} +{{/* */}} +{{/* */}} +{{/*
*/}} +{{/* */}} +{{/*
*/}} + + + {{template "footer"}} {{end}} + +{{define "create_device_link"}} + {{if .Qr}} +
  • Random Access Memory
  • + {{- else}} +
  • Random Access Memory
  • + {{- end}} +{{end}} diff --git a/templates/header.gohtml b/templates/header.gohtml index 98cb663..907f15a 100644 --- a/templates/header.gohtml +++ b/templates/header.gohtml @@ -1,7 +1,7 @@ {{define "header"}} - PC Inventory {{if - {{.}} + PC Inventory{{if .}} - {{.}}{{- end}}

    PC Inventory - {{.}}

    diff --git a/views.go b/views.go index 70aa43a..6b74d62 100644 --- a/views.go +++ b/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")) - if err != nil { - c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("invalid qr: %v", err)) + 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, "/") +//}