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
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ gem 'jbuilder', '~> 2.5'

gem 'will_paginate'

# Provide suppert for cross-origin resource sharing
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
Expand Down
4 changes: 3 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -210,6 +211,7 @@ DEPENDENCIES
minitest-spec-rails
pry-rails
puma (~> 3.0)
rack-cors
rails (~> 5.0.1)
sass-rails (~> 5.0)
spring
Expand Down
56 changes: 55 additions & 1 deletion app/controllers/customers_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'date'

class CustomersController < ApplicationController
SORT_FIELDS = %w(name registered_at postal_code)

Expand All @@ -10,15 +12,67 @@ def index
data = Customer.all
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],
methods: [:movies_checked_out_count]
)
end

def show
data = Customer.find_by(id: params[:id])

render json: data.as_json(
only: [:id, :name, :registered_at, :address, :city, :state, :postal_code, :phone, :account_credit],
methods: [:movies_checked_out_count]
)
end

def create
customer = Customer.new(customer_params)
customer.registered_at = Date.today

if customer.save
render json: customer.as_json(
only: [:id, :name, :registered_at, :address, :city, :state, :postal_code, :phone, :account_credit],
methods: [:movies_checked_out_count]
), status: :ok
else
render status: :bad_request
end
end

def update
customer = Customer.find_by(id: params[:id])

customer.assign_attributes(customer_params)

if customer.save
render json: customer.as_json(
only: [:id, :name, :registered_at, :address, :city, :state, :postal_code, :phone, :account_credit],
methods: [:movies_checked_out_count]
), status: :ok
else
render status: :bad_request
end

end

def destroy
if Customer.delete(params[:id])
render status: :ok
else
render status: :bad_request
end
end

private

def customer_params
return params.permit(:name, :address, :city, :state, :postal_code, :phone, :account_credit)
end

def parse_query_args
errors = {}
@sort = params[:sort]
Expand Down
18 changes: 18 additions & 0 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ def show
)
end

def create

movie = Movie.new(
title: params[:title],
overview: params[:overview],
release_date: params[:release_date],
image_url: params[:image_url],
external_id: params[:external_id],
inventory: 1
)

if movie.save
render :nothing => true, status: :ok
else
render status: :bad_request
end
end

private

def require_movie
Expand Down
10 changes: 6 additions & 4 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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, :options]
end
end
end
end
4 changes: 2 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
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, :create, :update, :destroy]

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"
Expand Down