diff --git a/.gitignore b/.gitignore index ce865237..5bc6e37c 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,4 @@ # Ignore Byebug command history file. .byebug_history .env +.DS_Store diff --git a/Gemfile b/Gemfile index 009415af..e0ec0062 100644 --- a/Gemfile +++ b/Gemfile @@ -5,6 +5,8 @@ git_source(:github) do |repo_name| "https://github.com/#{repo_name}.git" end +gem 'rack-cors', require: 'rack/cors' + gem 'awesome_print' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.0.1' diff --git a/Gemfile.lock b/Gemfile.lock index 9f7601a8..75617a53 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 @@ -222,4 +224,4 @@ DEPENDENCIES will_paginate BUNDLED WITH - 1.16.1 + 1.16.2 diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..7d72436a 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -17,10 +17,29 @@ def show json: @movie.as_json( only: [:title, :overview, :release_date, :inventory], methods: [:available_inventory] - ) ) + ) end + + def create + movie = Movie.find_by(external_id: params[:external_id]) + if !movie + movie = Movie.new(movie_params) + else + render json: { ok: false, errors: movie.errors }, + status: :bad_request + end + + if movie.save + render json: movie.as_json, status: :ok + else + render json: { ok: false, errors: movie.errors }, + status: :bad_request + end + end + + private def require_movie @@ -29,4 +48,8 @@ def require_movie render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } } end end + + def movie_params + return params.permit(:title, :overview, :release_date, :image_url, :external_id, :inventory) + end end diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 67e77073..2aa82f90 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -4,8 +4,7 @@ class RentalsController < ApplicationController # TODO: make sure that wave 2 works all the way def check_out - rental = Rental.new(movie: @movie, customer: @customer, due_date: params[:due_date]) - + rental = Rental.new(movie: @movie, customer: @customer, due_date: Date.today + 7, returned: false) if rental.save render status: :ok, json: {} else diff --git a/app/models/movie.rb b/app/models/movie.rb index 0016080b..a8e77710 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -10,7 +10,7 @@ def image_url orig_value = read_attribute :image_url if !orig_value MovieWrapper::DEFAULT_IMG_URL - elsif external_id + elsif external_id && !orig_value.include?("http") MovieWrapper.construct_image_url(orig_value) else orig_value diff --git a/app/serializers/movie_serializer.rb b/app/serializers/movie_serializer.rb index 8a1cdc35..194cc45f 100644 --- a/app/serializers/movie_serializer.rb +++ b/app/serializers/movie_serializer.rb @@ -1,5 +1,6 @@ class MovieSerializer < ActiveModel::Serializer attribute :id, if: -> { object.id != nil } + attribute :available_inventory, if: -> { object.id != nil } attributes :title, :overview, :release_date, :image_url, :external_id end diff --git a/config/application.rb b/config/application.rb index cc803322..f0638b57 100644 --- a/config/application.rb +++ b/config/application.rb @@ -19,5 +19,12 @@ class Application < Rails::Application '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..8eff1ff6 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"