diff --git a/models/plan.go b/models/plan.go index 02e0212..b6bb432 100644 --- a/models/plan.go +++ b/models/plan.go @@ -8,6 +8,7 @@ import ( type Plan struct { PlanID int64 `json:"plan_id"` PlanDate *time.Time `json:"plan_date"` + UserID int64 `json:"user_id"` } // Plans returns all plans in the model. diff --git a/routes/plans.go b/routes/plans.go index edcab49..ff1c64d 100644 --- a/routes/plans.go +++ b/routes/plans.go @@ -79,7 +79,8 @@ func postPlanFunc(m *models.Model) http.HandlerFunc { return } - plan := &models.Plan{PlanDate: p.PlanDate} + // Map the fields we allow to be set to the plan to be created. + plan := &models.Plan{PlanDate: p.PlanDate, UserID: p.UserID} id, err := m.AddPlan(plan) if err != nil { serverError(w, err) diff --git a/routes/plans_test.go b/routes/plans_test.go index 03fd375..51043be 100644 --- a/routes/plans_test.go +++ b/routes/plans_test.go @@ -37,7 +37,7 @@ func TestOnePlan(t *testing.T) { // set up assert := assert.New(t) planDate, _ := time.Parse("2006-01-02", "2021-01-01") - p := &models.Plan{PlanID: 6, PlanDate: &planDate} + p := &models.Plan{PlanID: 6, PlanDate: &planDate, UserID: 3} m := getEmptyModel() m.AddPlan(p) router := routes.NewPlanRouter(m) @@ -55,7 +55,8 @@ func TestOnePlan(t *testing.T) { expected := `[ { "plan_id": 1, - "plan_date": "2021-01-01T00:00:00Z" + "plan_date": "2021-01-01T00:00:00Z", + "user_id": 3 } ]` assert.JSONEq(expected, rr.Body.String()) @@ -109,7 +110,7 @@ func TestOnePlanByID(t *testing.T) { // set up assert := assert.New(t) planDate, _ := time.Parse("2006-01-02", "2021-01-01") - p := &models.Plan{PlanID: 6, PlanDate: &planDate} + p := &models.Plan{PlanID: 6, PlanDate: &planDate, UserID: 3} m := getEmptyModel() m.AddPlan(p) router := routes.NewPlanRouter(m) @@ -126,7 +127,8 @@ func TestOnePlanByID(t *testing.T) { // We pass in the date as a time.time so it makes sense that it comes back with a midnight timestamp. expected := `{ "plan_id": 1, - "plan_date": "2021-01-01T00:00:00Z" + "plan_date": "2021-01-01T00:00:00Z", + "user_id": 3 }` assert.JSONEq(expected, rr.Body.String()) contentType := rr.Header().Get("Content-Type") diff --git a/routes/post_plan_test.go b/routes/post_plan_test.go index 77ec78a..9ad7902 100644 --- a/routes/post_plan_test.go +++ b/routes/post_plan_test.go @@ -17,7 +17,7 @@ func TestCreatePlanRoute(t *testing.T) { // set up assert := assert.New(t) planDate, _ := time.Parse("2006-01-02", "2021-01-01") - p := &models.Plan{PlanID: 6, PlanDate: &planDate} + p := &models.Plan{PlanID: 6, PlanDate: &planDate, UserID: 3} m := getEmptyModel() m.AddPlan(p) router := routes.NewPlanRouter(m) @@ -36,7 +36,8 @@ func TestCreatePlanRoute(t *testing.T) { expected := `{ "created_plan": { "plan_id": 2, - "plan_date": "2021-01-01T00:00:00Z" + "plan_date": "2021-01-01T00:00:00Z", + "user_id": 3 }, "id": 2 }` @@ -49,13 +50,14 @@ func TestPureJSON(t *testing.T) { // set up assert := assert.New(t) planDate, _ := time.Parse("2006-01-02", "2021-01-01") - p := &models.Plan{PlanID: 1, PlanDate: &planDate} + p := &models.Plan{PlanID: 1, PlanDate: &planDate, UserID: 3} m := getEmptyModel() m.AddPlan(p) router := routes.NewPlanRouter(m) data := []byte(`{ "plan_date": "2021-01-01T00:00:00Z", - "plan_id": 1 + "plan_id": 1, + "user_id": 3 }`) req, _ := http.NewRequest("POST", "/", bytes.NewBuffer(data)) req.Header.Set("Content-Type", "application/json") @@ -71,6 +73,7 @@ func TestPureJSON(t *testing.T) { expected := `{ "created_plan": { "plan_id": 2, + "user_id": 3, "plan_date": "2021-01-01T00:00:00Z" }, "id": 2 diff --git a/store/migrations/000001_create_action_table.up.sql b/store/migrations/000001_create_action_table.up.sql index 9b48f0b..b95dcd0 100644 --- a/store/migrations/000001_create_action_table.up.sql +++ b/store/migrations/000001_create_action_table.up.sql @@ -1,6 +1,7 @@ CREATE TABLE IF NOT EXISTS plans( plan_id serial PRIMARY KEY, plan_date DATE NOT NULL, + user_id int REFERENCES users(user_id), created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL, updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP NOT NULL );