diff --git a/Gemfile.lock b/Gemfile.lock index c5a1110a..d82e93d9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -197,4 +197,4 @@ RUBY VERSION ruby 2.5.5p157 BUNDLED WITH - 1.17.3 + 2.0.2 diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..d83432b8 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -1,28 +1,38 @@ 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 show render( status: :ok, json: @movie.as_json( only: [:title, :overview, :release_date, :inventory], methods: [:available_inventory] - ) ) + ) end - + + def create + movie = MovieWrapper.construct_movie(params) + + if movie.save + render status: :ok, json: {} + else + render status: :bad_request, json: {errors: movie.errors.messages} + end + end + private - + def require_movie @movie = Movie.find_by(title: params[:title]) unless @movie diff --git a/config/routes.rb b/config/routes.rb index f4c99688..817f2958 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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