#22 完了
This commit is contained in:
@@ -1,2 +1,16 @@
|
||||
class ApplicationController < ActionController::API
|
||||
before_action :authenticate_user
|
||||
|
||||
def current_user
|
||||
@current_user
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def authenticate_user
|
||||
code = request.headers['X-Transfer-Code'] || request.headers['HTTP_X_TRANSFER_CODE']
|
||||
@current_user = User.find_by inheritance_code: code
|
||||
Rails.logger.info("X-Transfer-Code: #{request.headers['X-Transfer-Code']}")
|
||||
Rails.logger.info("current_user: #{@current_user&.id}")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -24,7 +24,10 @@ class PostsController < ApplicationController
|
||||
# GET /posts/1
|
||||
def show
|
||||
@post = Post.includes(:tags).find(params[:id])
|
||||
render json: @post.as_json(include: { tags: { only: [:id, :name, :category] } })
|
||||
viewed = current_user&.viewed?(@post)
|
||||
render json: (@post
|
||||
.as_json(include: { tags: { only: [:id, :name, :category] } })
|
||||
.merge(viewed: viewed))
|
||||
end
|
||||
|
||||
# POST /posts
|
||||
@@ -38,6 +41,20 @@ class PostsController < ApplicationController
|
||||
end
|
||||
end
|
||||
|
||||
def viewed
|
||||
return head :unauthorized unless current_user
|
||||
|
||||
current_user.viewed_posts << Post.find(params[:id])
|
||||
head :no_content
|
||||
end
|
||||
|
||||
def unviewed
|
||||
return head :unauthorized unless current_user
|
||||
|
||||
current_user.viewed_posts.delete(Post.find(params[:id]))
|
||||
head :no_content
|
||||
end
|
||||
|
||||
# PATCH/PUT /posts/1
|
||||
def update
|
||||
if @post.update(post_params)
|
||||
|
||||
Reference in New Issue
Block a user