Skip to content

Commit

Permalink
Update comment documentation to reflect new changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Osmond Oscar committed Feb 17, 2016
1 parent d61e8df commit 0501f56
Show file tree
Hide file tree
Showing 4 changed files with 278 additions and 15 deletions.
11 changes: 4 additions & 7 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class CommentsController < ApplicationController
before_action :set_resource
before_action :set_comment, only: [:show, :update, :destroy]
include OwnershipConcern

def index
render json: @resource_comments, status: 200
Expand All @@ -14,23 +15,23 @@ def create
comment = @resource_comments.new(content: comment_params[:content])
comment.user = current_user
if comment.save
render json: comment, root: false
render json: comment, status: 201
else
invalid_request("Comment body can not be empty!")
end
end

def update
if @comment.update(content: comment_params[:content])
render json: @comment
render json: @comment, status: 200
else
invalid_request("Comment body can not be empty!")
end
end

def destroy
if @comment.try(:destroy)
render json: { response: "Comment deleted." }, status: 410
render json: :head, status: 204
else
invalid_request
end
Expand All @@ -51,9 +52,5 @@ def set_resource
def set_comment
@comment = @resource_comments.find_by(id: comment_params[:id])
resource_not_found && return unless @comment

unless (params[:action] == 'show') || (@comment.user.eql? current_user)
render json: { error: "It is your comment?" }, status: 401
end
end
end
2 changes: 0 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

get "top_questions" => "questions#top_questions"

resources :answers, except: [:index, :show, :create, :destroy, :update, :new, :edit]

post "users/logout" => "user#logout"
get 'users/renew_token' => 'users#renew_token'
get "users" => "users#index"
Expand Down
173 changes: 167 additions & 6 deletions docs/comments_endpoints.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ Endpoints | Usage | Public Access
--------- | ----- | -------------
GET /answers/:id/comments | Returns all the comments for a particular answer | True
POST /answers/:id/comments/ | Creates a new comment for the specified answer | False
PUT /comments/:id | Updates the comment with certain attributes | False
DELETE /comments/:id | Deletes an comment | False
PATCH /answers/:answer_id/comments/:id | Updates the specified comment of the specified answer | False
DELETE /answers/:answer_id/comments/:id | Deletes the specified comment of the specified answer | False
GET /questions/:id/comments | Returns all the comments for a particular question | True
POST /questions/:id/comments/ | Creates a new comment for the specified question | False
PATCH /questions/:question_id/comments/:id | Updates the specified comment of the specified question | False
DELETE /questions/:question_id/comments/:id | Deletes the specified comment of the specified question | False

### GET /answers/:id/comments

Expand Down Expand Up @@ -49,11 +53,63 @@ Status: 200
}
}
```
Or

```ruby
Status: 404
{
message: "Answer not found"
error: "The resource you tried to access was not found"
}
```

### GET /questions/:id/comments

Request
```ruby
{
auth_token: "90ioji0j0i0i0ik0k0jmj0090jknieu93833r335"
}
```

Response
```ruby
Status: 200
{
answer: {
id: 1,
comments: [
{
id: 2,
text: 'What do you really want to do?',
date_created: 'Wed, 24TH Nov, 2017 10:00AM',
updated: false,
score: 8,
user: {
id: 2,
name: 'Oscar Laide'
}
},
{
id: 4,
text: 'This is such a scam',
date_created: 'Wed, 25TH Nov, 2017 12:00PM',
updated: true,
score: 9,
user: {
id: 3,
name: 'Bayo Owoade'
}
}
]
}
}
```
Or

```ruby
Status: 404
{
error: "The resource you tried to access was not found"
}
```

Expand All @@ -72,11 +128,52 @@ Response
```ruby
Status: 201
{
message: "Successfully added"
id: 4,
text: 'This is such a scam',
date_created: 'Wed, 25TH Nov, 2017 12:00PM',
updated: true,
score: 9,
user: {
id: 3,
name: 'Bayo Owoade'
}
}
```

### PUT /comments/:id
### POST /questions/:id/comments/

Request
```ruby
{
auth_token: "90ioji0j0i0i0ik0k0jmj0090jknieu93833r335",
content: "Why in the world do I have to do this?"
}
```

Response
```ruby
Status: 201
{
id: 4,
text: 'This is such a scam',
date_created: 'Wed, 25TH Nov, 2017 12:00PM',
updated: true,
score: 9,
user: {
id: 3,
name: 'Bayo Owoade'
}
}
```
OR
```ruby
Status: 404
{
error: "Comment body can not be empty!"
}
```

### PATCH /answers/:answer_id/comments/:id

Request
```ruby
Expand All @@ -86,12 +183,68 @@ Request
}
```

Response
```ruby
Status: 200
{
id: 4,
text: 'This is such a scam',
date_created: 'Wed, 25TH Nov, 2017 12:00PM',
updated: true,
score: 9,
user: {
id: 3,
name: 'Bayo Owoade'
}
}
```
### PATCH /questions/:question_id/comments/:id

Request
```ruby
{
auth_token: "90ioji0j0i0i0ik0k0jmj0090jknieu93833r335",
content: "What in the world does this mean?"
}
```

Response
```ruby
Status: 200
{
id: 4,
text: 'This is such a scam',
date_created: 'Wed, 25TH Nov, 2017 12:00PM',
updated: true,
score: 9,
user: {
id: 3,
name: 'Bayo Owoade'
}
}
```
OR
```ruby
Status: 404
{
error: "Comment body can not be empty!"
}
```
### DELETE /answers/:answer_id/comments/:id

Request
```ruby
{
auth_token: "90ioji0j0i0i0ik0k0jmj0090jknieu93833r335"
}
```

Response
```ruby
Status: 204
```

### DELETE /comments/:id
### DELETE /questions/:answer_id/comments/:id

Request
```ruby
Expand All @@ -104,3 +257,11 @@ Response
```ruby
Status: 204
```

OR
```ruby
Status: 404
{
error: "The operation could not be performed. Please check your request or try again later"
}
```
107 changes: 107 additions & 0 deletions docs/vote_endpoints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#Votes Resources

Endpoints | Usage | Public Access
--------- | ----- | -------------
POST /questions/:question_id/upvote | Increase the upvotes of the given question by one unit | False
POST /questions/:question_id/downvote | Increase the downvotes of the given question by one unit | False
POST /answers/:answer_id/upvote | Increase the upvotes of the given answer by one unit | False
POST /answers/:answer_id/downvote | Increase the downvotes of the given answers by one unit | False
POST /comments/:question_id/upvote | Increase the upvotes of the given comment by one unit | False
POST /comments/:question_id/downvote | Increase the downvotes of the given comments by one unit | False

### POST /questions/:question_id/upvote/

Response
```ruby
Status: 200
{
response: 11
}
```
#### OR
```ruby
Status: 403
{
error: "Invalid vote!"
}
```
### POST /comments/:comment_id/upvote/

Response
```ruby
Status: 200
{
response: 23
}
```
#### OR
```ruby
Status: 403
{
error: "Invalid vote!"
}
```
### POST /answers/:answer_id/upvote/

Response
```ruby
Status: 200
{
response: 0
}
```
#### OR
```ruby
Status: 403
{
error: "Invalid vote!"
}
```
### POST /questions/:question_id/downvote/

Response
```ruby
Status: 200
{
response: 2
}
```
#### OR
```ruby
Status: 403
{
error: "Invalid vote!"
}
```
### POST /comments/:comment_id/downvote/

Response
```ruby
Status: 200
{
response: -12
}
```
#### OR
```ruby
Status: 403
{
error: "Invalid vote!"
}
```
### POST /answers/:answer_id/downvote/

Response
```ruby
Status: 200
{
response: 34
}
```
#### OR
```ruby
Status: 403
{
error: "Invalid vote!"
}
```

0 comments on commit 0501f56

Please sign in to comment.