Compare commits
2 Commits
190ae9f302
...
6434fd0b9c
Author | SHA1 | Date | |
---|---|---|---|
6434fd0b9c | |||
3fa86ad23d |
12
main.go
12
main.go
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
gotemplate "html/template"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
@@ -25,7 +26,16 @@ func main() {
|
||||
f.Use(flamego.Renderer())
|
||||
|
||||
// initialize templating engine
|
||||
f.Use(template.Templater())
|
||||
f.Use(template.Templater(template.Options{
|
||||
FuncMaps: []gotemplate.FuncMap{
|
||||
{
|
||||
"contains": web.Contains,
|
||||
},
|
||||
},
|
||||
Directory: "templates",
|
||||
Extensions: []string{".tmpl", ".html"},
|
||||
},
|
||||
))
|
||||
|
||||
// inject custom middleware
|
||||
f.Use(middleware.CustomVars)
|
||||
|
@@ -4,7 +4,8 @@
|
||||
"description": "Self-hosted PC hardware benchmark logging tool",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"grunt": "grunt"
|
||||
"grunt": "grunt",
|
||||
"nodemon": "nodemon"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@@ -27,8 +28,8 @@
|
||||
"devDependencies": {
|
||||
"grunt": "^1.5.3",
|
||||
"grunt-cli": "^1.4.3",
|
||||
"grunt-contrib-sass": "^2.0.0",
|
||||
"grunt-contrib-coffee": "^2.1.0",
|
||||
"grunt-contrib-sass": "^2.0.0",
|
||||
"grunt-contrib-watch": "^1.1.0",
|
||||
"sass": "^1.55.0"
|
||||
}
|
||||
|
53
templates/test/edit.tmpl
Normal file
53
templates/test/edit.tmpl
Normal file
@@ -0,0 +1,53 @@
|
||||
{{ template "header" . }}
|
||||
|
||||
<div class="row">
|
||||
<h2>{{ .title }}</h2>
|
||||
|
||||
<form class="twelve columns" action="/test/{{ .test.ID }}/edit" method="POST">
|
||||
<div class="row">
|
||||
<div class="columns four">
|
||||
<label for="test_hardware">
|
||||
Hardware Component:
|
||||
<select id="test_hardware" class="u-full-width" name="test_hardware">
|
||||
{{ $testHardwareID := .test.Hardware.ID }}
|
||||
{{ range $hw := .hardware }}
|
||||
<option value="{{ $hw.ID }}" {{ if eq $testHardwareID $hw.ID }}selected{{ end }}>{{ $hw.Name }}</option>
|
||||
{{ end }}
|
||||
</select>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div class="columns eight">
|
||||
<label for="test_name">
|
||||
Test name:
|
||||
<input id="test_name" class="u-full-width" type="text" name="test_name" placeholder="My hardware benchmarking test" value="{{ .test.Name }}">
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<div class="columns twelve">
|
||||
<label for="test_benchmarks">
|
||||
Benchmarks to Test:
|
||||
<select id="test_benchmarks" class="u-full-width" name="test_benchmarks" multiple>
|
||||
{{ $testBenchmarks := .test.Benchmarks }}
|
||||
{{ range $bm := .benchmarks }}
|
||||
<option value="{{ $bm.ID }}" {{ if contains $testBenchmarks $bm.ID }}selected{{ end }}>{{ $bm.Name }}</option>
|
||||
{{ end }}
|
||||
</select>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
<label for="test_description">
|
||||
Test description:
|
||||
<textarea id="test_description" class="twelve columns" cols="30" rows="10" name="test_description"></textarea>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<input class="button-primary u-full-width" type="submit" value="Submit">
|
||||
</form>
|
||||
</div>
|
||||
|
||||
{{ template "footer" . }}
|
10
web/helpers.go
Normal file
10
web/helpers.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package web
|
||||
|
||||
func Contains(slice []string, val string) bool {
|
||||
for _, v := range slice {
|
||||
if v == val {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
@@ -51,7 +51,10 @@ func RegisterRoutes(f *flamego.Flame) {
|
||||
f.Get("/create", routes.TestGetCreate)
|
||||
f.Post("/create", binding.Form(forms.TestForm{}), routes.TestPostCreate)
|
||||
|
||||
f.Get("/{test_id}", routes.TestGetView)
|
||||
f.Group("/{test_id}", func() {
|
||||
f.Get("", routes.TestGetView)
|
||||
f.Get("/edit", routes.TestGetEdit)
|
||||
})
|
||||
})
|
||||
|
||||
// result routes
|
||||
|
@@ -81,3 +81,24 @@ func TestPostCreate(c flamego.Context, form forms.TestForm, errs binding.Errors)
|
||||
|
||||
c.Redirect(fmt.Sprintf("/test/%d", test.ID))
|
||||
}
|
||||
|
||||
func TestGetEdit(c flamego.Context, t template.Template, data template.Data) {
|
||||
// find test in DB
|
||||
testID := c.Param("test_id")
|
||||
var test models.Test
|
||||
models.DB.Preload("Hardware").Preload("Benchmarks").First(&test, testID)
|
||||
data["test"] = test
|
||||
|
||||
// add hardware components to template
|
||||
var hardware []models.Hardware
|
||||
models.DB.Find(&hardware)
|
||||
data["hardware"] = hardware
|
||||
|
||||
// add benchmarks to template
|
||||
var benchmarks []models.Benchmark
|
||||
models.DB.Find(&benchmarks)
|
||||
data["benchmarks"] = benchmarks
|
||||
|
||||
data["title"] = fmt.Sprintf("Editing Test: %s", test.Name)
|
||||
t.HTML(http.StatusOK, "test/edit")
|
||||
}
|
||||
|
Reference in New Issue
Block a user