Added models; started working on project routes

This commit is contained in:
Gregory Ballantine 2023-11-27 23:41:48 -05:00
parent 49925c6d8f
commit 19670e9abd
8 changed files with 131 additions and 2 deletions

12
models/benchmark.go Normal file
View 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
View File

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

View File

@ -7,14 +7,18 @@ import (
"gorm.io/driver/sqlite" "gorm.io/driver/sqlite"
) )
var (
DB *gorm.DB
)
func InitDatabase() { func InitDatabase() {
// Initialize database object // 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 { if err != nil {
panic("Failed to connect database") panic("Failed to connect database")
} }
// Migrate the schema // Migrate the schema
db.AutoMigrate(&Project{}) DB.AutoMigrate(&Project{}, &Hardware{}, &Benchmark{})
log.Println("Database migrations complete.") log.Println("Database migrations complete.")
} }

View 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" . }}

View 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" . }}

View 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" . }}

View File

@ -9,4 +9,15 @@ import (
func RegisterRoutes(f *flamego.Flame) { func RegisterRoutes(f *flamego.Flame) {
// index routes // index routes
f.Get("/", routes.GetDashboard) 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
View 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")
}