上映会のし組み作り(#295) (#296)
#295 #295 #295 #295 #295 #295 #295 Co-authored-by: miteruzo <miteruzo@naver.com> Reviewed-on: #296
This commit was merged in pull request #296.
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
class TheatresController < ApplicationController
|
||||
def show
|
||||
theatre = Theatre.find_by(id: params[:id])
|
||||
return head :not_found unless theatre
|
||||
|
||||
render json: TheatreRepr.base(theatre)
|
||||
end
|
||||
|
||||
def watching
|
||||
return head :unauthorized unless current_user
|
||||
|
||||
theatre = Theatre.find_by(id: params[:id])
|
||||
return head :not_found unless theatre
|
||||
|
||||
host_flg = false
|
||||
post_id = nil
|
||||
post_started_at = nil
|
||||
|
||||
theatre.with_lock do
|
||||
TheatreWatchingUser.find_or_initialize_by(theatre:, user: current_user).tap {
|
||||
_1.expires_at = 30.seconds.from_now
|
||||
}.save!
|
||||
|
||||
if (!(theatre.host_user_id?) ||
|
||||
!(theatre.watching_users.exists?(id: theatre.host_user_id)))
|
||||
theatre.update!(host_user_id: current_user.id)
|
||||
end
|
||||
|
||||
host_flg = theatre.host_user_id == current_user.id
|
||||
post_id = theatre.current_post_id
|
||||
post_started_at = theatre.current_post_started_at
|
||||
end
|
||||
|
||||
render json: { host_flg:, post_id:, post_started_at: }
|
||||
end
|
||||
|
||||
def next_post
|
||||
return head :unauthorized unless current_user
|
||||
|
||||
theatre = Theatre.find_by(id: params[:id])
|
||||
return head :not_found unless theatre
|
||||
return head :forbidden if theatre.host_user != current_user
|
||||
|
||||
post = Post.where("url LIKE '%nicovideo.jp%'")
|
||||
.or(Post.where("url LIKE '%youtube.com%'"))
|
||||
.order('RAND()')
|
||||
.first
|
||||
theatre.update!(current_post: post, current_post_started_at: Time.current)
|
||||
|
||||
head :no_content
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,13 @@
|
||||
class Theatre < ApplicationRecord
|
||||
include MyDiscard
|
||||
|
||||
has_many :comments, class_name: 'TheatreComment'
|
||||
has_many :theatre_watching_users, dependent: :delete_all
|
||||
has_many :active_theatre_watching_users, -> { active },
|
||||
class_name: 'TheatreWatchingUser', inverse_of: :theatre
|
||||
has_many :watching_users, through: :active_theatre_watching_users, source: :user
|
||||
|
||||
belongs_to :host_user, class_name: 'User', optional: true
|
||||
belongs_to :current_post, class_name: 'Post', optional: true
|
||||
belongs_to :created_by_user, class_name: 'User'
|
||||
end
|
||||
@@ -0,0 +1,8 @@
|
||||
class TheatreComment < ApplicationRecord
|
||||
include MyDiscard
|
||||
|
||||
self.primary_key = :theatre_id, :no
|
||||
|
||||
belongs_to :theatre
|
||||
end
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
class TheatreWatchingUser < ApplicationRecord
|
||||
self.primary_key = :theatre_id, :user_id
|
||||
|
||||
belongs_to :theatre
|
||||
belongs_to :user
|
||||
|
||||
scope :active, -> { where('expires_at >= ?', Time.current) }
|
||||
scope :expired, -> { where('expires_at < ?', Time.current) }
|
||||
|
||||
def active? = expires_at >= Time.current
|
||||
|
||||
def refresh! = update!(expires_at: 30.seconds.from_now)
|
||||
end
|
||||
@@ -0,0 +1,17 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
|
||||
module TheatreRepr
|
||||
BASE = { only: [:id, :name, :opens_at, :closes_at, :created_at, :updated_at],
|
||||
include: { created_by_user: { only: [:id, :name] } } }.freeze
|
||||
|
||||
module_function
|
||||
|
||||
def base theatre
|
||||
theatre.as_json(BASE)
|
||||
end
|
||||
|
||||
def many theatre
|
||||
theatre.map { |t| base(t) }
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user