225 lines
6.2 KiB
Go
225 lines
6.2 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(userID int) ([]*models.Action, error) {
|
|
queryString := store.db.Rebind("SELECT action_id, action_description, user_id, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE user_id = ?")
|
|
actions := make([]*models.Action, 0)
|
|
err := store.db.Select(&actions, queryString, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return actions, nil
|
|
}
|
|
|
|
func (store *postgresStore) SelectActionsByPlanID(plan *models.Plan, userID int) ([]*models.Action, error) {
|
|
queryString := store.db.Rebind("SELECT action_id, action_description, user_id, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE plan_id = ? AND user_id = ?")
|
|
actions := make([]*models.Action, 0)
|
|
err := store.db.Select(&actions, queryString, plan.PlanID, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return actions, nil
|
|
}
|
|
|
|
func (store *postgresStore) SelectActionByID(id int, userID int) (*models.Action, error) {
|
|
queryString := store.db.Rebind("SELECT action_id, action_description, user_id, estimated_chunks, completed_chunks, completed_on, created_at, updated_at, plan_id FROM actions WHERE action_id = ? AND user_id = ?")
|
|
action := models.Action{}
|
|
err := store.db.Get(&action, queryString, id, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &action, nil
|
|
}
|
|
|
|
func (store *postgresStore) InsertAction(action *models.Action, userID int) (int, error) {
|
|
queryString := store.db.Rebind(
|
|
`INSERT INTO actions (action_description,
|
|
user_id,
|
|
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,
|
|
userID,
|
|
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) UpdateAction(action *models.Action, userID int) error {
|
|
query := `UPDATE actions SET
|
|
action_description = :action_description,
|
|
estimated_chunks = :estimated_chunks,
|
|
completed_chunks = :completed_chunks,
|
|
completed_on = :completed_on,
|
|
plan_id = :plan_id
|
|
WHERE action_id = :action_id
|
|
AND user_id = :user_id`
|
|
tx := store.db.MustBegin()
|
|
actionToUse := &models.Action{
|
|
ActionDescription: action.ActionDescription,
|
|
EstimatedChunks: action.EstimatedChunks,
|
|
CompletedChunks: action.CompletedChunks,
|
|
CompletedOn: action.CompletedOn,
|
|
PlanID: action.PlanID,
|
|
ActionID: action.ActionID,
|
|
UserID: int64(userID),
|
|
}
|
|
_, err := store.db.NamedExec(query, actionToUse)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
return err
|
|
}
|
|
err = tx.Commit()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
|
|
}
|
|
|
|
func (store *postgresStore) SelectPlans(userID int) ([]*models.Plan, error) {
|
|
queryString := store.db.Rebind("SELECT plan_id, plan_date, user_id FROM plans WHERE user_id = ?")
|
|
plans := make([]*models.Plan, 0)
|
|
err := store.db.Select(&plans, queryString, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return plans, nil
|
|
}
|
|
|
|
func (store *postgresStore) SelectPlanByID(id int, userID int) (*models.Plan, error) {
|
|
plan := models.Plan{}
|
|
err := store.db.Get(&plan, store.db.Rebind("SELECT plan_id, plan_date, user_id FROM plans WHERE plan_id = ? AND user_id = ?"), id, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &plan, nil
|
|
}
|
|
|
|
func (store *postgresStore) InsertPlan(plan *models.Plan, userID int) (int, error) {
|
|
queryString := store.db.Rebind("INSERT INTO plans (plan_date, user_id) VALUES (?, ?) RETURNING plan_id")
|
|
tx := store.db.MustBegin()
|
|
var id int
|
|
err := tx.Get(&id, queryString, plan.PlanDate, userID)
|
|
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()
|
|
}
|
|
|
|
func (store *postgresStore) SelectUserByUsername(username string) (*models.User, error) {
|
|
user := models.User{}
|
|
err := store.db.Get(&user, store.db.Rebind("SELECT user_id, username, display_name, password FROM users WHERE username = ?"), username)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
func (store *postgresStore) InsertUser(user *models.User) (int, error) {
|
|
queryString := store.db.Rebind("INSERT INTO users (username, display_name, password) VALUES (?, ?, ?) RETURNING user_id")
|
|
tx := store.db.MustBegin()
|
|
var id int
|
|
err := tx.Get(&id, queryString, user.Username, user.DisplayName, user.Password)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
return -1, err
|
|
}
|
|
err = tx.Commit()
|
|
if err != nil {
|
|
return -1, err
|
|
}
|
|
return id, nil
|
|
}
|
|
|
|
func (store *postgresStore) SelectPrimaryPlan(userID int) (*models.PrimaryPlan, error) {
|
|
pp := models.PrimaryPlan{}
|
|
queryString := store.db.Rebind(`SELECT user_id, plan_id FROM user_primary_plan WHERE user_id = ?`)
|
|
err := store.db.Get(&pp, queryString, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &pp, nil
|
|
}
|
|
|
|
func (store *postgresStore) InsertPrimaryPlan(primaryPlan *models.PrimaryPlan, userID int) error {
|
|
queryString := store.db.Rebind("INSERT INTO user_primary_plan (user_id, plan_id) VALUES (?, ?) RETURNING user_id")
|
|
tx := store.db.MustBegin()
|
|
var id int
|
|
err := tx.Get(&id, queryString, primaryPlan.PlanID, userID)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
return err
|
|
}
|
|
err = tx.Commit()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (store *postgresStore) UpdatePrimaryPlan(primaryPlan *models.PrimaryPlan, userID int) error {
|
|
query := `UPDATE user_primary_plan SET
|
|
plan_id = :plan_id
|
|
WHERE user_id = :user_id`
|
|
tx := store.db.MustBegin()
|
|
ppToUse := &models.PrimaryPlan{
|
|
PlanID: primaryPlan.PlanID,
|
|
UserID: int64(userID),
|
|
}
|
|
_, err := store.db.NamedExec(query, ppToUse)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
return err
|
|
}
|
|
err = tx.Commit()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
|
|
}
|