Added hardware routes and views

This commit is contained in:
Gregory Ballantine 2023-12-02 22:37:46 -05:00
parent c5df026d04
commit 4ad9a92306
7 changed files with 144 additions and 1 deletions

View File

@ -8,7 +8,6 @@ type Hardware struct {
gorm.Model
Name string
Type string
Description string
// has many tests
Tests[] Test

View File

@ -0,0 +1,34 @@
{{ template "header" . }}
<div class="row">
<h2>Add new hardware</h2>
<form class="twelve columns" action="/hardware/create" method="POST">
<div class="row">
<div class="nine columns">
<label for="hardware_name">
Hardware name:
<input id="hardware_name" class="u-full-width" type="text" name="hardware_name" placeholder="EVGA RTX 3080 Ti">
</label>
</div>
<div class="three columns">
<label for="hardware_type">
Hardware type:
<select id="hardware_type" class="u-full-width" name="hardware_type">
<option value="cpu">Processor</option>
<option value="mem">Memory</option>
<option value="gpu">Graphics Card</option>
<option value="ssd">Solid State Drive</option>
<option value="hdd">Hard Drive</option>
<option value="nic">Network Card</option>
</select>
</label>
</div>
</div>
<input class="button-primary u-full-width" type="submit" value="Submit">
</form>
</div>
{{ template "footer" . }}

View File

@ -0,0 +1,27 @@
{{ template "header" . }}
<div class="row">
<h2>Hardware</h2>
<a href="/hardware/create">Add new hardware</a>
<table class="twelve columns">
<thead>
<tr>
<td>Name</td>
<td>Created at</td>
<td>Last updated</td>
</tr>
</thead>
<tbody>
{{ range $h := .hardware }}
<tr>
<td><a href="/hardware/{{ $h.ID }}">{{ $h.Name }}</a></td>
<td>{{ $h.CreatedAt.Format "01/02/2006 15:04am" }}</td>
<td>{{ $h.UpdatedAt.Format "01/02/2006 15:04am" }}</td>
</tr>
{{ end }}
</tbody>
</table>
</div>
{{ template "footer" . }}

View File

@ -0,0 +1,13 @@
{{ template "header" . }}
<div class="row">
<h2>{{ .hardware.Name }}</h2>
<p>{{ .hardware.Type }}</p>
<hr>
<p><a href="/hardware">Back</a></p>
</div>
{{ template "footer" . }}

6
web/forms/hardware.go Normal file
View File

@ -0,0 +1,6 @@
package forms
type HardwareForm struct {
Name string `form:"hardware_name" validate:"required"`
Type string `form:"hardware_type" validate:"required"`
}

View File

@ -12,6 +12,18 @@ func RegisterRoutes(f *flamego.Flame) {
// index routes
f.Get("/", routes.GetDashboard)
// hardware routes
f.Group("/hardware", func() {
f.Get("", func(c flamego.Context) {
c.Redirect("/hardware/list")
})
f.Get("/list", routes.HardwareGetList)
f.Get("/create", routes.HardwareGetCreate)
f.Post("/create", binding.Form(forms.HardwareForm{}), routes.HardwarePostCreate)
})
// test routes
f.Group("/test", func() {
f.Get("", func(c flamego.Context) {

52
web/routes/hardware.go Normal file
View File

@ -0,0 +1,52 @@
package routes
import (
"fmt"
"log"
"net/http"
"github.com/flamego/binding"
"github.com/flamego/flamego"
"github.com/flamego/template"
"github.com/flamego/validator"
"git.metaunix.net/bitgoblin/blt/models"
"git.metaunix.net/bitgoblin/blt/web/forms"
)
func HardwareGetList(t template.Template, data template.Data) {
// add hardwares to template
var hardware []models.Hardware
models.DB.Find(&hardware)
data["hardware"] = hardware
data["title"] = "List of Hardware"
t.HTML(http.StatusOK, "hardware/list")
}
func HardwareGetCreate(t template.Template, data template.Data) {
data["title"] = "Add New Hardware"
t.HTML(http.StatusOK, "hardware/create")
}
func HardwarePostCreate(c flamego.Context, form forms.HardwareForm, errs binding.Errors) {
if len(errs) > 0 {
var err error
switch errs[0].Category {
case binding.ErrorCategoryValidation:
err = errs[0].Err.(validator.ValidationErrors)[0]
default:
err = errs[0].Err
}
log.Fatal(err)
}
hardware := models.Hardware{
Name: form.Name,
Type: form.Type,
}
_ = models.DB.Create(&hardware)
c.Redirect(fmt.Sprintf("/hardware/%d", hardware.ID))
}