Add ability to edit devices
This commit is contained in:
10
main.go
10
main.go
@@ -43,10 +43,12 @@ func main() {
|
|||||||
|
|
||||||
templates, err := template.New("undefined.gohtml").
|
templates, err := template.New("undefined.gohtml").
|
||||||
Funcs(template.FuncMap{
|
Funcs(template.FuncMap{
|
||||||
"statusText": http.StatusText,
|
"statusText": http.StatusText,
|
||||||
"createDeviceLink": createDeviceLink,
|
"createDeviceLink": createDeviceLink,
|
||||||
"formatMemorySize": formatMemorySize,
|
"formatMemorySize": formatMemorySize,
|
||||||
"formatType": formatType,
|
"formatMemoryPlainSize": formatMemoryPlainSize,
|
||||||
|
"formatType": formatType,
|
||||||
|
"isRamType": isRamType,
|
||||||
}).
|
}).
|
||||||
ParseFS(templateFS, "templates/*.gohtml")
|
ParseFS(templateFS, "templates/*.gohtml")
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ type CreateDeviceLink struct {
|
|||||||
Qr *int
|
Qr *int
|
||||||
}
|
}
|
||||||
|
|
||||||
func formatMemorySize(size int) string {
|
func formatMemoryUnit(size int) string {
|
||||||
const (
|
const (
|
||||||
KB = 1024
|
KB = 1024
|
||||||
MB = KB * 1024
|
MB = KB * 1024
|
||||||
@@ -27,16 +27,66 @@ func formatMemorySize(size int) string {
|
|||||||
|
|
||||||
switch {
|
switch {
|
||||||
case size >= GB:
|
case size >= GB:
|
||||||
return fmt.Sprintf("%.2f GB", float64(size)/GB)
|
return "GB"
|
||||||
case size >= MB:
|
case size >= MB:
|
||||||
return fmt.Sprintf("%.2f MB", float64(size)/MB)
|
return "MB"
|
||||||
case size >= KB:
|
case size >= KB:
|
||||||
return fmt.Sprintf("%.2f KB", float64(size)/KB)
|
return "KB"
|
||||||
default:
|
default:
|
||||||
return fmt.Sprintf("%d B", size)
|
return "B"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func formatMemorySize(size int) string {
|
||||||
|
const (
|
||||||
|
KB = 1024
|
||||||
|
MB = KB * 1024
|
||||||
|
GB = MB * 1024
|
||||||
|
)
|
||||||
|
|
||||||
|
switch formatMemoryUnit(size) {
|
||||||
|
case "GB":
|
||||||
|
return fmt.Sprintf("%.2f GB", float64(size)/GB)
|
||||||
|
case "MB":
|
||||||
|
return fmt.Sprintf("%.2f MB", float64(size)/MB)
|
||||||
|
case "KB":
|
||||||
|
return fmt.Sprintf("%.2f KB", float64(size)/KB)
|
||||||
|
case "B":
|
||||||
|
return fmt.Sprintf("%d B", size)
|
||||||
|
default:
|
||||||
|
panic("invalid memory size")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func formatMemoryPlainSize(size int) int {
|
||||||
|
const (
|
||||||
|
KB = 1024
|
||||||
|
MB = KB * 1024
|
||||||
|
GB = MB * 1024
|
||||||
|
)
|
||||||
|
|
||||||
|
switch formatMemoryUnit(size) {
|
||||||
|
case "GB":
|
||||||
|
return size / GB
|
||||||
|
case "MB":
|
||||||
|
return size / MB
|
||||||
|
case "KB":
|
||||||
|
return size / KB
|
||||||
|
case "B":
|
||||||
|
return size
|
||||||
|
default:
|
||||||
|
panic("invalid memory size")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func isRamType(size int, unit string) bool {
|
||||||
|
if size == 0 && unit == "MB" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
actualUnit := formatMemoryUnit(size)
|
||||||
|
return unit == actualUnit
|
||||||
|
}
|
||||||
|
|
||||||
func formatType(t string) string {
|
func formatType(t string) string {
|
||||||
switch t {
|
switch t {
|
||||||
case "ram":
|
case "ram":
|
||||||
|
|||||||
@@ -18,23 +18,25 @@
|
|||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><label for="asset_brand">Brand:</label></td>
|
<td><label for="asset_brand">Brand:</label></td>
|
||||||
<td><select id="asset_brand" name="asset_brand" onchange="newOption('asset_brand', 'Brand')">
|
<td>
|
||||||
<option value="Unknown" selected>Unknown</option>
|
<select id="asset_brand" name="asset_brand" onchange="newOption('asset_brand', 'Brand')">
|
||||||
{{range .Brands}}
|
<option value="Unknown" {{if eq .AssetBrand "Unknown"}}selected{{end}}>Unknown</option>
|
||||||
{{if ne . "Unknown"}}
|
{{range .Brands}}
|
||||||
<option value="{{.}}">{{.}}</option>
|
{{if ne . "Unknown"}}
|
||||||
|
<option value="{{.}}" {{if eq . $.AssetBrand}}selected{{end}}>{{.}}</option>
|
||||||
|
{{end}}
|
||||||
{{end}}
|
{{end}}
|
||||||
{{end}}
|
<option>New...</option>
|
||||||
<option>New...</option>
|
</select>
|
||||||
</select></td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><label for="asset_name">Name:</label></td>
|
<td><label for="asset_name">Name:</label></td>
|
||||||
<td><input type="text" id="asset_name" name="asset_name"></td>
|
<td><input type="text" id="asset_name" name="asset_name" value="{{.AssetName}}"></td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><label for="asset_description">Description:</label></td>
|
<td><label for="asset_description">Description:</label></td>
|
||||||
<td><textarea id="asset_description" name="asset_description"></textarea></td>
|
<td><textarea id="asset_description" name="asset_description">{{.AssetDescription}}</textarea></td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
@@ -42,32 +44,38 @@
|
|||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<td><label for="ram_type">Type:</label></td>
|
<td><label for="ram_type">Type:</label></td>
|
||||||
<td><select id="ram_type" name="ram_type" onchange="newOption('ram_type', 'Memory Type')">
|
<td>
|
||||||
<option value="Unknown" selected>Unknown</option>
|
<select id="ram_type" name="ram_type" onchange="newOption('ram_type', 'Memory Type')">
|
||||||
{{range .RamTypes}}
|
<option value="Unknown" {{if eq .RamType "Unknown"}}selected{{end}}>Unknown</option>
|
||||||
{{if ne . "Unknown"}}
|
{{range .RamTypes}}
|
||||||
<option value="{{.}}">{{.}}</option>
|
{{if ne . "Unknown"}}
|
||||||
|
<option value="{{.}}" {{if eq . $.RamType}}selected{{end}}>{{.}}</option>
|
||||||
|
{{end}}
|
||||||
{{end}}
|
{{end}}
|
||||||
{{end}}
|
<option>New...</option>
|
||||||
<option>New...</option>
|
</select>
|
||||||
</select></td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<td><label for="ram_capacity">Capacity:</label></td>
|
<td><label for="ram_capacity">Capacity:</label></td>
|
||||||
<td>
|
<td>
|
||||||
<input type="number" id="ram_capacity" name="ram_capacity">
|
<input type="number" id="ram_capacity" name="ram_capacity" value="{{if .RamCapacity}}{{.RamCapacity | formatMemoryPlainSize}}{{end}}">
|
||||||
<select id="ram_capacity_unit" name="ram_capacity_unit">
|
<select id="ram_capacity_unit" name="ram_capacity_unit">
|
||||||
<option value="B">B</option>
|
<option value="B" {{if isRamType .RamCapacity "B"}}selected{{end}}>B</option>
|
||||||
<option value="KB">KB</option>
|
<option value="KB" {{if isRamType .RamCapacity "KB"}}selected{{end}}>KB</option>
|
||||||
<option value="MB" selected>MB</option>
|
<option value="MB" {{if isRamType .RamCapacity "MB"}}selected{{end}} selected>MB</option>
|
||||||
<option value="GB">GB</option>
|
<option value="GB" {{if isRamType .RamCapacity "GB"}}selected{{end}}>GB</option>
|
||||||
<option value="TB">TB</option>
|
<option value="TB" {{if isRamType .RamCapacity "TB"}}selected{{end}}>TB</option>
|
||||||
</select>
|
</select>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
<button type="submit">Create</button>
|
{{if .IsEdit}}
|
||||||
|
<button type="submit">Edit</button>
|
||||||
|
{{else}}
|
||||||
|
<button type="submit">Create</button>
|
||||||
|
{{end}}
|
||||||
</form>
|
</form>
|
||||||
{{template "footer"}}
|
{{template "footer"}}
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|||||||
@@ -29,5 +29,6 @@
|
|||||||
</tr>
|
</tr>
|
||||||
{{end}}
|
{{end}}
|
||||||
</table>
|
</table>
|
||||||
|
<a href="/create?id={{.Qr}}&edit=true"><button>Edit</button></a>
|
||||||
{{template "footer"}}
|
{{template "footer"}}
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|||||||
47
views.go
47
views.go
@@ -59,6 +59,7 @@ func (a *App) getIndex(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type DeviceVM struct {
|
type DeviceVM struct {
|
||||||
|
Qr int
|
||||||
Name string
|
Name string
|
||||||
Brand string
|
Brand string
|
||||||
Type string
|
Type string
|
||||||
@@ -88,7 +89,7 @@ func (a *App) getDevice(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
vm := &DeviceVM{}
|
vm := &DeviceVM{Qr: qr}
|
||||||
err = a.db.Query("SELECT name, brand, type, description FROM assets WHERE qr = ?").
|
err = a.db.Query("SELECT name, brand, type, description FROM assets WHERE qr = ?").
|
||||||
Bind(qr).
|
Bind(qr).
|
||||||
ScanSingle(&vm.Name, &vm.Brand, &vm.Type, &vm.Description)
|
ScanSingle(&vm.Name, &vm.Brand, &vm.Type, &vm.Description)
|
||||||
@@ -111,10 +112,16 @@ func (a *App) getDevice(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type CreateDeviceVM struct {
|
type CreateDeviceVM struct {
|
||||||
Qr *int
|
Qr *int
|
||||||
Type string
|
Type string
|
||||||
Brands []string
|
Brands []string
|
||||||
RamTypes []string
|
RamTypes []string
|
||||||
|
AssetName string
|
||||||
|
AssetBrand string
|
||||||
|
AssetDescription string
|
||||||
|
RamType string
|
||||||
|
RamCapacity int
|
||||||
|
IsEdit bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) getCreateDevice(c *gin.Context) {
|
func (a *App) getCreateDevice(c *gin.Context) {
|
||||||
@@ -146,6 +153,26 @@ func (a *App) getCreateDevice(c *gin.Context) {
|
|||||||
vm.Brands = brands
|
vm.Brands = brands
|
||||||
vm.RamTypes = types
|
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)
|
c.HTML(http.StatusOK, "create_device", vm)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -198,6 +225,16 @@ func (a *App) postCreateDeviceRam(c *gin.Context, qr int) error {
|
|||||||
return err
|
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 (?, ?, ?, ?, ?)").
|
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")).
|
Bind(qr, c.PostForm("asset_type"), c.PostForm("asset_brand"), c.PostForm("asset_name"), c.PostForm("asset_description")).
|
||||||
Exec()
|
Exec()
|
||||||
|
|||||||
Reference in New Issue
Block a user