Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ DEPENDENCIES
will_paginate

RUBY VERSION
ruby 2.6.5p114
ruby 2.6.4p104

BUNDLED WITH
2.1.4
28 changes: 27 additions & 1 deletion app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
)
)
Expand Down
9 changes: 7 additions & 2 deletions app/controllers/rentals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions app/models/movie.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/models/rental.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

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"
get "/rentals/overdue", to: "rentals#overdue", as: "overdue"

root 'movies#index'

end
end
63 changes: 33 additions & 30 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down