Initial create device page

This commit is contained in:
2025-03-24 06:41:51 +01:00
parent 0c81f56f3b
commit 6a834330c1
5 changed files with 68 additions and 7 deletions

View File

@@ -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, "/")
}