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
3 changes: 3 additions & 0 deletions app/controllers/customers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ class CustomersController < ApplicationController
def index
if @sort
data = Customer.all.order(@sort)
elsif params[:query]
data = Customer.where("name like ?", "%#{params[:query]}%")
else
data = Customer.all
@customers = Customer.all
end

data = data.paginate(page: params[:p], per_page: params[:n])
Expand Down
14 changes: 14 additions & 0 deletions app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,25 @@ def index
data = MovieWrapper.search(params[:query])
else
data = Movie.all
@movies = Movie.all
end

render status: :ok, json: data
end

def create
new_movie = MovieWrapper.construct_movie(params)
new_movie.inventory = 10
new_movie.image_url = params["image_url"]
new_movie.external_id = params["external_id"]

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

def show
render(
status: :ok,
Expand Down
17 changes: 17 additions & 0 deletions app/controllers/rentals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@ class RentalsController < ApplicationController
before_action :require_customer, only: [:check_out, :check_in]

# TODO: make sure that wave 2 works all the way
def index
data = Rental.all

cleaned_data = []
data.each do |rental|
cleaned_data << {
id: rental.id,
customer: Customer.find_by(id: rental.customer_id),
movie: Movie.find_by(id: rental.movie_id),
due_date: rental.due_date,
returned: rental.returned,
}
end

render status: :ok, json: cleaned_data
end

def check_out
rental = Rental.new(movie: @movie, customer: @customer, due_date: params[:due_date])

Expand Down
2 changes: 2 additions & 0 deletions app/models/movie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class Movie < ApplicationRecord
has_many :rentals
has_many :customers, through: :rentals

validates :title, uniqueness: true

def available_inventory
self.inventory - Rental.where(movie: self, returned: false).length
end
Expand Down
29 changes: 29 additions & 0 deletions app/views/customers/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<h2>List of Customers</h2>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Address</th>
<th>City</th>
<th>State</th>
<th>Postal Code</th>
<th>Phone</th>
<th>Account Credit</th>
</tr>
</thead>
<tbody>
<% @customers.each do |customer| %>
<tr>
<td><%= link_to customer.name, movie_path(customer) %></td>
<td><%= customer.address %></td>
<td><%= customer.city %></td>
<td><%= customer.state %></td>
<td><%= customer.postal_code %></td>
<td><%= customer.phone %></td>
<td><%= customer.account_credit %></td>
</tr>
<% end %>
</tbody>
</table>

<%= link_to "Back to Home", root_path, class: "btn btn-secondary" %>
23 changes: 23 additions & 0 deletions app/views/movies/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<h2>List of Movies</h2>
<table class="table">
<thead>
<tr>
<th>Title</th>
<th>Overview</th>
<th>Release Date</th>
<th>Inventory</th>
</tr>
</thead>
<tbody>
<% @movies.each do |movie| %>
<tr>
<td><%= link_to movie.title, movie_path(movie) %></td>
<td><%= movie.overview %></td>
<td><%= movie.release_date %></td>
<td><%= movie.inventory %></td>
</tr>
<% end %>
</tbody>
</table>

<%= link_to "Back to Home", root_path, class: "btn btn-secondary" %>
5 changes: 3 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
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], param: :name

resources :movies, only: [:index, :show], param: :title
resources :movies, only: [:index, :create, :show], param: :title

resources :rentals, only: [:index]
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"
Expand Down
35 changes: 35 additions & 0 deletions test/controllers/movies_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,41 @@ class MoviesControllerTest < ActionDispatch::IntegrationTest
end
end

describe "create" do
it "saves movie with valid parameters" do
params = {
title: "Totoro",
overview: "Death god conspiracy theory?",
release_date: "2002-11-14",
inventory: 8
}

start_count = Movie.count
post movies_path(params)
expect(Movie.count).must_equal start_count + 1
expect(Movie.last.title).must_equal params[:title]

must_respond_with :success
end

it "each movie once with the same params" do
params = {
title: "Totoro",
overview: "Death god conspiracy theory?",
release_date: "2002-11-14",
inventory: 8
}

post movies_path(params)
start_count = Movie.count
post movies_path(params)
expect(Movie.count).must_equal start_count
expect(Movie.last.title).must_equal params[:title]

must_respond_with :bad_request
end
end

describe "show" do
it "Returns a JSON object" do
get movie_url(title: movies(:one).title)
Expand Down