-
Notifications
You must be signed in to change notification settings - Fork 18
AC's Video Rental API #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
57a635c
9209823
f585e05
0d7503b
c6a1e01
fc7767e
294b884
9fd41cb
5a75609
e51426a
78b60ff
0a743be
8a34a8c
aa0edbb
7dfda2a
a0b5862
b47278c
b0f9f71
38fa756
f4ffbff
3352ac0
e7c7bee
d1e3f67
db4f71d
d6ca7d1
21a1e5e
7007f94
b37dd33
58f0afc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # See https://help.github.com/articles/ignoring-files for more about ignoring files. | ||
| # | ||
| # If you find yourself ignoring temporary files generated by your text editor | ||
| # or operating system, you probably want to add a global ignore instead: | ||
| # git config --global core.excludesfile '~/.gitignore_global' | ||
|
|
||
| # Ignore bundler config. | ||
| /.bundle | ||
|
|
||
| # Ignore all logfiles and tempfiles. | ||
| /log/* | ||
| /tmp/* | ||
| !/log/.keep | ||
| !/tmp/.keep | ||
|
|
||
| # Ignore Byebug command history file. | ||
| .byebug_history |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| source 'https://rubygems.org' | ||
|
|
||
| git_source(:github) do |repo_name| | ||
| repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/") | ||
| "https://github.com/#{repo_name}.git" | ||
| end | ||
|
|
||
|
|
||
| # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' | ||
| gem 'rails', '~> 5.0.2' | ||
| # Use postgresql as the database for Active Record | ||
| gem 'pg', '~> 0.18' | ||
| # Use Puma as the app server | ||
| gem 'puma', '~> 3.0' | ||
| # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder | ||
| # gem 'jbuilder', '~> 2.5' | ||
| # Use Redis adapter to run Action Cable in production | ||
| # gem 'redis', '~> 3.0' | ||
| # Use ActiveModel has_secure_password | ||
| # gem 'bcrypt', '~> 3.1.7' | ||
|
|
||
| # Use Capistrano for deployment | ||
| # gem 'capistrano-rails', group: :development | ||
|
|
||
| # Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible | ||
| # gem 'rack-cors' | ||
|
|
||
| group :development, :test do | ||
| # Call 'byebug' anywhere in the code to stop execution and get a debugger console | ||
| gem 'byebug', platform: :mri | ||
| end | ||
|
|
||
| group :development do | ||
| gem 'listen', '~> 3.0.5' | ||
| # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring | ||
| gem 'spring' | ||
| gem 'spring-watcher-listen', '~> 2.0.0' | ||
| end | ||
|
|
||
| # Windows does not include zoneinfo files, so bundle the tzinfo-data gem | ||
| gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] | ||
|
|
||
| group :development do | ||
| gem 'better_errors' | ||
| gem 'pry-rails' | ||
| end | ||
|
|
||
| gem 'awesome_print' | ||
| gem 'foundation-rails' | ||
| group :test do | ||
| gem 'minitest-rails' | ||
| gem 'minitest-reporters' | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| GEM | ||
| remote: https://rubygems.org/ | ||
| specs: | ||
| actioncable (5.0.2) | ||
| actionpack (= 5.0.2) | ||
| nio4r (>= 1.2, < 3.0) | ||
| websocket-driver (~> 0.6.1) | ||
| actionmailer (5.0.2) | ||
| actionpack (= 5.0.2) | ||
| actionview (= 5.0.2) | ||
| activejob (= 5.0.2) | ||
| mail (~> 2.5, >= 2.5.4) | ||
| rails-dom-testing (~> 2.0) | ||
| actionpack (5.0.2) | ||
| actionview (= 5.0.2) | ||
| activesupport (= 5.0.2) | ||
| rack (~> 2.0) | ||
| rack-test (~> 0.6.3) | ||
| rails-dom-testing (~> 2.0) | ||
| rails-html-sanitizer (~> 1.0, >= 1.0.2) | ||
| actionview (5.0.2) | ||
| activesupport (= 5.0.2) | ||
| builder (~> 3.1) | ||
| erubis (~> 2.7.0) | ||
| rails-dom-testing (~> 2.0) | ||
| rails-html-sanitizer (~> 1.0, >= 1.0.3) | ||
| activejob (5.0.2) | ||
| activesupport (= 5.0.2) | ||
| globalid (>= 0.3.6) | ||
| activemodel (5.0.2) | ||
| activesupport (= 5.0.2) | ||
| activerecord (5.0.2) | ||
| activemodel (= 5.0.2) | ||
| activesupport (= 5.0.2) | ||
| arel (~> 7.0) | ||
| activesupport (5.0.2) | ||
| concurrent-ruby (~> 1.0, >= 1.0.2) | ||
| i18n (~> 0.7) | ||
| minitest (~> 5.1) | ||
| tzinfo (~> 1.1) | ||
| ansi (1.5.0) | ||
| arel (7.1.4) | ||
| awesome_print (1.7.0) | ||
| babel-source (5.8.35) | ||
| babel-transpiler (0.7.0) | ||
| babel-source (>= 4.0, < 6) | ||
| execjs (~> 2.0) | ||
| better_errors (2.1.1) | ||
| coderay (>= 1.0.0) | ||
| erubis (>= 2.6.6) | ||
| rack (>= 0.9.0) | ||
| builder (3.2.3) | ||
| byebug (9.0.6) | ||
| coderay (1.1.1) | ||
| concurrent-ruby (1.0.5) | ||
| erubis (2.7.0) | ||
| execjs (2.7.0) | ||
| ffi (1.9.18) | ||
| foundation-rails (6.3.1.0) | ||
| railties (>= 3.1.0) | ||
| sass (>= 3.3.0, < 3.5) | ||
| sprockets-es6 (>= 0.9.0) | ||
| globalid (0.4.0) | ||
| activesupport (>= 4.2.0) | ||
| i18n (0.8.1) | ||
| listen (3.0.8) | ||
| rb-fsevent (~> 0.9, >= 0.9.4) | ||
| rb-inotify (~> 0.9, >= 0.9.7) | ||
| loofah (2.0.3) | ||
| nokogiri (>= 1.5.9) | ||
| mail (2.6.5) | ||
| mime-types (>= 1.16, < 4) | ||
| method_source (0.8.2) | ||
| mime-types (3.1) | ||
| mime-types-data (~> 3.2015) | ||
| mime-types-data (3.2016.0521) | ||
| mini_portile2 (2.1.0) | ||
| minitest (5.10.2) | ||
| minitest-rails (3.0.0) | ||
| minitest (~> 5.8) | ||
| railties (~> 5.0) | ||
| minitest-reporters (1.1.14) | ||
| ansi | ||
| builder | ||
| minitest (>= 5.0) | ||
| ruby-progressbar | ||
| nio4r (2.0.0) | ||
| nokogiri (1.7.2) | ||
| mini_portile2 (~> 2.1.0) | ||
| pg (0.20.0) | ||
| pry (0.10.4) | ||
| coderay (~> 1.1.0) | ||
| method_source (~> 0.8.1) | ||
| slop (~> 3.4) | ||
| pry-rails (0.3.6) | ||
| pry (>= 0.10.4) | ||
| puma (3.8.2) | ||
| rack (2.0.2) | ||
| rack-test (0.6.3) | ||
| rack (>= 1.0) | ||
| rails (5.0.2) | ||
| actioncable (= 5.0.2) | ||
| actionmailer (= 5.0.2) | ||
| actionpack (= 5.0.2) | ||
| actionview (= 5.0.2) | ||
| activejob (= 5.0.2) | ||
| activemodel (= 5.0.2) | ||
| activerecord (= 5.0.2) | ||
| activesupport (= 5.0.2) | ||
| bundler (>= 1.3.0, < 2.0) | ||
| railties (= 5.0.2) | ||
| sprockets-rails (>= 2.0.0) | ||
| rails-dom-testing (2.0.2) | ||
| activesupport (>= 4.2.0, < 6.0) | ||
| nokogiri (~> 1.6) | ||
| rails-html-sanitizer (1.0.3) | ||
| loofah (~> 2.0) | ||
| railties (5.0.2) | ||
| actionpack (= 5.0.2) | ||
| activesupport (= 5.0.2) | ||
| method_source | ||
| rake (>= 0.8.7) | ||
| thor (>= 0.18.1, < 2.0) | ||
| rake (12.0.0) | ||
| rb-fsevent (0.9.8) | ||
| rb-inotify (0.9.8) | ||
| ffi (>= 0.5.0) | ||
| ruby-progressbar (1.8.1) | ||
| sass (3.4.23) | ||
| slop (3.6.0) | ||
| spring (2.0.1) | ||
| activesupport (>= 4.2) | ||
| spring-watcher-listen (2.0.1) | ||
| listen (>= 2.7, < 4.0) | ||
| spring (>= 1.2, < 3.0) | ||
| sprockets (3.7.1) | ||
| concurrent-ruby (~> 1.0) | ||
| rack (> 1, < 3) | ||
| sprockets-es6 (0.9.2) | ||
| babel-source (>= 5.8.11) | ||
| babel-transpiler | ||
| sprockets (>= 3.0.0) | ||
| sprockets-rails (3.2.0) | ||
| actionpack (>= 4.0) | ||
| activesupport (>= 4.0) | ||
| sprockets (>= 3.0.0) | ||
| thor (0.19.4) | ||
| thread_safe (0.3.6) | ||
| tzinfo (1.2.3) | ||
| thread_safe (~> 0.1) | ||
| websocket-driver (0.6.5) | ||
| websocket-extensions (>= 0.1.0) | ||
| websocket-extensions (0.1.2) | ||
|
|
||
| PLATFORMS | ||
| ruby | ||
|
|
||
| DEPENDENCIES | ||
| awesome_print | ||
| better_errors | ||
| byebug | ||
| foundation-rails | ||
| listen (~> 3.0.5) | ||
| minitest-rails | ||
| minitest-reporters | ||
| pg (~> 0.18) | ||
| pry-rails | ||
| puma (~> 3.0) | ||
| rails (~> 5.0.2) | ||
| spring | ||
| spring-watcher-listen (~> 2.0.0) | ||
| tzinfo-data | ||
|
|
||
| BUNDLED WITH | ||
| 1.14.6 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| # Add your own tasks in files placed in lib/tasks ending in .rake, | ||
| # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. | ||
|
|
||
| require_relative 'config/application' | ||
|
|
||
| Rails.application.load_tasks |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| module ApplicationCable | ||
| class Channel < ActionCable::Channel::Base | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| module ApplicationCable | ||
| class Connection < ActionCable::Connection::Base | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| class ApplicationController < ActionController::API | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| class CustomersController < ApplicationController | ||
| def index | ||
| customers = Customer.all | ||
| render json: customers.as_json(only: | ||
| [:id, :name, :registered_at, :postal_code, :phone, :movies_checked_out_count]), :status => :ok | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| class MoviesController < ApplicationController | ||
|
|
||
| def index | ||
| movies = Movie.all | ||
| render json: movies.as_json(only: | ||
| [:title, :release_date ]), :status => :ok | ||
| end | ||
|
|
||
| def show | ||
| movie = Movie.find_by title: params[:title] | ||
| if movie | ||
| render json: movie.as_json(only: | ||
| [:title, :release_date, :overview, :inventory], methods: :available_inventory) | ||
| else | ||
| render status: :not_found, json: { error: "#{params[:title]} not found"} | ||
| end | ||
| end | ||
|
|
||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| class RentalsController < ApplicationController | ||
|
|
||
| def create | ||
| rental = Rental.new(rental_params) | ||
| rental.movie = Movie.find_by title: params[:title] | ||
| rental.check_out = Date.today | ||
| if rental.movie.available_inventory == 0 | ||
| render status: :bad_request, json: { error: "Not enough inventory" } | ||
| elsif rental.save | ||
| render status: :ok, json: { id: rental.id } | ||
| rental.customer.movies_checked_out_count += 1 | ||
| else | ||
| render statis: :bad_request, json: { errors: rental.errors.messages } | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def rental_params | ||
| params.require(:rental).permit(:customer_id, :due_date) | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| class ApplicationJob < ActiveJob::Base | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| class ApplicationMailer < ActionMailer::Base | ||
| default from: 'from@example.com' | ||
| layout 'mailer' | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| class ApplicationRecord < ActiveRecord::Base | ||
| self.abstract_class = true | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| class Customer < ApplicationRecord | ||
| validates :name, presence: true | ||
| validates :registered_at, presence: true | ||
| validates :address, presence: true | ||
| validates :city, presence: true | ||
| validates :state, presence: true | ||
| validates :postal_code, presence: true | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could consider validating whether the data you're getting for these fields is consistent with what's expected. For example, is the postal code 6 digits? Big picture, you want to always validate that what is required exists, and if it exists, that the format (data type, length, etc) is what you want it to be. |
||
| validates :phone, presence: true | ||
| validates :account_credit, presence: true | ||
|
|
||
| has_many :rentals | ||
|
|
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| class Movie < ApplicationRecord | ||
| validates :title, presence: true, uniqueness: true | ||
| validates :overview, presence: true | ||
| validates :release_date, presence: true | ||
| validates :inventory, presence: true, numericality: { greater_than: -1, only_integer: true } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah! like, here where you check that the inventory is an integer or that movie names are unique. That's what I was thinking in my previous comment 🙂 |
||
|
|
||
| has_many :rentals | ||
|
|
||
| def available_inventory | ||
| return self.inventory - self.rentals.length | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| class Rental < ApplicationRecord | ||
| belongs_to :customer | ||
| belongs_to :movie | ||
|
|
||
| validates :customer_id, presence: true | ||
| validates :due_date, presence: true | ||
| validates :movie_id, presence: true | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <!DOCTYPE html> | ||
| <html> | ||
| <head> | ||
| <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | ||
| <style> | ||
| /* Email styles need to be inline */ | ||
| </style> | ||
| </head> | ||
|
|
||
| <body> | ||
| <%= yield %> | ||
| </body> | ||
| </html> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <%= yield %> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| #!/usr/bin/env ruby | ||
| ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) | ||
| load Gem.bin_path('bundler', 'bundle') |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| #!/usr/bin/env ruby | ||
| begin | ||
| load File.expand_path('../spring', __FILE__) | ||
| rescue LoadError => e | ||
| raise unless e.message.include?('spring') | ||
| end | ||
| APP_PATH = File.expand_path('../config/application', __dir__) | ||
| require_relative '../config/boot' | ||
| require 'rails/commands' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the rental is processed, the customer.movies_checked_out_count increments up one, which makes sense. Does the movie.available_inventory also need to decrease by one?