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
14 changes: 14 additions & 0 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ def show
)
end

def create
movie = Movie.new(movie_params)

if movie.save
render json: { id: movie.id }, status: :ok
else
render json: { ok: false, errors: movie.errors.messages}, status: :bad_request
end
end

private

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

def movie_params
params.permit(:title, :overview, :release_date, :inventory, :image_url, :external_id)
end
end
3 changes: 2 additions & 1 deletion app/controllers/rentals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def overdue
name: rental.customer.name,
postal_code: rental.customer.postal_code,
checkout_date: rental.checkout_date,
due_date: rental.due_date
due_date: rental.due_date,
image_url: rental.movie.image_url
}
end
render status: :ok, json: rentals
Expand Down
2 changes: 2 additions & 0 deletions app/models/movie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class Movie < ApplicationRecord
has_many :rentals
has_many :customers, through: :rentals

validates :external_id, presence:true, uniqueness: true

def available_inventory
self.inventory - Rental.where(movie: self, returned: false).length
end
Expand Down
1 change: 1 addition & 0 deletions app/models/rental.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Rental < ApplicationRecord

# validates :movie, uniqueness: { scope: :customer }
validates :due_date, presence: true
# Comment out "validate :due_date_in_future, on: :create" to seed DB
validate :due_date_in_future, on: :create

after_initialize :set_checkout_date
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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"
post "/movies", to: "movies#create", as: "create_movie"

root 'movies#index'

Expand Down
12 changes: 12 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,15 @@
movies.first.inventory = movie_data['inventory']
movies.first.save unless movies.empty?
end

# Comment out "validate :due_date_in_future, on: :create" to seed DB

JSON.parse(File.read('db/seeds/rentals.json')).each do |rental|
new_rental = Rental.new(rental)
customer = Customer.all.sample
movie = Movie.all.sample
new_rental.customer = customer
new_rental.movie = movie

new_rental.save
end
72 changes: 72 additions & 0 deletions db/seeds/rentals.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
[
{
"checkout_date": "Mon, 03 Dec 2018",
"due_date": "Tue, 18 Dec 2018",
"returned": false,
"customer_id": 0,
"movie_id": 0
},
{
"checkout_date": "Mon, 03 Dec 2018",
"due_date": "Mon, 17 Dec 2018",
"returned": false,
"customer_id": 0,
"movie_id": 0
},
{
"checkout_date": "Mon, 27 Nov 2018",
"due_date": "Tue, 18 Dec 2018",
"returned": false,
"customer_id": 0,
"movie_id": 0
},
{
"checkout_date": "Tue, 27 Nov 2018",
"due_date": "Tue, 18 Dec 2018",
"returned": false,
"customer_id": 0,
"movie_id": 0
},
{
"checkout_date": "Tue, 04 Dec 2018",
"due_date": "Tue, 18 Dec 2018",
"returned": false,
"customer_id": 0,
"movie_id": 0
},
{
"checkout_date": "Mon, 03 Dec 2018",
"due_date": "Tue, 18 Dec 2018",
"returned": false,
"customer_id": 0,
"movie_id": 0
},
{
"checkout_date": "Sun, 26 Nov 2018",
"due_date": "Mon, 17 Dec 2018",
"returned": false,
"customer_id": 0,
"movie_id": 0
},
{
"checkout_date": "Wed, 28 Nov 2018",
"due_date": "Sun, 16 Dec 2018",
"returned": false,
"customer_id": 0,
"movie_id": 0
},
{
"checkout_date": "Wed, 28 Nov 2018",
"due_date": "Sun, 16 Dec 2018",
"returned": false,
"customer_id": 0,
"movie_id": 0
},
{
"checkout_date": "Wed, 28 Nov 2018",
"due_date": "Sun, 16 Dec 2018",
"returned": false,
"customer_id": 0,
"movie_id": 0
}
]
5 changes: 4 additions & 1 deletion lib/movie_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class MovieWrapper
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 []
Expand Down Expand Up @@ -38,4 +37,8 @@ def self.construct_image_url(img_name)
return BASE_IMG_URL + DEFAULT_IMG_SIZE + img_name
end

# def self.construct_image_url_new(img_name)
# return DEFAULT_IMG_SIZE + img_name
# end

end