diff options
author | Matthew Lemon <y@yulqen.org> | 2024-02-10 19:22:56 +0000 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-02-10 19:22:56 +0000 |
commit | 58967839c646c717b5345f3a3d2924be5c6d5a4b (patch) | |
tree | 3c537852823fb90c374eae385e660b18e05a2ced /internal/models | |
parent | 030806353ca9b329bfb0102ebc10518b641511ee (diff) |
Adds the Get handler for organisation model and fixes
Also fixes the NotAuthorised response I was mistakenly getting when it
should have been a 404.
Diffstat (limited to '')
-rw-r--r-- | internal/models/errors.go | 7 | ||||
-rw-r--r-- | internal/models/organisations.go | 18 |
2 files changed, 24 insertions, 1 deletions
diff --git a/internal/models/errors.go b/internal/models/errors.go new file mode 100644 index 0000000..a70c7dc --- /dev/null +++ b/internal/models/errors.go @@ -0,0 +1,7 @@ +package models + +import ( + "errors" +) + +var ErrNoRecord = errors.New("models: no matching record found") diff --git a/internal/models/organisations.go b/internal/models/organisations.go index 291562b..edaa992 100644 --- a/internal/models/organisations.go +++ b/internal/models/organisations.go @@ -2,6 +2,7 @@ package models import ( "database/sql" + "errors" "time" ) @@ -32,7 +33,22 @@ func (m *OrganisationModel) Insert(name string) (int, error) { } func (m *OrganisationModel) Get(id int) (Organisation, error) { - return Organisation{}, nil + stmt := `SELECT id, name, created FROM organisations + WHERE id = ?` + + row := m.DB.QueryRow(stmt, id) + + var o Organisation + + err := row.Scan(&o.ID, &o.Name, &o.Created) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return Organisation{}, ErrNoRecord + } else { + return Organisation{}, err + } + } + return o, nil } // Ten most recent... |