Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Branches - Xinran & Eve #17

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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/customers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ def index
)
end

def show
customer = Customer.find_by(id: params[:id])
if customer
rental_data = customer.rentals.where(returned: false)

render status: :ok, json: rental_data.as_json(
only: [:returned, :due_date],
methods: [:movie]
)
else
render status: :not_found, json: { errors: { id: ["No customer with id #{params["id"]}"] } }
end
end

private
def parse_query_args
errors = {}
Expand Down
35 changes: 31 additions & 4 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,59 @@
class MoviesController < ApplicationController
before_action :require_movie, only: [:show]

def index
if params[:query]
data = MovieWrapper.search(params[:query])
else
data = Movie.all
end

if params[:p] || params[:n]
data = data.paginate(page: params[:p], per_page: params[:n])
end

render status: :ok, json: data
end

def show
render(
status: :ok,
json: @movie.as_json(
only: [:title, :overview, :release_date, :inventory],
methods: [:available_inventory]
)
)
end


def create
new_movie = Movie.new(movie_params)

if new_movie.save
render(
status: :ok,
json: new_movie.as_json(
only: [:title, :overview, :release_date, :external_id, :image_url]
)
)
else
render json: {
ok: false,
errors: new_movie.errors.messages
}, status: :bad_request
end
end

private

def movie_params
return params.permit(:title, :overview, :release_date, :external_id, :image_url)
end

def require_movie
@movie = Movie.find_by(title: params[:title])
unless @movie
render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } }
end
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,
movie: rental.movie
}
end
render status: :ok, json: rentals
Expand Down
1 change: 1 addition & 0 deletions app/models/movie.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Movie < ApplicationRecord
validates :external_id, presence: true,uniqueness: { message: "This movie already exists in your library!" }
has_many :rentals
has_many :customers, through: :rentals

Expand Down
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html

resources :customers, only: [:index]
resources :customers, only: [:index, :show]

resources :movies, only: [:index, :show], param: :title
post "/movies", to: "movies#create"

post "/rentals/:title/check-out", to: "rentals#check_out", as: "check_out"
post "/rentals/:title/return", to: "rentals#check_in", as: "check_in"
Expand Down