From eb2ca936ad4512b106d3727cf17ee7ab2d5c22bb Mon Sep 17 00:00:00 2001 From: sai samant Date: Tue, 13 Jun 2017 15:56:12 -0700 Subject: [PATCH 1/8] change db to postgres (not sure why) --- Gemfile | 4 ++-- Gemfile.lock | 6 +++--- config/database.yml | 17 +++++++++++------ 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/Gemfile b/Gemfile index d5c7affd..4608c404 100644 --- a/Gemfile +++ b/Gemfile @@ -8,8 +8,8 @@ end # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.0.1' -# Use sqlite3 as the database for Active Record -gem 'sqlite3' +# Use postgresql as the database for Active Record +gem 'pg', '~> 0.18' # Use Puma as the app server gem 'puma', '~> 3.0' # Use SCSS for stylesheets diff --git a/Gemfile.lock b/Gemfile.lock index b39604fa..6ba5a0a2 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -111,6 +111,7 @@ GEM nio4r (1.2.1) nokogiri (1.7.0.1) mini_portile2 (~> 2.1.0) + pg (0.20.0) pry (0.10.4) coderay (~> 1.1.0) method_source (~> 0.8.1) @@ -169,7 +170,6 @@ GEM actionpack (>= 4.0) activesupport (>= 4.0) sprockets (>= 3.0.0) - sqlite3 (1.3.13) thor (0.19.4) thread_safe (0.3.5) tilt (2.0.5) @@ -206,13 +206,13 @@ DEPENDENCIES listen (~> 3.0.5) minitest-reporters minitest-spec-rails + pg (~> 0.18) pry-rails puma (~> 3.0) rails (~> 5.0.1) sass-rails (~> 5.0) spring spring-watcher-listen (~> 2.0.0) - sqlite3 turbolinks (~> 5) tzinfo-data uglifier (>= 1.3.0) @@ -220,4 +220,4 @@ DEPENDENCIES will_paginate BUNDLED WITH - 1.14.6 + 1.15.1 diff --git a/config/database.yml b/config/database.yml index 1c1a37ca..faafea9b 100644 --- a/config/database.yml +++ b/config/database.yml @@ -5,21 +5,26 @@ # gem 'sqlite3' # default: &default - adapter: sqlite3 - pool: 5 - timeout: 5000 + adapter: postgresql + encoding: unicode + # For details on connection pooling, see rails configuration guide + # http://guides.rubyonrails.org/configuring.html#database-pooling + pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> + development: <<: *default - database: db/development.sqlite3 + database: VideoStoreConsumer-API_development # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: <<: *default - database: db/test.sqlite3 + database: VideoStoreConsumer-API_test production: <<: *default - database: db/production.sqlite3 + database: VideoStoreConsumer-API_production + username: VideoStoreConsumer-API + password: <%= ENV['VideoStoreConsumer-API_DATABASE_PASSWORD'] %> From b40b5dbf4410dd246202ce2e44aaf8834289d973 Mon Sep 17 00:00:00 2001 From: Dan Roberts Date: Wed, 14 Jun 2017 12:53:52 -0700 Subject: [PATCH 2/8] Fix the CORS issue using the rack-cors gem --- Gemfile | 2 ++ Gemfile.lock | 2 ++ config/application.rb | 10 ++++++---- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Gemfile b/Gemfile index d5c7affd..d837ba0e 100644 --- a/Gemfile +++ b/Gemfile @@ -37,6 +37,8 @@ gem 'jbuilder', '~> 2.5' gem 'will_paginate' +gem 'rack-cors', :require => 'rack/cors' + group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platform: :mri diff --git a/Gemfile.lock b/Gemfile.lock index b39604fa..f80bcea9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -119,6 +119,7 @@ GEM pry (>= 0.9.10) puma (3.6.2) rack (2.0.1) + rack-cors (0.4.1) rack-test (0.6.3) rack (>= 1.0) rails (5.0.1) @@ -208,6 +209,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 2ac21a62..9f8a6f91 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' => 'http://localhost:8081', - '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, :put, :delete, :options] + end + end end end From 23b868189da5f357d69d373035c131ee02d15608 Mon Sep 17 00:00:00 2001 From: sai samant Date: Thu, 15 Jun 2017 09:46:22 -0700 Subject: [PATCH 3/8] pseudo code for create method in movies controller --- app/controllers/movies_controller.rb | 7 +++++++ db/schema.rb | 7 +++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..6e9c8de7 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -21,6 +21,13 @@ def show ) end + def create + # check if the movie is already in the rails db by checking if there is a movie in the rails db with the same title and release date from params (use find by) + # if the movie already exists in the rails db, then increase the inventory by 1 + # set inventory to 1 when we create the movie in the rails db + end + + private def require_movie diff --git a/db/schema.rb b/db/schema.rb index 15e75977..4fa96b50 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -12,6 +12,9 @@ ActiveRecord::Schema.define(version: 20170612201103) 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.datetime "registered_at" @@ -43,8 +46,8 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.boolean "returned", default: false - t.index ["customer_id"], name: "index_rentals_on_customer_id" - t.index ["movie_id"], name: "index_rentals_on_movie_id" + t.index ["customer_id"], name: "index_rentals_on_customer_id", using: :btree + t.index ["movie_id"], name: "index_rentals_on_movie_id", using: :btree end end From 7ee646243b9a597fceda2c669dece7ab72392765 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Fri, 16 Jun 2017 09:23:54 -0700 Subject: [PATCH 4/8] switching the database to postgress changed schema --- db/schema.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/db/schema.rb b/db/schema.rb index 15e75977..4fa96b50 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -12,6 +12,9 @@ ActiveRecord::Schema.define(version: 20170612201103) 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.datetime "registered_at" @@ -43,8 +46,8 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.boolean "returned", default: false - t.index ["customer_id"], name: "index_rentals_on_customer_id" - t.index ["movie_id"], name: "index_rentals_on_movie_id" + t.index ["customer_id"], name: "index_rentals_on_customer_id", using: :btree + t.index ["movie_id"], name: "index_rentals_on_movie_id", using: :btree end end From c069f4d7c984f8521bbfd7d8acb442e31991695c Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Fri, 16 Jun 2017 10:16:50 -0700 Subject: [PATCH 5/8] added route for create and create function in the movies controller --- app/controllers/movies_controller.rb | 20 +++++++++++++++++++- config/routes.rb | 2 +- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 6e9c8de7..d40907df 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -24,7 +24,21 @@ def show def create # check if the movie is already in the rails db by checking if there is a movie in the rails db with the same title and release date from params (use find by) # if the movie already exists in the rails db, then increase the inventory by 1 - # set inventory to 1 when we create the movie in the rails db + # set inventory to 1 when we create the movie in the rails db + + # movie = Movie.find_by(title: params[:title]) + # if movie + # movie.inventory += 1 + # else + movie = Movie.new(movie_params) + if movie.save + render status: :ok, json: {id: movie.id} + else + render status: :bad_request, json: { errors: movie.errors.messages } + end + # end + + end @@ -36,4 +50,8 @@ def require_movie render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } } end end + + def movie_params + params.require.permit(:title, :overview, :release_date, :inventory, :image_url) + 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" From 3000d5e55aeb9f869a3716e2254c884d62fbe201 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Fri, 16 Jun 2017 10:56:20 -0700 Subject: [PATCH 6/8] added inventory handling to the create method in movies controller --- app/controllers/movies_controller.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index d40907df..47cb98df 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -26,19 +26,19 @@ def create # if the movie already exists in the rails db, then increase the inventory by 1 # set inventory to 1 when we create the movie in the rails db - # movie = Movie.find_by(title: params[:title]) - # if movie - # movie.inventory += 1 - # else + movie = Movie.find_by(title: params[:title], release_date: params[:release_date]) + if movie + movie.inventory += 1 + movie.save + else movie = Movie.new(movie_params) + movie.inventory = 1 if movie.save render status: :ok, json: {id: movie.id} else render status: :bad_request, json: { errors: movie.errors.messages } end - # end - - + end end @@ -52,6 +52,6 @@ def require_movie end def movie_params - params.require.permit(:title, :overview, :release_date, :inventory, :image_url) + params.require(:movie).permit(:title, :overview, :release_date, :inventory, :image_url) end end From 6b0670cfd37d6332c1dc97d507fd0517c163f2fa Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Fri, 16 Jun 2017 11:19:20 -0700 Subject: [PATCH 7/8] fixed image_url method that constructs the url for movie posters --- app/models/movie.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/models/movie.rb b/app/models/movie.rb index 0327a4d6..2d5bd4d3 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -12,8 +12,10 @@ def image_url orig_value = read_attribute :image_url if !orig_value MovieWrapper::DEFAULT_IMG_URL - else + elsif external_id MovieWrapper.construct_image_url(orig_value) + else + orig_value end end end From 6dd7c202e6d01818d569dac6ad483fbaf9771ee6 Mon Sep 17 00:00:00 2001 From: sai samant Date: Mon, 19 Jun 2017 16:48:52 -0700 Subject: [PATCH 8/8] can fetch customer object from rails from customer dropdown --- app/controllers/customers_controller.rb | 13 +++++++++++++ config/routes.rb | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index be25f1be..b773ee79 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -18,6 +18,19 @@ def index ) end + def show + customer = Customer.find_by(id: params[:id]) + if customer + render json: customer.as_json( + only: [:id, :name, :registered_at, :address, :city, :state, :postal_code, :phone, :account_credit, :rentals], + methods: [:movies_checked_out_count], + include: [:rentals] + ) + else + render status: :bad_request, json: { errors: errors } + end + end + private def parse_query_args errors = {} diff --git a/config/routes.rb b/config/routes.rb index 8eff1ff6..f808027a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,7 @@ Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html - resources :customers, only: [:index] + resources :customers, only: [:index, :show] resources :movies, only: [:index, :show, :create], param: :title