|
- class TheatreCommentsController < ApplicationController
- def index
- no_gt = params[:no_gt].to_i
- no_gt = 0 if no_gt.negative?
-
- comments = TheatreComment
- .where(theatre_id: params[:theatre_id])
- .where('no > ?', no_gt)
- .order(:no)
-
- render json: comments.as_json(include: { user: { only: [:id, :name] } })
- end
-
- def create
- return head :unauthorized unless current_user
-
- content = params[:content]
- return head :unprocessable_entity if content.blank?
-
- theatre = Theatre.find_by(id: params[:theatre_id])
- return head :not_found unless theatre
-
- comment = nil
- theatre.with_lock do
- no = theatre.next_comment_no
- comment = TheatreComment.create!(theatre:, no:, user: current_user, content:)
- theatre.update!(next_comment_no: no + 1)
- end
-
- render json: comment, status: :created
- end
- end
|