156 lines
3.2 KiB
Go
156 lines
3.2 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"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
|
|
Type string
|
|
Brands []string
|
|
RamTypes []string
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
brands, err := a.GetAllBrands()
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
types, err := a.GetAllRAMTypes()
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
vm.Type = c.Query("type")
|
|
vm.Brands = brands
|
|
vm.RamTypes = types
|
|
|
|
c.HTML(http.StatusOK, "create_device", vm)
|
|
}
|
|
|
|
func (a *App) postCreateDevice(c *gin.Context) {
|
|
qr, err := strconv.Atoi(c.PostForm("qr"))
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("invalid qr: %v", err))
|
|
}
|
|
assetType := c.PostForm("asset_type")
|
|
|
|
if assetType == "ram" {
|
|
err = a.postCreateDeviceRam(c, qr)
|
|
} else {
|
|
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("invalid type: %s", assetType))
|
|
return
|
|
}
|
|
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
c.Redirect(http.StatusSeeOther, "/")
|
|
}
|
|
|
|
func (a *App) postCreateDeviceRam(c *gin.Context, qr int) error {
|
|
var err error
|
|
capacity := 0
|
|
capacityString := c.PostForm("ram_capacity")
|
|
if capacityString != "" {
|
|
capacity, err = strconv.Atoi(c.PostForm("ram_capacity"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
switch c.PostForm("ram_capacity_unit") {
|
|
case "B":
|
|
case "KB":
|
|
capacity *= 1024
|
|
case "MB":
|
|
capacity *= 1024 * 1024
|
|
case "GB":
|
|
capacity *= 1024 * 1024 * 1024
|
|
default:
|
|
return errors.New("invalid ram_capacity_unit")
|
|
}
|
|
|
|
tx, err := a.db.Begin()
|
|
defer tx.MustRollback()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = tx.Query("INSERT INTO assets (qr, type, brand, name, description) VALUES (?, ?, ?, ?, ?)").
|
|
Bind(qr, c.PostForm("asset_type"), c.PostForm("asset_brand"), c.PostForm("asset_name"), c.PostForm("asset_description")).
|
|
Exec()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = tx.Query("INSERT INTO info_ram (asset, type, capacity) VALUES (?, ?, ?)").
|
|
Bind(qr, c.PostForm("ram_type"), capacity).Exec()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return tx.Commit()
|
|
}
|