Adds some db tests using sql mock and slightly reorganises to facilitate testing
All checks were successful
gitea-deepak/gogmagog/pipeline/head This commit looks good
All checks were successful
gitea-deepak/gogmagog/pipeline/head This commit looks good
This commit is contained in:
37
store/postgres.go
Normal file
37
store/postgres.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
||||
"gitea.deepak.science/deepak/gogmagog/models"
|
||||
"gitea.deepak.science/deepak/gogmagog/util"
|
||||
)
|
||||
|
||||
type postgresStore struct {
|
||||
db *sqlx.DB
|
||||
}
|
||||
|
||||
// GetPostgresStore provides a store to back the model based on a postgres connection.
|
||||
func GetPostgresStore(db *sqlx.DB) (models.Store, error) {
|
||||
|
||||
db.MapperFunc(util.ToSnake)
|
||||
return &postgresStore{db: db}, nil
|
||||
}
|
||||
|
||||
func (store *postgresStore) SelectActions() ([]*models.Action, error) {
|
||||
actions := make([]*models.Action, 0)
|
||||
err := store.db.Select(&actions, "SELECT action_id, action_description, created_at, updated_at FROM actions")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return actions, nil
|
||||
}
|
||||
|
||||
func (store *postgresStore) SelectPlans() ([]*models.Plan, error) {
|
||||
plans := make([]*models.Plan, 0)
|
||||
err := store.db.Select(&plans, "SELECT plan_id, plan_date FROM plans")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return plans, nil
|
||||
}
|
||||
Reference in New Issue
Block a user