diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb
index be25f1be..92f2a212 100644
--- a/app/controllers/customers_controller.rb
+++ b/app/controllers/customers_controller.rb
@@ -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])
diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb
index 362e2791..1a1d0b70 100644
--- a/app/controllers/movies_controller.rb
+++ b/app/controllers/movies_controller.rb
@@ -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,
diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb
index 67e77073..86994551 100644
--- a/app/controllers/rentals_controller.rb
+++ b/app/controllers/rentals_controller.rb
@@ -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])
diff --git a/app/models/movie.rb b/app/models/movie.rb
index fda94941..aa02d505 100644
--- a/app/models/movie.rb
+++ b/app/models/movie.rb
@@ -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
diff --git a/app/views/customers/index.html.erb b/app/views/customers/index.html.erb
new file mode 100644
index 00000000..bcd92b7b
--- /dev/null
+++ b/app/views/customers/index.html.erb
@@ -0,0 +1,29 @@
+
List of Customers
+
+
+
+ | Name |
+ Address |
+ City |
+ State |
+ Postal Code |
+ Phone |
+ Account Credit |
+
+
+
+ <% @customers.each do |customer| %>
+
+ | <%= link_to customer.name, movie_path(customer) %> |
+ <%= customer.address %> |
+ <%= customer.city %> |
+ <%= customer.state %> |
+ <%= customer.postal_code %> |
+ <%= customer.phone %> |
+ <%= customer.account_credit %> |
+
+ <% end %>
+
+
+
+<%= link_to "Back to Home", root_path, class: "btn btn-secondary" %>
diff --git a/app/views/movies/index.html.erb b/app/views/movies/index.html.erb
new file mode 100644
index 00000000..b1dee41a
--- /dev/null
+++ b/app/views/movies/index.html.erb
@@ -0,0 +1,23 @@
+List of Movies
+
+
+
+ | Title |
+ Overview |
+ Release Date |
+ Inventory |
+
+
+
+ <% @movies.each do |movie| %>
+
+ | <%= link_to movie.title, movie_path(movie) %> |
+ <%= movie.overview %> |
+ <%= movie.release_date %> |
+ <%= movie.inventory %> |
+
+ <% end %>
+
+
+
+<%= link_to "Back to Home", root_path, class: "btn btn-secondary" %>
diff --git a/config/routes.rb b/config/routes.rb
index f4c99688..a4bfa099 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -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"
diff --git a/test/controllers/movies_controller_test.rb b/test/controllers/movies_controller_test.rb
index 9172cf6e..f7d3c291 100644
--- a/test/controllers/movies_controller_test.rb
+++ b/test/controllers/movies_controller_test.rb
@@ -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)