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

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