package main import ( "github.com/gin-gonic/gin" "net/http" "strconv" ) 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) }