From bb509970700543d0b4462f4f051ba2e268403b00 Mon Sep 17 00:00:00 2001 From: Trang Date: Mon, 17 Dec 2018 14:38:47 -0800 Subject: [PATCH 1/8] new rails api --- db/schema.rb | 53 +++++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/db/schema.rb b/db/schema.rb index ffb28f7e..c7cb7c0a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,40 +10,43 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180618042754) do +ActiveRecord::Schema.define(version: 2018_06_18_042754) do + + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" create_table "customers", force: :cascade do |t| - t.string "name" + t.string "name" t.datetime "registered_at" - t.string "address" - t.string "city" - t.string "state" - t.string "postal_code" - t.string "phone" - t.float "account_credit" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.string "address" + t.string "city" + t.string "state" + t.string "postal_code" + t.string "phone" + t.float "account_credit" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false end create_table "movies", force: :cascade do |t| - t.string "title" - t.text "overview" - t.date "release_date" - t.integer "inventory" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.string "image_url" - t.integer "external_id" + t.string "title" + t.text "overview" + t.date "release_date" + t.integer "inventory" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.string "image_url" + t.integer "external_id" end create_table "rentals", force: :cascade do |t| - t.integer "customer_id" - t.integer "movie_id" - t.date "checkout_date" - t.date "due_date" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.boolean "returned" + t.integer "customer_id" + t.integer "movie_id" + t.date "checkout_date" + t.date "due_date" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.boolean "returned" t.index ["customer_id"], name: "index_rentals_on_customer_id" t.index ["movie_id"], name: "index_rentals_on_movie_id" end From 8461b27501bbe6cb95b73faf5ed19361d8b7a90f Mon Sep 17 00:00:00 2001 From: Shelan Date: Mon, 17 Dec 2018 14:39:02 -0800 Subject: [PATCH 2/8] added .env --- .DS_Store | Bin 0 -> 6148 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..c736245e2d8b0153eaf6fe756e9c7fb9fb26d263 GIT binary patch literal 6148 zcmeHKOKQU~5Pegd5a=e$E_H?8K!o%Jxj_02h13qIo3)PKH=1d|wbvyrGa$Xud?fjS zB@F;se>~j+D*#J2MNwr&)I96D^56wgbdFPZ=)1j*+;{1qdv9=$BaY|oX#Wd5qDO}p zykh^u*Wf_w!;H0`#I_jljCTx^=mBq&RWF^d*k3RZ3HUAlUuL$*AE(497zhUb83WR8wwpCS%J0_6 y=jmOW*mi6x8dp@IP#--4IM8$CraH}^RA*dvEQUIZ+G{#79s-q+=z@V?VBiyVyDDG+ literal 0 HcmV?d00001 From d6e398297ce2a6bd3588f9947af0bfae8c1d4694 Mon Sep 17 00:00:00 2001 From: Trang Date: Wed, 19 Dec 2018 11:28:25 -0800 Subject: [PATCH 3/8] add movie create action --- app/controllers/movies_controller.rb | 17 +++++++++++++++++ config/routes.rb | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..975ab7e2 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -21,6 +21,23 @@ def show ) end + def create + movie = Movie.new( + title: params["title"], + overview: params["overview"], + release_date: params["release_date"], + image_url: params["image_url"], #(api_result["poster_path"] ? self.construct_image_url(api_result["poster_path"]) : nil), + external_id: params["external_id"]) + + if movie.save + render json: movie.asjson(only: [:id]), status: :ok + else + render json: { + erros: movie.errors.messages + }, status: :bad_request + end + end + private def require_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 7d87bdee7e7b0af9d0f2454d862c53896addca4a Mon Sep 17 00:00:00 2001 From: Shelan Date: Wed, 19 Dec 2018 11:31:57 -0800 Subject: [PATCH 4/8] erge --- app/controllers/movies_controller.rb | 18 +----------------- app/models/movie.rb | 2 ++ 2 files changed, 3 insertions(+), 17 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 975ab7e2..2524f0da 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -11,6 +11,7 @@ def index render status: :ok, json: data end + def show render( status: :ok, @@ -21,23 +22,6 @@ def show ) end - def create - movie = Movie.new( - title: params["title"], - overview: params["overview"], - release_date: params["release_date"], - image_url: params["image_url"], #(api_result["poster_path"] ? self.construct_image_url(api_result["poster_path"]) : nil), - external_id: params["external_id"]) - - if movie.save - render json: movie.asjson(only: [:id]), status: :ok - else - render json: { - erros: movie.errors.messages - }, status: :bad_request - end - end - private def require_movie diff --git a/app/models/movie.rb b/app/models/movie.rb index 0016080b..d81a3e58 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -16,4 +16,6 @@ def image_url orig_value end end + + end From ecf603101a755e7a2a939247325e0ea698ee7da1 Mon Sep 17 00:00:00 2001 From: Shelan Date: Wed, 19 Dec 2018 11:34:22 -0800 Subject: [PATCH 5/8] merge --- app/controllers/movies_controller.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 2524f0da..1d792c1f 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -1,6 +1,23 @@ class MoviesController < ApplicationController before_action :require_movie, only: [:show] + def create + movie = Movie.new( + title: params[“title”], + overview: params[“overview”], + release_date: params[“release_date”], + image_url: params[“image_url”], #(api_result[“poster_path”] ? self.construct_image_url(api_result[“poster_path”]) : nil), + external_id: params[“external_id”]) + + if movie.save + render json: movie.asjson(only: [:id]), status: :ok + else + render json: { + erros: movie.errors.messages + }, status: :bad_request + end + end + def index if params[:query] data = MovieWrapper.search(params[:query]) From 6ea17420c27178e9923ddfa4af4aec69c28e57a7 Mon Sep 17 00:00:00 2001 From: Trang Date: Thu, 20 Dec 2018 10:37:44 -0800 Subject: [PATCH 6/8] add validation for uniqueness on external_id --- app/controllers/movies_controller.rb | 23 +++++++++++------------ app/models/movie.rb | 4 +++- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 1d792c1f..09345dd4 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -2,22 +2,17 @@ class MoviesController < ApplicationController before_action :require_movie, only: [:show] def create - movie = Movie.new( - title: params[“title”], - overview: params[“overview”], - release_date: params[“release_date”], - image_url: params[“image_url”], #(api_result[“poster_path”] ? self.construct_image_url(api_result[“poster_path”]) : nil), - external_id: params[“external_id”]) + movie = Movie.new(movie_params) if movie.save - render json: movie.asjson(only: [:id]), status: :ok + render json: movie.as_json(only: [:id]), status: :ok else - render json: { - erros: movie.errors.messages - }, status: :bad_request - end + render json: { + errors: movie.errors.messages + }, status: :bad_request + end end - + def index if params[:query] data = MovieWrapper.search(params[:query]) @@ -47,4 +42,8 @@ def require_movie render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } } end end + + def movie_params + params.permit(:title, :overview, :release_date, :image_url, :external_id) + end end diff --git a/app/models/movie.rb b/app/models/movie.rb index d81a3e58..f4256223 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -2,6 +2,8 @@ class Movie < ApplicationRecord has_many :rentals has_many :customers, through: :rentals + validates :external_id, :presence => true, :uniqueness => true + def available_inventory self.inventory - Rental.where(movie: self, returned: false).length end @@ -17,5 +19,5 @@ def image_url end end - + end From 18cc5c1a98f4f942111e81ef1dc57717e23fc1e9 Mon Sep 17 00:00:00 2001 From: Shelan Date: Thu, 20 Dec 2018 11:31:13 -0800 Subject: [PATCH 7/8] errors syntax --- app/controllers/movies_controller.rb | 94 ++++++++++++++-------------- 1 file changed, 47 insertions(+), 47 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 09345dd4..b7378968 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -1,49 +1,49 @@ class MoviesController < ApplicationController - before_action :require_movie, only: [:show] - - def create - movie = Movie.new(movie_params) - - if movie.save - render json: movie.as_json(only: [:id]), status: :ok - else - render json: { - errors: movie.errors.messages - }, status: :bad_request - end - end - - def index - if params[:query] - data = MovieWrapper.search(params[:query]) - else - data = Movie.all - end - - render status: :ok, json: data - end - - - def show - render( - status: :ok, - json: @movie.as_json( - only: [:title, :overview, :release_date, :inventory], - methods: [:available_inventory] - ) - ) - end - - private - - def require_movie - @movie = Movie.find_by(title: params[:title]) - unless @movie - render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } } - end - end - - def movie_params - params.permit(:title, :overview, :release_date, :image_url, :external_id) - end + before_action :require_movie, only: [:show] + + def create + movie = Movie.new(movie_params) + + if movie.save + render json: movie.as_json(only: [:id]), status: :ok + else + render json: { + error: movie.errors.messages + }, status: :bad_request + end + end + + def index + if params[:query] + data = MovieWrapper.search(params[:query]) + else + data = Movie.all + end + + render status: :ok, json: data + end + + + def show + render( + status: :ok, + json: @movie.as_json( + only: [:title, :overview, :release_date, :inventory], + methods: [:available_inventory] + ) + ) + end + + private + + def require_movie + @movie = Movie.find_by(title: params[:title]) + unless @movie + render status: :not_found, json: { error: { title: [“No movie with title #{params[“title”]}“] } } + end + end + + def movie_params + params.permit(:title, :overview, :release_date, :image_url, :external_id) + end end From 40c2b7e56e1d76fab64901fbebf931d0a53329f6 Mon Sep 17 00:00:00 2001 From: Trang Date: Thu, 20 Dec 2018 14:36:34 -0800 Subject: [PATCH 8/8] correct type errors --- app/controllers/movies_controller.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index b7378968..484e4f26 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -8,7 +8,7 @@ def create render json: movie.as_json(only: [:id]), status: :ok else render json: { - error: movie.errors.messages + errors: movie.errors.messages }, status: :bad_request end end @@ -37,11 +37,11 @@ def show private def require_movie - @movie = Movie.find_by(title: params[:title]) - unless @movie - render status: :not_found, json: { error: { title: [“No movie with title #{params[“title”]}“] } } - end - end + @movie = Movie.find_by(title: params[:title]) + unless @movie + render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } } + end + end def movie_params params.permit(:title, :overview, :release_date, :image_url, :external_id)