Working on using new data model
This commit is contained in:
73
view_device.go
Normal file
73
view_device.go
Normal file
@@ -0,0 +1,73 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user