gogmagog/store/postgres.go
Deepak 2c630aff95
Some checks are pending
gitea-deepak/gogmagog/pipeline/head This commit looks good
gitea-deepak/gogmagog/pipeline/pr-master Build queued...
Adds additional postgres insert fields for action
2021-01-09 22:25:14 -06:00

115 lines
3.1 KiB
Go

package store
import (
"gitea.deepak.science/deepak/gogmagog/models"
"gitea.deepak.science/deepak/gogmagog/util"
"github.com/jmoiron/sqlx"
)
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, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions")
if err != nil {
return nil, err
}
return actions, nil
}
func (store *postgresStore) SelectActionsByPlanID(plan *models.Plan) ([]*models.Action, error) {
queryString := store.db.Rebind("SELECT action_id, action_description, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE plan_id = ?")
actions := make([]*models.Action, 0)
err := store.db.Select(&actions, queryString, plan.PlanID)
if err != nil {
return nil, err
}
return actions, nil
}
func (store *postgresStore) SelectActionByID(id int) (*models.Action, error) {
action := models.Action{}
err := store.db.Get(&action, store.db.Rebind("SELECT action_id, action_description, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE action_id = ?"), id)
if err != nil {
return nil, err
}
return &action, nil
}
func (store *postgresStore) InsertAction(action *models.Action) (int, error) {
queryString := store.db.Rebind(
`INSERT INTO actions (action_description,
estimated_chunks,
completed_chunks,
completed_on,
plan_id) VALUES (?, ?, ?, ?, ?) RETURNING action_id`,
)
tx := store.db.MustBegin()
var id int
err := tx.Get(
&id,
queryString,
action.ActionDescription,
action.EstimatedChunks,
action.CompletedChunks,
action.CompletedOn,
action.PlanID,
)
if err != nil {
tx.Rollback()
return -1, err
}
err = tx.Commit()
if err != nil {
return -1, err
}
return id, 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
}
func (store *postgresStore) SelectPlanByID(id int) (*models.Plan, error) {
plan := models.Plan{}
err := store.db.Get(&plan, store.db.Rebind("SELECT plan_id, plan_date FROM plans WHERE plan_id = ?"), id)
if err != nil {
return nil, err
}
return &plan, nil
}
func (store *postgresStore) InsertPlan(plan *models.Plan) (int, error) {
queryString := store.db.Rebind("INSERT INTO plans (plan_date) VALUES (?) RETURNING plan_id")
tx := store.db.MustBegin()
var id int
err := tx.Get(&id, queryString, plan.PlanDate)
if err != nil {
tx.Rollback()
return -1, err
}
err = tx.Commit()
if err != nil {
return -1, err
}
return id, nil
}
func (store *postgresStore) ConnectionLive() error {
return store.db.Ping()
}