diff options
author | Matthew Lemon <y@yulqen.org> | 2024-02-09 16:38:46 +0000 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-02-09 16:38:46 +0000 |
commit | 7c4a913645ba1e347ab94097de44eb04d7df4e47 (patch) | |
tree | 82fe2f9db6c549756f4f1e96ff1e781c0099603c | |
parent | b8ed10b800d24a94bb6a9339d5764c6dbe7d9f6e (diff) |
Adds the Organisations model
We have separation of concerns: the model is initalised and passed into
the app struct, therefore our database logic will not be tied to our
handlers.
Our model, with its methods, is nicely encapsulated. We can initialise
it and pass it to our handlers as a dependency.
We can in future create an interface to mock the OrganisationModel
object to be used in unit testing.
We can also switch databases (theoretically) but providing a -dsn
command-line flag.
-rw-r--r-- | cmd/web/main.go | 7 | ||||
-rw-r--r-- | internal/models/organisations.go | 29 |
2 files changed, 34 insertions, 2 deletions
diff --git a/cmd/web/main.go b/cmd/web/main.go index b767275..cbfda5e 100644 --- a/cmd/web/main.go +++ b/cmd/web/main.go @@ -8,10 +8,12 @@ import ( "os" _ "github.com/go-sql-driver/mysql" + "github.com/yulqen/ded-go-core/internal/models" ) type application struct { - logger *slog.Logger + logger *slog.Logger + organisations *models.OrganisationModel } func main() { @@ -31,7 +33,8 @@ func main() { defer db.Close() app := &application{ - logger: logger, + logger: logger, + organisations: &models.OrganisationModel{DB: db}, } // mux := http.NewServeMux() diff --git a/internal/models/organisations.go b/internal/models/organisations.go new file mode 100644 index 0000000..7a8898c --- /dev/null +++ b/internal/models/organisations.go @@ -0,0 +1,29 @@ +package models + +import ( + "database/sql" + "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) { + return 0, nil +} + +func (m *OrganisationModel) Get(id int) (Organisation, error) { + return Organisation{}, nil +} + +// Ten most recent... +func (m *OrganisationModel) Latest() ([]Organisation, error) { + return nil, nil +} |