diff options
author | Matthew Lemon <y@yulqen.org> | 2024-02-10 19:39:51 +0000 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-02-10 19:39:51 +0000 |
commit | ceae84f1ae0ce2f23a70aa23b899e135384332f6 (patch) | |
tree | 0ce91d2dd2a30ef67ff051f5659c5fdc42a11a61 | |
parent | 58967839c646c717b5345f3a3d2924be5c6d5a4b (diff) |
Implements Latest() organisation model method
And puts the list in plain text on the home page.
-rw-r--r-- | cmd/web/handlers.go | 32 | ||||
-rw-r--r-- | internal/models/organisations.go | 30 |
2 files changed, 51 insertions, 11 deletions
diff --git a/cmd/web/handlers.go b/cmd/web/handlers.go index 1f3a58f..b0e83ac 100644 --- a/cmd/web/handlers.go +++ b/cmd/web/handlers.go @@ -39,22 +39,32 @@ func (app *application) home(w http.ResponseWriter, r *http.Request) { return } - files := []string{ - "./ui/html/base.tmpl.html", - "./ui/html/pages/home.tmpl.html", - "./ui/html/partials/nav.tmpl.html", - } - - ts, err := template.ParseFiles(files...) + organisations, err := app.organisations.Latest() if err != nil { - app.serverError(w, r, err) // Use the serverError() helper + app.serverError(w, r, err) return } - err = ts.ExecuteTemplate(w, "base", nil) - if err != nil { - app.serverError(w, r, err) // Use the serverError() helper + for _, organisation := range organisations { + fmt.Fprintf(w, "%+v\n", organisation) } + + // files := []string{ + // "./ui/html/base.tmpl.html", + // "./ui/html/pages/home.tmpl.html", + // "./ui/html/partials/nav.tmpl.html", + // } + // + // ts, err := template.ParseFiles(files...) + // if err != nil { + // app.serverError(w, r, err) // Use the serverError() helper + // return + // } + // + // err = ts.ExecuteTemplate(w, "base", nil) + // if err != nil { + // app.serverError(w, r, err) // Use the serverError() helper + // } } func (app *application) organisationView(w http.ResponseWriter, r *http.Request) { diff --git a/internal/models/organisations.go b/internal/models/organisations.go index edaa992..b765f91 100644 --- a/internal/models/organisations.go +++ b/internal/models/organisations.go @@ -53,5 +53,35 @@ func (m *OrganisationModel) Get(id int) (Organisation, error) { // Ten most recent... func (m *OrganisationModel) Latest() ([]Organisation, error) { + // Pick out the last 10 + stmt := `SELECT id, name, created FROM organisations + ORDER BY id DESC LIMIT 10` + + rows, err := m.DB.Query(stmt) + if err != nil { + return nil, err + } + + defer rows.Close() + + var organisations []Organisation + + for rows.Next() { + var o Organisation + + err = rows.Scan(&o.ID, &o.Name, &o.Created) + if err != nil { + return nil, err + } + + organisations = append(organisations, o) + } + + if err = rows.Err(); err != nil { + return nil, err + } + + return organisations, err + return nil, nil } |