diff --git a/Gemfile b/Gemfile index 009415af..7ddb7130 100644 --- a/Gemfile +++ b/Gemfile @@ -71,3 +71,5 @@ 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 9f7601a8..fcb76c2a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -118,8 +118,9 @@ 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-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/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..e71a5485 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -11,18 +11,42 @@ def index render status: :ok, json: data end + def create + # Search for movie in our library: + @movie = Movie.find_by(title: params[:title]) + + if !@movie + # If movie doesnt already exists in our library: + @movie = Movie.new(movie_params) + @movie.inventory = 1 + else + # If movie already exists: + @movie.inventory += 1 + end + + if @movie.save + render status: :ok, json: @movie + else + render status: 500, json: { errors: { title: ["Did not save movie to library"] } } + end + end + def show render( status: :ok, json: @movie.as_json( only: [:title, :overview, :release_date, :inventory], methods: [:available_inventory] - ) ) + ) end private + def movie_params + return params.permit(:title, :overview, :release_date, :image_url, :external_id) + end + def require_movie @movie = Movie.find_by(title: params[:title]) unless @movie diff --git a/app/models/customer.rb b/app/models/customer.rb index 6fc89447..d7f4deca 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -3,6 +3,7 @@ class Customer < ApplicationRecord has_many :movies, through: :rentals def movies_checked_out_count - self.rentals.where(returned: false).length + # self.rentals.where(returned: false).length + self.rentals.count end end diff --git a/app/serializers/movie_serializer.rb b/app/serializers/movie_serializer.rb index 8a1cdc35..c1871302 100644 --- a/app/serializers/movie_serializer.rb +++ b/app/serializers/movie_serializer.rb @@ -1,5 +1,5 @@ class MovieSerializer < ActiveModel::Serializer attribute :id, if: -> { object.id != nil } - attributes :title, :overview, :release_date, :image_url, :external_id + attributes :title, :overview, :release_date, :image_url, :external_id, :inventory end 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 diff --git a/config/routes.rb b/config/routes.rb index 54bf033e..55a751a3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,9 +3,9 @@ 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/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"