diff --git a/Gemfile b/Gemfile index 009415af..555a5547 100644 --- a/Gemfile +++ b/Gemfile @@ -40,6 +40,7 @@ gem 'will_paginate' group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platform: :mri + gem 'dotenv-rails' end group :development do @@ -56,8 +57,6 @@ group :development do # Use pry for rails console gem 'pry-rails' - - gem 'dotenv-rails' end group :test do @@ -71,3 +70,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..96fcfb19 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -7,10 +7,18 @@ def index else data = Movie.all end - render status: :ok, json: data end + def create + movie = Movie.new(movie_params) + if movie.save + render json: movie.as_json(except: [:created_at, :updated_at], status: :ok) + else + render json: { errors: movie.errors.messages }, status: :bad_request + end + end + def show render( status: :ok, @@ -23,6 +31,12 @@ def show private + def movie_params + # return params.permit(:title, :overview, :release_date, :image_url, :external_id) + + return params.permit(:title, :overview, :release_date, :image_url, :external_id, :inventory) + end + def require_movie @movie = Movie.find_by(title: params[:title]) unless @movie diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 67e77073..a06f843e 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -6,6 +6,7 @@ class RentalsController < ApplicationController def check_out rental = Rental.new(movie: @movie, customer: @customer, due_date: params[:due_date]) + rental.returned = false if rental.save render status: :ok, json: {} else diff --git a/config/application.rb b/config/application.rb index cc803322..a765a73e 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, :patch, :delete, :options] + end + end end end diff --git a/config/routes.rb b/config/routes.rb index 54bf033e..2878aa76 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, :create, :show], 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" diff --git a/package.json b/package.json deleted file mode 100644 index d39b2640..00000000 --- a/package.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "name": "video-store-api", - "version": "0.0.0", - "private": true, - "scripts": { - "start": "nodemon ./bin/www", - "test": "clear; jasmine-node --verbose spec/" - }, - "dependencies": { - "body-parser": "~1.13.2", - "cookie-parser": "~1.3.5", - "debug": "~2.2.0", - "express": "~4.13.1", - "jade": "~1.11.0", - "morgan": "~1.6.1", - "sequelize": "^3.23.3", - "serve-favicon": "~2.3.0" - }, - "devDependencies": { - "jasmine-node": "^1.14.5", - "nodemon": "^1.9.2", - "request": "^2.72.0" - } -}