From 92ddd9e0feb5fea9934986a125bc34ae68367d1d Mon Sep 17 00:00:00 2001 From: Deepak Date: Sun, 31 Jan 2021 11:54:41 -0600 Subject: [PATCH] Current plan beats primary plan --- models/models.go | 6 +-- models/primary_plan.go | 22 ++++---- routes/primary_plans.go | 52 +++++++++---------- routes/routes.go | 2 +- store/errorStore.go | 6 +-- store/inmemory.go | 22 ++++---- .../000001_create_action_table.down.sql | 2 +- .../000001_create_action_table.up.sql | 2 +- store/postgres.go | 20 +++---- 9 files changed, 67 insertions(+), 67 deletions(-) diff --git a/models/models.go b/models/models.go index a9d1a0a..b044696 100644 --- a/models/models.go +++ b/models/models.go @@ -17,9 +17,9 @@ type Store interface { SelectActionsByPlanID(plan *Plan, userID int) ([]*Action, error) SelectUserByUsername(username string) (*User, error) InsertUser(user *User) (int, error) - SelectPrimaryPlan(userID int) (*PrimaryPlan, error) - InsertPrimaryPlan(primaryPlan *PrimaryPlan, userID int) error - UpdatePrimaryPlan(primaryPlan *PrimaryPlan, userID int) error + SelectCurrentPlan(userID int) (*CurrentPlan, error) + InsertCurrentPlan(currentPlan *CurrentPlan, userID int) error + UpdateCurrentPlan(currentPlan *CurrentPlan, userID int) error } // Model represents a current model item. diff --git a/models/primary_plan.go b/models/primary_plan.go index 406ede5..9e460a3 100644 --- a/models/primary_plan.go +++ b/models/primary_plan.go @@ -1,23 +1,23 @@ package models -// PrimaryPlan represents the primary plan ID for a particular user ID. -type PrimaryPlan struct { +// CurrentPlan represents the primary plan ID for a particular user ID. +type CurrentPlan struct { UserID int64 `json:"user_id"` PlanID int64 `json:"plan_id"` } -// PrimaryPlan returns the primary plan for a provided user ID in the given model. -func (m *Model) PrimaryPlan(userID int) (*PrimaryPlan, error) { - pp, err := m.SelectPrimaryPlan(userID) +// CurrentPlan returns the primary plan for a provided user ID in the given model. +func (m *Model) CurrentPlan(userID int) (*CurrentPlan, error) { + pp, err := m.SelectCurrentPlan(userID) return pp, wrapNotFound(err) } -// AddPrimaryPlan inserts a given primary plan into the store, returning nothing. -func (m *Model) AddPrimaryPlan(pp *PrimaryPlan, userID int) error { - return m.InsertPrimaryPlan(pp, userID) +// AddCurrentPlan inserts a given primary plan into the store, returning nothing. +func (m *Model) AddCurrentPlan(pp *CurrentPlan, userID int) error { + return m.InsertCurrentPlan(pp, userID) } -// SavePrimaryPlan saves and updates a primary plan. -func (m *Model) SavePrimaryPlan(pp *PrimaryPlan, userID int) error { - return m.UpdatePrimaryPlan(pp, userID) +// SaveCurrentPlan saves and updates a primary plan. +func (m *Model) SaveCurrentPlan(pp *CurrentPlan, userID int) error { + return m.UpdateCurrentPlan(pp, userID) } diff --git a/routes/primary_plans.go b/routes/primary_plans.go index 707ac46..c6482c4 100644 --- a/routes/primary_plans.go +++ b/routes/primary_plans.go @@ -9,16 +9,16 @@ import ( "net/http" ) -// NewPrimaryPlanRouter returns a new primary plan router -func NewPrimaryPlanRouter(m *models.Model) http.Handler { +// NewCurrentPlanRouter returns a new primary plan router +func NewCurrentPlanRouter(m *models.Model) http.Handler { router := chi.NewRouter() - router.Get("/", getPrimaryPlanFunc(m)) - router.Post("/", postPrimaryPlanFunc(m)) - router.Put("/", putPrimaryPlanFunc(m)) + router.Get("/", getCurrentPlanFunc(m)) + router.Post("/", postCurrentPlanFunc(m)) + router.Put("/", putCurrentPlanFunc(m)) return router } -func getPrimaryPlanFunc(m *models.Model) http.HandlerFunc { +func getCurrentPlanFunc(m *models.Model) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { userID, userErr := tokens.GetUserID(r.Context()) if userErr != nil { @@ -26,7 +26,7 @@ func getPrimaryPlanFunc(m *models.Model) http.HandlerFunc { return } - pp, err := m.PrimaryPlan(userID) + pp, err := m.CurrentPlan(userID) if err != nil { if models.IsNotFoundError(err) { notFoundHandler(w, r) @@ -43,11 +43,11 @@ func getPrimaryPlanFunc(m *models.Model) http.HandlerFunc { } } -type createPrimaryPlanResponse struct { - CreatedPrimaryPlan *models.PrimaryPlan `json:"created_current_plan"` +type createCurrentPlanResponse struct { + 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) { 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) dec := json.NewDecoder(r.Body) dec.DisallowUnknownFields() - var pp models.PrimaryPlan + var pp models.CurrentPlan err = dec.Decode(&pp) if err != nil { badRequestError(w, err) @@ -71,23 +71,23 @@ func postPrimaryPlanFunc(m *models.Model) http.HandlerFunc { return } - newPP := &models.PrimaryPlan{ + newPP := &models.CurrentPlan{ PlanID: pp.PlanID, UserID: int64(userID), } - err = m.AddPrimaryPlan(newPP, userID) + err = m.AddCurrentPlan(newPP, userID) if err != nil { serverError(w, err) return } - finishedPP, err := m.PrimaryPlan(userID) + finishedPP, err := m.CurrentPlan(userID) if err != nil { serverError(w, err) return } - response := &createPrimaryPlanResponse{ - CreatedPrimaryPlan: finishedPP, + response := &createCurrentPlanResponse{ + CreatedCurrentPlan: finishedPP, } w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusCreated) @@ -98,11 +98,11 @@ func postPrimaryPlanFunc(m *models.Model) http.HandlerFunc { } } -type updatePrimaryPlanResponse struct { - UpdatedPrimaryPlan *models.PrimaryPlan `json:"updated_current_plan"` +type updateCurrentPlanResponse struct { + 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) { userID, err := tokens.GetUserID(r.Context()) @@ -111,7 +111,7 @@ func putPrimaryPlanFunc(m *models.Model) http.HandlerFunc { return } - _, err = m.PrimaryPlan(userID) + _, err = m.CurrentPlan(userID) if models.IsNotFoundError(err) { notFoundHandler(w, r) return @@ -120,7 +120,7 @@ func putPrimaryPlanFunc(m *models.Model) http.HandlerFunc { r.Body = http.MaxBytesReader(w, r.Body, 1024) dec := json.NewDecoder(r.Body) dec.DisallowUnknownFields() - var pp models.PrimaryPlan + var pp models.CurrentPlan err = dec.Decode(&pp) if err != nil { badRequestError(w, err) @@ -132,23 +132,23 @@ func putPrimaryPlanFunc(m *models.Model) http.HandlerFunc { return } - newPP := &models.PrimaryPlan{ + newPP := &models.CurrentPlan{ PlanID: pp.PlanID, UserID: int64(userID), } - err = m.SavePrimaryPlan(newPP, userID) + err = m.SaveCurrentPlan(newPP, userID) if err != nil { serverError(w, err) return } - newPP, err = m.PrimaryPlan(userID) + newPP, err = m.CurrentPlan(userID) if err != nil { serverError(w, err) return } - response := &updatePrimaryPlanResponse{ - UpdatedPrimaryPlan: newPP, + response := &updateCurrentPlanResponse{ + UpdatedCurrentPlan: newPP, } w.Header().Add("Content-Type", "application/json") w.WriteHeader(http.StatusOK) diff --git a/routes/routes.go b/routes/routes.go index 96eeef0..cc95052 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -18,7 +18,7 @@ func NewRouter(m *models.Model, tok tokens.Toker) http.Handler { r.Mount("/actions", NewActionRouter(m)) r.Mount("/plans", NewPlanRouter(m)) r.Mount("/me", NewCurrentUserRouter(m)) - r.Mount("/currentPlan", NewPrimaryPlanRouter(m)) + r.Mount("/currentPlan", NewCurrentPlanRouter(m)) }) router.Mount("/auth", NewAuthRouter(m, tok)) router.Mount("/health", newHealthRouter(m)) diff --git a/store/errorStore.go b/store/errorStore.go index 7b8657b..eb3f9bf 100644 --- a/store/errorStore.go +++ b/store/errorStore.go @@ -57,18 +57,18 @@ func (e *errorStore) InsertUser(user *models.User) (int, error) { 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 } -func (e *errorStore) InsertPrimaryPlan(primaryPlan *models.PrimaryPlan, userID int) error { +func (e *errorStore) InsertCurrentPlan(currentPlan *models.CurrentPlan, userID int) error { if e.errorOnInsert { return e.error } 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 { return e.error } diff --git a/store/inmemory.go b/store/inmemory.go index 95305ff..783e322 100644 --- a/store/inmemory.go +++ b/store/inmemory.go @@ -10,7 +10,7 @@ type inMemoryStore struct { actions []*models.Action plans []*models.Plan users []*models.User - primaryPlans []*models.PrimaryPlan + currentPlans []*models.CurrentPlan } // 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 } -func (store *inMemoryStore) SelectPrimaryPlan(userID int) (*models.PrimaryPlan, error) { - for _, primaryPlan := range store.primaryPlans { - if userID == int(primaryPlan.UserID) { - return primaryPlan, nil +func (store *inMemoryStore) SelectCurrentPlan(userID int) (*models.CurrentPlan, error) { + for _, currentPlan := range store.currentPlans { + if userID == int(currentPlan.UserID) { + return currentPlan, nil } } return nil, sql.ErrNoRows } -func (store *inMemoryStore) InsertPrimaryPlan(primaryPlan *models.PrimaryPlan, userID int) error { - _, err := store.SelectPrimaryPlan(userID) +func (store *inMemoryStore) InsertCurrentPlan(currentPlan *models.CurrentPlan, userID int) error { + _, err := store.SelectCurrentPlan(userID) if err != sql.ErrNoRows { return err } @@ -118,16 +118,16 @@ func (store *inMemoryStore) InsertPrimaryPlan(primaryPlan *models.PrimaryPlan, u 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 } -func (store *inMemoryStore) UpdatePrimaryPlan(primaryPlan *models.PrimaryPlan, userID int) error { - current, err := store.SelectPrimaryPlan(userID) +func (store *inMemoryStore) UpdateCurrentPlan(currentPlan *models.CurrentPlan, userID int) error { + current, err := store.SelectCurrentPlan(userID) if err != nil { return err } - current.PlanID = primaryPlan.PlanID + current.PlanID = currentPlan.PlanID return nil } diff --git a/store/migrations/000001_create_action_table.down.sql b/store/migrations/000001_create_action_table.down.sql index 1d07bed..954ca69 100644 --- a/store/migrations/000001_create_action_table.down.sql +++ b/store/migrations/000001_create_action_table.down.sql @@ -1,5 +1,5 @@ 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 users; diff --git a/store/migrations/000001_create_action_table.up.sql b/store/migrations/000001_create_action_table.up.sql index bec2321..32a5677 100644 --- a/store/migrations/000001_create_action_table.up.sql +++ b/store/migrations/000001_create_action_table.up.sql @@ -16,7 +16,7 @@ CREATE TABLE IF NOT EXISTS plans( 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, plan_id int, FOREIGN KEY (user_id, plan_id) REFERENCES plans(user_id, plan_id) diff --git a/store/postgres.go b/store/postgres.go index 3d83ef1..e075df8 100644 --- a/store/postgres.go +++ b/store/postgres.go @@ -175,9 +175,9 @@ func (store *postgresStore) InsertUser(user *models.User) (int, error) { 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 = ?`) +func (store *postgresStore) SelectCurrentPlan(userID int) (*models.CurrentPlan, error) { + pp := models.CurrentPlan{} + queryString := store.db.Rebind(`SELECT user_id, plan_id FROM user_current_plan WHERE user_id = ?`) err := store.db.Get(&pp, queryString, userID) if err != nil { return nil, err @@ -185,11 +185,11 @@ func (store *postgresStore) SelectPrimaryPlan(userID int) (*models.PrimaryPlan, 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") +func (store *postgresStore) InsertCurrentPlan(currentPlan *models.CurrentPlan, userID int) error { + queryString := store.db.Rebind("INSERT INTO user_current_plan (user_id, plan_id) VALUES (?, ?) RETURNING user_id") tx := store.db.MustBegin() var id int - err := tx.Get(&id, queryString, primaryPlan.PlanID, userID) + err := tx.Get(&id, queryString, currentPlan.PlanID, userID) if err != nil { tx.Rollback() return err @@ -201,13 +201,13 @@ func (store *postgresStore) InsertPrimaryPlan(primaryPlan *models.PrimaryPlan, u return nil } -func (store *postgresStore) UpdatePrimaryPlan(primaryPlan *models.PrimaryPlan, userID int) error { - query := `UPDATE user_primary_plan SET +func (store *postgresStore) UpdateCurrentPlan(currentPlan *models.CurrentPlan, userID int) error { + query := `UPDATE user_current_plan SET plan_id = :plan_id WHERE user_id = :user_id` tx := store.db.MustBegin() - ppToUse := &models.PrimaryPlan{ - PlanID: primaryPlan.PlanID, + ppToUse := &models.CurrentPlan{ + PlanID: currentPlan.PlanID, UserID: int64(userID), } _, err := store.db.NamedExec(query, ppToUse)