Skip to content

Commit

Permalink
Update MovieController to be DRYer
Browse files Browse the repository at this point in the history
  • Loading branch information
James Fernandez authored Aug 7, 2017
1 parent a0527f9 commit 943d4e8
Showing 1 changed file with 49 additions and 67 deletions.
116 changes: 49 additions & 67 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,91 +1,73 @@
class MoviesController < ApplicationController
get '/movies' do
if logged_in? #show only logged_in CURRENT user's movies by alphabetized list
@movies = current_user.movies.all.alphabetical_order #defined ActiveRecord method in model.rb
erb :'movies/movies'
else
redirect to '/login'
end
redirect to '/login' if !logged_in?
@movies = current_user.movies.all.alphabetical_order #defined ActiveRecord method in model.rb
erb :'movies/movies'
end

get '/movies/all' do
redirect to '/login' if !logged_in?
@movies = Movie.all.alphabetical_order #defined ActiveRecord method in model.rb
erb :'movies/all'
end

get '/movies/all' do
if logged_in? #show all movies by users by alphabetized list
@movies = Movie.all.alphabetical_order #defined ActiveRecord method in model.rb
erb :'movies/all'
else
redirect to '/login'
end
end

get '/movies/new' do
if logged_in?
erb :'movies/new'
else
redirect to '/login'
end
end
get '/movies/new' do
redirect to '/login' if !logged_in?
erb :'movies/new'
end

post '/movies' do
if params[:title].empty? || #If to test if user enters inputs
params[:comment].empty? == ""
redirect to '/movies/new'
end
@movie = Movie.new(
title: params[:title],
comment: params[:comment],
date_purchased: params[:date_purchased],
user_id: current_user.id)
@movie.save
redirect to "/movies/#{@movie.id}"
end
post '/movies' do
redirect to '/login' if !logged_in?
if params[:title].empty? || params[:comment].empty?
redirect to '/movies/new'
end
@movie = current_user.movies.build(params)
if @movie.save
redirect to "/movies/#{@movie.id}"
else
redirect to '/movies/new'
end
end

get '/movies/:id' do
if logged_in?
@movie = Movie.find_by_id(params[:id])
erb :'movies/show'
else
redirect to '/login'
end
redirect to '/login' if !logged_in?
@movie = Movie.find_by_id(params[:id])
erb :'movies/show'
end

get '/movies/:id/edit' do
if logged_in?
@movie = Movie.find_by_id(params[:id])
if @movie && @movie.user_id == current_user.id
erb :'movies/edit'
else
redirect to "/movies/#{params[:id]}" #Test user can't edit another user movie
end
redirect to '/login' if !logged_in?
@movie = Movie.find_by_id(params[:id])
if @movie && @movie.user_id == current_user.id
erb :'movies/edit'
else
redirect to '/login'
redirect to "/movies/#{params[:id]}" #Test user can't edit another user movie
end
end

patch '/movies/:id' do #PATCH use Rack::MethodOverride
if params[:title].empty? || params[:comment].empty? == ""
patch '/movies/:id' do
redirect to '/login' if !logged_in?
if params[:title].empty? || params[:comment].empty?
redirect to "/movies/#{params[:id]}/edit"
end
@movie = Movie.find_by_id(params[:id])
if @movie && @movie.user_id == current_user.id #Validate User if this his movie
@movie.update(title: params[:title])
@movie.update(comment: params[:comment])
@movie.update(date_purchased: params[:date_purchased])
@movie.save
redirect to "/movies/#{params[:id]}" #show new edit(s)
if @movie && @movie.user_id == current_user.id
if @movie.update(params)
redirect to "/movies/#{params[:id]}"
else
redirect to "/movies/#{params[:id]}/edit"
end
end
end

delete '/movies/:id/delete' do#DELETE need HIDDEN button erb
if logged_in?
@movie = Movie.find_by_id(params[:id])
if @movie.user_id == current_user.id #validate if user owns this movie or not?
@movie.delete
redirect to '/movies' #go back to user list of movies
else
redirect to "/movies/#{params[:id]}" #Test user can't delete another user movie
end
delete '/movies/:id/delete' do
redirect to '/login' if !logged_in?
@movie = Movie.find_by_id(params[:id])
if @movie.user_id == current_user.id
@movie.delete
redirect to '/movies'
else
redirect to '/login'
redirect to "/movies/#{params[:id]}"
end
end

Expand Down

0 comments on commit 943d4e8

Please sign in to comment.