forked from AdaGold/video-store-api
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathrentals_controller.rb
More file actions
46 lines (43 loc) · 1.59 KB
/
rentals_controller.rb
File metadata and controls
46 lines (43 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class RentalsController < ApplicationController
def check_out
customer = Customer.find_by(id: params[:customer_id])
movie = Movie.find_by(id: params[:movie_id])
if customer && movie
if movie.available_inventory > 0
rental = Rental.new(customer: customer, movie: movie, checkout_date: Date.today, due_date: Date.today + 7.days)
customer.movies_checked_out_count += 1
movie.available_inventory -= 1
if rental.save && movie.save && customer.save
render json: { status: 200}
else
render json: { ok: false, errors: rental.errors}, status: :bad_request
end
else
render json: { ok: false, errors: "Sorry, not enough inventory"}, status: 404
end
else
render json: { ok: false, errors: "Invalid movie or customer"}, status: 404
end
end
def check_in
customer = Customer.find_by(id: params[:customer_id])
movie = Movie.find_by(id: params[:movie_id])
if customer && movie
rental = Rental.find_by(customer: customer, movie: movie, returned: false)
if rental
rental.returned = true
customer.movies_checked_out_count -= 1
movie.available_inventory += 1
if rental.save && movie.save && customer.save
render json: { status: 200}
else
render json: { ok: false, errors: "Something went wrong."}, status: :bad_request
end
else
render json: { ok: false, errors: "Invalid rental"}, status: 404
end
else
render json: { ok: false, errors: "Invalid movie or customer"}, status: 404
end
end
end