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

54 lines
1.3 KiB

  1. class TheatreCommentsController < ApplicationController
  2. def index
  3. limit = params[:limit].to_i
  4. limit = 20 if limit <= 0
  5. no_gt = params[:no_gt].to_i
  6. no_gt = 0 if no_gt < 0
  7. comments = TheatreComment
  8. .where(theatre_id: params[:theatre_id])
  9. .where('no > ?', no_gt)
  10. .order(no: :desc)
  11. .limit(limit)
  12. render json: comments.map {
  13. _1.as_json(include: { user: { only: [:id, :name] } })
  14. .merge(content: _1.discarded? ? nil : _1.content, deleted: _1.discarded?)
  15. }
  16. end
  17. def create
  18. return head :unauthorized unless current_user
  19. content = params[:content]
  20. return head :unprocessable_entity if content.blank?
  21. theatre = Theatre.find_by(id: params[:theatre_id])
  22. return head :not_found unless theatre
  23. comment = nil
  24. theatre.with_lock do
  25. no = theatre.next_comment_no
  26. comment = TheatreComment.create!(theatre:, no:, user: current_user, content:)
  27. theatre.update!(next_comment_no: no + 1)
  28. end
  29. render json: comment, status: :created
  30. end
  31. def destroy
  32. return head :unauthorized unless current_user
  33. theatre_id = params[:theatre_id].to_i
  34. no = params[:id].to_i
  35. comment = TheatreComment.find_by(theatre_id:, no:)
  36. return head :not_found unless comment
  37. comment.discard!
  38. head :no_content
  39. end
  40. end