From c3df229e32a0a50ce4c7b6994e2ed01d6be1e13c Mon Sep 17 00:00:00 2001 From: Matthew Lemon Date: Sat, 10 Feb 2024 12:19:10 +0000 Subject: Have added Insert handler for Organisation model This is a straight forward wrapper around INSERT. Also added a clientError into a new helpers.go file. --- internal/models/organisations.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'internal') diff --git a/internal/models/organisations.go b/internal/models/organisations.go index 7a8898c..291562b 100644 --- a/internal/models/organisations.go +++ b/internal/models/organisations.go @@ -16,7 +16,19 @@ type OrganisationModel struct { } func (m *OrganisationModel) Insert(name string) (int, error) { - return 0, nil + 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) { -- cgit v1.2.3