summaryrefslogtreecommitdiffstats
path: root/cmd
diff options
context:
space:
mode:
authorMatthew Lemon <y@yulqen.org>2024-02-11 20:37:53 +0000
committerMatthew Lemon <y@yulqen.org>2024-02-11 20:37:53 +0000
commit21ec508717ecd6112984b56ca376ebac11477a2a (patch)
tree89f1b56a76c2ca6cb91be463924bd8563a9a20f0 /cmd
parent9d18b9e9d9ab7561deadc867654f7cc4db15d2e1 (diff)
Have added a listAll handler for new operation model
This is the first attempt at querying a table with a foreign key relation and returning the result to be used in Go code. Learned quite a bit! Including getting a null pointer error because I forgot to initialise the sql.DB in the application structure.
Diffstat (limited to '')
-rw-r--r--cmd/web/handlers.go13
-rw-r--r--cmd/web/main.go2
-rw-r--r--cmd/web/routes.go1
3 files changed, 16 insertions, 0 deletions
diff --git a/cmd/web/handlers.go b/cmd/web/handlers.go
index b0e83ac..78d4029 100644
--- a/cmd/web/handlers.go
+++ b/cmd/web/handlers.go
@@ -10,6 +10,19 @@ import (
"github.com/yulqen/ded-go-core/internal/models"
)
+func (app *application) listOperations(w http.ResponseWriter, r *http.Request) {
+
+ ops, err := app.operations.ListAll()
+ if err != nil {
+ app.serverError(w, r, err)
+ return
+ }
+
+ for _, op := range ops {
+ fmt.Fprintf(w, "%+v\n", op)
+ }
+}
+
func (app *application) listOrganisation(w http.ResponseWriter, r *http.Request) {
files := []string{
"./ui/html/base.tmpl.html",
diff --git a/cmd/web/main.go b/cmd/web/main.go
index cbfda5e..a8b8cb4 100644
--- a/cmd/web/main.go
+++ b/cmd/web/main.go
@@ -13,6 +13,7 @@ import (
type application struct {
logger *slog.Logger
+ operations *models.OperationModel
organisations *models.OrganisationModel
}
@@ -34,6 +35,7 @@ func main() {
app := &application{
logger: logger,
+ operations: &models.OperationModel{DB: db},
organisations: &models.OrganisationModel{DB: db},
}
diff --git a/cmd/web/routes.go b/cmd/web/routes.go
index 23ecf7b..e21b25d 100644
--- a/cmd/web/routes.go
+++ b/cmd/web/routes.go
@@ -12,5 +12,6 @@ func (app *application) routes() *http.ServeMux {
mux.HandleFunc("/organisation/list", app.listOrganisation)
mux.HandleFunc("/organisation/view", app.organisationView)
mux.HandleFunc("/organisation/create", app.organisationCreate)
+ mux.HandleFunc("/operation/list", app.listOperations)
return mux
}