diff --git a/Gemfile.lock b/Gemfile.lock index a1969bba..a46a2cbd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -238,7 +238,7 @@ DEPENDENCIES will_paginate RUBY VERSION - ruby 2.6.5p114 + ruby 2.6.4p104 BUNDLED WITH 2.1.4 diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 362e2791..44c2ec9b 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -11,11 +11,37 @@ def index render status: :ok, json: data end + # TODO: create a new movie instance in our rental library + def create + @movie = Movie.new( + title: params["title"], + overview: params["overview"], + release_date: params["release_date"], + image_url: params["image_url"], + external_id: params["external_id"], + inventory: 10 + ) + + if @movie.save! + render( + status: :ok, + json: @movie.as_json( + only: [:title, :overview, :release_date, :inventory], + methods: [:available_inventory] + ) + ) + else + render status: :bad_request, json: { errors: { title: ["#{params["title"]} already exisits in our library"] } } + end + + + end + def show render( status: :ok, json: @movie.as_json( - only: [:title, :overview, :release_date, :inventory], + only: [:title, :overview, :release_date, :inventory, :image_url], methods: [:available_inventory] ) ) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 67e77073..2426dce8 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -4,10 +4,15 @@ class RentalsController < ApplicationController # TODO: make sure that wave 2 works all the way def check_out - rental = Rental.new(movie: @movie, customer: @customer, due_date: params[:due_date]) + rental = Rental.new(movie: @movie, customer: @customer, due_date: Date.today + 7) if rental.save - render status: :ok, json: {} + render( + status: :ok, + json: rental.as_json( + only: [:customer_id, :move_id, :checkout_date, :due_date, :returned] + ) + ) else render status: :bad_request, json: { errors: rental.errors.messages } end diff --git a/app/models/movie.rb b/app/models/movie.rb index fda94941..62af281a 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -1,6 +1,8 @@ class Movie < ApplicationRecord has_many :rentals has_many :customers, through: :rentals + validates :title, :overview, :release_date, :inventory, :image_url, :external_id, presence: true + validates :external_id, uniqueness: true def available_inventory self.inventory - Rental.where(movie: self, returned: false).length diff --git a/app/models/rental.rb b/app/models/rental.rb index 18654f04..f0539e94 100644 --- a/app/models/rental.rb +++ b/app/models/rental.rb @@ -2,7 +2,7 @@ class Rental < ApplicationRecord belongs_to :movie belongs_to :customer - # validates :movie, uniqueness: { scope: :customer } + validates :movie, uniqueness: { scope: :customer } validates :due_date, presence: true validate :due_date_in_future, on: :create diff --git a/config/routes.rb b/config/routes.rb index f4c99688..12bdc0e4 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,:create, :show], 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" @@ -11,4 +11,4 @@ root 'movies#index' -end +end \ No newline at end of file diff --git a/db/schema.rb b/db/schema.rb index ffb28f7e..8c8b42d4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -2,48 +2,51 @@ # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # -# Note that this schema.rb definition is the authoritative source for your -# database schema. If you need to create the application database on another -# system, you should be using db:schema:load, not running all the migrations -# from scratch. The latter is a flawed and unsustainable approach (the more migrations -# you'll amass, the slower it'll run and the greater likelihood for issues). +# This file is the source Rails uses to define your schema when running `rails +# db:schema:load`. When creating a new database, `rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180618042754) do +ActiveRecord::Schema.define(version: 2018_06_18_042754) do + + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" create_table "customers", force: :cascade do |t| - t.string "name" + t.string "name" t.datetime "registered_at" - t.string "address" - t.string "city" - t.string "state" - t.string "postal_code" - t.string "phone" - t.float "account_credit" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.string "address" + t.string "city" + t.string "state" + t.string "postal_code" + t.string "phone" + t.float "account_credit" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false end create_table "movies", force: :cascade do |t| - t.string "title" - t.text "overview" - t.date "release_date" - t.integer "inventory" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.string "image_url" - t.integer "external_id" + t.string "title" + t.text "overview" + t.date "release_date" + t.integer "inventory" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.string "image_url" + t.integer "external_id" end create_table "rentals", force: :cascade do |t| - t.integer "customer_id" - t.integer "movie_id" - t.date "checkout_date" - t.date "due_date" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.boolean "returned" + t.integer "customer_id" + t.integer "movie_id" + t.date "checkout_date" + t.date "due_date" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.boolean "returned" t.index ["customer_id"], name: "index_rentals_on_customer_id" t.index ["movie_id"], name: "index_rentals_on_movie_id" end