diff --git a/main.go b/main.go
index a469093..23f693a 100644
--- a/main.go
+++ b/main.go
@@ -43,10 +43,12 @@ func main() {
templates, err := template.New("undefined.gohtml").
Funcs(template.FuncMap{
- "statusText": http.StatusText,
- "createDeviceLink": createDeviceLink,
- "formatMemorySize": formatMemorySize,
- "formatType": formatType,
+ "statusText": http.StatusText,
+ "createDeviceLink": createDeviceLink,
+ "formatMemorySize": formatMemorySize,
+ "formatMemoryPlainSize": formatMemoryPlainSize,
+ "formatType": formatType,
+ "isRamType": isRamType,
}).
ParseFS(templateFS, "templates/*.gohtml")
diff --git a/template_funcs.go b/template_funcs.go
index fad1a49..c573463 100644
--- a/template_funcs.go
+++ b/template_funcs.go
@@ -18,7 +18,7 @@ type CreateDeviceLink struct {
Qr *int
}
-func formatMemorySize(size int) string {
+func formatMemoryUnit(size int) string {
const (
KB = 1024
MB = KB * 1024
@@ -27,16 +27,66 @@ func formatMemorySize(size int) string {
switch {
case size >= GB:
- return fmt.Sprintf("%.2f GB", float64(size)/GB)
+ return "GB"
case size >= MB:
- return fmt.Sprintf("%.2f MB", float64(size)/MB)
+ return "MB"
case size >= KB:
- return fmt.Sprintf("%.2f KB", float64(size)/KB)
+ return "KB"
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 {
switch t {
case "ram":
diff --git a/templates/create_device_step2.gohtml b/templates/create_device_step2.gohtml
index 92aee0b..d68927d 100644
--- a/templates/create_device_step2.gohtml
+++ b/templates/create_device_step2.gohtml
@@ -18,23 +18,25 @@
|
-
+ |
|
- |
+ |
|
- |
+ |
@@ -42,32 +44,38 @@
-
+ {{if .IsEdit}}
+
+ {{else}}
+
+ {{end}}
{{template "footer"}}
{{end}}
diff --git a/templates/device.gohtml b/templates/device.gohtml
index bafc0f7..6de071c 100644
--- a/templates/device.gohtml
+++ b/templates/device.gohtml
@@ -29,5 +29,6 @@
{{end}}
+
{{template "footer"}}
{{end}}
diff --git a/views.go b/views.go
index 8c6a726..d413681 100644
--- a/views.go
+++ b/views.go
@@ -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()