raven/app/web/routes.go

54 lines
1.3 KiB
Go
Raw Permalink Normal View History

2022-12-07 21:24:11 -05:00
package web
import (
"net/http"
"github.com/flamego/binding"
2022-12-07 21:24:11 -05:00
"github.com/flamego/flamego"
"github.com/flamego/template"
"git.metaunix.net/metaunix/raven/app/db"
"git.metaunix.net/metaunix/raven/app/web/form"
2022-12-07 21:24:11 -05:00
)
func RegisterRoutes(f *flamego.Flame) {
// index route - landing page for the user
f.Get("/", func(t template.Template, data template.Data) {
data["title"] = "Dashboard"
2022-12-07 21:24:11 -05:00
t.HTML(http.StatusOK, "index")
})
// item list route - lists all of the items in the database
2022-12-07 21:51:49 -05:00
f.Group("/item", func() {
f.Get("/list", func(t template.Template, data template.Data) {
var items []db.Item
db.DB.Find(&items)
data["inventory"] = items
data["title"] = "List of Inventory"
2022-12-07 21:51:49 -05:00
t.HTML(http.StatusOK, "item/list")
})
f.Get("/create", func(t template.Template, data template.Data) {
data["title"] = "Create New Item"
2022-12-07 21:51:49 -05:00
t.HTML(http.StatusOK, "item/create")
})
f.Post("/create", binding.Form(form.Item{}), func(c flamego.Context, form form.Item) {
// Create new Item object
item := db.Item{
Name: form.Name,
SerialNumber: form.SerialNumber,
SkuNumber: form.SkuNumber,
Manufacturer: form.Manufacturer,
Type: form.Type,
PurchasedFrom: form.PurchasedFrom,
PurchasedAt: form.PurchasedAt,
}
db.DB.Create(&item)
// Redirect user to items list
c.Redirect("/item/list")
})
2022-12-07 21:51:49 -05:00
})
2022-12-07 21:24:11 -05:00
}