Initial create device page

This commit is contained in:
2025-03-24 06:41:51 +01:00
parent 0c81f56f3b
commit 6a834330c1
5 changed files with 68 additions and 7 deletions

View File

@@ -2,11 +2,12 @@ package main
import (
"embed"
"gitea.seeseepuff.be/seeseemelk/mysqlite"
"github.com/gin-gonic/gin"
"html/template"
"log"
"net/http"
"gitea.seeseepuff.be/seeseemelk/mysqlite"
"github.com/gin-gonic/gin"
)
//go:embed migrations/*.sql
@@ -50,6 +51,8 @@ func main() {
r.Use(errorHandler)
r.GET("/", app.getIndex)
r.GET("/device", app.getDevice)
r.GET("/create", app.getCreateDevice)
r.POST("/create", app.postCreateDevice)
err = r.Run()
if err != nil {
log.Fatalf("error serving website: %v", err)

View File

@@ -0,0 +1,23 @@
{{- /*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>
{{template "footer"}}
{{end}}

View File

@@ -1,7 +1,7 @@
{{define "header"}}
<html>
<head>
<title>PC Inventory - {{.}}</title>
<title>PC Inventory {{if - {{.}}</title>
</head>
<body>
<h1>PC Inventory - <i>{{.}}</i></h1>

View File

@@ -1,6 +1,6 @@
{{- /*gotype: main.IndexVM */}}
{{define "index"}}
{{template "header" "wow"}}
{{template "header"}}
Some statistics:
<ul>
<li>Database contains {{.AssetCount}} assets.</li>

View File

@@ -1,10 +1,12 @@
package main
import (
"gitea.seeseepuff.be/seeseemelk/mysqlite"
"github.com/gin-gonic/gin"
"fmt"
"net/http"
"strconv"
"gitea.seeseepuff.be/seeseemelk/mysqlite"
"github.com/gin-gonic/gin"
)
type App struct {
@@ -43,7 +45,40 @@ func (a *App) getDevice(c *gin.Context) {
}
if count == 0 {
c.Redirect(http.StatusTemporaryRedirect, "/create")
c.Redirect(http.StatusTemporaryRedirect, "/create?id="+strconv.Itoa(qr))
return
}
}
type CreateDeviceVM struct {
Qr int
}
func (a *App) getCreateDevice(c *gin.Context) {
qr, err := strconv.Atoi(c.Query("id"))
if err != nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("invalid qr: %v", err))
}
vm := &CreateDeviceVM{
Qr: qr,
}
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, "/")
}