Working on using new data model

This commit is contained in:
2025-06-04 05:33:51 +02:00
parent ff1d95edab
commit 6e72582d77
13 changed files with 734 additions and 390 deletions

266
views.go
View File

@@ -3,217 +3,22 @@ package main
import (
"errors"
"fmt"
"net/http"
"strconv"
"gitea.seeseepuff.be/seeseemelk/mysqlite"
"github.com/gin-gonic/gin"
"net/http"
"strconv"
"strings"
)
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
HddCapacity int
HddType string
HddFormFactor string
HddConnection string
HddRpm 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
}
} else if vm.Type == "hdd" {
err = a.db.Query("SELECT capacity, type, form_factor, connection, rpm FROM info_hdd WHERE asset = ?").
Bind(qr).
ScanSingle(&vm.HddCapacity, &vm.HddType, &vm.HddFormFactor, &vm.HddConnection, &vm.HddRpm)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
}
c.HTML(http.StatusOK, "device", vm)
}
type CreateDeviceVM struct {
IsEdit bool
// Assets
Qr *int
Type string
AssetBrand string
AssetBrands []string
AssetName string
AssetDescription string
// RAM
RamType string
RamTypes []string
RamCapacity int
// HDD
HddCapacity int
HddType string
HddTypes []string
HddFormFactor string
HddFormFactors []string
HddConnection string
HddConnections []string
HddRpm string
HddRpms []string
}
func (a *App) getCreateDevice(c *gin.Context) {
var err error
vm := &CreateDeviceVM{}
vm.Type = c.Query("type")
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
}
err = a.GetAllGroups(vm)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
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
}
} else if vm.Type == "hdd" {
err = a.db.Query("SELECT capacity, type, form_factor, connection, rpm FROM info_hdd WHERE asset = ?").
Bind(*vm.Qr).
ScanSingle(&vm.HddCapacity, &vm.HddType, &vm.HddFormFactor, &vm.HddConnection, &vm.HddRpm)
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"))
qr, err := strconv.Atoi(c.PostForm("asset-qr"))
if err != nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("invalid qr: %v", err))
}
assetType := c.PostForm("asset_type")
tx, err := a.db.Begin()
defer tx.MustRollback()
@@ -227,20 +32,18 @@ func (a *App) postCreateDevice(c *gin.Context) {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("error removing assets: %v", err))
return
}
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()
// Insert the asset into the database
var id int
err = a.createAsset("asset", tx, c, &id)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("error inserting assets: %v", err))
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("error inserting asset: %v", err))
return
}
if assetType == "ram" {
err = a.postCreateDeviceRam(c, qr, tx)
} else if assetType == "hdd" {
err = a.postCreateDeviceHdd(c, qr, tx)
} else {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("invalid type: %s", assetType))
assetType := c.PostForm("asset-type")
err = a.createAsset(assetType, tx, c, &id)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("error inserting asset type: %v", err))
return
}
@@ -257,6 +60,49 @@ func (a *App) postCreateDevice(c *gin.Context) {
c.Redirect(http.StatusSeeOther, "/")
}
func (a *App) createAsset(assetType string, tx *mysqlite.Tx, c *gin.Context, assetId *int) error {
for _, assetDescriptor := range DescriptorTree.AssetTypes {
if assetDescriptor.Id != assetType {
continue
}
var fields []string
var questions []string
var values []string
for _, field := range assetDescriptor.Fields {
fields = append(fields, field.Id)
questions = append(questions, "?")
values = append(values, c.PostForm(assetType+"-"+field.Id))
}
if *assetId != 0 {
fields = append(fields, "asset")
questions = append(questions, "?")
values = append(values, strconv.Itoa(*assetId))
}
fieldsStr := strings.Join(fields, ", ")
questionsStr := strings.Join(questions, ", ")
err := tx.Query(fmt.Sprintf("INSERT INTO %s (%s) values (%s)", assetDescriptor.Table, fieldsStr, questionsStr)).
Bind(values).
Exec()
if err != nil {
return err
}
if *assetId == 0 {
err := tx.Query("SELECT LAST_INSERT_ROWID()").ScanSingle(assetId)
if err != nil {
return fmt.Errorf("error getting last insert id: %v", err)
}
}
return nil
}
return errors.New("Create Asset not implemented for type: " + assetType)
}
func (a *App) postCreateDeviceRam(c *gin.Context, qr int, tx *mysqlite.Tx) error {
var err error
capacity := 0