blt/web/routes/hardware.go
2024-05-29 09:11:13 -04:00

66 lines
1.5 KiB
Go

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 benchmarks to template
var benchmarks []models.Benchmark
models.DB.First(&benchmarks)
data["benchmarks"] = benchmarks
data["title"] = "List of Benchmarks"
t.HTML(http.StatusOK, "benchmark/list")
}
func HardwareGetView(c flamego.Context, t template.Template, data template.Data) {
// find hardware ID from request
hardwareID := c.Param("hardware_id")
// find hardware from DB
var hardware models.Hardware
models.DB.Find(&hardware, hardwareID)
data["hardware"] = hardware
data["title"] = hardware.Name
t.HTML(http.StatusOK, "hardware/view")
}
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))
}