From a0920cb497cb49f4bdbe887e001f0e9603f28ff0 Mon Sep 17 00:00:00 2001 From: Jessie Zhang Date: Fri, 21 Dec 2018 09:25:33 -0800 Subject: [PATCH 1/5] add movie create method in controller --- Gemfile | 2 +- Gemfile.lock | 2 +- app/controllers/movies_controller.rb | 19 +++++++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index 2d53ba9c..b88c3bd3 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,7 @@ source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } -ruby '2.5.1' +ruby '2.3.1' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' diff --git a/Gemfile.lock b/Gemfile.lock index a0a07df1..c514dd89 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -196,7 +196,7 @@ DEPENDENCIES will_paginate RUBY VERSION - ruby 2.5.1p57 + ruby 2.3.1p112 BUNDLED WITH 1.16.6 diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..f648a4f8 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -21,6 +21,25 @@ def show ) end + + def create + if Movie.find_by(title: movie_params[:title]) + movie = Movie.new(movie_params) + + if movie.save + render json: { id: movie.id }, status: :ok + else + render json: { ok: false, errors: movie.errors.messages}, status: :bad_request + end + else + render json:{ + errors:{ + title: ["movie exists indb"] + } + } + end + end + private def require_movie From 4012592a68ad8384536f0fc314534ff75a0caf70 Mon Sep 17 00:00:00 2001 From: Jessie Zhang Date: Fri, 21 Dec 2018 11:24:15 -0800 Subject: [PATCH 2/5] fixed the movie controller and router --- app/controllers/movies_controller.rb | 15 ++++++++++----- config/routes.rb | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index f648a4f8..33d060e5 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -24,17 +24,18 @@ def show def create if Movie.find_by(title: movie_params[:title]) - movie = Movie.new(movie_params) + @movie = Movie.new(movie_params) + @movie.image_url = movie_parmas["image_url"].slice(31..-1) - if movie.save - render json: { id: movie.id }, status: :ok + if @movie.save + render json: { id: @movie.id }, status: :ok else - render json: { ok: false, errors: movie.errors.messages}, status: :bad_request + render json: { ok: false, errors: @movie.errors.messages}, status: :bad_request end else render json:{ errors:{ - title: ["movie exists indb"] + title: ["movie exists in database"] } } end @@ -42,6 +43,10 @@ def create private + def movie_params + parms.permit(:title, :overview, :image_url, :release_date, :external_id, :inventory) + end + def require_movie @movie = Movie.find_by(title: params[:title]) unless @movie diff --git a/config/routes.rb b/config/routes.rb index f4c99688..76715f9a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,7 +3,7 @@ resources :customers, only: [:index] - resources :movies, only: [:index, :show], param: :title + resources :movies, only: [:index, :show, :create], param: :title post "/rentals/:title/check-out", to: "rentals#check_out", as: "check_out" post "/rentals/:title/return", to: "rentals#check_in", as: "check_in" From 35704d32f9d242d3c54c97b51d12e8d965843f90 Mon Sep 17 00:00:00 2001 From: Jessie Zhang Date: Fri, 21 Dec 2018 11:25:04 -0800 Subject: [PATCH 3/5] fixed controller create method --- app/controllers/movies_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 33d060e5..75154544 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -26,7 +26,7 @@ def create if Movie.find_by(title: movie_params[:title]) @movie = Movie.new(movie_params) @movie.image_url = movie_parmas["image_url"].slice(31..-1) - + if @movie.save render json: { id: @movie.id }, status: :ok else From b580193c563aa59da65bc91f60c9ace746490cc7 Mon Sep 17 00:00:00 2001 From: Jessie Zhang Date: Fri, 21 Dec 2018 11:38:14 -0800 Subject: [PATCH 4/5] fixed bugs --- app/controllers/movies_controller.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 75154544..ecd4c67a 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -1,3 +1,5 @@ +require 'pry' + class MoviesController < ApplicationController before_action :require_movie, only: [:show] @@ -25,8 +27,8 @@ def show def create if Movie.find_by(title: movie_params[:title]) @movie = Movie.new(movie_params) - @movie.image_url = movie_parmas["image_url"].slice(31..-1) - + @movie.image_url = movie_params["image_url"].slice(31..-1) + binding.pry if @movie.save render json: { id: @movie.id }, status: :ok else @@ -44,7 +46,7 @@ def create private def movie_params - parms.permit(:title, :overview, :image_url, :release_date, :external_id, :inventory) + params.permit(:title, :overview, :image_url, :release_date, :external_id, :inventory) end def require_movie From 36bb3808cee0bfa60c680db1611b96d0d5a5961c Mon Sep 17 00:00:00 2001 From: Jessie Zhang Date: Fri, 21 Dec 2018 12:19:47 -0800 Subject: [PATCH 5/5] fixed the controler --- app/controllers/movies_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index ecd4c67a..9ec3d976 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -25,10 +25,10 @@ def show def create - if Movie.find_by(title: movie_params[:title]) + + if !Movie.find_by(title: movie_params[:title]) @movie = Movie.new(movie_params) @movie.image_url = movie_params["image_url"].slice(31..-1) - binding.pry if @movie.save render json: { id: @movie.id }, status: :ok else