Updated the test edit page (still need to do the post page)
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
This commit is contained in:
@ -1,6 +1,8 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -16,3 +18,7 @@ type Benchmark struct {
|
|||||||
// has many results
|
// has many results
|
||||||
Results []Result
|
Results []Result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Benchmark) StringID() string {
|
||||||
|
return strconv.Itoa(int(b.ID))
|
||||||
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -19,3 +21,12 @@ type Test struct {
|
|||||||
// has many results
|
// has many results
|
||||||
Results []Result
|
Results []Result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *Test) SelectedBenchmarks() []string {
|
||||||
|
benchmarks := t.Benchmarks
|
||||||
|
ids := make([]string, len(benchmarks))
|
||||||
|
for i, b := range benchmarks {
|
||||||
|
ids[i] = strconv.Itoa(int(b.ID))
|
||||||
|
}
|
||||||
|
return ids
|
||||||
|
}
|
||||||
|
@ -30,9 +30,9 @@
|
|||||||
<label for="test_benchmarks">
|
<label for="test_benchmarks">
|
||||||
Benchmarks to Test:
|
Benchmarks to Test:
|
||||||
<select id="test_benchmarks" class="u-full-width" name="test_benchmarks" multiple>
|
<select id="test_benchmarks" class="u-full-width" name="test_benchmarks" multiple>
|
||||||
{{ $testBenchmarks := .test.Benchmarks }}
|
{{ $selectedBenchmarks := .selectedBenchmarks }}
|
||||||
{{ range $bm := .benchmarks }}
|
{{ range $bm := .benchmarks }}
|
||||||
<option value="{{ $bm.ID }}" {{ if contains $testBenchmarks $bm.ID }}selected{{ end }}>{{ $bm.Name }}</option>
|
<option value="{{ $bm.ID }}" {{ if contains $selectedBenchmarks $bm.StringID }}selected{{ end }}>{{ $bm.Name }}</option>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</select>
|
</select>
|
||||||
</label>
|
</label>
|
||||||
|
@ -99,6 +99,47 @@ func TestGetEdit(c flamego.Context, t template.Template, data template.Data) {
|
|||||||
models.DB.Find(&benchmarks)
|
models.DB.Find(&benchmarks)
|
||||||
data["benchmarks"] = benchmarks
|
data["benchmarks"] = benchmarks
|
||||||
|
|
||||||
|
// determine which benchmarks are selected in a test
|
||||||
|
selectedBenchmarks := test.SelectedBenchmarks()
|
||||||
|
data["selectedBenchmarks"] = selectedBenchmarks
|
||||||
|
|
||||||
data["title"] = fmt.Sprintf("Editing Test: %s", test.Name)
|
data["title"] = fmt.Sprintf("Editing Test: %s", test.Name)
|
||||||
t.HTML(http.StatusOK, "test/edit")
|
t.HTML(http.StatusOK, "test/edit")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPostEdit(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)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
|
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user