SchoolOfCode / bc17-w4d2-project-i-need-a-rest

bootcamp-17-bc17-w4d2-project-empty-repo created by GitHub Classroom
0 stars 0 forks source link

User Story 4: Implement functionality to POST a new activity. #4

Closed faisalagood closed 3 months ago

faisalagood commented 3 months ago

Description:

As Kenny, a busy front end engineer I need to be able to send a POST request with a JSON request body (new activity) to the API so that I can make a simple form that allows my user to submit new activity.

As Norman, a Tech Lead working across lots of projects I need to be able to depend on conventions like: the response object always containing a key called data or error, or the status code being an accurate indication of the success/failure (and reason) of the request.

Acceptance Criteria:

Given I am a developer who has the Activity API running, When I make a POST request to “http://localhost:3000/activities” with a request body containing a JSON object (new activity), Then the API should save the new activity to the activities.json file giving it a unique “id” (UUID) and activity_submitted (time stamp - Date.now()), Then the the request should succeed, responding with the correct status code and the activity object that I posted as the response body (response.data).

// request object - new user activity
{
  "activity_type": "run",
  "activity_duration": "30",
}

// save the newly sumbitted activity in activities.JSON 

[
  {}, // exisitng activity
  {}, // exisitng activity
  {
    "id": "54321234", // Add a UUID
    "date": "1719486190058", // Add the date time stamp
    "activity_type": "run",
    "activity_duration": "30",
    }
]

Given I am a developer who has made an invalid POST request by missing a required request body field like, activity_type or activity_duration, When I inspect the API response, Then the request should fail, responding with the correct status code and a clear error message (response.error).

### Tasks for User Story 4
- [x] Implement a route to handle POST requests to http://localhost:3000/activities
- [x] Parse the request body to extract the new activity JSON object
- [x] Generate a unique id (UUID) for the new activity
- [x] Add an activity_submitted timestamp (Date.now()) to the new activity
- [x] Save the new activity to the activities.json file
- [x] Respond with the correct status code (201 Created)
- [x] Return the new activity object in the response body (response.data)
- [x] Validate the request body to ensure all required fields are present (e.g., activity_type and activity_duration)
- [x] If a required field is missing, respond with the correct status code (400 Bad Request)
- [x] Include a clear error message in the response body (response.error)
- [x] Test if criteria is all met!
faisalagood commented 3 months ago

Done.