-
Notifications
You must be signed in to change notification settings - Fork 1
Api endpoints #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
evans22j
wants to merge
41
commits into
dev
Choose a base branch
from
api-endpoints
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Api endpoints #13
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
a14160a
add devise gem configuration
evans22j c4c3ab2
update styles
evans22j 26aca8b
add user devise config in application controller
evans22j 50e2873
create new html file in devise folder
evans22j 78ea350
Update new.html.erb in registrations and sessions
evans22j 79d7be3
Update application.erb in views/layouts
evans22j 3ee66aa
Run rails db:migrate, and update index and shot html file in views/users
evans22j 25cd143
add confirmable
evans22j 5f420cf
add devise gem configuration
evans22j 5c29be8
Run rails db:migrate, and update index and shot html file in views/users
evans22j 52ffcd4
add user devise config in application controller
evans22j 9e74141
resolve linter errors
evans22j 5820d15
Update comments_controller.rb and create destroy method
evans22j 1612884
Update post_controller.rb and create destroy method
evans22j 8670658
add cancancan authorization
evans22j e58a588
Update comment.rb and create update_comments_counter_on_destroy method
evans22j b468531
Update post.rb
evans22j 2779baf
Update user.rb
evans22j fba2715
Update posts show.html.erb to include delete option
evans22j d0a59b3
Update users show.html.erb
evans22j fe51071
Update routes.rb to include destroy action
evans22j a8cb60b
add cancancan authorization
evans22j 74ec27d
add cancancan authorization
evans22j 68ff6cf
correct linter errors
evans22j 7f5e080
Create users_controller.rb in app/controllers/api/v1
evans22j ad24efc
Update comments_controller.rb in app/api/v1
evans22j c255cae
Update postscontroller.rb in app/api/v1
evans22j 88f8c87
Update userscontroller.rb
evans22j 63a8d0b
Create exception_handler.rb module in app/controllers/concerns
evans22j 9babb07
Create response.rb module in app/controllers/concerns
evans22j 5b58ba7
update files
evans22j 85b802c
Create json_web_token.rb in app/lib
evans22j 678209d
Add jwt gem in gemfile
evans22j e9fa22f
Edit Linters
0556a61
Change DB Password Config
721d16b
Added Devise Model
9a03452
Migrate DB
2cdef9c
Delete Devise Views
5f0fce3
Added Devise Views
231363b
Edit Devise Registration View
cf02c5e
Edit README.md
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| class Api::V1::PostsController < ApplicationController | ||
| def index | ||
| @posts = Post.where(author_id: params[:author_id]) | ||
| if @posts.empty? | ||
| json_response({ msg: 'no post or user found' }, 400) | ||
| else | ||
| render json: @posts | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| class Api::V1::CommentsController < ApplicationController | ||
| before_action :authorize_request | ||
| protect_from_forgery with: :null_session | ||
|
|
||
| def index | ||
| @comments = Comment.where(post_id: params[:post_id]) | ||
|
|
||
| if @comments.empty? | ||
| json_response({ msg: 'No post, comments or user found' }, 400) | ||
| else | ||
| render json: @comments | ||
| end | ||
| end | ||
|
|
||
| def create | ||
| @comment = Comment.create( | ||
| text: comment_params[:text], | ||
| author_id: params[:author_id], | ||
| post_id: params[:post_id] | ||
| ) | ||
|
|
||
| json_response(@comment, :created) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def comment_params | ||
| params.require(:comment).permit(:text) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| class Api::V1::PostsController < ApplicationController | ||
| before_action :authorize_request | ||
| def index | ||
| @posts = Post.where(author_id: params[:author_id]) | ||
| if @posts.empty? | ||
| json_response({ msg: 'no post or user found' }, 400) | ||
| else | ||
| render json: @posts | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| class Api::V1::UsersController < ApplicationController | ||
| before_action :authorize_request | ||
| def index | ||
| @users = User.all | ||
| render json: @users, status: :ok | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,30 @@ | ||
| class ApplicationController < ActionController::Base | ||
| def current_user | ||
| User.first | ||
| include Response | ||
| include ExceptionHandler | ||
|
|
||
| before_action :authenticate_user! | ||
| protect_from_forgery with: :exception | ||
| before_action :update_allowed_parameters, if: :devise_controller? | ||
|
|
||
| protected | ||
|
|
||
| def update_allowed_parameters | ||
| devise_parameter_sanitizer.permit(:sign_up) do |field| | ||
| field.permit(:name, :photo, :bio, :posts_counter, :email, :password, :password_confirmation) | ||
| end | ||
| end | ||
|
|
||
| def authorize_request | ||
| header = request.headers['Auth'] | ||
| header = header.split.last if header | ||
| begin | ||
| @decoded = JsonWebToken.decode(header) | ||
| p @decoded | ||
| @current_user = User.find(@decoded['id']) | ||
| rescue ActiveRecord::RecordNotFound => e | ||
| render json: { errors: e.message }, status: :unauthorized | ||
| rescue JWT::DecodeError => e | ||
| render json: { error: e.message }, status: :unauthorized | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| module ExceptionHandler | ||
| # provides the more graceful `included` method | ||
| extend ActiveSupport::Concern | ||
|
|
||
| included do | ||
| rescue_from ActiveRecord::RecordNotFound do |e| | ||
| json_response({ message: e.message }, :not_found) | ||
| end | ||
|
|
||
| rescue_from ActiveRecord::RecordInvalid do |e| | ||
| json_response({ message: e.message }, :unprocessable_entity) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| module Response | ||
| def json_response(object, status = :ok) | ||
| render json: object, status: | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.