diff --git a/Gemfile b/Gemfile index 2d53ba9c..b88c3bd3 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,7 @@ source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } -ruby '2.5.1' +ruby '2.3.1' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' diff --git a/Gemfile.lock b/Gemfile.lock index a0a07df1..c514dd89 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -196,7 +196,7 @@ DEPENDENCIES will_paginate RUBY VERSION - ruby 2.5.1p57 + ruby 2.3.1p112 BUNDLED WITH 1.16.6 diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..9ec3d976 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -1,3 +1,5 @@ +require 'pry' + class MoviesController < ApplicationController before_action :require_movie, only: [:show] @@ -21,8 +23,32 @@ def show ) end + + def create + + if !Movie.find_by(title: movie_params[:title]) + @movie = Movie.new(movie_params) + @movie.image_url = movie_params["image_url"].slice(31..-1) + if @movie.save + render json: { id: @movie.id }, status: :ok + else + render json: { ok: false, errors: @movie.errors.messages}, status: :bad_request + end + else + render json:{ + errors:{ + title: ["movie exists in database"] + } + } + end + end + private + def movie_params + params.permit(:title, :overview, :image_url, :release_date, :external_id, :inventory) + end + def require_movie @movie = Movie.find_by(title: params[:title]) unless @movie diff --git a/config/routes.rb b/config/routes.rb index f4c99688..76715f9a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,7 +3,7 @@ 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"