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
37 lines (34 loc) · 892 Bytes
/
rentals_controller.rb
File metadata and controls
37 lines (34 loc) · 892 Bytes
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
class RentalsController < ApplicationController
def checkout
rental = Rental.create(rental_params)
if rental.valid?
render json: {
id: rental.id,
due_date: rental.get_due_date,
checked_out?: rental.is_checked_out?,
overdue?: rental.is_overdue?
}, status: :ok
else
render json: { errors: rental.errors.messages },
status: :bad_request
end
end
def checkin
rental = Rental.find_by(customer_id: params["customer_id"], movie_id: params["movie_id"])
if rental
rental.return_rental
if rental.save
render json: {id: rental.id}, status: :ok
else
render json: { errors: rental.errors.messages },
status: :bad_request
end
else
render json: {ok: false, errors: "Rental not found"}, status: :not_found
end
end
private
def rental_params
params.require(:rental).permit(:id, :check_in_date, :movie_id, :customer_id)
end
end