320 lines
6.9 KiB
Go
320 lines
6.9 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
|
|
BrandCount int
|
|
TotalRamCapacity int
|
|
Brands []string
|
|
Types []string
|
|
}
|
|
|
|
func (a *App) getIndex(c *gin.Context) {
|
|
vm := &IndexVM{}
|
|
var err error
|
|
vm.AssetCount, err = a.GetAssetCount()
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
vm.BrandCount, err = a.GetBrandCount()
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
vm.TotalRamCapacity, err = a.GetTotalRamCapacity()
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
vm.Brands, err = a.GetAllBrands()
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
vm.Types, err = a.GetAllTypes()
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
c.HTML(http.StatusOK, "index", vm)
|
|
}
|
|
|
|
type DeviceVM struct {
|
|
Qr int
|
|
Name string
|
|
Brand string
|
|
Type string
|
|
Description string
|
|
RamType string
|
|
RamCapacity int
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
vm := &DeviceVM{Qr: qr}
|
|
err = a.db.Query("SELECT name, brand, type, description FROM assets WHERE qr = ?").
|
|
Bind(qr).
|
|
ScanSingle(&vm.Name, &vm.Brand, &vm.Type, &vm.Description)
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
if vm.Type == "ram" {
|
|
err = a.db.Query("SELECT type, capacity FROM info_ram WHERE asset = ?").
|
|
Bind(qr).
|
|
ScanSingle(&vm.RamType, &vm.RamCapacity)
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
}
|
|
|
|
c.HTML(http.StatusOK, "device", vm)
|
|
}
|
|
|
|
type CreateDeviceVM struct {
|
|
Qr *int
|
|
Type string
|
|
Brands []string
|
|
RamTypes []string
|
|
AssetName string
|
|
AssetBrand string
|
|
AssetDescription string
|
|
RamType string
|
|
RamCapacity int
|
|
IsEdit bool
|
|
}
|
|
|
|
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
|
|
|
|
if c.Query("edit") != "" && vm.Qr != nil {
|
|
vm.IsEdit = true
|
|
err = a.db.Query("SELECT name, type, brand, description FROM assets WHERE qr = ?").
|
|
Bind(*vm.Qr).
|
|
ScanSingle(&vm.AssetName, &vm.Type, &vm.AssetBrand, &vm.AssetDescription)
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
if vm.Type == "ram" {
|
|
err = a.db.Query("SELECT type, capacity FROM info_ram WHERE asset = ?").
|
|
Bind(*vm.Qr).
|
|
ScanSingle(&vm.RamType, &vm.RamCapacity)
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
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("DELETE FROM assets WHERE qr=?").Bind(qr).Exec()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = tx.Query("DELETE FROM info_ram WHERE asset=?").Bind(qr).Exec()
|
|
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()
|
|
}
|
|
|
|
type BrowseVM struct {
|
|
Assets []Asset
|
|
HasRam bool
|
|
}
|
|
|
|
type Asset struct {
|
|
Qr int
|
|
Name string
|
|
Brand string
|
|
Type string
|
|
Description string
|
|
RamType string
|
|
RamCapacity int
|
|
}
|
|
|
|
func (a *App) getBrowse(c *gin.Context) {
|
|
brands := c.QueryArray("brand")
|
|
types := c.QueryArray("type")
|
|
|
|
query := `SELECT assets.qr, assets.name, assets.brand, assets.type, assets.description,
|
|
info_ram.type, info_ram.capacity
|
|
FROM assets
|
|
JOIN info_ram ON info_ram.asset = assets.qr
|
|
WHERE 1=1`
|
|
if len(brands) > 0 {
|
|
query += " AND assets.brand IN (" + placeholders(len(brands)) + ")"
|
|
}
|
|
if len(types) > 0 {
|
|
query += " AND assets.type IN (" + placeholders(len(types)) + ")"
|
|
}
|
|
query += "ORDER BY assets.type, assets.brand, assets.name, assets.qr"
|
|
|
|
vm := &BrowseVM{}
|
|
|
|
var err error
|
|
q := a.db.Query(query).Bind(brands, types)
|
|
for row := range q.Range(&err) {
|
|
var asset Asset
|
|
err := row.Scan(&asset.Qr, &asset.Name, &asset.Brand, &asset.Type, &asset.Description, &asset.RamType, &asset.RamCapacity)
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
vm.Assets = append(vm.Assets, asset)
|
|
if asset.Type == "ram" {
|
|
vm.HasRam = true
|
|
}
|
|
}
|
|
if err != nil {
|
|
c.AbortWithError(http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
c.HTML(http.StatusOK, "browse", vm)
|
|
}
|
|
|
|
func placeholders(count int) string {
|
|
if count == 0 {
|
|
return ""
|
|
}
|
|
placeholder := "?"
|
|
for count > 1 {
|
|
placeholder += ", ?"
|
|
count--
|
|
}
|
|
return placeholder
|
|
}
|