namespace '/video' do get '' do redirect '/video/list' end get '/list' do videos = Video.reverse(:updated_at).all() erb :'video/list', :locals => { :title => 'List of videos', :videos => videos } end get '/create' do channels = Channel.all() erb :'video/create', :locals => { :title => 'Create new video', :channels => channels } end post '/create' do channel = Channel.where(id: params[:video_channel]).first() video_serial = params[:video_serial].to_s.rjust(4, '0') video_path = File.join( channel.directory_path, "##{video_serial} - #{params[:video_name]}" ) video = Video.create( serial: params[:video_serial], name: params[:video_name], channel_id: params[:video_channel], directory_path: video_path, description: params[:video_description], script: "# Introduction\n\n# Body\n\n# Conclusions" ) Dir.mkdir(video_path) redirect "/video/#{video.id}" end get '/:video_id' do video = Video.where(id: params[:video_id]).first() erb :'video/view', :locals => { :title => video.name, :video => video } end get '/:video_id/script' do video = Video.where(id: params[:video_id]).first() erb :'video/script', :locals => { :title => "Script: #{video.name}", :video => video } end get '/:video_id/edit' do video = Video.where(id: params[:video_id]).first() channels = Channel.all() erb :'video/edit', :locals => { :title => "Editing: #{video.name}", :video => video, :channels => channels } end post '/:video_id/edit' do channel = Channel.where(id: params[:video_channel]).first() video_serial = params[:video_serial].to_s.rjust(4, '0') video_path = File.join( channel.directory_path, "##{video_serial} - #{params[:video_name]}" ) # find video and temporarily save the old video path video = Video.where(id: params[:video_id]).first() old_path = video.directory_path # edit video attributes video.update( name: params[:video_name], serial: params[:video_serial], channel_id: params[:video_channel], directory_path: video_path, description: params[:video_description] ) # rename the video project directory File.rename(old_path, video_path) # redirect the user redirect "/video/#{video.id}" end get '/:video_id/edit/script' do video = Video.where(id: params[:video_id]).first() erb :'video/edit-script', :locals => { :title => "Editing script: #{video.name}", :video => video } end post '/:video_id/edit/script' do # find video and temporarily save the old video path video = Video.where(id: params[:video_id]).first() # edit video attributes video.update( script: params[:video_script] ) # redirect the user redirect "/video/#{video.id}" end end