summaryrefslogtreecommitdiffstats
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
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.
-rw-r--r--cmd/web/handlers.go13
-rw-r--r--cmd/web/main.go2
-rw-r--r--cmd/web/routes.go1
-rw-r--r--internal/models/operation.go43
4 files changed, 54 insertions, 5 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
}
diff --git a/internal/models/operation.go b/internal/models/operation.go
index 8547030..abb874c 100644
--- a/internal/models/operation.go
+++ b/internal/models/operation.go
@@ -2,15 +2,48 @@ package models
import (
"database/sql"
+ "time"
)
type Operation struct {
- ID int
- Name string
- Description string
- Organisation Organisation
+ ID int
+ Name string
+ Description string
+ Created time.Time
+ OrganisationName string
}
-type OperationMOdel struct {
+type OperationModel struct {
DB *sql.DB
}
+
+func (m *OperationModel) ListAll() ([]Operation, error) {
+
+ stmt := `SELECT op.id, op.name, op.description, op.created, org.name FROM operations op INNER JOIN organisations org ON op.organisation_id=org.id`
+
+ // stmt := `SELECT * FROM operations`
+
+ rows, err := m.DB.Query(stmt)
+ if err != nil {
+ return nil, err
+ }
+
+ defer rows.Close()
+
+ var ops []Operation
+
+ for rows.Next() {
+ var o Operation
+ err = rows.Scan(&o.ID, &o.Name, &o.Description, &o.Created, &o.OrganisationName)
+ if err != nil {
+ return nil, err
+ }
+
+ ops = append(ops, o)
+ }
+
+ if err = rows.Err(); err != nil {
+ return nil, err
+ }
+ return ops, err
+}