Add ability to edit devices

This commit is contained in:
2025-03-24 15:01:42 +01:00
parent 5df1e9485a
commit b799ac5aa6
5 changed files with 137 additions and 39 deletions

View File

@@ -59,6 +59,7 @@ func (a *App) getIndex(c *gin.Context) {
}
type DeviceVM struct {
Qr int
Name string
Brand string
Type string
@@ -88,7 +89,7 @@ func (a *App) getDevice(c *gin.Context) {
return
}
vm := &DeviceVM{}
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)
@@ -111,10 +112,16 @@ func (a *App) getDevice(c *gin.Context) {
}
type CreateDeviceVM struct {
Qr *int
Type string
Brands []string
RamTypes []string
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) {
@@ -146,6 +153,26 @@ func (a *App) getCreateDevice(c *gin.Context) {
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)
}
@@ -198,6 +225,16 @@ func (a *App) postCreateDeviceRam(c *gin.Context, qr int) error {
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()