From c459148d33ddd41505de157c5e8549dba2d12c7c Mon Sep 17 00:00:00 2001 From: Sebastiaan de Schaetzen Date: Mon, 24 Mar 2025 09:43:41 +0100 Subject: [PATCH] Can create devices nicely --- data.go | 29 ++++++++ main.go | 2 +- migrations/1_initial.sql | 6 ++ static/scripts.js | 16 +++++ templates/create_device.gohtml | 39 ++--------- templates/create_device_step1.gohtml | 16 +++++ templates/create_device_step2.gohtml | 73 +++++++++++++++++++ templates/header.gohtml | 5 +- views.go | 101 ++++++++++++++++++++++----- 9 files changed, 233 insertions(+), 54 deletions(-) create mode 100644 data.go create mode 100644 static/scripts.js create mode 100644 templates/create_device_step1.gohtml create mode 100644 templates/create_device_step2.gohtml diff --git a/data.go b/data.go new file mode 100644 index 0000000..a49ef13 --- /dev/null +++ b/data.go @@ -0,0 +1,29 @@ +package main + +func (a *App) GetAllBrands() ([]string, error) { + var brands []string + var err error + for row := range a.db.Query("SELECT brand FROM assets GROUP BY brand ORDER BY brand ASC").Range(&err) { + var name string + err := row.Scan(&name) + if err != nil { + return nil, err + } + brands = append(brands, name) + } + return brands, err +} + +func (a *App) GetAllRAMTypes() ([]string, error) { + var types []string + var err error + for row := range a.db.Query("SELECT type FROM info_ram GROUP BY type ORDER BY type ASC").Range(&err) { + var name string + err := row.Scan(&name) + if err != nil { + return nil, err + } + types = append(types, name) + } + return types, err +} diff --git a/main.go b/main.go index ad196d6..0a073c1 100644 --- a/main.go +++ b/main.go @@ -53,7 +53,7 @@ func main() { r.GET("/", app.getIndex) r.GET("/device", app.getDevice) r.GET("/create", app.getCreateDevice) - //r.POST("/create", app.postCreateDevice) + r.POST("/create", app.postCreateDevice) err = r.Run() if err != nil { log.Fatalf("error serving website: %v", err) diff --git a/migrations/1_initial.sql b/migrations/1_initial.sql index c035325..d6a9938 100644 --- a/migrations/1_initial.sql +++ b/migrations/1_initial.sql @@ -11,3 +11,9 @@ create table worklog ( timestamp integer not null, action text not null ) strict; + +create table info_ram ( + asset integer not null unique, + capacity integer, + type text +) strict; diff --git a/static/scripts.js b/static/scripts.js new file mode 100644 index 0000000..34e9308 --- /dev/null +++ b/static/scripts.js @@ -0,0 +1,16 @@ +function newOption(elementId, name) { + var el = document.getElementById(elementId) + if (el.value !== "New...") { + return + } + + var newValue = window.prompt("Enter " + name + " Name") + if (newValue === null) { + return; + } + var child = document.createElement("option") + child.value = newValue + child.innerText = newValue + el.prepend(child) + el.value = newValue +} diff --git a/templates/create_device.gohtml b/templates/create_device.gohtml index e3e1151..3825755 100644 --- a/templates/create_device.gohtml +++ b/templates/create_device.gohtml @@ -1,37 +1,8 @@ {{- /*gotype: main.CreateDeviceVM */}} {{define "create_device"}} -{{template "header" "Create Device"}} - -{{/*
*/}} -{{/* */}} -{{/* */}} -{{/*
*/}} -{{/* */}} -{{/* */}} -{{/*
*/}} -{{/* */}} -{{/* */}} -{{/*
*/}} -{{/* */}} -{{/* */}} -{{/*
*/}} -{{/* */}} -{{/* */}} -{{/*
*/}} -{{/* */}} -{{/*
*/}} - - - -{{template "footer"}} -{{end}} - -{{define "create_device_link"}} - {{if .Qr}} -
  • Random Access Memory
  • - {{- else}} -
  • Random Access Memory
  • - {{- end}} + {{if .Type}} + {{template "create_device_step2" .}} + {{else}} + {{template "create_device_step1" .}} + {{end}} {{end}} diff --git a/templates/create_device_step1.gohtml b/templates/create_device_step1.gohtml new file mode 100644 index 0000000..c100beb --- /dev/null +++ b/templates/create_device_step1.gohtml @@ -0,0 +1,16 @@ +{{- /*gotype: main.CreateDeviceVM */}} +{{define "create_device_step1"}} +{{template "header" "Create Device - Choose Device Type"}} + +{{template "footer"}} +{{end}} + +{{define "create_device_link"}} + {{if .Qr}} +
  • Random Access Memory
  • + {{- else}} +
  • Random Access Memory
  • + {{- end}} +{{end}} diff --git a/templates/create_device_step2.gohtml b/templates/create_device_step2.gohtml new file mode 100644 index 0000000..92aee0b --- /dev/null +++ b/templates/create_device_step2.gohtml @@ -0,0 +1,73 @@ +{{- /*gotype: main.CreateDeviceVM */}} +{{define "create_device_step2"}} +{{template "header" "Create Device - Enter Device Data"}} +
    +

    General Information

    + + + + {{if .Qr}} + + {{else}} + + {{end}} + + + + + + + + + + + + + + + + + +
    + +

    Memory Information

    + + + + + + + + + +
    + + +
    + + +
    +{{template "footer"}} +{{end}} diff --git a/templates/header.gohtml b/templates/header.gohtml index 907f15a..ba9c862 100644 --- a/templates/header.gohtml +++ b/templates/header.gohtml @@ -1,10 +1,11 @@ {{define "header"}} - PC Inventory{{if .}} - {{.}}{{- end}} + PC Inventory{{if .}} - {{.}}{{end}} + -

    PC Inventory - {{.}}

    +

    PC Inventory{{if .}} - {{.}}{{end}}

    diff --git a/views.go b/views.go index 6b74d62..8ba770e 100644 --- a/views.go +++ b/views.go @@ -1,6 +1,7 @@ package main import ( + "errors" "fmt" "net/http" "strconv" @@ -51,7 +52,10 @@ func (a *App) getDevice(c *gin.Context) { } type CreateDeviceVM struct { - Qr *int + Qr *int + Type string + Brands []string + RamTypes []string } func (a *App) getCreateDevice(c *gin.Context) { @@ -67,22 +71,85 @@ func (a *App) getCreateDevice(c *gin.Context) { vm.Qr = &qrInt } + brands, err := a.GetAllBrands() + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + return + } + + types, err := a.GetAllRAMTypes() + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + return + } + + vm.Type = c.Query("type") + vm.Brands = brands + vm.RamTypes = types + c.HTML(http.StatusOK, "create_device", vm) } -//func (a *App) postCreateDevice(c *gin.Context) { -// qr, err := strconv.Atoi(c.Query("id")) -// if err != nil { -// c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("invalid qr: %v", err)) -// } -// -// err = a.db.Query("INSERT INTO assets (qr, type, brand, name, description) VALUES (?, ?, ?, ?, ?)"). -// Bind(qr, c.PostForm("type"), c.PostForm("brand"), c.PostForm("name"), c.PostForm("description")). -// Exec() -// if err != nil { -// c.AbortWithError(http.StatusInternalServerError, err) -// return -// } -// -// c.Redirect(http.StatusSeeOther, "/") -//} +func (a *App) postCreateDevice(c *gin.Context) { + qr, err := strconv.Atoi(c.PostForm("qr")) + if err != nil { + c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("invalid qr: %v", err)) + } + assetType := c.PostForm("asset_type") + + if assetType == "ram" { + err = a.postCreateDeviceRam(c, qr) + } else { + c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("invalid type: %s", assetType)) + return + } + + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + return + } + c.Redirect(http.StatusSeeOther, "/") +} + +func (a *App) postCreateDeviceRam(c *gin.Context, qr int) error { + var err error + capacity := 0 + capacityString := c.PostForm("ram_capacity") + if capacityString != "" { + capacity, err = strconv.Atoi(c.PostForm("ram_capacity")) + if err != nil { + return err + } + } + switch c.PostForm("ram_capacity_unit") { + case "B": + case "KB": + capacity *= 1024 + case "MB": + capacity *= 1024 * 1024 + case "GB": + capacity *= 1024 * 1024 * 1024 + default: + return errors.New("invalid ram_capacity_unit") + } + + tx, err := a.db.Begin() + defer tx.MustRollback() + 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() + if err != nil { + return err + } + + err = tx.Query("INSERT INTO info_ram (asset, type, capacity) VALUES (?, ?, ?)"). + Bind(qr, c.PostForm("ram_type"), capacity).Exec() + if err != nil { + return err + } + return tx.Commit() +}