From a79ac0c9eb837daab2f49babf5152bd7ef08fd50 Mon Sep 17 00:00:00 2001 From: Jackie Date: Tue, 19 Jun 2018 14:24:12 -0700 Subject: [PATCH 1/5] Added post movie route and create method --- Gemfile.lock | 2 +- app/controllers/movies_controller.rb | 13 +++++++++++++ config/routes.rb | 3 +-- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 9f7601a8..22779079 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -118,7 +118,7 @@ GEM slop (~> 3.4) pry-rails (0.3.4) pry (>= 0.9.10) - puma (3.6.2) + puma (3.11.4) rack (2.0.1) rack-test (0.6.3) rack (>= 1.0) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..3756e3b3 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -21,6 +21,15 @@ def show ) end + def create + movie = Movie.create(movies_params) + if movie.valid? + render json: { id: movie.id }, status: :ok + else + render json: { errors: movie.errors }, status: :bad_request + end + end + private def require_movie @@ -29,4 +38,8 @@ def require_movie render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } } end end + + def movies_params + return params.permit(:title, :overview, :release_date, :image_url, :external_id) + end end diff --git a/config/routes.rb b/config/routes.rb index 54bf033e..ee65ac4d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,11 +3,10 @@ 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" get "/rentals/overdue", to: "rentals#overdue", as: "overdue" - end From 342254a7222d6eafd3eeb2459a0185f7ea1c601d Mon Sep 17 00:00:00 2001 From: Jackie Date: Tue, 19 Jun 2018 15:15:06 -0700 Subject: [PATCH 2/5] changed configuration for application to change Cors change --- Gemfile | 1 + Gemfile.lock | 2 ++ config/application.rb | 10 ++++++---- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/Gemfile b/Gemfile index 009415af..368fad14 100644 --- a/Gemfile +++ b/Gemfile @@ -71,3 +71,4 @@ gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] gem 'httparty' gem "active_model_serializers" +gem 'rack-cors', require: 'rack/cors' diff --git a/Gemfile.lock b/Gemfile.lock index 22779079..fcb76c2a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -120,6 +120,7 @@ GEM pry (>= 0.9.10) puma (3.11.4) rack (2.0.1) + rack-cors (1.0.2) rack-test (0.6.3) rack (>= 1.0) rails (5.0.1) @@ -210,6 +211,7 @@ DEPENDENCIES minitest-spec-rails pry-rails puma (~> 3.0) + rack-cors rails (~> 5.0.1) sass-rails (~> 5.0) spring diff --git a/config/application.rb b/config/application.rb index cc803322..0e371183 100644 --- a/config/application.rb +++ b/config/application.rb @@ -15,9 +15,11 @@ class Application < Rails::Application #this loads everything in the lib folder automatically config.eager_load_paths << Rails.root.join('lib') - config.action_dispatch.default_headers = { - 'Access-Control-Allow-Origin' => '*', - 'Access-Control-Request-Method' => %w{GET POST OPTIONS}.join(",") - } + config.middleware.insert_before 0, Rack::Cors do + allow do + origins '*' + resource '*', headers: :any, methods: [:get, :post, :options] + end + end end end From 55675efd743eb2dbef563ccd9c99d21633b25f73 Mon Sep 17 00:00:00 2001 From: Jackie Date: Wed, 20 Jun 2018 13:18:12 -0700 Subject: [PATCH 3/5] Added validation to movie model to ensure uniqueness --- app/models/movie.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/movie.rb b/app/models/movie.rb index 0016080b..aa92172b 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -1,6 +1,7 @@ class Movie < ApplicationRecord has_many :rentals has_many :customers, through: :rentals + validates_uniqueness_of :title def available_inventory self.inventory - Rental.where(movie: self, returned: false).length From b0f265340f2399be8d919ab5220be45c2da7dd1b Mon Sep 17 00:00:00 2001 From: Jackie Date: Thu, 21 Jun 2018 20:29:42 -0700 Subject: [PATCH 4/5] Updated customer model to get count of movies rented by customer --- app/models/customer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/customer.rb b/app/models/customer.rb index 6fc89447..ea5aa006 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -3,6 +3,6 @@ class Customer < ApplicationRecord has_many :movies, through: :rentals def movies_checked_out_count - self.rentals.where(returned: false).length + self.rentals.count end end From dcde4817c1a2a7ce4ee857c3e86aa6c423d42c69 Mon Sep 17 00:00:00 2001 From: Jackie Date: Fri, 22 Jun 2018 13:04:52 -0700 Subject: [PATCH 5/5] Updated gemfile --- Gemfile.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index fcb76c2a..f68a2fef 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -64,7 +64,7 @@ GEM coffee-script-source execjs coffee-script-source (1.12.2) - concurrent-ruby (1.0.4) + concurrent-ruby (1.0.5) debug_inspector (0.0.3) dotenv (2.2.0) dotenv-rails (2.2.0) @@ -119,7 +119,7 @@ GEM pry-rails (0.3.4) pry (>= 0.9.10) puma (3.11.4) - rack (2.0.1) + rack (2.0.5) rack-cors (1.0.2) rack-test (0.6.3) rack (>= 1.0) @@ -164,7 +164,7 @@ GEM spring-watcher-listen (2.0.1) listen (>= 2.7, < 4.0) spring (>= 1.2, < 3.0) - sprockets (3.7.1) + sprockets (3.7.2) concurrent-ruby (~> 1.0) rack (> 1, < 3) sprockets-rails (3.2.0) @@ -224,4 +224,4 @@ DEPENDENCIES will_paginate BUNDLED WITH - 1.16.1 + 1.16.2