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 - Amaria #10

Open
wants to merge 5 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
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Rails server",
"type": "Ruby",
"request": "launch",
"program": "${workspaceRoot}/bin/rails",
"args": [
"server"
]
}
]
}
27 changes: 24 additions & 3 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,35 @@ def index
render status: :ok, json: data
end

def new
@movie = Movie.new
end

def create
# @movie = Movie.find_by(title: params[:title])

# @movie_params = request.body.read
# puts @movie_params
@movie = Movie.new
@movie.title = params[:title]
@movie.overview = params[:overview]
@movie.release_date = params[:release_date]
@movie.image_url = params[:image_url]
@movie.inventory = 1

save_success = @movie.save

puts save_success
end

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

private
Expand Down
40 changes: 29 additions & 11 deletions app/controllers/rentals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ def check_out
rental = Rental.new(movie: @movie, customer: @customer, due_date: params[:due_date])

if rental.save
# If we successfully rent, get the movie and decrement its inventory
movie = Movie.find_by(id: @movie.id)

if (movie.inventory)
movie.inventory -= 1
else
movie.inventory = 0
end
puts "goodbye"
movie.save
puts "hello"
render status: :ok, json: {}
else
render status: :bad_request, json: { errors: rental.errors.messages }
Expand All @@ -17,13 +28,19 @@ def check_in
rental = Rental.first_outstanding(@movie, @customer)
unless rental
return render status: :not_found, json: {
errors: {
rental: ["Customer #{@customer.id} does not have #{@movie.title} checked out"]
}
}
errors: {
rental: ["Customer #{@customer.id} does not have #{@movie.title} checked out"],
},
}
end
rental.returned = true
if rental.save
# If we successfully check in the movie, increment the inventory of the movie
movie = Movie.find_by(id: @movie.id)

movie.inventory += 1

movie.save
render status: :ok, json: {}
else
render status: :bad_request, json: { errors: rental.errors.messages }
Expand All @@ -33,18 +50,19 @@ def check_in
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]
Expand Down
4 changes: 2 additions & 2 deletions app/models/rental.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class Rental < ApplicationRecord
after_initialize :set_checkout_date
after_initialize :set_returned


def self.first_outstanding(movie, customer)
self.where(movie: movie, customer: customer, returned: false).order(:due_date).first
end
Expand All @@ -18,7 +17,8 @@ def self.overdue
self.where(returned: false).where("due_date < ?", Date.today)
end

private
private

def due_date_in_future
return unless self.due_date
unless due_date > Date.today
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/movie_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class MovieSerializer < ActiveModel::Serializer
attribute :id, if: -> { object.id != nil }

attributes :title, :overview, :release_date, :image_url, :external_id
attributes :title, :overview, :release_date, :image_url, :external_id, :inventory
end
6 changes: 3 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

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"
#post "/movies", to: "movies#create", as: "movies"

root 'movies#index'

root "movies#index"
end
15 changes: 11 additions & 4 deletions lib/movie_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,26 @@ class MovieWrapper
DEFAULT_IMG_SIZE = "w185"
DEFAULT_IMG_URL = "http://lorempixel.com/185/278/"

def self.search(query, retries_left=3)
def self.search(query, retries_left = 3)
raise ArgumentError.new("Can't search without a MOVIEDB_KEY. Please check your .env file!") unless KEY

url = BASE_URL + "search/movie?api_key=" + KEY + "&query=" + query

response = HTTParty.get(url)
response = HTTParty.get(url)

if response.success?
if response["total_results"] == 0
return []
else
movies = response["results"].map do |result|
self.construct_movie(result)
# look in table to see if the movie already exists, if so add it to the results
movie = Movie.find_by(title: result["title"])
if (movie == nil)
self.construct_movie(result)
end
end

movies.reject { |item| item.nil? || item == "" }
return movies
end
elsif retries_left > 0
Expand All @@ -39,7 +45,8 @@ def self.construct_movie(api_result)
overview: api_result["overview"],
release_date: api_result["release_date"],
image_url: self.construct_image_url(api_result["poster_path"]),
external_id: api_result["id"]
external_id: api_result["id"],
inventory: api_result["inventory"],
)
end

Expand Down