blt/web/routes/benchmark.go
2024-05-29 09:16:32 -04:00

67 lines
1.6 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 BenchmarkGetList(t template.Template, data template.Data) {
// add benchmarks to template
var benchmarks []models.Benchmark
models.DB.Find(&benchmarks)
data["benchmarks"] = benchmarks
data["title"] = "List of Benchmarks"
t.HTML(http.StatusOK, "benchmark/list")
}
func BenchmarkGetView(c flamego.Context, t template.Template, data template.Data) {
// find benchmark ID from request
benchmarkID := c.Param("benchmark_id")
// find benchmark from DB
var benchmark models.Benchmark
models.DB.First(&benchmark, benchmarkID)
data["benchmark"] = benchmark
data["title"] = benchmark.Name
t.HTML(http.StatusOK, "benchmark/view")
}
func BenchmarkGetCreate(t template.Template, data template.Data) {
data["title"] = "Add New Benchmark"
t.HTML(http.StatusOK, "benchmark/create")
}
func BenchmarkPostCreate(c flamego.Context, form forms.BenchmarkForm, 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)
}
benchmark := models.Benchmark{
Name: form.Name,
ScoringType: form.ScoringType,
Description: form.Description,
}
_ = models.DB.Create(&benchmark)
c.Redirect(fmt.Sprintf("/benchmark/%d", benchmark.ID))
}