diff --git a/controller/controller.go b/controller/controller.go index 55cefc42e..fad69a862 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -3,8 +3,6 @@ package controller import ( "context" "database/sql" - - "github.com/tailwarden/komiser/repository" ) type totalOutput struct { @@ -32,7 +30,7 @@ type accountOutput struct { } type Repository interface { - HandleQuery(context.Context, repository.QueryType, interface{}, [][3]string) (sql.Result, error) + HandleQuery(context.Context, string, interface{}, [][3]string) (sql.Result, error) } type Controller struct { diff --git a/handlers/resources_handler.go b/handlers/resources_handler.go index aecc52247..84f4f5ae3 100644 --- a/handlers/resources_handler.go +++ b/handlers/resources_handler.go @@ -12,6 +12,8 @@ import ( "github.com/sirupsen/logrus" "github.com/tailwarden/komiser/controller" "github.com/tailwarden/komiser/models" + "github.com/tailwarden/komiser/repository/postgres" + "github.com/tailwarden/komiser/repository/sqlite" "github.com/tailwarden/komiser/utils" "github.com/uptrace/bun" "github.com/uptrace/bun/dialect" @@ -29,8 +31,16 @@ type ApiHandler struct { } func NewApiHandler(ctx context.Context, telemetry bool, analytics utils.Analytics, db *bun.DB, cfg models.Config, configPath string, accounts []models.Account) *ApiHandler { + var repo controller.Repository + if db.Dialect().Name() == dialect.SQLite { + repo = sqlite.NewRepository(db) + } else { + repo = postgres.NewRepository(db) + } + handler := ApiHandler{ db: db, + ctrl: controller.New(repo), ctx: ctx, telemetry: telemetry, cfg: cfg, diff --git a/repository/postgres/postgres.go b/repository/postgres/postgres.go index b216bcb67..356722cd5 100644 --- a/repository/postgres/postgres.go +++ b/repository/postgres/postgres.go @@ -1,4 +1,4 @@ -package sql +package postgres import ( "context" @@ -9,11 +9,12 @@ import ( ) type Repository struct { - db *bun.DB + db *bun.DB + queries map[string]repository.Object } func NewRepository(db *bun.DB) *Repository { - return &Repository{db: db} + return &Repository{db: db, queries: Queries} } var Queries = map[string]repository.Object{ diff --git a/repository/sqlite/sqlite.go b/repository/sqlite/sqlite.go index cf329c43c..b7b8cb65d 100644 --- a/repository/sqlite/sqlite.go +++ b/repository/sqlite/sqlite.go @@ -1,4 +1,4 @@ -package sql +package sqlite import ( "context"