Current plan beats primary plan

This commit is contained in:
Deepak Mallubhotla 2021-01-31 11:54:41 -06:00
parent 8c17aa9d6d
commit 92ddd9e0fe
Signed by: deepak
GPG Key ID: 64BF53A3369104E7
9 changed files with 67 additions and 67 deletions

View File

@ -17,9 +17,9 @@ type Store interface {
SelectActionsByPlanID(plan *Plan, userID int) ([]*Action, error) SelectActionsByPlanID(plan *Plan, userID int) ([]*Action, error)
SelectUserByUsername(username string) (*User, error) SelectUserByUsername(username string) (*User, error)
InsertUser(user *User) (int, error) InsertUser(user *User) (int, error)
SelectPrimaryPlan(userID int) (*PrimaryPlan, error) SelectCurrentPlan(userID int) (*CurrentPlan, error)
InsertPrimaryPlan(primaryPlan *PrimaryPlan, userID int) error InsertCurrentPlan(currentPlan *CurrentPlan, userID int) error
UpdatePrimaryPlan(primaryPlan *PrimaryPlan, userID int) error UpdateCurrentPlan(currentPlan *CurrentPlan, userID int) error
} }
// Model represents a current model item. // Model represents a current model item.

View File

@ -1,23 +1,23 @@
package models package models
// PrimaryPlan represents the primary plan ID for a particular user ID. // CurrentPlan represents the primary plan ID for a particular user ID.
type PrimaryPlan struct { type CurrentPlan struct {
UserID int64 `json:"user_id"` UserID int64 `json:"user_id"`
PlanID int64 `json:"plan_id"` PlanID int64 `json:"plan_id"`
} }
// PrimaryPlan returns the primary plan for a provided user ID in the given model. // CurrentPlan returns the primary plan for a provided user ID in the given model.
func (m *Model) PrimaryPlan(userID int) (*PrimaryPlan, error) { func (m *Model) CurrentPlan(userID int) (*CurrentPlan, error) {
pp, err := m.SelectPrimaryPlan(userID) pp, err := m.SelectCurrentPlan(userID)
return pp, wrapNotFound(err) return pp, wrapNotFound(err)
} }
// AddPrimaryPlan inserts a given primary plan into the store, returning nothing. // AddCurrentPlan inserts a given primary plan into the store, returning nothing.
func (m *Model) AddPrimaryPlan(pp *PrimaryPlan, userID int) error { func (m *Model) AddCurrentPlan(pp *CurrentPlan, userID int) error {
return m.InsertPrimaryPlan(pp, userID) return m.InsertCurrentPlan(pp, userID)
} }
// SavePrimaryPlan saves and updates a primary plan. // SaveCurrentPlan saves and updates a primary plan.
func (m *Model) SavePrimaryPlan(pp *PrimaryPlan, userID int) error { func (m *Model) SaveCurrentPlan(pp *CurrentPlan, userID int) error {
return m.UpdatePrimaryPlan(pp, userID) return m.UpdateCurrentPlan(pp, userID)
} }

View File

@ -9,16 +9,16 @@ import (
"net/http" "net/http"
) )
// NewPrimaryPlanRouter returns a new primary plan router // NewCurrentPlanRouter returns a new primary plan router
func NewPrimaryPlanRouter(m *models.Model) http.Handler { func NewCurrentPlanRouter(m *models.Model) http.Handler {
router := chi.NewRouter() router := chi.NewRouter()
router.Get("/", getPrimaryPlanFunc(m)) router.Get("/", getCurrentPlanFunc(m))
router.Post("/", postPrimaryPlanFunc(m)) router.Post("/", postCurrentPlanFunc(m))
router.Put("/", putPrimaryPlanFunc(m)) router.Put("/", putCurrentPlanFunc(m))
return router return router
} }
func getPrimaryPlanFunc(m *models.Model) http.HandlerFunc { func getCurrentPlanFunc(m *models.Model) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
userID, userErr := tokens.GetUserID(r.Context()) userID, userErr := tokens.GetUserID(r.Context())
if userErr != nil { if userErr != nil {
@ -26,7 +26,7 @@ func getPrimaryPlanFunc(m *models.Model) http.HandlerFunc {
return return
} }
pp, err := m.PrimaryPlan(userID) pp, err := m.CurrentPlan(userID)
if err != nil { if err != nil {
if models.IsNotFoundError(err) { if models.IsNotFoundError(err) {
notFoundHandler(w, r) notFoundHandler(w, r)
@ -43,11 +43,11 @@ func getPrimaryPlanFunc(m *models.Model) http.HandlerFunc {
} }
} }
type createPrimaryPlanResponse struct { type createCurrentPlanResponse struct {
CreatedPrimaryPlan *models.PrimaryPlan `json:"created_current_plan"` CreatedCurrentPlan *models.CurrentPlan `json:"created_current_plan"`
} }
func postPrimaryPlanFunc(m *models.Model) http.HandlerFunc { func postCurrentPlanFunc(m *models.Model) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
userID, err := tokens.GetUserID(r.Context()) userID, err := tokens.GetUserID(r.Context())
@ -59,7 +59,7 @@ func postPrimaryPlanFunc(m *models.Model) http.HandlerFunc {
r.Body = http.MaxBytesReader(w, r.Body, 1024) r.Body = http.MaxBytesReader(w, r.Body, 1024)
dec := json.NewDecoder(r.Body) dec := json.NewDecoder(r.Body)
dec.DisallowUnknownFields() dec.DisallowUnknownFields()
var pp models.PrimaryPlan var pp models.CurrentPlan
err = dec.Decode(&pp) err = dec.Decode(&pp)
if err != nil { if err != nil {
badRequestError(w, err) badRequestError(w, err)
@ -71,23 +71,23 @@ func postPrimaryPlanFunc(m *models.Model) http.HandlerFunc {
return return
} }
newPP := &models.PrimaryPlan{ newPP := &models.CurrentPlan{
PlanID: pp.PlanID, PlanID: pp.PlanID,
UserID: int64(userID), UserID: int64(userID),
} }
err = m.AddPrimaryPlan(newPP, userID) err = m.AddCurrentPlan(newPP, userID)
if err != nil { if err != nil {
serverError(w, err) serverError(w, err)
return return
} }
finishedPP, err := m.PrimaryPlan(userID) finishedPP, err := m.CurrentPlan(userID)
if err != nil { if err != nil {
serverError(w, err) serverError(w, err)
return return
} }
response := &createPrimaryPlanResponse{ response := &createCurrentPlanResponse{
CreatedPrimaryPlan: finishedPP, CreatedCurrentPlan: finishedPP,
} }
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated) w.WriteHeader(http.StatusCreated)
@ -98,11 +98,11 @@ func postPrimaryPlanFunc(m *models.Model) http.HandlerFunc {
} }
} }
type updatePrimaryPlanResponse struct { type updateCurrentPlanResponse struct {
UpdatedPrimaryPlan *models.PrimaryPlan `json:"updated_current_plan"` UpdatedCurrentPlan *models.CurrentPlan `json:"updated_current_plan"`
} }
func putPrimaryPlanFunc(m *models.Model) http.HandlerFunc { func putCurrentPlanFunc(m *models.Model) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
userID, err := tokens.GetUserID(r.Context()) userID, err := tokens.GetUserID(r.Context())
@ -111,7 +111,7 @@ func putPrimaryPlanFunc(m *models.Model) http.HandlerFunc {
return return
} }
_, err = m.PrimaryPlan(userID) _, err = m.CurrentPlan(userID)
if models.IsNotFoundError(err) { if models.IsNotFoundError(err) {
notFoundHandler(w, r) notFoundHandler(w, r)
return return
@ -120,7 +120,7 @@ func putPrimaryPlanFunc(m *models.Model) http.HandlerFunc {
r.Body = http.MaxBytesReader(w, r.Body, 1024) r.Body = http.MaxBytesReader(w, r.Body, 1024)
dec := json.NewDecoder(r.Body) dec := json.NewDecoder(r.Body)
dec.DisallowUnknownFields() dec.DisallowUnknownFields()
var pp models.PrimaryPlan var pp models.CurrentPlan
err = dec.Decode(&pp) err = dec.Decode(&pp)
if err != nil { if err != nil {
badRequestError(w, err) badRequestError(w, err)
@ -132,23 +132,23 @@ func putPrimaryPlanFunc(m *models.Model) http.HandlerFunc {
return return
} }
newPP := &models.PrimaryPlan{ newPP := &models.CurrentPlan{
PlanID: pp.PlanID, PlanID: pp.PlanID,
UserID: int64(userID), UserID: int64(userID),
} }
err = m.SavePrimaryPlan(newPP, userID) err = m.SaveCurrentPlan(newPP, userID)
if err != nil { if err != nil {
serverError(w, err) serverError(w, err)
return return
} }
newPP, err = m.PrimaryPlan(userID) newPP, err = m.CurrentPlan(userID)
if err != nil { if err != nil {
serverError(w, err) serverError(w, err)
return return
} }
response := &updatePrimaryPlanResponse{ response := &updateCurrentPlanResponse{
UpdatedPrimaryPlan: newPP, UpdatedCurrentPlan: newPP,
} }
w.Header().Add("Content-Type", "application/json") w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)

View File

@ -18,7 +18,7 @@ func NewRouter(m *models.Model, tok tokens.Toker) http.Handler {
r.Mount("/actions", NewActionRouter(m)) r.Mount("/actions", NewActionRouter(m))
r.Mount("/plans", NewPlanRouter(m)) r.Mount("/plans", NewPlanRouter(m))
r.Mount("/me", NewCurrentUserRouter(m)) r.Mount("/me", NewCurrentUserRouter(m))
r.Mount("/currentPlan", NewPrimaryPlanRouter(m)) r.Mount("/currentPlan", NewCurrentPlanRouter(m))
}) })
router.Mount("/auth", NewAuthRouter(m, tok)) router.Mount("/auth", NewAuthRouter(m, tok))
router.Mount("/health", newHealthRouter(m)) router.Mount("/health", newHealthRouter(m))

View File

@ -57,18 +57,18 @@ func (e *errorStore) InsertUser(user *models.User) (int, error) {
return 0, nil return 0, nil
} }
func (e *errorStore) SelectPrimaryPlan(userID int) (*models.PrimaryPlan, error) { func (e *errorStore) SelectCurrentPlan(userID int) (*models.CurrentPlan, error) {
return nil, e.error return nil, e.error
} }
func (e *errorStore) InsertPrimaryPlan(primaryPlan *models.PrimaryPlan, userID int) error { func (e *errorStore) InsertCurrentPlan(currentPlan *models.CurrentPlan, userID int) error {
if e.errorOnInsert { if e.errorOnInsert {
return e.error return e.error
} }
return nil return nil
} }
func (e *errorStore) UpdatePrimaryPlan(primaryPlan *models.PrimaryPlan, userID int) error { func (e *errorStore) UpdateCurrentPlan(currentPlan *models.CurrentPlan, userID int) error {
if e.errorOnInsert { if e.errorOnInsert {
return e.error return e.error
} }

View File

@ -10,7 +10,7 @@ type inMemoryStore struct {
actions []*models.Action actions []*models.Action
plans []*models.Plan plans []*models.Plan
users []*models.User users []*models.User
primaryPlans []*models.PrimaryPlan currentPlans []*models.CurrentPlan
} }
// GetInMemoryStore provides a purely in memory store, for testing purposes only, with no persistence. // GetInMemoryStore provides a purely in memory store, for testing purposes only, with no persistence.
@ -100,17 +100,17 @@ func (store *inMemoryStore) InsertPlan(plan *models.Plan, userID int) (int, erro
return id, nil return id, nil
} }
func (store *inMemoryStore) SelectPrimaryPlan(userID int) (*models.PrimaryPlan, error) { func (store *inMemoryStore) SelectCurrentPlan(userID int) (*models.CurrentPlan, error) {
for _, primaryPlan := range store.primaryPlans { for _, currentPlan := range store.currentPlans {
if userID == int(primaryPlan.UserID) { if userID == int(currentPlan.UserID) {
return primaryPlan, nil return currentPlan, nil
} }
} }
return nil, sql.ErrNoRows return nil, sql.ErrNoRows
} }
func (store *inMemoryStore) InsertPrimaryPlan(primaryPlan *models.PrimaryPlan, userID int) error { func (store *inMemoryStore) InsertCurrentPlan(currentPlan *models.CurrentPlan, userID int) error {
_, err := store.SelectPrimaryPlan(userID) _, err := store.SelectCurrentPlan(userID)
if err != sql.ErrNoRows { if err != sql.ErrNoRows {
return err return err
} }
@ -118,16 +118,16 @@ func (store *inMemoryStore) InsertPrimaryPlan(primaryPlan *models.PrimaryPlan, u
return fmt.Errorf("Can't insert primary plan") return fmt.Errorf("Can't insert primary plan")
} }
store.primaryPlans = append(store.primaryPlans, &models.PrimaryPlan{PlanID: int64(primaryPlan.PlanID), UserID: int64(userID)}) store.currentPlans = append(store.currentPlans, &models.CurrentPlan{PlanID: int64(currentPlan.PlanID), UserID: int64(userID)})
return nil return nil
} }
func (store *inMemoryStore) UpdatePrimaryPlan(primaryPlan *models.PrimaryPlan, userID int) error { func (store *inMemoryStore) UpdateCurrentPlan(currentPlan *models.CurrentPlan, userID int) error {
current, err := store.SelectPrimaryPlan(userID) current, err := store.SelectCurrentPlan(userID)
if err != nil { if err != nil {
return err return err
} }
current.PlanID = primaryPlan.PlanID current.PlanID = currentPlan.PlanID
return nil return nil
} }

View File

@ -1,5 +1,5 @@
DROP TABLE IF EXISTS actions; DROP TABLE IF EXISTS actions;
DROP TABLE IF EXISTS user_primary_plan; DROP TABLE IF EXISTS user_current_plan;
DROP TABLE IF EXISTS plans; DROP TABLE IF EXISTS plans;
DROP TABLE IF EXISTS users; DROP TABLE IF EXISTS users;

View File

@ -16,7 +16,7 @@ CREATE TABLE IF NOT EXISTS plans(
UNIQUE (user_id, plan_id) UNIQUE (user_id, plan_id)
); );
CREATE TABLE IF NOT EXISTS user_primary_plan( CREATE TABLE IF NOT EXISTS user_current_plan(
user_id int PRIMARY KEY, user_id int PRIMARY KEY,
plan_id int, plan_id int,
FOREIGN KEY (user_id, plan_id) REFERENCES plans(user_id, plan_id) FOREIGN KEY (user_id, plan_id) REFERENCES plans(user_id, plan_id)

View File

@ -175,9 +175,9 @@ func (store *postgresStore) InsertUser(user *models.User) (int, error) {
return id, nil return id, nil
} }
func (store *postgresStore) SelectPrimaryPlan(userID int) (*models.PrimaryPlan, error) { func (store *postgresStore) SelectCurrentPlan(userID int) (*models.CurrentPlan, error) {
pp := models.PrimaryPlan{} pp := models.CurrentPlan{}
queryString := store.db.Rebind(`SELECT user_id, plan_id FROM user_primary_plan WHERE user_id = ?`) queryString := store.db.Rebind(`SELECT user_id, plan_id FROM user_current_plan WHERE user_id = ?`)
err := store.db.Get(&pp, queryString, userID) err := store.db.Get(&pp, queryString, userID)
if err != nil { if err != nil {
return nil, err return nil, err
@ -185,11 +185,11 @@ func (store *postgresStore) SelectPrimaryPlan(userID int) (*models.PrimaryPlan,
return &pp, nil return &pp, nil
} }
func (store *postgresStore) InsertPrimaryPlan(primaryPlan *models.PrimaryPlan, userID int) error { func (store *postgresStore) InsertCurrentPlan(currentPlan *models.CurrentPlan, userID int) error {
queryString := store.db.Rebind("INSERT INTO user_primary_plan (user_id, plan_id) VALUES (?, ?) RETURNING user_id") queryString := store.db.Rebind("INSERT INTO user_current_plan (user_id, plan_id) VALUES (?, ?) RETURNING user_id")
tx := store.db.MustBegin() tx := store.db.MustBegin()
var id int var id int
err := tx.Get(&id, queryString, primaryPlan.PlanID, userID) err := tx.Get(&id, queryString, currentPlan.PlanID, userID)
if err != nil { if err != nil {
tx.Rollback() tx.Rollback()
return err return err
@ -201,13 +201,13 @@ func (store *postgresStore) InsertPrimaryPlan(primaryPlan *models.PrimaryPlan, u
return nil return nil
} }
func (store *postgresStore) UpdatePrimaryPlan(primaryPlan *models.PrimaryPlan, userID int) error { func (store *postgresStore) UpdateCurrentPlan(currentPlan *models.CurrentPlan, userID int) error {
query := `UPDATE user_primary_plan SET query := `UPDATE user_current_plan SET
plan_id = :plan_id plan_id = :plan_id
WHERE user_id = :user_id` WHERE user_id = :user_id`
tx := store.db.MustBegin() tx := store.db.MustBegin()
ppToUse := &models.PrimaryPlan{ ppToUse := &models.CurrentPlan{
PlanID: primaryPlan.PlanID, PlanID: currentPlan.PlanID,
UserID: int64(userID), UserID: int64(userID),
} }
_, err := store.db.NamedExec(query, ppToUse) _, err := store.db.NamedExec(query, ppToUse)