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
43 changes: 40 additions & 3 deletions app/controllers/customers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,44 @@ def index
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]
only: [:id, :name, :registered_at, :address, :city, :state, :postal_code, :phone, :account_credit],
methods: [:movies_checked_out_count]
)
end

private
def show
@customer = Customer.find(params[:id])
end

def new
@customer = Customer.new
end

def create
@customer = Customer.create(customer_params)
end

def update
@customer = Customer.find_by(id: params[:id])
if @customer.nil?
head :not_found
else
@customer.update_attributes(customer_params)
redirect_to customer_path
end
end

def edit
@customer = Customer.find_by(id: params[:id])
end

def destroy
@customer = Customer.find_by(id: params[:id])
@customer.destroy
redirect_to customers_path
end

private
def parse_query_args
errors = {}
@sort = params[:sort]
Expand All @@ -30,4 +62,9 @@ def parse_query_args
render status: :bad_request, json: { errors: errors }
end
end

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

end
21 changes: 15 additions & 6 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ def index
render status: :ok, json: data
end

def create
@rental_movie = Movie.create(movie_params)
end

def show
render(
status: :ok,
json: @movie.as_json(
only: [:title, :overview, :release_date, :inventory],
methods: [:available_inventory]
)
)
status: :ok,
json: @movie.as_json(
only: [:title, :overview, :release_date, :inventory],
methods: [:available_inventory]
)
)
end

private
Expand All @@ -29,4 +33,9 @@ def require_movie
render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } }
end
end

def movie_params
return params.require(:movie).permit(:title, :overview, :release_date, :image_url)
end

end
4 changes: 3 additions & 1 deletion app/models/movie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ def available_inventory

def image_url
orig_value = read_attribute :image_url
if !orig_value
if !orig_value || orig_value == "http://lorempixel.com/185/278/"
MovieWrapper::DEFAULT_IMG_URL
else
# using slice to remove the part of the url i do not want printed twice, the ! will modify the original variable
orig_value.slice!('https://image.tmdb.org/t/p/w185')
MovieWrapper.construct_image_url(orig_value)
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/movie_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class MovieSerializer < ActiveModel::Serializer
attribute :id, if: -> { object.id != nil }

attributes :title, :overview, :release_date, :image_url, :external_id
attributes :title, :overview, :release_date, :image_url, :external_id, :inventory
end
5 changes: 3 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
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

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"
get "/rentals/overdue", to: "rentals#overdue", as: "overdue"

# get "/movies?query=", to: "movies#index", as: "external_api"

end
12 changes: 7 additions & 5 deletions lib/movie_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ def self.search(query)

def self.construct_movie(api_result)
Movie.new(
title: api_result["title"],
overview: api_result["overview"],
release_date: api_result["release_date"],
image_url: api_result["poster_path"], #(api_result["poster_path"] ? self.construct_image_url(api_result["poster_path"]) : nil),
external_id: api_result["id"])
title: api_result["title"],
overview: api_result["overview"],
release_date: api_result["release_date"],
image_url: api_result["poster_path"],
#(api_result["poster_path"] ? self.construct_image_url(api_result["poster_path"]) : nil),
external_id: api_result["id"])
end


def self.construct_image_url(img_name)
return BASE_IMG_URL + DEFAULT_IMG_SIZE + img_name
end
Expand Down