Compare commits
3 Commits
27cff3e79b
...
main
Author | SHA1 | Date | |
---|---|---|---|
4b98322022 | |||
60d8554cf1 | |||
c19bb2108c |
1
.gitignore
vendored
1
.gitignore
vendored
@ -3,6 +3,7 @@ blt
|
||||
|
||||
# Local data files
|
||||
data/
|
||||
tmp/
|
||||
|
||||
# Compiled assets
|
||||
public/css/
|
||||
|
@ -1,6 +1,8 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@ -16,3 +18,7 @@ type Benchmark struct {
|
||||
// has many results
|
||||
Results []Result
|
||||
}
|
||||
|
||||
func (b *Benchmark) StringID() string {
|
||||
return strconv.Itoa(int(b.ID))
|
||||
}
|
||||
|
@ -1,6 +1,9 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@ -19,3 +22,29 @@ type Test struct {
|
||||
// has many results
|
||||
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
|
||||
}
|
||||
|
||||
func (t *Test) IsBenchmarkSelected(benchmarkID string) bool {
|
||||
benchmarkConv, err := strconv.ParseUint(benchmarkID, 10, 0)
|
||||
if err != nil {
|
||||
fmt.Println("Error parsing Uint: ", err)
|
||||
}
|
||||
|
||||
benchmarkUint := uint(benchmarkConv)
|
||||
|
||||
for _, b := range t.Benchmarks {
|
||||
if b.ID == benchmarkUint {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
@ -30,9 +30,9 @@
|
||||
<label for="test_benchmarks">
|
||||
Benchmarks to Test:
|
||||
<select id="test_benchmarks" class="u-full-width" name="test_benchmarks" multiple>
|
||||
{{ $testBenchmarks := .test.Benchmarks }}
|
||||
{{ $selectedBenchmarks := .selectedBenchmarks }}
|
||||
{{ 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 }}
|
||||
</select>
|
||||
</label>
|
||||
|
@ -99,6 +99,50 @@ func TestGetEdit(c flamego.Context, t template.Template, data template.Data) {
|
||||
models.DB.Find(&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)
|
||||
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.Name = form.Name
|
||||
test.Description = form.Description
|
||||
test.HardwareID = form.Hardware
|
||||
|
||||
// bind benchmarks to test that aren't already associated
|
||||
for _, b := range form.Benchmarks {
|
||||
if ! test.IsBenchmarkSelected(b) {
|
||||
var benchmark models.Benchmark
|
||||
models.DB.First(&benchmark, b) // find benchmark
|
||||
models.DB.Model(&test).Association("Benchmarks").Append(&benchmark)
|
||||
}
|
||||
}
|
||||
|
||||
// removed associated benchmarks that weren't in the form
|
||||
for _, b := range test.Benchmarks {
|
||||
|
||||
}
|
||||
|
||||
c.Redirect(fmt.Sprintf("/test/%d", test.ID))
|
||||
}
|
||||
|
Reference in New Issue
Block a user