Add ability to add hard drives
All checks were successful
Build / build (push) Successful in 1m30s

This commit is contained in:
2025-03-24 16:15:52 +01:00
parent 982943fac3
commit ff1d95edab
9 changed files with 354 additions and 120 deletions

191
views.go
View File

@@ -59,13 +59,18 @@ func (a *App) getIndex(c *gin.Context) {
}
type DeviceVM struct {
Qr int
Name string
Brand string
Type string
Description string
RamType string
RamCapacity int
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) {
@@ -106,26 +111,55 @@ func (a *App) getDevice(c *gin.Context) {
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 {
Qr *int
Type string
Brands []string
RamTypes []string
IsEdit bool
// Assets
Qr *int
Type string
AssetBrand string
AssetBrands []string
AssetName string
AssetBrand string
AssetDescription string
RamType string
RamCapacity int
IsEdit bool
// 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 != "" {
@@ -137,22 +171,12 @@ func (a *App) getCreateDevice(c *gin.Context) {
vm.Qr = &qrInt
}
brands, err := a.GetAllBrands()
err = a.GetAllGroups(vm)
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 = ?").
@@ -170,6 +194,14 @@ func (a *App) getCreateDevice(c *gin.Context) {
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
}
}
}
@@ -183,13 +215,41 @@ func (a *App) postCreateDevice(c *gin.Context) {
}
assetType := c.PostForm("asset_type")
tx, err := a.db.Begin()
defer tx.MustRollback()
if err != nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("error beginning tx: %v", err))
return
}
err = a.DeleteAsset(tx, qr)
if err != nil {
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()
if err != nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("error inserting assets: %v", err))
return
}
if assetType == "ram" {
err = a.postCreateDeviceRam(c, qr)
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))
return
}
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
err = tx.Commit()
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
@@ -197,7 +257,7 @@ func (a *App) postCreateDevice(c *gin.Context) {
c.Redirect(http.StatusSeeOther, "/")
}
func (a *App) postCreateDeviceRam(c *gin.Context, qr int) error {
func (a *App) postCreateDeviceRam(c *gin.Context, qr int, tx *mysqlite.Tx) error {
var err error
capacity := 0
capacityString := c.PostForm("ram_capacity")
@@ -219,27 +279,37 @@ func (a *App) postCreateDeviceRam(c *gin.Context, qr int) error {
return errors.New("invalid ram_capacity_unit")
}
tx, err := a.db.Begin()
defer tx.MustRollback()
if err != nil {
return err
}
err = a.DeleteAsset(tx, qr)
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 err
}
func (a *App) postCreateDeviceHdd(c *gin.Context, qr int, tx *mysqlite.Tx) error {
var err error
capacity := 0
capacityString := c.PostForm("hdd_capacity")
if capacityString != "" {
capacity, err = strconv.Atoi(c.PostForm("hdd_capacity"))
if err != nil {
return err
}
}
return tx.Commit()
switch c.PostForm("hdd_capacity_unit") {
case "B":
case "KB":
capacity *= 1024
case "MB":
capacity *= 1024 * 1024
case "GB":
capacity *= 1024 * 1024 * 1024
default:
return errors.New("invalid hdd_capacity_unit")
}
err = tx.Query("INSERT INTO info_hdd (asset, capacity, type, form_factor, connection, rpm) VALUES (?, ?, ?, ?, ?, ?)").
Bind(qr, capacity, c.PostForm("hdd_type"), c.PostForm("hdd_form_factor"), c.PostForm("hdd_connection"), c.PostForm("hdd_rpm")).
Exec()
return err
}
type BrowseVM struct {
@@ -248,13 +318,18 @@ type BrowseVM struct {
}
type Asset struct {
Qr int
Name string
Brand string
Type string
Description string
RamType string
RamCapacity int
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) getBrowse(c *gin.Context) {
@@ -262,9 +337,11 @@ func (a *App) getBrowse(c *gin.Context) {
types := c.QueryArray("type")
query := `SELECT assets.qr, assets.name, assets.brand, assets.type, assets.description,
info_ram.type, info_ram.capacity
info_ram.type, info_ram.capacity,
info_hdd.capacity, info_hdd.type, info_hdd.form_factor, info_hdd.connection, info_hdd.rpm
FROM assets
JOIN info_ram ON info_ram.asset = assets.qr
LEFT JOIN info_ram ON info_ram.asset = assets.qr
LEFT JOIN info_hdd ON info_hdd.asset = assets.qr
WHERE 1=1`
if len(brands) > 0 {
query += " AND assets.brand IN (" + placeholders(len(brands)) + ")"
@@ -280,7 +357,9 @@ func (a *App) getBrowse(c *gin.Context) {
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)
err := row.Scan(&asset.Qr, &asset.Name, &asset.Brand, &asset.Type, &asset.Description,
&asset.RamType, &asset.RamCapacity,
&asset.HddCapacity, &asset.HddType, &asset.HddFormFactor, &asset.HddConnection, &asset.HddRpm)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return