ぼざクリ タグ広場 https://hub.nizika.monster
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

107 lines
2.9 KiB

  1. require 'open-uri'
  2. require 'nokogiri'
  3. class PostsController < ApplicationController
  4. # GET /posts
  5. def index
  6. posts = filtered_posts
  7. render json: posts.as_json(include: { tags: { only: [:id, :name, :category, :post_count] } })
  8. end
  9. def random
  10. post = filtered_posts.order('RAND()').first
  11. return head :not_found unless post
  12. viewed = current_user&.viewed?(post) || false
  13. render json: (post
  14. .as_json(include: { tags: { only: [:id, :name, :category] } })
  15. .merge(viewed: viewed))
  16. end
  17. # GET /posts/1
  18. def show
  19. post = Post.includes(:tags).find(params[:id])
  20. viewed = current_user&.viewed?(post)
  21. render json: (post
  22. .as_json(include: { tags: { only: [:id, :name, :category] } })
  23. .merge(viewed: viewed))
  24. end
  25. # POST /posts
  26. def create
  27. return head :unauthorized unless current_user
  28. return head :forbidden unless ['admin', 'member'].include?(current_user.role)
  29. # TODO: URL が正規のものがチェック,不正ならエラー
  30. title = params[:title]
  31. post = Post.new(title: title, url: params[:url], thumbnail_base: '', uploaded_user: current_user)
  32. post.thumbnail.attach(params[:thumbnail])
  33. if post.save
  34. post.resized_thumbnail!
  35. if params[:tags].present?
  36. tag_ids = JSON.parse(params[:tags])
  37. post.tags = Tag.where(id: tag_ids)
  38. end
  39. render json: post, status: :created
  40. else
  41. render json: { errors: post.errors.full_messages }, status: :unprocessable_entity
  42. end
  43. end
  44. def viewed
  45. return head :unauthorized unless current_user
  46. current_user.viewed_posts << Post.find(params[:id])
  47. head :no_content
  48. end
  49. def unviewed
  50. return head :unauthorized unless current_user
  51. current_user.viewed_posts.delete(Post.find(params[:id]))
  52. head :no_content
  53. end
  54. # PATCH/PUT /posts/1
  55. def update
  56. return head :unauthorized unless current_user
  57. return head :forbidden unless ['admin', 'member'].include?(current_user.role)
  58. post = Post.find(params[:id])
  59. tag_ids = JSON.parse(params[:tags])
  60. if post.update(title: params[:title], tags: Tag.where(id: tag_ids))
  61. render({ json: (post
  62. .as_json(include: { tags: { only: [:id, :name, :category] } })),
  63. status: :created })
  64. else
  65. render json: post.errors, status: :unprocessable_entity
  66. end
  67. end
  68. # DELETE /posts/1
  69. def destroy
  70. end
  71. private
  72. def filtered_posts
  73. tag_names = params[:tags]&.split(',')
  74. match_type = params[:match]
  75. tag_names.present? ? filter_posts_by_tags(tag_names, match_type) : Post.all
  76. end
  77. def filter_posts_by_tags tag_names, match_type
  78. posts = Post.joins(:tags)
  79. if match_type == 'any'
  80. posts = posts.where(tags: { name: tag_names }).distinct
  81. else
  82. tag_names.each do |tag|
  83. posts = posts.where(id: Post.joins(:tags).where(tags: { name: tag }))
  84. end
  85. end
  86. posts.distinct
  87. end
  88. end