Can create devices nicely
This commit is contained in:
101
views.go
101
views.go
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
@@ -51,7 +52,10 @@ func (a *App) getDevice(c *gin.Context) {
|
||||
}
|
||||
|
||||
type CreateDeviceVM struct {
|
||||
Qr *int
|
||||
Qr *int
|
||||
Type string
|
||||
Brands []string
|
||||
RamTypes []string
|
||||
}
|
||||
|
||||
func (a *App) getCreateDevice(c *gin.Context) {
|
||||
@@ -67,22 +71,85 @@ func (a *App) getCreateDevice(c *gin.Context) {
|
||||
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.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.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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user