From b39ce645d63db238406d33544090e5cf76029179 Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Thu, 29 Feb 2024 20:33:20 +0100 Subject: [PATCH] Proper interface for the database --- backend/internal/database/db.go | 11 ++++++++++- backend/internal/database/db_test.go | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/backend/internal/database/db.go b/backend/internal/database/db.go index c14ba97..b850289 100644 --- a/backend/internal/database/db.go +++ b/backend/internal/database/db.go @@ -10,6 +10,15 @@ import ( _ "github.com/mattn/go-sqlite3" ) +// Interface for the database +type Database interface { + AddUser(username string, password string) error + RemoveUser(username string) error + GetUserId(username string) (int, error) + AddProject(name string, description string, username string) error + Migrate(dirname string) error +} + // This struct is a wrapper type that holds the database connection // Internally DB holds a connection pool, so it's safe for concurrent use type Db struct { @@ -23,7 +32,7 @@ const userInsert = "INSERT INTO users (username, password) VALUES (?, ?)" const projectInsert = "INSERT INTO projects (name, description, user_id) SELECT ?, ?, id FROM users WHERE username = ?" // DbConnect connects to the database -func DbConnect(dbpath string) *Db { +func DbConnect(dbpath string) Database { // Open the database db, err := sqlx.Connect("sqlite3", dbpath) if err != nil { diff --git a/backend/internal/database/db_test.go b/backend/internal/database/db_test.go index 62e47db..716bde8 100644 --- a/backend/internal/database/db_test.go +++ b/backend/internal/database/db_test.go @@ -6,7 +6,7 @@ import ( // Tests are not guaranteed to be sequential -func setupState() (*Db, error) { +func setupState() (Database, error) { db := DbConnect(":memory:") err := db.Migrate("../../migrations") if err != nil {