From 5ed1771b1815488b0e58b8dabdd9ffc42c32600c Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Sat, 4 Mar 2023 08:47:07 -0500 Subject: [PATCH] Added channel and video edit pages; added link back to channel from video view page --- lib/routes/channel.rb | 32 +++++++++++++++++++++++++++++++ lib/routes/video.rb | 37 ++++++++++++++++++++++++++++++++++++ views/channel/edit.erb | 34 +++++++++++++++++++++++++++++++++ views/video/edit.erb | 43 ++++++++++++++++++++++++++++++++++++++++++ views/video/view.erb | 2 +- 5 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 views/channel/edit.erb create mode 100644 views/video/edit.erb diff --git a/lib/routes/channel.rb b/lib/routes/channel.rb index c68e49d..7a4fd83 100644 --- a/lib/routes/channel.rb +++ b/lib/routes/channel.rb @@ -38,4 +38,36 @@ namespace '/channel' do } 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 diff --git a/lib/routes/video.rb b/lib/routes/video.rb index fb6b641..ec639ca 100644 --- a/lib/routes/video.rb +++ b/lib/routes/video.rb @@ -48,4 +48,41 @@ namespace '/video' do } 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 + end diff --git a/views/channel/edit.erb b/views/channel/edit.erb new file mode 100644 index 0000000..5552a5f --- /dev/null +++ b/views/channel/edit.erb @@ -0,0 +1,34 @@ +
+
+

Editing: <%= channel.name %>

+
+
+ +
+
+
+
+
+ + +
+
+ +
+
+ + +
+
+ +
+
+ + +
+
+ + +
+
+
diff --git a/views/video/edit.erb b/views/video/edit.erb new file mode 100644 index 0000000..80c931a --- /dev/null +++ b/views/video/edit.erb @@ -0,0 +1,43 @@ +
+
+

Editing: <%= video.name %>

+
+
+ +
+
+
+
+
+ + +
+
+ +
+
+ + +
+ +
+ + +
+
+ +
+
+ + +
+
+ + +
+
+
diff --git a/views/video/view.erb b/views/video/view.erb index 84eb781..660fe1c 100644 --- a/views/video/view.erb +++ b/views/video/view.erb @@ -15,7 +15,7 @@ Delete
-

Channel: <%= video.channel.name %>

+

Channel: <%= video.channel.name %>

Serial: <%= serialize(video.serial) %>