Outline
CRUD = Create, Read, Update, Destroy.
Articles
First things first, lets create our spec for the article data.
{
"article": {
"slug": "how-to-train-your-dragon",
"title": "How to train your dragon",
"description": "Ever wonder how?",
"body": "It takes a Jacobian",
"createdAt": "2016-02-18T03:22:56.637Z",
"updatedAt": "2016-02-18T03:48:35.824Z",
"favorited": false,
"favoritesCount": 0,
"author": {
"username": "jake",
"bio": "I work at statefarm",
"image": "https://i.stack.imgur.com/xHWG8.jpg",
"following": false
}
}
}
Create Article
POST /api/articles
Example request body:
{
"article": {
"title": "How to train your dragon",
"description": "Ever wonder how?",
"body": "You have to believe",
"tagList": ['reactjs', 'angularjs', 'dragons']
}
}
Authentication required, will return an Article
Required fields: title, description, body
Optional fields: tagList as an array of Strings
Retrieve Article
GET /api/articles/:slug
No authentication required, will return single article
Update article
PUT /api/articles/:slug
Example request body:
{
"article": {
"title": "Did you train your dragon?"
}
}
Authentication required, returns the updated Article
Optional fields: title, description, body
The slug also gets updated when the title is changed
Deleting an article
DELETE /api/articles/:slug
Authentication required
Associated actions with articles
Users can favorite articles -- so lets create endpoints that deal with that.
Favoriting an article
POST /api/articles/:slug/favorite
Authentication required, returns the Article
No additional parameters required
Unfavoriting an article
DELETE /api/articles/:slug/favorite
Authentication required, returns the Article
No additional parameters required
Comments
Here's the spec for our comments:
{
"comment": {
"id": 1,
"body": "It takes a Jacobian",
"createdAt": "2016-02-18T03:22:56.637Z",
"author": {
"username": "jake",
"bio": "I work at statefarm",
"image": "https://i.stack.imgur.com/xHWG8.jpg",
"following": false
}
}
}
Creating comments on an article
POST /api/articles/:slug/comments
Example request body:
{
"comment": {
"body": "His name was my name too."
}
}
Authentication required, returns the created Comment
Required fields: body
Deleting a comment
DELETE /api/articles/:slug/comments/:id
Authentication required
Getting comments of an article
GET /api/articles/:slug/comments
Authentication optional, returns multiple comments
This is different from any of the other endpoints, because we're going to receive a collection of them back instead of just one comment. We'll need to create a new spec for this:
{
"comments": [{
"id": 1,
"body": "It takes a Jacobian",
"createdAt": "2016-02-18T03:22:56.637Z",
"author": {
"username": "jake",
"bio": "I work at statefarm",
"image": "https://i.stack.imgur.com/xHWG8.jpg",
"following": false
}
}]
}
Tags
Tags are created when articles are created, and deleted when no articles are using them any more. On our home page we want to show a list of all the tags, so we'll need an endpoint for that.
Getting tags
GET /api/tags
We'll need to return a list of tags (similar to what we did with comments), so lets create a spec for it:
{
"tags": [
"reactjs",
"angularjs"
]
}