ぼざクリ タグ広場 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.
 
 
 
 
 
 

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