blt/web/routes/test.go

53 lines
1.1 KiB
Go
Raw Normal View History

2023-12-02 22:16:16 -05:00
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 TestGetList(t template.Template, data template.Data) {
// add tests to template
var tests []models.Test
models.DB.Find(&tests)
data["tests"] = tests
data["title"] = "List of Tests"
t.HTML(http.StatusOK, "test/list")
}
func TestGetCreate(t template.Template, data template.Data) {
data["title"] = "Create a Test"
t.HTML(http.StatusOK, "test/create")
}
func TestPostCreate(c flamego.Context, form forms.TestForm, 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)
}
test := models.Test{
DateTag: form.DateTag,
HardwareID: form.Hardware,
}
_ = models.DB.Create(&test)
c.Redirect(fmt.Sprintf("/test/%d", test.ID))
}