diff --git a/main.go b/main.go
index 741c923..295d945 100644
--- a/main.go
+++ b/main.go
@@ -2,11 +2,12 @@ package main
import (
"embed"
- "gitea.seeseepuff.be/seeseemelk/mysqlite"
- "github.com/gin-gonic/gin"
"html/template"
"log"
"net/http"
+
+ "gitea.seeseepuff.be/seeseemelk/mysqlite"
+ "github.com/gin-gonic/gin"
)
//go:embed migrations/*.sql
@@ -50,6 +51,8 @@ func main() {
r.Use(errorHandler)
r.GET("/", app.getIndex)
r.GET("/device", app.getDevice)
+ r.GET("/create", app.getCreateDevice)
+ r.POST("/create", app.postCreateDevice)
err = r.Run()
if err != nil {
log.Fatalf("error serving website: %v", err)
diff --git a/templates/create_device.gohtml b/templates/create_device.gohtml
new file mode 100644
index 0000000..3252809
--- /dev/null
+++ b/templates/create_device.gohtml
@@ -0,0 +1,23 @@
+{{- /*gotype: main.CreateDeviceVM */}}
+{{define "create_device"}}
+{{template "header" "Create Device"}}
+
+{{template "footer"}}
+{{end}}
diff --git a/templates/header.gohtml b/templates/header.gohtml
index 945042c..96d95fc 100644
--- a/templates/header.gohtml
+++ b/templates/header.gohtml
@@ -1,7 +1,7 @@
{{define "header"}}
- PC Inventory - {{.}}
+ PC Inventory {{if - {{.}}
PC Inventory - {{.}}
diff --git a/templates/index.gohtml b/templates/index.gohtml
index 9f30593..3d0ee11 100644
--- a/templates/index.gohtml
+++ b/templates/index.gohtml
@@ -1,6 +1,6 @@
{{- /*gotype: main.IndexVM */}}
{{define "index"}}
-{{template "header" "wow"}}
+{{template "header"}}
Some statistics:
- Database contains {{.AssetCount}} assets.
diff --git a/views.go b/views.go
index 11c7dd5..70aa43a 100644
--- a/views.go
+++ b/views.go
@@ -1,10 +1,12 @@
package main
import (
- "gitea.seeseepuff.be/seeseemelk/mysqlite"
- "github.com/gin-gonic/gin"
+ "fmt"
"net/http"
"strconv"
+
+ "gitea.seeseepuff.be/seeseemelk/mysqlite"
+ "github.com/gin-gonic/gin"
)
type App struct {
@@ -43,7 +45,40 @@ func (a *App) getDevice(c *gin.Context) {
}
if count == 0 {
- c.Redirect(http.StatusTemporaryRedirect, "/create")
+ c.Redirect(http.StatusTemporaryRedirect, "/create?id="+strconv.Itoa(qr))
return
}
}
+
+type CreateDeviceVM struct {
+ 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: 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, "/")
+}