add posts scope filter

This commit is contained in:
João Victor Geonizeli
2022-02-28 11:22:37 -03:00
parent e1f96c04a4
commit bf3253dc9e
5 changed files with 71 additions and 24 deletions

View File

@@ -2,7 +2,7 @@ class PostsController < ApplicationController
# GET /posts
# GET /posts.json
def index
@posts = Post.all
@posts = PostsQueryResolverService.call(query_params, current_user)
end
# GET /posts/1
@@ -14,7 +14,10 @@ class PostsController < ApplicationController
# POST /posts
# POST /posts.json
def create
@post = Post.new(post_params)
@post = Post.new({
user: current_user,
**post_params
})
if @post.save
render :show, status: :created, location: @post
@@ -25,11 +28,11 @@ class PostsController < ApplicationController
private
# Only allow a list of trusted parameters through.
def query_params
params.permit(:scope) || {}
end
def post_params
{
user: current_user,
**params.require(:post).permit(:content, :quoted_post_id)
}
params.require(:post).permit(:content, :quoted_post_id)
end
end

View File

@@ -0,0 +1,22 @@
class PostsQueryResolverService
attr_reader :filter, :current_user
def initialize(filter, current_user)
@filter = filter
@current_user = current_user
end
def self.call(filter, current_user)
new(filter, current_user).call
end
def call
scope = Post.all
if filter[:scope] == 'follows' && current_user
scope = scope.by_user_follows(current_user)
end
scope
end
end