Can create devices nicely
This commit is contained in:
29
data.go
Normal file
29
data.go
Normal file
@@ -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
|
||||
}
|
||||
2
main.go
2
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)
|
||||
|
||||
@@ -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;
|
||||
|
||||
16
static/scripts.js
Normal file
16
static/scripts.js
Normal file
@@ -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
|
||||
}
|
||||
@@ -1,37 +1,8 @@
|
||||
{{- /*gotype: main.CreateDeviceVM */}}
|
||||
{{define "create_device"}}
|
||||
{{template "header" "Create Device"}}
|
||||
|
||||
{{/*<form action="/create?id={{.Qr}}" method="post">*/}}
|
||||
{{/* <label for="qr">QR Code:</label>*/}}
|
||||
{{/* <input type="text" id="qr" name="qr" value="{{.Qr}}" required disabled>*/}}
|
||||
{{/* <br>*/}}
|
||||
{{/* <label for="type">Type:</label>*/}}
|
||||
{{/* <input type="text" id="type" name="type" required>*/}}
|
||||
{{/* <br>*/}}
|
||||
{{/* <label for="brand">Brand:</label>*/}}
|
||||
{{/* <input type="text" id="brand" name="brand">*/}}
|
||||
{{/* <br>*/}}
|
||||
{{/* <label for="name">Name:</label>*/}}
|
||||
{{/* <input type="text" id="name" name="name">*/}}
|
||||
{{/* <br>*/}}
|
||||
{{/* <label for="description">Description:</label>*/}}
|
||||
{{/* <textarea id="description" name="description"></textarea>*/}}
|
||||
{{/* <br>*/}}
|
||||
{{/* <button type="submit">Create</button>*/}}
|
||||
{{/*</form>*/}}
|
||||
|
||||
<ul>
|
||||
{{template "create_device_link" createDeviceLink "ram" "Random Access Memory" .Qr}}
|
||||
</ul>
|
||||
|
||||
{{template "footer"}}
|
||||
{{end}}
|
||||
|
||||
{{define "create_device_link"}}
|
||||
{{if .Qr}}
|
||||
<li><a href="/create?type=ram&qr={{.Qr}}">Random Access Memory</a></li>
|
||||
{{- else}}
|
||||
<li><a href="/create?type=ram">Random Access Memory</a></li>
|
||||
{{- end}}
|
||||
{{if .Type}}
|
||||
{{template "create_device_step2" .}}
|
||||
{{else}}
|
||||
{{template "create_device_step1" .}}
|
||||
{{end}}
|
||||
{{end}}
|
||||
|
||||
16
templates/create_device_step1.gohtml
Normal file
16
templates/create_device_step1.gohtml
Normal file
@@ -0,0 +1,16 @@
|
||||
{{- /*gotype: main.CreateDeviceVM */}}
|
||||
{{define "create_device_step1"}}
|
||||
{{template "header" "Create Device - Choose Device Type"}}
|
||||
<ul>
|
||||
{{template "create_device_link" createDeviceLink "ram" "Random Access Memory" .Qr}}
|
||||
</ul>
|
||||
{{template "footer"}}
|
||||
{{end}}
|
||||
|
||||
{{define "create_device_link"}}
|
||||
{{if .Qr}}
|
||||
<li><a href="/create?type=ram&qr={{.Qr}}">Random Access Memory</a></li>
|
||||
{{- else}}
|
||||
<li><a href="/create?type=ram">Random Access Memory</a></li>
|
||||
{{- end}}
|
||||
{{end}}
|
||||
73
templates/create_device_step2.gohtml
Normal file
73
templates/create_device_step2.gohtml
Normal file
@@ -0,0 +1,73 @@
|
||||
{{- /*gotype: main.CreateDeviceVM */}}
|
||||
{{define "create_device_step2"}}
|
||||
{{template "header" "Create Device - Enter Device Data"}}
|
||||
<form action="/create" method="post">
|
||||
<h2>General Information</h2>
|
||||
<table>
|
||||
<tr>
|
||||
<td><label for="qr">QR Code:</label></td>
|
||||
{{if .Qr}}
|
||||
<td><input type="number" id="qr" name="qr" value="{{.Qr}}" required readonly></td>
|
||||
{{else}}
|
||||
<td><input type="number" id="qr" name="qr" required></td>
|
||||
{{end}}
|
||||
</tr>
|
||||
<tr>
|
||||
<td><label for="asset_type">Type:</label></td>
|
||||
<td><input type="text" id="asset_type" name="asset_type" required readonly value="{{.Type}}"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><label for="asset_brand">Brand:</label></td>
|
||||
<td><select id="asset_brand" name="asset_brand" onchange="newOption('asset_brand', 'Brand')">
|
||||
<option value="Unknown" selected>Unknown</option>
|
||||
{{range .Brands}}
|
||||
{{if ne . "Unknown"}}
|
||||
<option value="{{.}}">{{.}}</option>
|
||||
{{end}}
|
||||
{{end}}
|
||||
<option>New...</option>
|
||||
</select></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><label for="asset_name">Name:</label></td>
|
||||
<td><input type="text" id="asset_name" name="asset_name"></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><label for="asset_description">Description:</label></td>
|
||||
<td><textarea id="asset_description" name="asset_description"></textarea></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<h2>Memory Information</h2>
|
||||
<table>
|
||||
<tr>
|
||||
<td><label for="ram_type">Type:</label></td>
|
||||
<td><select id="ram_type" name="ram_type" onchange="newOption('ram_type', 'Memory Type')">
|
||||
<option value="Unknown" selected>Unknown</option>
|
||||
{{range .RamTypes}}
|
||||
{{if ne . "Unknown"}}
|
||||
<option value="{{.}}">{{.}}</option>
|
||||
{{end}}
|
||||
{{end}}
|
||||
<option>New...</option>
|
||||
</select></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><label for="ram_capacity">Capacity:</label></td>
|
||||
<td>
|
||||
<input type="number" id="ram_capacity" name="ram_capacity">
|
||||
<select id="ram_capacity_unit" name="ram_capacity_unit">
|
||||
<option value="B">B</option>
|
||||
<option value="KB">KB</option>
|
||||
<option value="MB" selected>MB</option>
|
||||
<option value="GB">GB</option>
|
||||
<option value="TB">TB</option>
|
||||
</select>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<button type="submit">Create</button>
|
||||
</form>
|
||||
{{template "footer"}}
|
||||
{{end}}
|
||||
@@ -1,10 +1,11 @@
|
||||
{{define "header"}}
|
||||
<html>
|
||||
<head>
|
||||
<title>PC Inventory{{if .}} - {{.}}{{- end}}</title>
|
||||
<title>PC Inventory{{if .}} - {{.}}{{end}}</title>
|
||||
<script src="/static/scripts.js" type="application/javascript"></script>
|
||||
</head>
|
||||
<body>
|
||||
<h1>PC Inventory - <i>{{.}}</i></h1>
|
||||
<h1><a href="/">PC Inventory</a>{{if .}} - <i>{{.}}{{end}}</i></h1>
|
||||
<form action="/device" method="get">
|
||||
<label for="id">Device ID:</label>
|
||||
<input type="text" id="id" name="id">
|
||||
|
||||
101
views.go
101
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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user