Skip to content

Commit 910853b

Browse files
committed
Merge branch 'feature/api-versioning' into develop
* feature/api-versioning: rake rswag Added api/v1 path in request. routing Added api_v1 path in testing Added api/v1/ path in spec Added Api::V1:: in controller spec Changed location: @model to location: api_v1_model(@model) in spec, testing Changed location: @model to location: api_v1_model(@model) Added /api/v1 path in controller path
2 parents 3236f70 + 6b54a90 commit 910853b

35 files changed

+510
-485
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module Api
2+
module V1
3+
class AuthenticationController < ApplicationController
4+
before_action :authorize_request, except: :login
5+
6+
# POST /auth/login
7+
def login
8+
@user = User.find_by_email(login_params[:email])
9+
if @user&.authenticate(login_params[:password])
10+
token = JsonWebToken.encode(user_id: @user.id)
11+
time = Time.now + 24.hours.to_i
12+
render json: { token: token, exp: time.strftime("%m-%d-%Y %H:%M"),
13+
username: @user.username, email: @user.email }, status: :ok
14+
else
15+
render json: { error: 'unauthorized' }, status: :unauthorized
16+
end
17+
end
18+
19+
private
20+
21+
def login_params
22+
params.permit(:email, :password)
23+
end
24+
end
25+
end
26+
end
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
module Api
2+
module V1
3+
class CategoriesController < ApplicationController
4+
include CategoryHelper
5+
before_action :authorize_request
6+
before_action :post_pagination_params, only: [:index, :show]
7+
before_action except: [:index, :show] do
8+
is_role :admin
9+
end
10+
before_action :set_category, only: [:show, :update, :destroy]
11+
before_action only: [:edit, :update, :destroy] do
12+
is_owner_object @category ##your object
13+
end
14+
15+
# GET /categories
16+
def index
17+
page = params[:page].present? ? params[:page] : 1
18+
per = params[:per].present? ? params[:per] : 10
19+
pagaination_param = {
20+
category_page: page,
21+
category_per: per,
22+
post_page: @post_page,
23+
post_per: @post_per
24+
}
25+
@categories = fetch_categories pagaination_param
26+
render json: @categories
27+
end
28+
29+
# GET /categories/1
30+
def show
31+
pagaination_param = {
32+
post_page: @post_page,
33+
post_per: @post_per
34+
}
35+
render json: @category, pagaination_param: pagaination_param
36+
end
37+
38+
# POST /categories
39+
def create
40+
@category = Category.new(category_params)
41+
42+
if @category.save
43+
render json: @category, status: :created, location: api_v1_category_url(@category)
44+
else
45+
render json: @category.errors, status: :unprocessable_entity
46+
end
47+
end
48+
49+
# PATCH/PUT /categories/1
50+
def update
51+
if @category.update(category_params)
52+
render json: @category
53+
else
54+
render json: @category.errors, status: :unprocessable_entity
55+
end
56+
end
57+
58+
# DELETE /categories/1
59+
def destroy
60+
@category.published = false
61+
@category.save
62+
end
63+
64+
private
65+
# Use callbacks to share common setup or constraints between actions.
66+
def set_category
67+
@category = Category.published.find(params[:id])
68+
end
69+
70+
# Only allow a trusted parameter "white list" through.
71+
def category_params
72+
params.require(:category).permit(:title, :body).merge(user_id: @current_user.id)
73+
end
74+
75+
def post_pagination_params
76+
@post_page = params[:post_page].present? ? params[:post_page] : 1
77+
@post_per = params[:post_per].present? ? params[:post_per] : 10
78+
end
79+
end
80+
end
81+
end
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
module Api
2+
module V1
3+
class CommentsController < ApplicationController
4+
before_action :authorize_request
5+
before_action :set_comment, only: [:show, :update, :destroy]
6+
before_action only: [:edit, :update, :destroy] do
7+
is_owner_object @comment ##your object
8+
end
9+
10+
# GET /comments
11+
def index
12+
post_id = params[:post_id]
13+
page = params[:page].present? ? params[:page] : 1
14+
per = params[:per].present? ? params[:per] : 10
15+
@post = Post.published.find(post_id) if post_id.present?
16+
category_id = @post.category_id
17+
@category = Category.published.find(category_id)
18+
@comments = Comment.post(post_id).published.by_date.page(page).per(per)
19+
render json: Pagination.build_json(@comments)
20+
end
21+
22+
# GET /comments/1
23+
def show
24+
render json: @comment
25+
end
26+
27+
# POST /comments
28+
def create
29+
@comment = Comment.new(comment_params)
30+
31+
if @comment.save
32+
render json: @comment, status: :created, location: api_v1_comment_url(@comment)
33+
else
34+
render json: @comment.errors, status: :unprocessable_entity
35+
end
36+
end
37+
38+
# PATCH/PUT /comments/1
39+
def update
40+
if @comment.update(comment_params)
41+
render json: @comment
42+
else
43+
render json: @comment.errors, status: :unprocessable_entity
44+
end
45+
end
46+
47+
# DELETE /comments/1
48+
def destroy
49+
@comment.published = false
50+
@comment.save
51+
end
52+
53+
private
54+
# Use callbacks to share common setup or constraints between actions.
55+
def set_comment
56+
@comment = Comment.find(params[:id])
57+
end
58+
59+
# Only allow a trusted parameter "white list" through.
60+
def comment_params
61+
params.require(:comment).permit(:body, :post_id).merge(user_id: @current_user.id)
62+
end
63+
end
64+
end
65+
end
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
module Api
2+
module V1
3+
class PostsController < ApplicationController
4+
include PostHelper
5+
before_action :authorize_request
6+
before_action :comment_pagination_params, only: [:index, :show]
7+
before_action :set_post, only: [:show, :update, :destroy]
8+
before_action only: [:update, :destroy] do
9+
is_owner_object @post ##your object
10+
end
11+
12+
# GET /posts
13+
def index
14+
category_id = params[:category_id]
15+
search = params[:search]
16+
page = params[:page].present? ? params[:page] : 1
17+
per = params[:per].present? ? params[:per] : 10
18+
@category = Category.published.find(category_id) if category_id.present?
19+
pagaination_param = {
20+
category_id: category_id,
21+
search: search,
22+
post_page: page,
23+
post_per: per,
24+
comment_page: @comment_page,
25+
comment_per: @comment_per
26+
}
27+
@posts = fetch_posts pagaination_param
28+
render json: @posts
29+
end
30+
31+
# GET /posts/1
32+
def show
33+
pagaination_param = {
34+
comment_page: @comment_page,
35+
comment_per: @comment_per
36+
}
37+
render json: @post, pagaination_param: pagaination_param
38+
end
39+
40+
# POST /posts
41+
def create
42+
@post = Post.new(post_params)
43+
set_category @post.category_id
44+
45+
if @post.save
46+
render json: @post, status: :created, location: api_v1_post_url(@post)
47+
else
48+
render json: @post.errors, status: :unprocessable_entity
49+
end
50+
end
51+
52+
# PATCH/PUT /posts/1
53+
def update
54+
if @post.update(post_params)
55+
render json: @post
56+
else
57+
render json: @post.errors, status: :unprocessable_entity
58+
end
59+
end
60+
61+
# DELETE /posts/1
62+
def destroy
63+
@post.published = false
64+
@post.save
65+
end
66+
67+
private
68+
# Use callbacks to share common setup or constraints between actions.
69+
def comment_pagination_params
70+
@comment_page = params[:comment_page].present? ? params[:comment_page] : 1
71+
@comment_per = params[:comment_per].present? ? params[:comment_per] : 10
72+
end
73+
74+
def set_post
75+
@post = Post.published.find(params[:id])
76+
set_category @post.category_id
77+
end
78+
79+
def set_category category_id
80+
@category = Category.published.find(category_id)
81+
end
82+
83+
# Only allow a trusted parameter "white list" through.
84+
def post_params
85+
params.require(:post).permit(:body, :category_id).merge(user_id: @current_user.id)
86+
end
87+
end
88+
end
89+
end
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
module Api
2+
module V1
3+
class UsersController < ApplicationController
4+
before_action :authorize_request, except: :create
5+
before_action :find_user, except: %i[create index]
6+
before_action only: [:show, :update, :destroy] do
7+
is_owner @user.id ##your object
8+
end
9+
10+
# GET /users
11+
def index
12+
@users = User.all
13+
render json: @users
14+
end
15+
16+
# GET /users/{username}
17+
def show
18+
render json: @user
19+
end
20+
21+
# POST /users
22+
def create
23+
@user = User.new(user_params)
24+
if @user.save
25+
render json: @user, status: :created, location: api_v1_user_url(@user.username)
26+
else
27+
render json: { errors: @user.errors.full_messages },
28+
status: :unprocessable_entity
29+
end
30+
end
31+
32+
# PUT /users/{username}
33+
def update
34+
if @user.update(user_params)
35+
render json: @user
36+
else
37+
render json: @user.errors, status: :unprocessable_entity
38+
end
39+
end
40+
41+
# DELETE /users/{username}
42+
def destroy
43+
@user.destroy
44+
end
45+
46+
private
47+
48+
def find_user
49+
@user = User.find_by_username!(params[:_username])
50+
rescue ActiveRecord::RecordNotFound
51+
render json: { errors: 'User not found' }, status: :not_found
52+
end
53+
54+
def user_params
55+
params.require(:user).permit(:name, :username, :email, :password, :password_confirmation)
56+
end
57+
end
58+
end
59+
end

app/controllers/authentication_controller.rb

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)