Skip to content
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
18 changes: 18 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"workbench.colorCustomizations": {
"activityBar.background": "#1accff",
"activityBar.activeBorder": "#df00ad",
"activityBar.foreground": "#15202b",
"activityBar.inactiveForeground": "#15202b99",
"activityBarBadge.background": "#df00ad",
"activityBarBadge.foreground": "#e7e7e7",
"titleBar.activeBackground": "#00b3e6",
"titleBar.inactiveBackground": "#00b3e699",
"titleBar.activeForeground": "#15202b",
"titleBar.inactiveForeground": "#15202b99",
"statusBar.background": "#00b3e6",
"statusBarItem.hoverBackground": "#008bb3",
"statusBar.foreground": "#15202b"
},
"peacock.color": "#00b3e6"
}
37 changes: 30 additions & 7 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,55 @@
class MoviesController < ApplicationController
before_action :require_movie, only: [:show]

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

render status: :ok, json: data
end


def create
if Movie.no_available_movie_count(movie_params[:external_id], movie_params[:image_url])
render json: {
message: ["#{movie_params[:title]} quantity increased by 1"],
status: :ok
}
return
end

@movie = Movie.new(movie_params)

if @movie.save
render json: {movie: {id: @movie.id, title:@movie.title}}, status: :ok
else
render json: {errors: @movie.errors.messages},
status: :bad_request
end
end

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

private

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

def movie_params
return params.require(:movie).permit(:title, :overview, :release_date, :image_url, :external_id)
end
end
28 changes: 14 additions & 14 deletions app/controllers/rentals_controller.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
class RentalsController < ApplicationController
before_action :require_movie, only: [:check_out, :check_in]
before_action :require_customer, only: [:check_out, :check_in]

# TODO: make sure that wave 2 works all the way
def check_out
rental = Rental.new(movie: @movie, customer: @customer, due_date: params[:due_date])

if rental.save
render status: :ok, json: {}
render status: :ok, json: { id: rental.id }
else
render status: :bad_request, json: { errors: rental.errors.messages }
end
end

def check_in
rental = Rental.first_outstanding(@movie, @customer)
unless rental
Expand All @@ -29,30 +29,30 @@ def check_in
render status: :bad_request, json: { errors: rental.errors.messages }
end
end

def overdue
rentals = Rental.overdue.map do |rental|
{
title: rental.movie.title,
customer_id: rental.customer_id,
name: rental.customer.name,
postal_code: rental.customer.postal_code,
checkout_date: rental.checkout_date,
due_date: rental.due_date
title: rental.movie.title,
customer_id: rental.customer_id,
name: rental.customer.name,
postal_code: rental.customer.postal_code,
checkout_date: rental.checkout_date,
due_date: rental.due_date
}
end
render status: :ok, json: rentals
end

private
private
# TODO: make error payloads arrays
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

def require_customer
@customer = Customer.find_by id: params[:customer_id]
unless @customer
Expand Down
15 changes: 13 additions & 2 deletions app/models/movie.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
class Movie < ApplicationRecord
has_many :rentals
has_many :customers, through: :rentals

def available_inventory
self.inventory - Rental.where(movie: self, returned: false).length
end


def self.no_available_movie_count(external_id, image_url)
availableMovie = Movie.find_by(external_id: external_id)
if availableMovie
availableMovie.image_url = image_url
availableMovie.inventory = (availableMovie.inventory || 0) + 1
availableMovie.save
return true
end
return false
end

def image_url
raw_value = read_attribute :image_url
if !raw_value
Expand Down
12 changes: 6 additions & 6 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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 :movies, only: [:index, :show], param: :title

resources :movies, only: [:index, :show, :create], param: :title
post "/rentals/:title/check-out", to: "rentals#check_out", as: "check_out"
post "/rentals/:title/return", to: "rentals#check_in", as: "check_in"
get "/rentals/overdue", to: "rentals#overdue", as: "overdue"

root 'movies#index'

end
Loading