diff --git a/.gitignore b/.gitignore index ce865237..c83a3c88 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,8 @@ # Ignore Byebug command history file. .byebug_history .env + +# Elastic Beanstalk Files +.elasticbeanstalk/* +!.elasticbeanstalk/*.cfg.yml +!.elasticbeanstalk/*.global.yml diff --git a/Gemfile b/Gemfile index 009415af..61b38b73 100644 --- a/Gemfile +++ b/Gemfile @@ -5,11 +5,21 @@ git_source(:github) do |repo_name| "https://github.com/#{repo_name}.git" end + + +group :development, :test do + gem 'sqlite3' +end + +group :production do + gem 'pg', '~> 0.18' +end + gem 'awesome_print' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.0.1' # Use sqlite3 as the database for Active Record -gem 'sqlite3' +# gem 'sqlite3' # Use Puma as the app server gem 'puma', '~> 3.0' # Use SCSS for stylesheets @@ -35,6 +45,8 @@ gem 'jbuilder', '~> 2.5' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development +gem 'rack-cors', require: 'rack/cors' + gem 'will_paginate' group :development, :test do @@ -42,6 +54,8 @@ group :development, :test do gem 'byebug', platform: :mri end + + group :development do # Access an IRB console on exception pages or by using <%= console %> anywhere in the code. gem 'web-console', '>= 3.3.0' diff --git a/Gemfile.lock b/Gemfile.lock index 9f7601a8..a12f4cec 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -112,14 +112,16 @@ GEM nio4r (1.2.1) nokogiri (1.8.1) mini_portile2 (~> 2.3.0) + pg (0.21.0) pry (0.10.4) coderay (~> 1.1.0) method_source (~> 0.8.1) 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) @@ -208,8 +210,10 @@ DEPENDENCIES listen (~> 3.0.5) minitest-reporters minitest-spec-rails + pg (~> 0.18) pry-rails puma (~> 3.0) + rack-cors rails (~> 5.0.1) sass-rails (~> 5.0) spring diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index be25f1be..54e244a0 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -7,10 +7,10 @@ def index if @sort data = Customer.all.order(@sort) else - data = Customer.all + data = Customer.order(:name) end - data = data.paginate(page: params[:p], per_page: params[:n]) + # data = data.paginate(page: params[:p], per_page: params[:n]) render json: data.as_json( only: [:id, :name, :registered_at, :address, :city, :state, :postal_code, :phone, :account_credit], diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..8eec0b26 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -5,7 +5,7 @@ def index if params[:query] data = MovieWrapper.search(params[:query]) else - data = Movie.all + data = Movie.order(:title) end render status: :ok, json: data @@ -21,6 +21,16 @@ def show ) end + def create + movie = Movie.new(movie_params) + + if movie.save + render :json => movie.to_json, :status => :ok + else + render :json => { :errors=>movie.errors.messages }, :status => :bad_request + end + end + private def require_movie @@ -29,4 +39,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/models/movie.rb b/app/models/movie.rb index 0016080b..ad523386 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -11,7 +11,13 @@ def image_url if !orig_value MovieWrapper::DEFAULT_IMG_URL elsif external_id - MovieWrapper.construct_image_url(orig_value) + str = MovieWrapper.construct_image_url(orig_value) + index = str.rindex('https://image.tmdb.org/t/p/w185') + if index != 0 + return str[index..-1] + else + return str + end else orig_value end diff --git a/config/application.rb b/config/application.rb index cc803322..33b80c2d 100644 --- a/config/application.rb +++ b/config/application.rb @@ -11,6 +11,12 @@ class Application < Rails::Application # Settings in config/environments/* take precedence over those specified here. # Application configuration should go into files in config/initializers # -- all .rb files in that directory are automatically loaded. + config.middleware.insert_before 0, Rack::Cors do + allow do + origins '*' + resource '*', headers: :any, methods: [:get, :post, :options] + end + end #this loads everything in the lib folder automatically config.eager_load_paths << Rails.root.join('lib') diff --git a/config/database.yml b/config/database.yml index 1c1a37ca..b62c5ff3 100644 --- a/config/database.yml +++ b/config/database.yml @@ -22,4 +22,9 @@ test: production: <<: *default - database: db/production.sqlite3 + adapter: postgresql + encoding: unicode + pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> + database: _production + username: + password: <%= ENV['_DATABASE_PASSWORD'] %> 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"