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
81 lines (57 loc) · 1.67 KB
/
rentals_controller.rb
File metadata and controls
81 lines (57 loc) · 1.67 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
require 'date'
class RentalsController < ApplicationController
RENTAL_PERIOD = 7
def check_in
movie = Movie.find_by(id: rental_params[:movie_id])
customer = Customer.find_by(id: rental_params[:customer_id])
if movie
if customer
rental = Rental.find_by(movie_id: movie.id, customer_id: customer.id)
movie.update_checkin
customer.update_count("in")
else
render json: {ok: false, errors: "Customer not found"}, status: :bad_request
return
end
else
render json: {ok: false, errors: "Movie not found"}, status: :bad_request
return
end
if rental
rental.delete
render json: { id: movie.id }, status: :ok
else
render json: {ok: false, errors: "Rental could not be found"}, status: :bad_request
end
end
def check_out
movie = Movie.find_by(id: rental_params[:movie_id])
rental = Rental.new(rental_params)
if movie
if movie.available_inventory > 0
movie.update_checkout
rental.checkout_date = Date.today
rental.due_date = rental.checkout_date + RENTAL_PERIOD
rental.save
customer = Customer.find_by(id: rental_params[:customer_id])
if customer
customer.update_count("out")
end
else
render json: {
errors: ["No copies are currently available"]
}, status: :ok
return
end
end
if rental.valid?
render json: {due_date: rental.due_date}, status: :ok
else
render json: {ok: false, errors: rental.errors}, status: :bad_request
end
end
private
def rental_params
params.permit(:customer_id, :movie_id)
end
end