diff options
author | Matthew Lemon <y@yulqen.org> | 2024-04-23 11:16:38 +0100 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-04-23 11:16:38 +0100 |
commit | 0f951dcf029d4af284467543a3afdf5bf6581a20 (patch) | |
tree | a48384210cdc168e3bd3ccff6d6d516eeed9e748 /internal/models/organisations.go | |
parent | 8b084e9fe7a5f3a04c32daf9a24f7f2cf67300f9 (diff) |
switched to Django
Diffstat (limited to 'internal/models/organisations.go')
-rw-r--r-- | internal/models/organisations.go | 85 |
1 files changed, 0 insertions, 85 deletions
diff --git a/internal/models/organisations.go b/internal/models/organisations.go deleted file mode 100644 index 88f7bc9..0000000 --- a/internal/models/organisations.go +++ /dev/null @@ -1,85 +0,0 @@ -package models - -import ( - "database/sql" - "errors" - "time" -) - -type Organisation struct { - ID int - Name string - Created time.Time -} - -type OrganisationModel struct { - DB *sql.DB -} - -func (m *OrganisationModel) Insert(name string) (int, error) { - stmt := `INSERT INTO organisations (name, created) - VALUEs (?, UTC_TIMESTAMP())` - - result, err := m.DB.Exec(stmt, name) - if err != nil { - return 0, err - } - - id, err := result.LastInsertId() - if err != nil { - return 0, err - } - return int(id), nil -} - -func (m *OrganisationModel) Get(id int) (Organisation, error) { - 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... -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 -} |