Added models; started working on project routes
This commit is contained in:
parent
49925c6d8f
commit
19670e9abd
12
models/benchmark.go
Normal file
12
models/benchmark.go
Normal file
@ -0,0 +1,12 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Benchmark struct {
|
||||
gorm.Model
|
||||
Name string
|
||||
ScoringType string
|
||||
Description string
|
||||
}
|
13
models/hardware.go
Normal file
13
models/hardware.go
Normal file
@ -0,0 +1,13 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Hardware struct {
|
||||
gorm.Model
|
||||
Name string
|
||||
Type string
|
||||
SerialNumber string
|
||||
Description string
|
||||
}
|
@ -7,14 +7,18 @@ import (
|
||||
"gorm.io/driver/sqlite"
|
||||
)
|
||||
|
||||
var (
|
||||
DB *gorm.DB
|
||||
)
|
||||
|
||||
func InitDatabase() {
|
||||
// Initialize database object
|
||||
db, err := gorm.Open(sqlite.Open("data/blt.db"), &gorm.Config{})
|
||||
DB, err := gorm.Open(sqlite.Open("data/blt.db"), &gorm.Config{})
|
||||
if err != nil {
|
||||
panic("Failed to connect database")
|
||||
}
|
||||
|
||||
// Migrate the schema
|
||||
db.AutoMigrate(&Project{})
|
||||
DB.AutoMigrate(&Project{}, &Hardware{}, &Benchmark{})
|
||||
log.Println("Database migrations complete.")
|
||||
}
|
||||
|
25
templates/project/create.tmpl
Normal file
25
templates/project/create.tmpl
Normal file
@ -0,0 +1,25 @@
|
||||
{{ template "header" . }}
|
||||
|
||||
<div class="row">
|
||||
<h2>Add a project</h2>
|
||||
|
||||
<form class="twelve columns" action="/project/add" method="POST">
|
||||
<div class="row">
|
||||
<label for="project_title">
|
||||
Project title:
|
||||
<input id="project_title" class="u-full-width" type="text" name="project_title" 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" . }}
|
27
templates/project/list.tmpl
Normal file
27
templates/project/list.tmpl
Normal file
@ -0,0 +1,27 @@
|
||||
{{ template "header" . }}
|
||||
|
||||
<div class="row">
|
||||
<h2>Projects</h2>
|
||||
<a href="/project/add">Add a project</a>
|
||||
|
||||
<table class="twelve columns">
|
||||
<thead>
|
||||
<tr>
|
||||
<td>Title</td>
|
||||
<td>Created at</td>
|
||||
<td>Last updated</td>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{{ range $p := .projects }}
|
||||
<tr>
|
||||
<td><a href="/project/{{ $p.Id }}">{{ $p.Name }}</a></td>
|
||||
<td>{{ $p.CreatedAt.Format "01/02/2006 15:04am" }}</td>
|
||||
<td>{{ $p.UpdatedAt.Format "01/02/2006 15:04am" }}</td>
|
||||
</tr>
|
||||
{{ end }}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
{{ template "footer" . }}
|
13
templates/project/view.tmpl
Normal file
13
templates/project/view.tmpl
Normal file
@ -0,0 +1,13 @@
|
||||
{{ template "header" . }}
|
||||
|
||||
<div class="row">
|
||||
<h2>{{ .project.Name }}</h2>
|
||||
|
||||
<p>{{ .project.Description }}</p>
|
||||
|
||||
<hr>
|
||||
|
||||
<p><a href="/project">Back</a></p>
|
||||
</div>
|
||||
|
||||
{{ template "footer" . }}
|
@ -9,4 +9,15 @@ import (
|
||||
func RegisterRoutes(f *flamego.Flame) {
|
||||
// index routes
|
||||
f.Get("/", routes.GetDashboard)
|
||||
|
||||
// project routes
|
||||
f.Group("/project", func() {
|
||||
f.Get("", func(c flamego.Context) {
|
||||
c.Redirect("/project/list")
|
||||
})
|
||||
|
||||
f.Get("/list", routes.ProjectGetList)
|
||||
|
||||
f.Get("/create", routes.ProjectGetCreate)
|
||||
})
|
||||
}
|
||||
|
24
web/routes/project.go
Normal file
24
web/routes/project.go
Normal file
@ -0,0 +1,24 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/flamego/template"
|
||||
|
||||
"git.metaunix.net/bitgoblin/blt/models"
|
||||
)
|
||||
|
||||
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")
|
||||
}
|
Loading…
Reference in New Issue
Block a user