Files
pcinv/views.go

89 lines
1.8 KiB
Go

package main
import (
"fmt"
"net/http"
"strconv"
"gitea.seeseepuff.be/seeseemelk/mysqlite"
"github.com/gin-gonic/gin"
)
type App struct {
db *mysqlite.Db
}
type IndexVM struct {
AssetCount int
}
func (a *App) getIndex(c *gin.Context) {
vm := &IndexVM{}
err := a.db.Query("SELECT COUNT(*) FROM assets").ScanSingle(&vm.AssetCount)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.HTML(http.StatusOK, "index", vm)
}
func (a *App) getDevice(c *gin.Context) {
qr, err := strconv.Atoi(c.Query("id"))
if err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
var count int
err = a.db.Query("SELECT COUNT(*) FROM assets WHERE qr = ?").
Bind(qr).
ScanSingle(&count)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
if count == 0 {
c.Redirect(http.StatusTemporaryRedirect, "/create?id="+strconv.Itoa(qr))
return
}
}
type CreateDeviceVM struct {
Qr *int
}
func (a *App) getCreateDevice(c *gin.Context) {
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
}
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, "/")
//}