Adds user id field to plan

This commit is contained in:
Deepak Mallubhotla 2021-01-17 11:22:14 -06:00
parent 2a3d789292
commit d02d48e7c8
Signed by: deepak
GPG Key ID: 64BF53A3369104E7
5 changed files with 17 additions and 9 deletions

View File

@ -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.

View File

@ -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)

View File

@ -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")

View File

@ -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

View File

@ -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
);