Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
d812640
Add schema file generated by migrations
jfahmy Dec 17, 2018
aa0fe2f
Add create movie functionality to the Rails App with a movie create r…
jfahmy Dec 18, 2018
c901e5f
Add validation requiring new movie to have at least a title and an ex…
jfahmy Dec 18, 2018
42fffff
Adjust movie controller test to expect the word errors instead of the…
jfahmy Dec 18, 2018
58e7309
Add validation for uniqueness of external_id with a customer error me…
jfahmy Dec 18, 2018
4adc36d
Add model test verifying error messages are present when there is an …
jfahmy Dec 18, 2018
bbfd731
Change create controller method to slice movie URL when creating movi…
jfahmy Dec 19, 2018
d35d038
Add new model methods for rentals (tests pass)
sjlee3157 Dec 19, 2018
8988475
Add endpoints for rentals/returned and rentals/out-ok (tests pass)
sjlee3157 Dec 19, 2018
32c17a5
Add one more rental model test
sjlee3157 Dec 19, 2018
ababf0c
Add image url to json response from movie show controller
jfahmy Dec 20, 2018
76c559f
Merge branch 'master' of https://github.com/jfahmy/VideoStoreConsumer…
jfahmy Dec 20, 2018
1e50fb7
change database.yml
jfahmy Dec 20, 2018
b00d6e2
Add travis.yml file
jfahmy Dec 20, 2018
c0425dc
Fix missing overview for movie create
sjlee3157 Dec 20, 2018
ce7adfc
Merge branch 'master' of https://github.com/jfahmy/VideoStoreConsumer…
sjlee3157 Dec 20, 2018
f7f225b
Modify travis.yml file to connect to Heroku
jfahmy Dec 20, 2018
07a0884
Add sleep command to seed data so we don't hit TMDB request rate limi…
jfahmy Dec 20, 2018
296717b
Adjust gemfile ruby version
jfahmy Dec 20, 2018
b839ae2
One last change to gemfile
jfahmy Dec 20, 2018
5aaaf68
Adjust ruby version to see if it will help TravisCI not fail
jfahmy Dec 21, 2018
249d8f9
Change gemfile back to previous version
jfahmy Dec 21, 2018
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,8 @@

# Ignore master key for decrypting credentials and more.
/config/master.key

# Elastic Beanstalk Files
.elasticbeanstalk/*
!.elasticbeanstalk/*.cfg.yml
!.elasticbeanstalk/*.global.yml
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: ruby
rvm:
- 2.2
- jruby

deploy:
provider: heroku
api_key:
secure: GItCjV2InWRINoetFPfjsKkbGrxkpEzBVudcXAaZb9sQxhG6Sqg3T6TqRyRKheY6aHJMQyjFCtxsRUbGV3p/MZJpFDOTFsor/V08yjixIy4OGOvEhp69f7tlIvJ6lfGaftv9Jd/dhV1ZfbSLXvvVDBbsDJP3OhY9gt34SSu0IfajDvvPK+JfaYr96KlEITFZVFA+dtU5KKbuUE8+wRJF2wRx1ZDXIbaG4V3sLykajm8wqxTqdcb5uDXll4zz8/s5xNaKSufYMcTeUiUpH/dvxFTn82ydT+xEbRL6MoMCWCbtyjVcddeJ/8gfnkM5ka+pNnTDv9u2nM6XMhJibdSqWWgWMkNQeuFpD7rfh3sn70+RTLtHbLqTWG7vDHiMamfGpVret1CDM2/naAesiiwhER8KR1gm1bPNEeSGn+WHKJHFiWfgVehfIeYRaYxaoXiZ6hCYHH0rmvFzSRYMpAUZyEsTt71rAUxi6iScV6K5pKvglnsKIbOUoQoixKcw4auQiwZYSqVh0Q/D1w6wZRi1WIJK7kvAF36A/1NNzJSykCVHZz4xQyXZ4i10PfmLtgSnv+VsNTXq9zXEwoMnmZI6SrpTPHW68+PSy/Vq35gQHp0u4dLRCODUe9V3aMjJgk+niZhIS3SXSmQjHrmaJyp4iuglE96iNUV0B2mOig80tJs=
app: rails-videostore-api
run:
- "sleep 15"
- "rake db:migrate"
- "run rake db:seed"
34 changes: 33 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ GEM
awesome_print (1.8.0)
bootsnap (1.3.2)
msgpack (~> 1.0)
bootsnap (1.3.2-java)
msgpack (~> 1.0)
builder (3.2.3)
byebug (10.0.2)
case_transform (0.2)
Expand All @@ -65,6 +67,9 @@ GEM
railties (>= 3.2, < 6.0)
erubi (1.7.1)
ffi (1.9.25)
ffi (1.9.25-java)
ffi (1.9.25-x64-mingw32)
ffi (1.9.25-x86-mingw32)
globalid (0.4.1)
activesupport (>= 4.2.0)
httparty (0.16.3)
Expand Down Expand Up @@ -104,18 +109,34 @@ GEM
minitest (>= 5.0)
ruby-progressbar
msgpack (1.2.4)
msgpack (1.2.4-java)
msgpack (1.2.4-x64-mingw32)
msgpack (1.2.4-x86-mingw32)
multi_json (1.13.1)
multi_xml (0.6.0)
nio4r (2.3.1)
nio4r (2.3.1-java)
nokogiri (1.8.5)
mini_portile2 (~> 2.3.0)
nokogiri (1.8.5-java)
nokogiri (1.8.5-x64-mingw32)
mini_portile2 (~> 2.3.0)
nokogiri (1.8.5-x86-mingw32)
mini_portile2 (~> 2.3.0)
pg (1.1.3)
pg (1.1.3-x64-mingw32)
pg (1.1.3-x86-mingw32)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
pry (0.12.2-java)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
spoon (~> 0.0)
pry-rails (0.3.8)
pry (>= 0.10.4)
puma (3.12.0)
puma (3.12.0-java)
rack (2.0.6)
rack-cors (1.0.2)
rack-test (1.1.0)
Expand Down Expand Up @@ -150,6 +171,8 @@ GEM
ffi (~> 1.0)
ruby-progressbar (1.10.0)
ruby_dep (1.5.0)
spoon (0.0.6)
ffi
spring (2.0.2)
activesupport (>= 4.2)
spring-watcher-listen (2.0.1)
Expand All @@ -164,15 +187,24 @@ GEM
sprockets (>= 3.0.0)
thor (0.20.3)
thread_safe (0.3.6)
thread_safe (0.3.6-java)
tzinfo (1.2.5)
thread_safe (~> 0.1)
tzinfo-data (1.2018.7)
tzinfo (>= 1.0.0)
websocket-driver (0.7.0)
websocket-extensions (>= 0.1.0)
websocket-driver (0.7.0-java)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.3)
will_paginate (3.1.6)

PLATFORMS
java
ruby
x64-mingw32
x86-mingw32
x86-mswin32

DEPENDENCIES
active_model_serializers
Expand All @@ -199,4 +231,4 @@ RUBY VERSION
ruby 2.5.1p57

BUNDLED WITH
1.16.6
1.17.2
43 changes: 42 additions & 1 deletion app/controllers/movies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,48 @@ def index
render status: :ok, json: data
end

def create
if movie_params[:image_url] != nil
url = movie_params[:image_url]
url = url.slice(31, url.length)
else
url = movie_params[:image_url]
end
movie = Movie.new({external_id: movie_params[:external_id],
overview: movie_params[:overview],
image_url: url,
release_date: movie_params[:release_date],
title: movie_params[:title],
inventory: movie_params[:inventory]})
if movie.save
render json: {
id: movie.id,
title: movie.title,
overview: movie.overview,
image_url: movie.image_url,
release_date: movie.release_date,
inventory: movie.inventory,
external_id: movie.external_id }, status: :ok
else
if movie.errors.messages[:external_id] == ["has already been taken"]
render json: {
errors: "Hmm...
We think you already have that movie in your film library."
}, status: :bad_request
else
render json: {
errors: movie.errors.messages
}, status: :bad_request
end
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 All @@ -29,4 +66,8 @@ def require_movie
render status: :not_found, json: { errors: { title: ["No movie with title #{params["title"]}"] } }
end
end

def movie_params
params.permit(:external_id, :image_url, :overview, :release_date, :title, :inventory)
end
end
28 changes: 28 additions & 0 deletions app/controllers/rentals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,34 @@ def overdue
render status: :ok, json: rentals
end

def returned
rentals = Rental.returned.map do |rental|
{
title: rental.movie.title,
customer_id: rental.customer_id,
name: rental.customer.name,
postal_code: rental.customer.postal_code,
checkout_date: rental.checkout_date,
due_date: rental.due_date
}
end
render status: :ok, json: rentals
end

def out_ok
rentals = Rental.out_ok.map do |rental|
{
title: rental.movie.title,
customer_id: rental.customer_id,
name: rental.customer.name,
postal_code: rental.customer.postal_code,
checkout_date: rental.checkout_date,
due_date: rental.due_date
}
end
render status: :ok, json: rentals
end

private
# TODO: make error payloads arrays
def require_movie
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, :external_id, presence: true
validates :external_id, uniqueness: true

def available_inventory
self.inventory - Rental.where(movie: self, returned: false).length
Expand Down
10 changes: 9 additions & 1 deletion app/models/rental.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ def self.first_outstanding(movie, customer)
end

def self.overdue
self.where(returned: false).where("due_date < ?", Date.today)
self.where(returned: false).where("due_date < ?", Date.today).order(:due_date)
end

def self.returned
self.where(returned: true).order(:due_date)
end

def self.out_ok
self.where(returned: false).where("due_date >= ?", Date.today).order(:due_date)
end

private
Expand Down
8 changes: 3 additions & 5 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ test:
database: <APPLICATION_NAME>_test

production:
<<: *default
database: <APPLICATION_NAME>_production
username: <APPLICATION_NAME>
password: <%= ENV['<APPLICATION_NAME>_DATABASE_PASSWORD'] %>

<<: *default
adapter: postgresql
database: db/production.sqlite3
5 changes: 4 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@

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"

get "/rentals/returned", to: "rentals#returned", as: "returned"
get "/rentals/out-ok", to: "rentals#out_ok", as: "out_ok"

root 'movies#index'

end
53 changes: 28 additions & 25 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,43 @@
#
# 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
1 change: 1 addition & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
ap "#{movie_data['title']} Added to the library!"
movies.first.inventory = movie_data['inventory']
movies.first.save unless movies.empty?
sleep(2)
end
44 changes: 44 additions & 0 deletions test/controllers/movies_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'test_helper'
require 'pry'

class MoviesControllerTest < ActionDispatch::IntegrationTest
describe "index" do
Expand Down Expand Up @@ -42,6 +43,49 @@ class MoviesControllerTest < ActionDispatch::IntegrationTest
end
end

describe "create" do
let(:movie_data) {
{
title: "Savior Of The Curse",
overview: "The strange woman claims the boy has a DNA strain which very likely offers the key to cure several crippling diseases. Tests will have to be done, but the key to save millions of lives is within the grasp of science through this DNA strain. Unsure what to think of all this and of this strange woman, the boy hesitantly agrees to the proposal, there's something exciting about this whole situation, surely the right choice was made.",
release_date: "2010-11-05",
inventory: 5,
image_url: "https://image.tmdb.org/t/p/w185/kMQPixpwZ9KDTxvjlXJ5HHJtYOG.jpg",
external_id: 999999999
}
}

it "creates a new movie given valid data" do
expect {
post movies_path, params: movie_data
}.must_change "Movie.count", 1

body = JSON.parse(response.body)
expect(body).must_be_kind_of Hash
expect(body).must_include "id"

movie = Movie.find(body["id"].to_i)

expect(movie.title).must_equal movie_data[:title]
must_respond_with :success
end

it "returns an error for missing needed movie info to create object" do
expect {
post movies_path
}.wont_change "Movie.count"

body = JSON.parse(response.body)

expect(body).must_be_kind_of Hash
expect(body).must_include "errors"
expect(body["errors"]).must_include "title"
must_respond_with :bad_request
end

end


describe "show" do
it "Returns a JSON object" do
get movie_url(title: movies(:one).title)
Expand Down
Loading