From 9b276bd64448e062c740cd99eaf040ed9b9c7cbf Mon Sep 17 00:00:00 2001 From: Deepak Date: Sun, 3 Jan 2021 11:12:31 -0600 Subject: [PATCH] Adds route for filtering actions by planID --- routes/actions.go | 22 +++++++++++++--- routes/actions_test.go | 60 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 3 deletions(-) diff --git a/routes/actions.go b/routes/actions.go index 4850d8e..8be29f8 100644 --- a/routes/actions.go +++ b/routes/actions.go @@ -10,14 +10,30 @@ import ( func newActionRouter(m *models.Model) http.Handler { router := chi.NewRouter() - router.Get("/", getAllActionsFunc(m)) + router.Get("/", getActionsFunc(m)) router.Get("/{actionid}", getActionByIDFunc(m)) return router } -func getAllActionsFunc(m *models.Model) http.HandlerFunc { +func getActionsFunc(m *models.Model) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - actions, err := m.Actions() + var ( + actions []*models.Action + err error + ) + planIDString := r.URL.Query().Get("plan_id") + if planIDString == "" { + actions, err = m.Actions() + } else { + planID, convErr := strconv.ParseInt(planIDString, 10, 64) + if convErr != nil { + actions = []*models.Action{} + err = nil + } else { + plan := &models.Plan{PlanID: planID} + actions, err = m.GetActions(plan) + } + } if err != nil { serverError(w, err) return diff --git a/routes/actions_test.go b/routes/actions_test.go index aad5b85..2008eef 100644 --- a/routes/actions_test.go +++ b/routes/actions_test.go @@ -226,3 +226,63 @@ func TestNotFoundActionByIDEmpty(t *testing.T) { assert.Equal(http.StatusNotFound, status) } + +func TestActionsByPlanID(t *testing.T) { + // set up + assert := assert.New(t) + createdDate, _ := time.Parse("2006-01-02", "2021-01-01") + updatedDate, _ := time.Parse("2006-01-02", "2021-01-02") + a := &models.Action{ActionID: 6, ActionDescription: "howdy", CompletedOn: nil, CreatedAt: &createdDate, UpdatedAt: &updatedDate, CompletedChunks: 0, EstimatedChunks: 54, PlanID: 3} + m := getModel([]*models.Plan{}, []*models.Action{a}) + router := routes.NewRouter(m) + req, _ := http.NewRequest("GET", "/actions?plan_id=6", nil) + + rr := httptest.NewRecorder() + + // function under test + router.ServeHTTP(rr, req) + + // check results + status := rr.Code + assert.Equal(http.StatusOK, status) + // We pass in the date as a time.time so it makes sense that it comes back with a midnight timestamp. + expected := `[ + { + "action_id": 6, + "action_description": "howdy", + "estimated_chunks": 54, + "completed_chunks": 0, + "updated_at": "2021-01-02T00:00:00Z", + "created_at": "2021-01-01T00:00:00Z", + "plan_id": 3 + } + ]` + assert.JSONEq(expected, rr.Body.String()) + contentType := rr.Header().Get("Content-Type") + assert.Equal("application/json", contentType) +} + +func TestActionsByPlanIDInvalidID(t *testing.T) { + // set up + assert := assert.New(t) + createdDate, _ := time.Parse("2006-01-02", "2021-01-01") + updatedDate, _ := time.Parse("2006-01-02", "2021-01-02") + a := &models.Action{ActionID: 6, ActionDescription: "howdy", CompletedOn: nil, CreatedAt: &createdDate, UpdatedAt: &updatedDate, CompletedChunks: 0, EstimatedChunks: 54, PlanID: 3} + m := getModel([]*models.Plan{}, []*models.Action{a}) + router := routes.NewRouter(m) + req, _ := http.NewRequest("GET", "/actions?plan_id=aoeu", nil) + + rr := httptest.NewRecorder() + + // function under test + router.ServeHTTP(rr, req) + + // check results + status := rr.Code + assert.Equal(http.StatusOK, status) + // We pass in the date as a time.time so it makes sense that it comes back with a midnight timestamp. + expected := `[]` + assert.JSONEq(expected, rr.Body.String()) + contentType := rr.Header().Get("Content-Type") + assert.Equal("application/json", contentType) +}