diff --git a/app/controllers/videos_controller.rb b/app/controllers/videos_controller.rb index c9a2bb08..7050a935 100644 --- a/app/controllers/videos_controller.rb +++ b/app/controllers/videos_controller.rb @@ -15,12 +15,26 @@ def show render( status: :ok, json: @video.as_json( - only: [:title, :overview, :release_date, :inventory], + only: [:title, :overview, :release_date, :inventory, :image_url], methods: [:available_inventory] ) ) end + def create + @video = Video.new(video_params) + + if Video.find_by(title: @video.title) + render json: {ok: false, cause: "duplicate errors", errors: @video.errors}, status: :bad_request + return + end + + unless @video.save + render json: {ok: false, cause: "validation errors", errors: @video.errors}, status: :bad_request + return + end + end + private def require_video @@ -29,4 +43,8 @@ def require_video render status: :not_found, json: { errors: { title: ["No video with title #{params["title"]}"] } } end end + + def video_params + return params.permit(:external_id, :title, :overview, :release_date, :image_url, :inventory) + end end diff --git a/config/routes.rb b/config/routes.rb index 16fc2214..1462862a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,7 @@ resources :customers, only: [:index] resources :videos, only: [:index, :show], param: :title + resources :videos, only: [:create] post "/rentals/:title/check-out", to: "rentals#check_out", as: "check_out" post "/rentals/:title/return", to: "rentals#check_in", as: "check_in"