diff --git a/Gemfile b/Gemfile index d837ba0e..358ecc33 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 f80bcea9..7224ad08 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) @@ -170,7 +171,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) @@ -207,6 +207,7 @@ DEPENDENCIES listen (~> 3.0.5) minitest-reporters minitest-spec-rails + pg (~> 0.18) pry-rails puma (~> 3.0) rack-cors @@ -214,7 +215,6 @@ DEPENDENCIES sass-rails (~> 5.0) spring spring-watcher-listen (~> 2.0.0) - sqlite3 turbolinks (~> 5) tzinfo-data uglifier (>= 1.3.0) @@ -222,4 +222,4 @@ DEPENDENCIES will_paginate BUNDLED WITH - 1.14.6 + 1.15.1 diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index be25f1be..a88dab2f 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -14,10 +14,22 @@ def index render json: data.as_json( only: [:id, :name, :registered_at, :address, :city, :state, :postal_code, :phone, :account_credit], - methods: [:movies_checked_out_count] + methods: [:movies_checked_out_count, :all_rentals] ) 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], + methods: [:movies_checked_out_count, :all_rentals] + ) + else + render status: :bad_request, json: { errors: errors } + end + end + private def parse_query_args errors = {} diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..47cb98df 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -21,6 +21,27 @@ 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 + + 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 + + private def require_movie @@ -29,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(:movie).permit(:title, :overview, :release_date, :inventory, :image_url) + end end diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 92744380..1f6abf27 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -2,11 +2,17 @@ class RentalsController < ApplicationController before_action :require_movie, only: [:check_out, :check_in] before_action :require_customer, only: [:check_out, :check_in] + def index + data = Rental.all + + render status: :ok, json: data + end + def check_out rental = Rental.new(movie: @movie, customer: @customer, due_date: params[:due_date], returned: false) if rental.save - render status: :ok, json: {} + render status: :ok, json: { success: rental} else render status: :bad_request, json: { errors: rental.errors.messages } end diff --git a/app/models/customer.rb b/app/models/customer.rb index 6fc89447..f238811c 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -5,4 +5,8 @@ class Customer < ApplicationRecord def movies_checked_out_count self.rentals.where(returned: false).length end + + def all_rentals + self.movies + end end 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 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'] %> diff --git a/config/routes.rb b/config/routes.rb index 54bf033e..406c882f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,9 +1,11 @@ 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], param: :title + resources :movies, only: [:index, :show, :create], param: :title + + get '/rentals', to: 'rentals#index' 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/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