Added result creation route

This commit is contained in:
Gregory Ballantine 2024-05-29 11:40:42 -04:00
parent fa92321176
commit 958c72ee63
5 changed files with 82 additions and 16 deletions

View File

@ -6,8 +6,9 @@ import (
type Result struct {
gorm.Model
Name string
Description string
AverageScore float
MinimumScore float
MaximumScore float
// belongs to Hardware
HardwareID int

View File

@ -22,23 +22,35 @@
<form class="u-full-width" action="/result/add" method="POST">
<div class="row">
<div class="columns four">
<select class="u-full-width" name="result_benchmark">
<label for="result_benchmark">
Benchmark:
<select id="result_benchmark" class="u-full-width" name="result_benchmark">
{{ range $bm := .test.Benchmarks }}
<option value="{{ $bm.ID }}">{{ $bm.Name }}</option>
{{ end }}
</select>
</label>
</div>
<div class="columns two">
<input class="u-full-width" type="number" name="result_avg" value="0">
<label for="result_avg">
Average score:
<input id="result_avg" class="u-full-width" type="number" name="result_avg" value="0">
</label>
</div>
<div class="columns two">
<input class="u-full-width" type="number" name="result_min" value="0">
<label for="result_min">
Minimum score:
<input id="result_min" class="u-full-width" type="number" name="result_min" value="0">
</label>
</div>
<div class="columns two">
<input class="u-full-width" type="number" name="result_max" value="0">
<label for="result_max">
Maximum score:
<input id="result_max" class="u-full-width" type="number" name="result_max" value="0">
</label>
</div>
<div class="columns two">

10
web/forms/result.go Normal file
View File

@ -0,0 +1,10 @@
package forms
type ResultForm struct {
Test uint `form:"result_test" validate:"required"`
Hardware uint `form:"result_hardware" validate:"required"`
Benchmark uint `form:"result_benchmark" validate:"required"`
AverageScore float `form:"result_avg" validate:"required"`
MinimumScore float `form:"result_min"`
MaximumScore float `form:"result_max"`
}

View File

@ -53,4 +53,9 @@ func RegisterRoutes(f *flamego.Flame) {
f.Get("/{test_id}", routes.TestGetView)
})
// result routes
f.Group("/result", func() {
f.Post("/add", routes.ResultPostCreate)
})
}

38
web/routes/result.go Normal file
View File

@ -0,0 +1,38 @@
package routes
import (
"log"
"github.com/flamego/binding"
"github.com/flamego/flamego"
"github.com/flamego/validator"
"git.metaunix.net/bitgoblin/blt/models"
"git.metaunix.net/bitgoblin/blt/web/forms"
)
func TestPostCreate(c flamego.Context, form forms.ResultForm, 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)
}
result := models.Result{
TestID: form.Test,
HardwareID: form.Hardware,
BenchmarkID: form.Benchmark,
AverageScore: form.AverageScore,
MinimumScore: form.MinimumScore,
MaximumScore: form.MaximumScore,
}
_ = models.DB.Create(&result)
c.Redirect(fmt.Sprintf("/test/%d", result.TestID))
}