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 TestGetView(c flamego.Context, t template.Template, data template.Data) { // find test ID from request testID := c.Param("test_id") // find hardware from DB var test models.Test models.DB.Preload("Hardware").Preload("Benchmarks").First(&test, testID) data["test"] = test data["title"] = test.Name t.HTML(http.StatusOK, "test/view") } func TestGetCreate(t template.Template, data template.Data) { // add hardware components to template var hardware []models.Hardware models.DB.Find(&hardware) data["hardware"] = hardware // add benchmarks to template var benchmarks []models.Benchmark models.DB.Find(&benchmarks) data["benchmarks"] = benchmarks 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{ Name: form.Name, Description: form.Description, HardwareID: form.Hardware, } _ = models.DB.Create(&test) // bind benchmarks to test for _, v := range form.Benchmarks { var benchmark models.Benchmark models.DB.First(&benchmark, v) // find benchmark models.DB.Model(&test).Association("Benchmarks").Append(&benchmark) } c.Redirect(fmt.Sprintf("/test/%d", test.ID)) }