Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -207,19 +207,19 @@ 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
spring-watcher-listen (~> 2.0.0)
sqlite3
turbolinks (~> 5)
tzinfo-data
uglifier (>= 1.3.0)
web-console (>= 3.3.0)
will_paginate

BUNDLED WITH
1.14.6
1.15.1
14 changes: 13 additions & 1 deletion app/controllers/customers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down
25 changes: 25 additions & 0 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
8 changes: 7 additions & 1 deletion app/controllers/rentals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions app/models/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion app/models/movie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 11 additions & 6 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'] %>
6 changes: 4 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
7 changes: 5 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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