namespace '/channel' do 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.ensureDirectoryStructure() 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 end