stage-manager/app/routes/channel.rb

101 lines
2.7 KiB
Ruby

# frozen_string_literal: true
class StageManager
# Channel that handles channel top-level routes
class ChannelController
get '/' do
redirect '/channel/list'
end
get '/list' do
channels = Channel.reverse(:updated_at).all()
erb :'channel/list', locals: {
title: 'List of channels',
channels: channels
}
end
get '/create' do
erb :'channel/create', locals: {
title: 'Create new channel',
base_directory: $conf.get('stgm.base_directory')
}
end
post '/create' do
channel = Channel.create(
name: params[:channel_name],
directory_path: params[:channel_dir],
description: params[:channel_description]
)
# create supporting directory structure
Dir.mkdir(channel.directory_path)
channel.ensure_directory_structure()
redirect "/channel/#{channel.id}"
end
get '/:channel_id' do
channel = Channel.where(id: params[:channel_id]).first()
channel_videos = channel.videos_dataset.reverse(:updated_at).limit(10).all()
erb :'channel/view', locals: {
title: channel.name,
channel: channel,
channel_videos: channel_videos
}
end
get '/:channel_id/edit' do
channel = Channel.where(id: params[:channel_id]).first()
erb :'channel/edit', locals: {
title: "Editing: #{channel.name}",
channel: channel
}
end
post '/:channel_id/edit' do
# get channel model and save old directory path
channel = Channel.where(id: params[:channel_id]).first()
old_path = channel.directory_path
# edit channel model
channel.update(
name: params[:channel_name],
directory_path: params[:channel_dir],
description: params[:channel_description]
)
# edit associate videos' directory paths
channel.videos.each do |v|
video_path = v.directory_path.sub(old_path, channel.directory_path)
v.update(directory_path: video_path)
end
# rename channel directory
File.rename(old_path, channel.directory_path)
# redirect user
redirect "/channel/#{channel.id}"
end
post '/:channel_id/edit/:attr' do
# find channel and temporarily save the old channel path
channel = Channel.where(id: params[:channel_id]).first()
attr_to_edit = params[:attr]
if attr_to_edit == 'directory_path'
File.rename(channel.directory_path, params[:value])
channel.videos.each do |v|
video_path = v.directory_path.sub(channel.directory_path, params[:value])
v.update(directory_path: video_path)
end
end
channel[attr_to_edit.to_sym] = params[:value]
channel.save_changes()
return 'success'
end
end
end