blt/web/routes/benchmark.go

67 lines
1.6 KiB
Go
Raw Normal View History

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