Lots of changes

This commit is contained in:
Gregory Ballantine 2023-12-02 22:16:16 -05:00
parent 16704aeeb1
commit c5df026d04
16 changed files with 135 additions and 122 deletions

View File

@ -9,4 +9,7 @@ type Benchmark struct {
Name string Name string
ScoringType string ScoringType string
Description string Description string
// many-to-many test
Tests[] Test `gorm:"many2many:tests_benchmarks;"`
} }

View File

@ -8,6 +8,8 @@ type Hardware struct {
gorm.Model gorm.Model
Name string Name string
Type string Type string
SerialNumber string
Description string Description string
// has many tests
Tests[] Test
} }

View File

@ -21,6 +21,6 @@ func Open() {
} }
// Migrate the schema // Migrate the schema
DB.AutoMigrate(&Project{}, &Hardware{}, &Benchmark{}) DB.AutoMigrate(&Test{}, &Hardware{}, &Benchmark{})
log.Println("Database migrations complete.") log.Println("Database migrations complete.")
} }

View File

@ -1,11 +0,0 @@
package models
import (
"gorm.io/gorm"
)
type Project struct {
gorm.Model
Name string
Description string
}

17
models/test.go Normal file
View File

@ -0,0 +1,17 @@
package models
import (
"gorm.io/gorm"
)
type Test struct {
gorm.Model
DateTag string
// belongs to hardware
HardwareID int
Hardware Hardware
// many-to-many benchmarks
Benchmarks[] Benchmark `gorm:"many2many:tests_benchmarks;"`
}

View File

@ -4,7 +4,7 @@
<h4>BLT</h4> <h4>BLT</h4>
<ul> <ul>
<li><a href="/">Dashboard</a></li> <li><a href="/">Dashboard</a></li>
<li><a href="/project">Projects</a></li> <li><a href="/test">Tests</a></li>
<li><a href="/hardware">Hardware</a></li> <li><a href="/hardware">Hardware</a></li>
<li><a href="/benchmark">Benchmarks</a></li> <li><a href="/benchmark">Benchmarks</a></li>
</ul> </ul>

View File

@ -1,25 +0,0 @@
{{ template "header" . }}
<div class="row">
<h2>Create a project</h2>
<form class="twelve columns" action="/project/create" method="POST">
<div class="row">
<label for="project_name">
Project name:
<input id="project_name" class="u-full-width" type="text" name="project_name" placeholder="My hardware benchmarking project">
</label>
</div>
<div class="row">
<label for="project_description">
Project description:
<textarea id="project_description" class="twelve columns" cols="30" rows="10" name="project_description"></textarea>
</label>
</div>
<input class="button-primary u-full-width" type="submit" value="Submit">
</form>
</div>
{{ template "footer" . }}

View File

@ -1,13 +0,0 @@
{{ template "header" . }}
<div class="row">
<h2>{{ .project.Name }}</h2>
<p>{{ .project.Description }}</p>
<hr>
<p><a href="/project">Back</a></p>
</div>
{{ template "footer" . }}

View File

@ -0,0 +1,25 @@
{{ template "header" . }}
<div class="row">
<h2>Create a test</h2>
<form class="twelve columns" action="/test/create" method="POST">
<div class="row">
<label for="test_name">
Test name:
<input id="test_name" class="u-full-width" type="text" name="test_name" placeholder="My hardware benchmarking test">
</label>
</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" . }}

View File

@ -1,8 +1,8 @@
{{ template "header" . }} {{ template "header" . }}
<div class="row"> <div class="row">
<h2>Projects</h2> <h2>Tests</h2>
<a href="/project/create">Create a new project</a> <a href="/test/create">Create a new test</a>
<table class="twelve columns"> <table class="twelve columns">
<thead> <thead>
@ -13,9 +13,9 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{{ range $p := .projects }} {{ range $p := .tests }}
<tr> <tr>
<td><a href="/project/{{ $p.ID }}">{{ $p.Name }}</a></td> <td><a href="/test/{{ $p.ID }}">{{ $p.Name }}</a></td>
<td>{{ $p.CreatedAt.Format "01/02/2006 15:04am" }}</td> <td>{{ $p.CreatedAt.Format "01/02/2006 15:04am" }}</td>
<td>{{ $p.UpdatedAt.Format "01/02/2006 15:04am" }}</td> <td>{{ $p.UpdatedAt.Format "01/02/2006 15:04am" }}</td>
</tr> </tr>

13
templates/test/view.tmpl Normal file
View File

@ -0,0 +1,13 @@
{{ template "header" . }}
<div class="row">
<h2>{{ .test.Name }}</h2>
<p>{{ .test.Description }}</p>
<hr>
<p><a href="/test">Back</a></p>
</div>
{{ template "footer" . }}

View File

@ -1,6 +0,0 @@
package forms
type ProjectForm struct {
Name string `form:"project_name" validate:"required"`
Description string `form:"project_description"`
}

7
web/forms/test.go Normal file
View File

@ -0,0 +1,7 @@
package forms
type TestForm struct {
DateTag string `form:"test_date_tag" validate:"required"`
Hardware int `form:"test_hardware" validate:"required"`
Benchmarks []string `form:"test_benchmarks" validate:"required"`
}

View File

@ -12,15 +12,15 @@ func RegisterRoutes(f *flamego.Flame) {
// index routes // index routes
f.Get("/", routes.GetDashboard) f.Get("/", routes.GetDashboard)
// project routes // test routes
f.Group("/project", func() { f.Group("/test", func() {
f.Get("", func(c flamego.Context) { f.Get("", func(c flamego.Context) {
c.Redirect("/project/list") c.Redirect("/test/list")
}) })
f.Get("/list", routes.ProjectGetList) f.Get("/list", routes.TestGetList)
f.Get("/create", routes.ProjectGetCreate) f.Get("/create", routes.TestGetCreate)
f.Post("/create", binding.Form(forms.ProjectForm{}), routes.ProjectPostCreate) f.Post("/create", binding.Form(forms.TestForm{}), routes.TestPostCreate)
}) })
} }

View File

@ -1,51 +0,0 @@
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 ProjectGetList(t template.Template, data template.Data) {
// add projects to template
var projects []models.Project
models.DB.Find(&projects)
data["projects"] = projects
data["title"] = "List of Projects"
t.HTML(http.StatusOK, "project/list")
}
func ProjectGetCreate(t template.Template, data template.Data) {
data["title"] = "Create a Project"
t.HTML(http.StatusOK, "project/create")
}
func ProjectPostCreate(c flamego.Context, form forms.ProjectForm, 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)
}
project := models.Project{
Name: form.Name,
Description: form.Description,
}
_ = models.DB.Create(&project)
c.Redirect(fmt.Sprintf("/project/%d", project.ID))
}

52
web/routes/test.go Normal file
View File

@ -0,0 +1,52 @@
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 TestGetList(t template.Template, data template.Data) {
// add tests to template
var tests []models.Test
models.DB.Find(&tests)
data["tests"] = tests
data["title"] = "List of Tests"
t.HTML(http.StatusOK, "test/list")
}
func TestGetCreate(t template.Template, data template.Data) {
data["title"] = "Create a Test"
t.HTML(http.StatusOK, "test/create")
}
func TestPostCreate(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)
}
test := models.Test{
DateTag: form.DateTag,
HardwareID: form.Hardware,
}
_ = models.DB.Create(&test)
c.Redirect(fmt.Sprintf("/test/%d", test.ID))
}