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
48 lines (38 loc) · 1.12 KB
/
rentals_controller.rb
File metadata and controls
48 lines (38 loc) · 1.12 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
class RentalsController < ApplicationController
def index
@rentals = Rental.all
end
def show
@rental = Rental.find_by(id: params[:id])
if @rental.nil?
render json: { ok: false, error: :not_found }, status: :not_found
end
end
def create
movie = Movie.find_by(id: params[:movie_id])
if movie.available?
rental = Rental.create(rental_params)
if rental.valid?
rental.assign_due_date
render json: { id: rental.id }, status: :ok
else
render json: {ok: false, errors: rental.errors}, status: :bad_request
end
else
render json: {ok: false, errors: "This will not work"}, status: :bad_request
end
end
def update
rental = Rental.where(customer_id: params[:customer_id]).where(movie_id: params[:movie_id]).find_by(returned: false)
if rental
rental.update_attributes(returned: true)
render json: { id: rental.id }, status: :ok
else
render json: { ok: false, errors: "This will not work" }, status: :bad_request
end
end
private
def rental_params
return params.permit(:customer_id, :movie_id)
end
end