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
2 changes: 2 additions & 0 deletions .elasticbeanstalk/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
global:
profile: eb-cli
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@

# Ignore master key for decrypting credentials and more.
/config/master.key

.env
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ end
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

group :development, :test do
gem 'pry-rails'
gem 'dotenv-rails'
end

Expand Down
9 changes: 1 addition & 8 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ GEM
byebug (10.0.2)
case_transform (0.2)
activesupport
coderay (1.1.2)
concurrent-ruby (1.1.4)
crass (1.0.4)
dotenv (2.5.0)
Expand Down Expand Up @@ -110,11 +109,6 @@ GEM
nokogiri (1.8.5)
mini_portile2 (~> 2.3.0)
pg (1.1.3)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
pry-rails (0.3.8)
pry (>= 0.10.4)
puma (3.12.0)
rack (2.0.6)
rack-cors (1.0.2)
Expand Down Expand Up @@ -186,7 +180,6 @@ DEPENDENCIES
minitest-rails
minitest-reporters
pg (>= 0.18, < 2.0)
pry-rails
puma (~> 3.11)
rack-cors
rails (~> 5.2.0)
Expand All @@ -199,4 +192,4 @@ RUBY VERSION
ruby 2.5.1p57

BUNDLED WITH
1.16.6
1.17.2
26 changes: 24 additions & 2 deletions app/controllers/customers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,37 @@ 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

private

def create

customer = Customer.new(customer_params)

if customer.save
render(
status: :ok, json: customer.as_json( only: [:name, :id] )
)
else
#errors
render(
status: :bad_request, json: {errors: {customer: customer.errors.messages }}
)
end
end

private

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

def parse_query_args
errors = {}
@sort = params[:sort]
Expand Down
23 changes: 22 additions & 1 deletion app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,29 @@ def show
json: @movie.as_json(
only: [:title, :overview, :release_date, :inventory],
methods: [:available_inventory]
)
)
)
end

def create
movie_exists = Movie.find_by(external_id: params[:id])
if movie_exists
render status: :bad_request, json: {errors: {movie: "Movie already exists in library"}}
else
movie = MovieWrapper.get_movie(params[:id])

if movie
movie.save
render(
status: :ok, json: movie.as_json( only: [:title] )
)
else
#errors
render(
status: :bad_request, json: {errors: {id: 'must be present'}}
)
end
end
end

private
Expand Down
1 change: 1 addition & 0 deletions app/models/customer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Customer < ApplicationRecord
has_many :rentals
has_many :movies, through: :rentals
validates :name, presence: true

def movies_checked_out_count
self.rentals.where(returned: false).length
Expand Down
1 change: 1 addition & 0 deletions app/models/movie.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Movie < ApplicationRecord
has_many :rentals
has_many :customers, through: :rentals
validates :external_id, uniqueness: true

def available_inventory
self.inventory - Rental.where(movie: self, returned: false).length
Expand Down
4 changes: 3 additions & 1 deletion 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, :create]
# resources :customers, only: [:create]

resources :movies, only: [:index, :show], param: :title
resources :movies, only: [:create], param: :id

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
26 changes: 20 additions & 6 deletions lib/movie_wrapper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

class MovieWrapper
BASE_URL = "https://api.themoviedb.org/3/"
KEY = ENV["MOVIEDB_KEY"]
Expand All @@ -8,13 +7,16 @@ class MovieWrapper
DEFAULT_IMG_URL = "http://lorempixel.com/185/278/"

def self.search(query)

url = BASE_URL + "search/movie?api_key=" + KEY + "&query=" + query

puts url
# puts url
response = HTTParty.get(url)
if response["total_results"] == 0
return []
else

puts response
movies = response["results"].map do |result|
self.construct_movie(result)
Expand All @@ -23,6 +25,18 @@ def self.search(query)
end
end

def self.get_movie(id)

url = "#{BASE_URL}movie/#{id}?api_key=#{KEY}"
response = HTTParty.get(url)

if response["id"]
return self.construct_movie(response)
else
return nil
end
end

private

def self.construct_movie(api_result)
Expand All @@ -32,10 +46,10 @@ def self.construct_movie(api_result)
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
end

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

end
end