From 50aa489864b96740ab8a83d670340d0c3c06a353 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Tue, 5 Nov 2019 12:57:13 -0800 Subject: [PATCH 01/86] initial commit --- .gitignore | 23 +++ .ruby-version | 1 + Gemfile | 59 ++++++ Gemfile.lock | 175 ++++++++++++++++++ Guardfile | 9 + Rakefile | 6 + app/channels/application_cable/channel.rb | 4 + app/channels/application_cable/connection.rb | 4 + app/controllers/application_controller.rb | 2 + app/controllers/concerns/.keep | 0 app/jobs/application_job.rb | 2 + app/mailers/application_mailer.rb | 4 + app/models/application_record.rb | 3 + app/models/concerns/.keep | 0 app/views/layouts/mailer.html.erb | 13 ++ app/views/layouts/mailer.text.erb | 1 + bin/bundle | 3 + bin/rails | 9 + bin/rake | 9 + bin/setup | 33 ++++ bin/spring | 17 ++ bin/update | 28 +++ config.ru | 5 + config/application.rb | 41 ++++ config/boot.rb | 4 + config/cable.yml | 10 + config/credentials.yml.enc | 1 + config/database.yml | 85 +++++++++ config/environment.rb | 5 + config/environments/development.rb | 54 ++++++ config/environments/production.rb | 85 +++++++++ config/environments/test.rb | 46 +++++ config/initializers/action_view.rb | 1 + .../application_controller_renderer.rb | 8 + config/initializers/backtrace_silencers.rb | 7 + config/initializers/cors.rb | 16 ++ .../initializers/filter_parameter_logging.rb | 4 + config/initializers/inflections.rb | 16 ++ config/initializers/mime_types.rb | 4 + config/initializers/wrap_parameters.rb | 14 ++ config/locales/en.yml | 33 ++++ config/puma.rb | 34 ++++ config/routes.rb | 3 + config/spring.rb | 6 + config/storage.yml | 34 ++++ db/schema.rb | 18 ++ lib/tasks/.keep | 0 log/.keep | 0 public/robots.txt | 1 + storage/.keep | 0 test/controllers/.keep | 0 test/fixtures/.keep | 0 test/fixtures/files/.keep | 0 test/integration/.keep | 0 test/mailers/.keep | 0 test/models/.keep | 0 test/test_helper.rb | 10 + tmp/.keep | 0 vendor/.keep | 0 59 files changed, 950 insertions(+) create mode 100644 .gitignore create mode 100644 .ruby-version create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 Guardfile create mode 100644 Rakefile create mode 100644 app/channels/application_cable/channel.rb create mode 100644 app/channels/application_cable/connection.rb create mode 100644 app/controllers/application_controller.rb create mode 100644 app/controllers/concerns/.keep create mode 100644 app/jobs/application_job.rb create mode 100644 app/mailers/application_mailer.rb create mode 100644 app/models/application_record.rb create mode 100644 app/models/concerns/.keep create mode 100644 app/views/layouts/mailer.html.erb create mode 100644 app/views/layouts/mailer.text.erb create mode 100755 bin/bundle create mode 100755 bin/rails create mode 100755 bin/rake create mode 100755 bin/setup create mode 100755 bin/spring create mode 100755 bin/update create mode 100644 config.ru create mode 100644 config/application.rb create mode 100644 config/boot.rb create mode 100644 config/cable.yml create mode 100644 config/credentials.yml.enc create mode 100644 config/database.yml create mode 100644 config/environment.rb create mode 100644 config/environments/development.rb create mode 100644 config/environments/production.rb create mode 100644 config/environments/test.rb create mode 100644 config/initializers/action_view.rb create mode 100644 config/initializers/application_controller_renderer.rb create mode 100644 config/initializers/backtrace_silencers.rb create mode 100644 config/initializers/cors.rb create mode 100644 config/initializers/filter_parameter_logging.rb create mode 100644 config/initializers/inflections.rb create mode 100644 config/initializers/mime_types.rb create mode 100644 config/initializers/wrap_parameters.rb create mode 100644 config/locales/en.yml create mode 100644 config/puma.rb create mode 100644 config/routes.rb create mode 100644 config/spring.rb create mode 100644 config/storage.yml create mode 100644 db/schema.rb create mode 100644 lib/tasks/.keep create mode 100644 log/.keep create mode 100644 public/robots.txt create mode 100644 storage/.keep create mode 100644 test/controllers/.keep create mode 100644 test/fixtures/.keep create mode 100644 test/fixtures/files/.keep create mode 100644 test/integration/.keep create mode 100644 test/mailers/.keep create mode 100644 test/models/.keep create mode 100644 test/test_helper.rb create mode 100644 tmp/.keep create mode 100644 vendor/.keep diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..ea4b32f59 --- /dev/null +++ b/.gitignore @@ -0,0 +1,23 @@ +# 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 uploaded files in development +/storage/* +!/storage/.keep + +.byebug_history + +# Ignore master key for decrypting credentials and more. +/config/master.key diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 000000000..160fe391c --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +2.5.5 \ No newline at end of file diff --git a/Gemfile b/Gemfile new file mode 100644 index 000000000..8143da43e --- /dev/null +++ b/Gemfile @@ -0,0 +1,59 @@ +source 'https://rubygems.org' +git_source(:github) { |repo| "https://github.com/#{repo}.git" } + +ruby '2.5.5' + +# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' +gem 'rails', '~> 5.2.3' +# Use postgresql as the database for Active Record +gem 'pg', '>= 0.18', '< 2.0' +# Use Puma as the app server +gem 'puma', '~> 3.11' +# 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', '~> 4.0' +# Use ActiveModel has_secure_password +# gem 'bcrypt', '~> 3.1.7' + +# Use ActiveStorage variant +# gem 'mini_magick', '~> 4.8' + +# Use Capistrano for deployment +# gem 'capistrano-rails', group: :development + +# Reduces boot times through caching; required in config/boot.rb +gem 'bootsnap', '>= 1.1.0', require: false + +# 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', platforms: [:mri, :mingw, :x64_mingw] +end + +group :development do + gem 'listen', '>= 3.0.5', '< 3.2' + # 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, :test do + gem 'pry-rails' +end + +group :development do + gem 'debase', '>= 0.2.4.1' + gem 'ruby-debug-ide', '>= 0.7.0' +end + +group :test do + gem 'minitest-rails' + gem 'minitest-reporters' +end diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 000000000..981f08e8a --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,175 @@ +GEM + remote: https://rubygems.org/ + specs: + actioncable (5.2.3) + actionpack (= 5.2.3) + nio4r (~> 2.0) + websocket-driver (>= 0.6.1) + actionmailer (5.2.3) + actionpack (= 5.2.3) + actionview (= 5.2.3) + activejob (= 5.2.3) + mail (~> 2.5, >= 2.5.4) + rails-dom-testing (~> 2.0) + actionpack (5.2.3) + actionview (= 5.2.3) + activesupport (= 5.2.3) + rack (~> 2.0) + rack-test (>= 0.6.3) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.0, >= 1.0.2) + actionview (5.2.3) + activesupport (= 5.2.3) + builder (~> 3.1) + erubi (~> 1.4) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.0, >= 1.0.3) + activejob (5.2.3) + activesupport (= 5.2.3) + globalid (>= 0.3.6) + activemodel (5.2.3) + activesupport (= 5.2.3) + activerecord (5.2.3) + activemodel (= 5.2.3) + activesupport (= 5.2.3) + arel (>= 9.0) + activestorage (5.2.3) + actionpack (= 5.2.3) + activerecord (= 5.2.3) + marcel (~> 0.3.1) + activesupport (5.2.3) + concurrent-ruby (~> 1.0, >= 1.0.2) + i18n (>= 0.7, < 2) + minitest (~> 5.1) + tzinfo (~> 1.1) + ansi (1.5.0) + arel (9.0.0) + bootsnap (1.4.5) + msgpack (~> 1.0) + builder (3.2.3) + byebug (11.0.1) + coderay (1.1.2) + concurrent-ruby (1.1.5) + crass (1.0.5) + debase (0.2.4.1) + debase-ruby_core_source (>= 0.10.2) + debase-ruby_core_source (0.10.6) + erubi (1.9.0) + ffi (1.11.1) + globalid (0.4.2) + activesupport (>= 4.2.0) + i18n (1.7.0) + concurrent-ruby (~> 1.0) + listen (3.1.5) + rb-fsevent (~> 0.9, >= 0.9.4) + rb-inotify (~> 0.9, >= 0.9.7) + ruby_dep (~> 1.2) + loofah (2.3.1) + crass (~> 1.0.2) + nokogiri (>= 1.5.9) + mail (2.7.1) + mini_mime (>= 0.1.1) + marcel (0.3.3) + mimemagic (~> 0.3.2) + method_source (0.9.2) + mimemagic (0.3.3) + mini_mime (1.0.2) + mini_portile2 (2.4.0) + minitest (5.13.0) + minitest-rails (5.2.0) + minitest (~> 5.10) + railties (~> 5.2.0) + minitest-reporters (1.4.2) + ansi + builder + minitest (>= 5.0) + ruby-progressbar + msgpack (1.3.1) + nio4r (2.5.2) + nokogiri (1.10.5) + mini_portile2 (~> 2.4.0) + pg (1.1.4) + pry (0.12.2) + coderay (~> 1.1.0) + method_source (~> 0.9.0) + pry-rails (0.3.9) + pry (>= 0.10.4) + puma (3.12.1) + rack (2.0.7) + rack-test (1.1.0) + rack (>= 1.0, < 3) + rails (5.2.3) + actioncable (= 5.2.3) + actionmailer (= 5.2.3) + actionpack (= 5.2.3) + actionview (= 5.2.3) + activejob (= 5.2.3) + activemodel (= 5.2.3) + activerecord (= 5.2.3) + activestorage (= 5.2.3) + activesupport (= 5.2.3) + bundler (>= 1.3.0) + railties (= 5.2.3) + sprockets-rails (>= 2.0.0) + rails-dom-testing (2.0.3) + activesupport (>= 4.2.0) + nokogiri (>= 1.6) + rails-html-sanitizer (1.3.0) + loofah (~> 2.3) + railties (5.2.3) + actionpack (= 5.2.3) + activesupport (= 5.2.3) + method_source + rake (>= 0.8.7) + thor (>= 0.19.0, < 2.0) + rake (13.0.0) + rb-fsevent (0.10.3) + rb-inotify (0.10.0) + ffi (~> 1.0) + ruby-debug-ide (0.7.0) + rake (>= 0.8.1) + ruby-progressbar (1.10.1) + ruby_dep (1.5.0) + spring (2.1.0) + spring-watcher-listen (2.0.1) + listen (>= 2.7, < 4.0) + spring (>= 1.2, < 3.0) + sprockets (4.0.0) + concurrent-ruby (~> 1.0) + rack (> 1, < 3) + sprockets-rails (3.2.1) + actionpack (>= 4.0) + activesupport (>= 4.0) + sprockets (>= 3.0.0) + thor (0.20.3) + thread_safe (0.3.6) + tzinfo (1.2.5) + thread_safe (~> 0.1) + websocket-driver (0.7.1) + websocket-extensions (>= 0.1.0) + websocket-extensions (0.1.4) + +PLATFORMS + ruby + +DEPENDENCIES + bootsnap (>= 1.1.0) + byebug + debase (>= 0.2.4.1) + listen (>= 3.0.5, < 3.2) + minitest-rails + minitest-reporters + pg (>= 0.18, < 2.0) + pry-rails + puma (~> 3.11) + rails (~> 5.2.3) + ruby-debug-ide (>= 0.7.0) + spring + spring-watcher-listen (~> 2.0.0) + tzinfo-data + +RUBY VERSION + ruby 2.5.5p157 + +BUNDLED WITH + 2.0.2 diff --git a/Guardfile b/Guardfile new file mode 100644 index 000000000..e34f706f4 --- /dev/null +++ b/Guardfile @@ -0,0 +1,9 @@ +guard :minitest, autorun: false, spring: true do + watch(%r{^app/(.+).rb$}) { |m| "test/#{m[1]}_test.rb" } + watch(%r{^app/controllers/application_controller.rb$}) { 'test/controllers' } + watch(%r{^app/controllers/(.+)_controller.rb$}) { |m| "test/integration/#{m[1]}_test.rb" } + watch(%r{^app/views/(.+)_mailer/.+}) { |m| "test/mailers/#{m[1]}_mailer_test.rb" } + watch(%r{^lib/(.+).rb$}) { |m| "test/lib/#{m[1]}_test.rb" } + watch(%r{^test/.+_test.rb$}) + watch(%r{^test/test_helper.rb$}) { 'test' } +end diff --git a/Rakefile b/Rakefile new file mode 100644 index 000000000..e85f91391 --- /dev/null +++ b/Rakefile @@ -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 diff --git a/app/channels/application_cable/channel.rb b/app/channels/application_cable/channel.rb new file mode 100644 index 000000000..d67269728 --- /dev/null +++ b/app/channels/application_cable/channel.rb @@ -0,0 +1,4 @@ +module ApplicationCable + class Channel < ActionCable::Channel::Base + end +end diff --git a/app/channels/application_cable/connection.rb b/app/channels/application_cable/connection.rb new file mode 100644 index 000000000..0ff5442f4 --- /dev/null +++ b/app/channels/application_cable/connection.rb @@ -0,0 +1,4 @@ +module ApplicationCable + class Connection < ActionCable::Connection::Base + end +end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb new file mode 100644 index 000000000..4ac8823b0 --- /dev/null +++ b/app/controllers/application_controller.rb @@ -0,0 +1,2 @@ +class ApplicationController < ActionController::API +end diff --git a/app/controllers/concerns/.keep b/app/controllers/concerns/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/app/jobs/application_job.rb b/app/jobs/application_job.rb new file mode 100644 index 000000000..a009ace51 --- /dev/null +++ b/app/jobs/application_job.rb @@ -0,0 +1,2 @@ +class ApplicationJob < ActiveJob::Base +end diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb new file mode 100644 index 000000000..286b2239d --- /dev/null +++ b/app/mailers/application_mailer.rb @@ -0,0 +1,4 @@ +class ApplicationMailer < ActionMailer::Base + default from: 'from@example.com' + layout 'mailer' +end diff --git a/app/models/application_record.rb b/app/models/application_record.rb new file mode 100644 index 000000000..10a4cba84 --- /dev/null +++ b/app/models/application_record.rb @@ -0,0 +1,3 @@ +class ApplicationRecord < ActiveRecord::Base + self.abstract_class = true +end diff --git a/app/models/concerns/.keep b/app/models/concerns/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/app/views/layouts/mailer.html.erb b/app/views/layouts/mailer.html.erb new file mode 100644 index 000000000..cbd34d2e9 --- /dev/null +++ b/app/views/layouts/mailer.html.erb @@ -0,0 +1,13 @@ + + + + + + + + + <%= yield %> + + diff --git a/app/views/layouts/mailer.text.erb b/app/views/layouts/mailer.text.erb new file mode 100644 index 000000000..37f0bddbd --- /dev/null +++ b/app/views/layouts/mailer.text.erb @@ -0,0 +1 @@ +<%= yield %> diff --git a/bin/bundle b/bin/bundle new file mode 100755 index 000000000..f19acf5b5 --- /dev/null +++ b/bin/bundle @@ -0,0 +1,3 @@ +#!/usr/bin/env ruby +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__) +load Gem.bin_path('bundler', 'bundle') diff --git a/bin/rails b/bin/rails new file mode 100755 index 000000000..5badb2fde --- /dev/null +++ b/bin/rails @@ -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' diff --git a/bin/rake b/bin/rake new file mode 100755 index 000000000..d87d5f578 --- /dev/null +++ b/bin/rake @@ -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 +require_relative '../config/boot' +require 'rake' +Rake.application.run diff --git a/bin/setup b/bin/setup new file mode 100755 index 000000000..a334d86a6 --- /dev/null +++ b/bin/setup @@ -0,0 +1,33 @@ +#!/usr/bin/env ruby +require 'fileutils' +include FileUtils + +# path to your application root. +APP_ROOT = File.expand_path('..', __dir__) + +def system!(*args) + system(*args) || abort("\n== Command #{args} failed ==") +end + +chdir APP_ROOT do + # This script is a starting point to setup your application. + # Add necessary setup steps to this file. + + puts '== Installing dependencies ==' + system! 'gem install bundler --conservative' + system('bundle check') || system!('bundle install') + + # puts "\n== Copying sample files ==" + # unless File.exist?('config/database.yml') + # cp 'config/database.yml.sample', 'config/database.yml' + # end + + puts "\n== Preparing database ==" + system! 'bin/rails db:setup' + + puts "\n== Removing old logs and tempfiles ==" + system! 'bin/rails log:clear tmp:clear' + + puts "\n== Restarting application server ==" + system! 'bin/rails restart' +end diff --git a/bin/spring b/bin/spring new file mode 100755 index 000000000..d89ee495f --- /dev/null +++ b/bin/spring @@ -0,0 +1,17 @@ +#!/usr/bin/env ruby + +# This file loads Spring without using Bundler, in order to be fast. +# It gets overwritten when you run the `spring binstub` command. + +unless defined?(Spring) + require 'rubygems' + require 'bundler' + + lockfile = Bundler::LockfileParser.new(Bundler.default_lockfile.read) + spring = lockfile.specs.detect { |spec| spec.name == 'spring' } + if spring + Gem.use_paths Gem.dir, Bundler.bundle_path.to_s, *Gem.path + gem 'spring', spring.version + require 'spring/binstub' + end +end diff --git a/bin/update b/bin/update new file mode 100755 index 000000000..67d0d4964 --- /dev/null +++ b/bin/update @@ -0,0 +1,28 @@ +#!/usr/bin/env ruby +require 'fileutils' +include FileUtils + +# path to your application root. +APP_ROOT = File.expand_path('..', __dir__) + +def system!(*args) + system(*args) || abort("\n== Command #{args} failed ==") +end + +chdir APP_ROOT do + # This script is a way to update your development environment automatically. + # Add necessary update steps to this file. + + puts '== Installing dependencies ==' + system! 'gem install bundler --conservative' + system('bundle check') || system!('bundle install') + + puts "\n== Updating database ==" + system! 'bin/rails db:migrate' + + puts "\n== Removing old logs and tempfiles ==" + system! 'bin/rails log:clear tmp:clear' + + puts "\n== Restarting application server ==" + system! 'bin/rails restart' +end diff --git a/config.ru b/config.ru new file mode 100644 index 000000000..f7ba0b527 --- /dev/null +++ b/config.ru @@ -0,0 +1,5 @@ +# This file is used by Rack-based servers to start the application. + +require_relative 'config/environment' + +run Rails.application diff --git a/config/application.rb b/config/application.rb new file mode 100644 index 000000000..56f84c20c --- /dev/null +++ b/config/application.rb @@ -0,0 +1,41 @@ +require_relative 'boot' + +require "rails" +# Pick the frameworks you want: +require "active_model/railtie" +require "active_job/railtie" +require "active_record/railtie" +require "active_storage/engine" +require "action_controller/railtie" +require "action_mailer/railtie" +require "action_view/railtie" +require "action_cable/engine" +# require "sprockets/railtie" +require "rails/test_unit/railtie" + +# Require the gems listed in Gemfile, including any gems +# you've limited to :test, :development, or :production. +Bundler.require(*Rails.groups) + +module VideoStoreApi + class Application < Rails::Application + config.generators do |g| + # Force new test files to be generated in the minitest-spec style + g.test_framework :minitest, spec: true + # Always use .js files, never .coffee + g.javascript_engine :js + end + # Initialize configuration defaults for originally generated Rails version. + config.load_defaults 5.2 + + # Settings in config/environments/* take precedence over those specified here. + # Application configuration can go into files in config/initializers + # -- all .rb files in that directory are automatically loaded after loading + # the framework and any gems in your application. + + # Only loads a smaller set of middleware suitable for API only apps. + # Middleware like session, flash, cookies can be added back manually. + # Skip views, helpers and assets when generating a new resource. + config.api_only = true + end +end diff --git a/config/boot.rb b/config/boot.rb new file mode 100644 index 000000000..b9e460cef --- /dev/null +++ b/config/boot.rb @@ -0,0 +1,4 @@ +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__) + +require 'bundler/setup' # Set up gems listed in the Gemfile. +require 'bootsnap/setup' # Speed up boot time by caching expensive operations. diff --git a/config/cable.yml b/config/cable.yml new file mode 100644 index 000000000..6cc616cd1 --- /dev/null +++ b/config/cable.yml @@ -0,0 +1,10 @@ +development: + adapter: async + +test: + adapter: async + +production: + adapter: redis + url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %> + channel_prefix: video-store-api_production diff --git a/config/credentials.yml.enc b/config/credentials.yml.enc new file mode 100644 index 000000000..2fe1d46e0 --- /dev/null +++ b/config/credentials.yml.enc @@ -0,0 +1 @@ +662IZAul6TtCKdeXAmkiXzyo73FgQt0OzDyut9Uux8/C/sSdzKq8RwALMhD0ORrE33XKhmoEss96G3gKpAWeq4JHGqswaTsSk3FDGHqtiHr11tr+36wRgcZtlKMMN0xgHv4H+/xUk3APQUfwCqxyqjvoq3YGtQBGRZWOHP7UenEtWrKR8gHYW4jwpNkymfaltPNhL/5yBPurASSCro+TgshH1A3PRFjXiiuniujOoXMpzxtk/vS9q9z75LQ4axynOInKF5RK5+2yfnKkc9YwYPLdYDgzx+SraBtUsjj6PjQvsHPhH2+U4J+qS92qhRE8G5t2x6d/SsFROCi+Z8jcj1qKttcCw0Inbjx7fpwDsj+Z4lQzJ/D1zW3GzyUdu6LFfk0cEXUd8kIpSkS1LzRa7zYtYGiDyHOhEvd6--wr9mOgYY8xt0dhOs--6Hb//mz2IgRYg2KO3ysdAw== \ No newline at end of file diff --git a/config/database.yml b/config/database.yml new file mode 100644 index 000000000..25f76c8e1 --- /dev/null +++ b/config/database.yml @@ -0,0 +1,85 @@ +# PostgreSQL. Versions 9.1 and up are supported. +# +# Install the pg driver: +# gem install pg +# On OS X with Homebrew: +# gem install pg -- --with-pg-config=/usr/local/bin/pg_config +# On OS X with MacPorts: +# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config +# On Windows: +# gem install pg +# Choose the win32 build. +# Install PostgreSQL and put its /bin directory on your path. +# +# Configure Using Gemfile +# gem 'pg' +# +default: &default + adapter: postgresql + encoding: unicode + # For details on connection pooling, see Rails configuration guide + # http://guides.rubyonrails.org/configuring.html#database-pooling + pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> + +development: + <<: *default + database: video-store-api_development + + # The specified database role being used to connect to postgres. + # To create additional roles in postgres see `$ createuser --help`. + # When left blank, postgres will use the default role. This is + # the same name as the operating system user that initialized the database. + #username: video-store-api + + # The password associated with the postgres role (username). + #password: + + # Connect on a TCP socket. Omitted by default since the client uses a + # domain socket that doesn't need configuration. Windows does not have + # domain sockets, so uncomment these lines. + #host: localhost + + # The TCP port the server listens on. Defaults to 5432. + # If your server runs on a different port number, change accordingly. + #port: 5432 + + # Schema search path. The server defaults to $user,public + #schema_search_path: myapp,sharedapp,public + + # Minimum log levels, in increasing order: + # debug5, debug4, debug3, debug2, debug1, + # log, notice, warning, error, fatal, and panic + # Defaults to warning. + #min_messages: notice + +# Warning: The database defined as "test" will be erased and +# re-generated from your development database when you run "rake". +# Do not set this db to the same as development or production. +test: + <<: *default + database: video-store-api_test + +# As with config/secrets.yml, you never want to store sensitive information, +# like your database password, in your source code. If your source code is +# ever seen by anyone, they now have access to your database. +# +# Instead, provide the password as a unix environment variable when you boot +# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database +# for a full rundown on how to provide these environment variables in a +# production deployment. +# +# On Heroku and other platform providers, you may have a full connection URL +# available as an environment variable. For example: +# +# DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase" +# +# You can use this database configuration with: +# +# production: +# url: <%= ENV['DATABASE_URL'] %> +# +production: + <<: *default + database: video-store-api_production + username: video-store-api + password: <%= ENV['VIDEO-STORE-API_DATABASE_PASSWORD'] %> diff --git a/config/environment.rb b/config/environment.rb new file mode 100644 index 000000000..426333bb4 --- /dev/null +++ b/config/environment.rb @@ -0,0 +1,5 @@ +# Load the Rails application. +require_relative 'application' + +# Initialize the Rails application. +Rails.application.initialize! diff --git a/config/environments/development.rb b/config/environments/development.rb new file mode 100644 index 000000000..d52ec9efb --- /dev/null +++ b/config/environments/development.rb @@ -0,0 +1,54 @@ +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + # In the development environment your application's code is reloaded on + # every request. This slows down response time but is perfect for development + # since you don't have to restart the web server when you make code changes. + config.cache_classes = false + + # Do not eager load code on boot. + config.eager_load = false + + # Show full error reports. + config.consider_all_requests_local = true + + # Enable/disable caching. By default caching is disabled. + # Run rails dev:cache to toggle caching. + if Rails.root.join('tmp', 'caching-dev.txt').exist? + config.action_controller.perform_caching = true + + config.cache_store = :memory_store + config.public_file_server.headers = { + 'Cache-Control' => "public, max-age=#{2.days.to_i}" + } + else + config.action_controller.perform_caching = false + + config.cache_store = :null_store + end + + # Store uploaded files on the local file system (see config/storage.yml for options) + config.active_storage.service = :local + + # Don't care if the mailer can't send. + config.action_mailer.raise_delivery_errors = false + + config.action_mailer.perform_caching = false + + # Print deprecation notices to the Rails logger. + config.active_support.deprecation = :log + + # Raise an error on page load if there are pending migrations. + config.active_record.migration_error = :page_load + + # Highlight code that triggered database queries in logs. + config.active_record.verbose_query_logs = true + + + # Raises error for missing translations + # config.action_view.raise_on_missing_translations = true + + # Use an evented file watcher to asynchronously detect changes in source code, + # routes, locales, etc. This feature depends on the listen gem. + config.file_watcher = ActiveSupport::EventedFileUpdateChecker +end diff --git a/config/environments/production.rb b/config/environments/production.rb new file mode 100644 index 000000000..5f023a796 --- /dev/null +++ b/config/environments/production.rb @@ -0,0 +1,85 @@ +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + # Code is not reloaded between requests. + config.cache_classes = true + + # Eager load code on boot. This eager loads most of Rails and + # your application in memory, allowing both threaded web servers + # and those relying on copy on write to perform better. + # Rake tasks automatically ignore this option for performance. + config.eager_load = true + + # Full error reports are disabled and caching is turned on. + config.consider_all_requests_local = false + config.action_controller.perform_caching = true + + # Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"] + # or in config/master.key. This key is used to decrypt credentials (and other encrypted files). + # config.require_master_key = true + + # Disable serving static files from the `/public` folder by default since + # Apache or NGINX already handles this. + config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present? + + # Enable serving of images, stylesheets, and JavaScripts from an asset server. + # config.action_controller.asset_host = 'http://assets.example.com' + + # Specifies the header that your server uses for sending files. + # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache + # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX + + # Store uploaded files on the local file system (see config/storage.yml for options) + config.active_storage.service = :local + + # Mount Action Cable outside main process or domain + # config.action_cable.mount_path = nil + # config.action_cable.url = 'wss://example.com/cable' + # config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ] + + # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. + # config.force_ssl = true + + # Use the lowest log level to ensure availability of diagnostic information + # when problems arise. + config.log_level = :debug + + # Prepend all log lines with the following tags. + config.log_tags = [ :request_id ] + + # Use a different cache store in production. + # config.cache_store = :mem_cache_store + + # Use a real queuing backend for Active Job (and separate queues per environment) + # config.active_job.queue_adapter = :resque + # config.active_job.queue_name_prefix = "video-store-api_#{Rails.env}" + + config.action_mailer.perform_caching = false + + # Ignore bad email addresses and do not raise email delivery errors. + # Set this to true and configure the email server for immediate delivery to raise delivery errors. + # config.action_mailer.raise_delivery_errors = false + + # Enable locale fallbacks for I18n (makes lookups for any locale fall back to + # the I18n.default_locale when a translation cannot be found). + config.i18n.fallbacks = true + + # Send deprecation notices to registered listeners. + config.active_support.deprecation = :notify + + # Use default logging formatter so that PID and timestamp are not suppressed. + config.log_formatter = ::Logger::Formatter.new + + # Use a different logger for distributed setups. + # require 'syslog/logger' + # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name') + + if ENV["RAILS_LOG_TO_STDOUT"].present? + logger = ActiveSupport::Logger.new(STDOUT) + logger.formatter = config.log_formatter + config.logger = ActiveSupport::TaggedLogging.new(logger) + end + + # Do not dump schema after migrations. + config.active_record.dump_schema_after_migration = false +end diff --git a/config/environments/test.rb b/config/environments/test.rb new file mode 100644 index 000000000..0a38fd3ce --- /dev/null +++ b/config/environments/test.rb @@ -0,0 +1,46 @@ +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + # The test environment is used exclusively to run your application's + # test suite. You never need to work with it otherwise. Remember that + # your test database is "scratch space" for the test suite and is wiped + # and recreated between test runs. Don't rely on the data there! + config.cache_classes = true + + # Do not eager load code on boot. This avoids loading your whole application + # just for the purpose of running a single test. If you are using a tool that + # preloads Rails for running tests, you may have to set it to true. + config.eager_load = false + + # Configure public file server for tests with Cache-Control for performance. + config.public_file_server.enabled = true + config.public_file_server.headers = { + 'Cache-Control' => "public, max-age=#{1.hour.to_i}" + } + + # Show full error reports and disable caching. + config.consider_all_requests_local = true + config.action_controller.perform_caching = false + + # Raise exceptions instead of rendering exception templates. + config.action_dispatch.show_exceptions = false + + # Disable request forgery protection in test environment. + config.action_controller.allow_forgery_protection = false + + # Store uploaded files on the local file system in a temporary directory + config.active_storage.service = :test + + config.action_mailer.perform_caching = false + + # Tell Action Mailer not to deliver emails to the real world. + # The :test delivery method accumulates sent emails in the + # ActionMailer::Base.deliveries array. + config.action_mailer.delivery_method = :test + + # Print deprecation notices to the stderr. + config.active_support.deprecation = :stderr + + # Raises error for missing translations + # config.action_view.raise_on_missing_translations = true +end diff --git a/config/initializers/action_view.rb b/config/initializers/action_view.rb new file mode 100644 index 000000000..142d382f8 --- /dev/null +++ b/config/initializers/action_view.rb @@ -0,0 +1 @@ +Rails.application.config.action_view.form_with_generates_remote_forms = false diff --git a/config/initializers/application_controller_renderer.rb b/config/initializers/application_controller_renderer.rb new file mode 100644 index 000000000..89d2efab2 --- /dev/null +++ b/config/initializers/application_controller_renderer.rb @@ -0,0 +1,8 @@ +# Be sure to restart your server when you modify this file. + +# ActiveSupport::Reloader.to_prepare do +# ApplicationController.renderer.defaults.merge!( +# http_host: 'example.org', +# https: false +# ) +# end diff --git a/config/initializers/backtrace_silencers.rb b/config/initializers/backtrace_silencers.rb new file mode 100644 index 000000000..59385cdf3 --- /dev/null +++ b/config/initializers/backtrace_silencers.rb @@ -0,0 +1,7 @@ +# Be sure to restart your server when you modify this file. + +# You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces. +# Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ } + +# You can also remove all the silencers if you're trying to debug a problem that might stem from framework code. +# Rails.backtrace_cleaner.remove_silencers! diff --git a/config/initializers/cors.rb b/config/initializers/cors.rb new file mode 100644 index 000000000..3b1c1b5ed --- /dev/null +++ b/config/initializers/cors.rb @@ -0,0 +1,16 @@ +# Be sure to restart your server when you modify this file. + +# Avoid CORS issues when API is called from the frontend app. +# Handle Cross-Origin Resource Sharing (CORS) in order to accept cross-origin AJAX requests. + +# Read more: https://github.com/cyu/rack-cors + +# Rails.application.config.middleware.insert_before 0, Rack::Cors do +# allow do +# origins 'example.com' +# +# resource '*', +# headers: :any, +# methods: [:get, :post, :put, :patch, :delete, :options, :head] +# end +# end diff --git a/config/initializers/filter_parameter_logging.rb b/config/initializers/filter_parameter_logging.rb new file mode 100644 index 000000000..4a994e1e7 --- /dev/null +++ b/config/initializers/filter_parameter_logging.rb @@ -0,0 +1,4 @@ +# Be sure to restart your server when you modify this file. + +# Configure sensitive parameters which will be filtered from the log file. +Rails.application.config.filter_parameters += [:password] diff --git a/config/initializers/inflections.rb b/config/initializers/inflections.rb new file mode 100644 index 000000000..ac033bf9d --- /dev/null +++ b/config/initializers/inflections.rb @@ -0,0 +1,16 @@ +# Be sure to restart your server when you modify this file. + +# Add new inflection rules using the following format. Inflections +# are locale specific, and you may define rules for as many different +# locales as you wish. All of these examples are active by default: +# ActiveSupport::Inflector.inflections(:en) do |inflect| +# inflect.plural /^(ox)$/i, '\1en' +# inflect.singular /^(ox)en/i, '\1' +# inflect.irregular 'person', 'people' +# inflect.uncountable %w( fish sheep ) +# end + +# These inflection rules are supported but not enabled by default: +# ActiveSupport::Inflector.inflections(:en) do |inflect| +# inflect.acronym 'RESTful' +# end diff --git a/config/initializers/mime_types.rb b/config/initializers/mime_types.rb new file mode 100644 index 000000000..dc1899682 --- /dev/null +++ b/config/initializers/mime_types.rb @@ -0,0 +1,4 @@ +# Be sure to restart your server when you modify this file. + +# Add new mime types for use in respond_to blocks: +# Mime::Type.register "text/richtext", :rtf diff --git a/config/initializers/wrap_parameters.rb b/config/initializers/wrap_parameters.rb new file mode 100644 index 000000000..bbfc3961b --- /dev/null +++ b/config/initializers/wrap_parameters.rb @@ -0,0 +1,14 @@ +# Be sure to restart your server when you modify this file. + +# This file contains settings for ActionController::ParamsWrapper which +# is enabled by default. + +# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array. +ActiveSupport.on_load(:action_controller) do + wrap_parameters format: [:json] +end + +# To enable root element in JSON for ActiveRecord objects. +# ActiveSupport.on_load(:active_record) do +# self.include_root_in_json = true +# end diff --git a/config/locales/en.yml b/config/locales/en.yml new file mode 100644 index 000000000..decc5a857 --- /dev/null +++ b/config/locales/en.yml @@ -0,0 +1,33 @@ +# Files in the config/locales directory are used for internationalization +# and are automatically loaded by Rails. If you want to use locales other +# than English, add the necessary files in this directory. +# +# To use the locales, use `I18n.t`: +# +# I18n.t 'hello' +# +# In views, this is aliased to just `t`: +# +# <%= t('hello') %> +# +# To use a different locale, set it with `I18n.locale`: +# +# I18n.locale = :es +# +# This would use the information in config/locales/es.yml. +# +# The following keys must be escaped otherwise they will not be retrieved by +# the default I18n backend: +# +# true, false, on, off, yes, no +# +# Instead, surround them with single quotes. +# +# en: +# 'true': 'foo' +# +# To learn more, please read the Rails Internationalization guide +# available at http://guides.rubyonrails.org/i18n.html. + +en: + hello: "Hello world" diff --git a/config/puma.rb b/config/puma.rb new file mode 100644 index 000000000..a5eccf816 --- /dev/null +++ b/config/puma.rb @@ -0,0 +1,34 @@ +# Puma can serve each request in a thread from an internal thread pool. +# The `threads` method setting takes two numbers: a minimum and maximum. +# Any libraries that use thread pools should be configured to match +# the maximum value specified for Puma. Default is set to 5 threads for minimum +# and maximum; this matches the default thread size of Active Record. +# +threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 } +threads threads_count, threads_count + +# Specifies the `port` that Puma will listen on to receive requests; default is 3000. +# +port ENV.fetch("PORT") { 3000 } + +# Specifies the `environment` that Puma will run in. +# +environment ENV.fetch("RAILS_ENV") { "development" } + +# Specifies the number of `workers` to boot in clustered mode. +# Workers are forked webserver processes. If using threads and workers together +# the concurrency of the application would be max `threads` * `workers`. +# Workers do not work on JRuby or Windows (both of which do not support +# processes). +# +# workers ENV.fetch("WEB_CONCURRENCY") { 2 } + +# Use the `preload_app!` method when specifying a `workers` number. +# This directive tells Puma to first boot the application and load code +# before forking the application. This takes advantage of Copy On Write +# process behavior so workers use less memory. +# +# preload_app! + +# Allow puma to be restarted by `rails restart` command. +plugin :tmp_restart diff --git a/config/routes.rb b/config/routes.rb new file mode 100644 index 000000000..787824f88 --- /dev/null +++ b/config/routes.rb @@ -0,0 +1,3 @@ +Rails.application.routes.draw do + # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html +end diff --git a/config/spring.rb b/config/spring.rb new file mode 100644 index 000000000..9fa7863f9 --- /dev/null +++ b/config/spring.rb @@ -0,0 +1,6 @@ +%w[ + .ruby-version + .rbenv-vars + tmp/restart.txt + tmp/caching-dev.txt +].each { |path| Spring.watch(path) } diff --git a/config/storage.yml b/config/storage.yml new file mode 100644 index 000000000..d32f76e8f --- /dev/null +++ b/config/storage.yml @@ -0,0 +1,34 @@ +test: + service: Disk + root: <%= Rails.root.join("tmp/storage") %> + +local: + service: Disk + root: <%= Rails.root.join("storage") %> + +# Use rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key) +# amazon: +# service: S3 +# access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %> +# secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %> +# region: us-east-1 +# bucket: your_own_bucket + +# Remember not to checkin your GCS keyfile to a repository +# google: +# service: GCS +# project: your_project +# credentials: <%= Rails.root.join("path/to/gcs.keyfile") %> +# bucket: your_own_bucket + +# Use rails credentials:edit to set the Azure Storage secret (as azure_storage:storage_access_key) +# microsoft: +# service: AzureStorage +# storage_account_name: your_account_name +# storage_access_key: <%= Rails.application.credentials.dig(:azure_storage, :storage_access_key) %> +# container: your_container_name + +# mirror: +# service: Mirror +# primary: local +# mirrors: [ amazon, google, microsoft ] diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 000000000..2611543b3 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,18 @@ +# This file is auto-generated from the current state of the database. Instead +# 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). +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 0) do + + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" + +end diff --git a/lib/tasks/.keep b/lib/tasks/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/log/.keep b/log/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/public/robots.txt b/public/robots.txt new file mode 100644 index 000000000..37b576a4a --- /dev/null +++ b/public/robots.txt @@ -0,0 +1 @@ +# See http://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file diff --git a/storage/.keep b/storage/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/test/controllers/.keep b/test/controllers/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/test/fixtures/.keep b/test/fixtures/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/test/fixtures/files/.keep b/test/fixtures/files/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/test/integration/.keep b/test/integration/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/test/mailers/.keep b/test/mailers/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/test/models/.keep b/test/models/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 000000000..3ab84e3d1 --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,10 @@ +ENV['RAILS_ENV'] ||= 'test' +require_relative '../config/environment' +require 'rails/test_help' + +class ActiveSupport::TestCase + # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. + fixtures :all + + # Add more helper methods to be used by all tests here... +end diff --git a/tmp/.keep b/tmp/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/vendor/.keep b/vendor/.keep new file mode 100644 index 000000000..e69de29bb From 02ee5279b41b88ef8f216b55e42282f822ca145d Mon Sep 17 00:00:00 2001 From: stupendousC Date: Tue, 5 Nov 2019 13:09:57 -0800 Subject: [PATCH 02/86] set up models, controllers, and schema --- app/controllers/customers_controller.rb | 2 ++ app/controllers/movies_controller.rb | 2 ++ app/controllers/rentals_controller.rb | 2 ++ app/models/customer.rb | 3 ++ app/models/movie.rb | 3 ++ app/models/rental.rb | 4 +++ db/migrate/20191105205944_create_movies.rb | 12 +++++++ db/migrate/20191105210059_create_customers.rb | 15 ++++++++ db/migrate/20191105210711_create_rentals.rb | 13 +++++++ db/schema.rb | 35 ++++++++++++++++++- test/controllers/customers_controller_test.rb | 7 ++++ test/controllers/movies_controller_test.rb | 7 ++++ test/controllers/rentals_controller_test.rb | 7 ++++ test/models/customer_test.rb | 7 ++++ test/models/movie_test.rb | 7 ++++ test/models/rental_test.rb | 7 ++++ 16 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 app/controllers/customers_controller.rb create mode 100644 app/controllers/movies_controller.rb create mode 100644 app/controllers/rentals_controller.rb create mode 100644 app/models/customer.rb create mode 100644 app/models/movie.rb create mode 100644 app/models/rental.rb create mode 100644 db/migrate/20191105205944_create_movies.rb create mode 100644 db/migrate/20191105210059_create_customers.rb create mode 100644 db/migrate/20191105210711_create_rentals.rb create mode 100644 test/controllers/customers_controller_test.rb create mode 100644 test/controllers/movies_controller_test.rb create mode 100644 test/controllers/rentals_controller_test.rb create mode 100644 test/models/customer_test.rb create mode 100644 test/models/movie_test.rb create mode 100644 test/models/rental_test.rb diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb new file mode 100644 index 000000000..ca3b6e024 --- /dev/null +++ b/app/controllers/customers_controller.rb @@ -0,0 +1,2 @@ +class CustomersController < ApplicationController +end diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb new file mode 100644 index 000000000..6c4c51614 --- /dev/null +++ b/app/controllers/movies_controller.rb @@ -0,0 +1,2 @@ +class MoviesController < ApplicationController +end diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb new file mode 100644 index 000000000..58c72b791 --- /dev/null +++ b/app/controllers/rentals_controller.rb @@ -0,0 +1,2 @@ +class RentalsController < ApplicationController +end diff --git a/app/models/customer.rb b/app/models/customer.rb new file mode 100644 index 000000000..854c5131e --- /dev/null +++ b/app/models/customer.rb @@ -0,0 +1,3 @@ +class Customer < ApplicationRecord + has_many :movies, through :rentals +end diff --git a/app/models/movie.rb b/app/models/movie.rb new file mode 100644 index 000000000..675e8b056 --- /dev/null +++ b/app/models/movie.rb @@ -0,0 +1,3 @@ +class Movie < ApplicationRecord + has_many :customers, through :rentals +end diff --git a/app/models/rental.rb b/app/models/rental.rb new file mode 100644 index 000000000..34d3f4df8 --- /dev/null +++ b/app/models/rental.rb @@ -0,0 +1,4 @@ +class Rental < ApplicationRecord + belongs_to :movie + belongs_to :customer +end diff --git a/db/migrate/20191105205944_create_movies.rb b/db/migrate/20191105205944_create_movies.rb new file mode 100644 index 000000000..95d31e0f8 --- /dev/null +++ b/db/migrate/20191105205944_create_movies.rb @@ -0,0 +1,12 @@ +class CreateMovies < ActiveRecord::Migration[5.2] + def change + create_table :movies do |t| + t.string :title + t.string :overview + t.string :release_date + t.integer :inventory + + t.timestamps + end + end +end diff --git a/db/migrate/20191105210059_create_customers.rb b/db/migrate/20191105210059_create_customers.rb new file mode 100644 index 000000000..d05cb792c --- /dev/null +++ b/db/migrate/20191105210059_create_customers.rb @@ -0,0 +1,15 @@ +class CreateCustomers < ActiveRecord::Migration[5.2] + def change + create_table :customers do |t| + t.string :name + t.string :registered_at + t.string :address + t.string :city + t.string :state + t.string :postal_code + t.string :phone + + t.timestamps + end + end +end diff --git a/db/migrate/20191105210711_create_rentals.rb b/db/migrate/20191105210711_create_rentals.rb new file mode 100644 index 000000000..e41682d46 --- /dev/null +++ b/db/migrate/20191105210711_create_rentals.rb @@ -0,0 +1,13 @@ +class CreateRentals < ActiveRecord::Migration[5.2] + def change + create_table :rentals do |t| + t.datetime :checkout_date + t.datetime :due_date + t.boolean :returned + t.belongs_to :movie + t.belongs_to :customer + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 2611543b3..af369d0f7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,9 +10,42 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 0) do +ActiveRecord::Schema.define(version: 2019_11_05_210711) 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 "registered_at" + t.string "address" + t.string "city" + t.string "state" + t.string "postal_code" + t.string "phone" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "movies", force: :cascade do |t| + t.string "title" + t.string "overview" + t.string "release_date" + t.integer "inventory" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "rentals", force: :cascade do |t| + t.datetime "checkout_date" + t.datetime "due_date" + t.boolean "returned" + t.bigint "movie_id" + t.bigint "customer_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["customer_id"], name: "index_rentals_on_customer_id" + t.index ["movie_id"], name: "index_rentals_on_movie_id" + end + end diff --git a/test/controllers/customers_controller_test.rb b/test/controllers/customers_controller_test.rb new file mode 100644 index 000000000..61881e925 --- /dev/null +++ b/test/controllers/customers_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +describe CustomersController do + # it "does a thing" do + # value(1+1).must_equal 2 + # end +end diff --git a/test/controllers/movies_controller_test.rb b/test/controllers/movies_controller_test.rb new file mode 100644 index 000000000..b11d50cea --- /dev/null +++ b/test/controllers/movies_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +describe MoviesController do + # it "does a thing" do + # value(1+1).must_equal 2 + # end +end diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb new file mode 100644 index 000000000..dc043d4df --- /dev/null +++ b/test/controllers/rentals_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +describe RentalsController do + # it "does a thing" do + # value(1+1).must_equal 2 + # end +end diff --git a/test/models/customer_test.rb b/test/models/customer_test.rb new file mode 100644 index 000000000..30d5c5309 --- /dev/null +++ b/test/models/customer_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +describe Customer do + # it "does a thing" do + # value(1+1).must_equal 2 + # end +end diff --git a/test/models/movie_test.rb b/test/models/movie_test.rb new file mode 100644 index 000000000..cd59ab14a --- /dev/null +++ b/test/models/movie_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +describe Movie do + # it "does a thing" do + # value(1+1).must_equal 2 + # end +end diff --git a/test/models/rental_test.rb b/test/models/rental_test.rb new file mode 100644 index 000000000..3e64959b4 --- /dev/null +++ b/test/models/rental_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +describe Rental do + # it "does a thing" do + # value(1+1).must_equal 2 + # end +end From 75be08b0469ae28a5a432a4a81cf82aeb64aad46 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Tue, 5 Nov 2019 13:13:58 -0800 Subject: [PATCH 03/86] fixed model typo, seeded successfully : --- app/models/customer.rb | 2 +- app/models/movie.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/customer.rb b/app/models/customer.rb index 854c5131e..666f87ef6 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -1,3 +1,3 @@ class Customer < ApplicationRecord - has_many :movies, through :rentals + has_many :movies, through: :rentals end diff --git a/app/models/movie.rb b/app/models/movie.rb index 675e8b056..7f51a6afd 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -1,3 +1,3 @@ class Movie < ApplicationRecord - has_many :customers, through :rentals + has_many :customers, through: :rentals end From 1e8089709b9275af44ca5951da152cb1b11009b3 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Tue, 5 Nov 2019 13:16:21 -0800 Subject: [PATCH 04/86] fixed more typos, rental associations tested in rails console --- app/models/customer.rb | 1 + app/models/movie.rb | 1 + 2 files changed, 2 insertions(+) diff --git a/app/models/customer.rb b/app/models/customer.rb index 666f87ef6..1d05ffeba 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -1,3 +1,4 @@ class Customer < ApplicationRecord + has_many :rentals has_many :movies, through: :rentals end diff --git a/app/models/movie.rb b/app/models/movie.rb index 7f51a6afd..6237ce84b 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -1,3 +1,4 @@ class Movie < ApplicationRecord + has_many :rentals has_many :customers, through: :rentals end From 82ec61b8fc625d293f4991316b922f8b43784e18 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Tue, 5 Nov 2019 13:22:43 -0800 Subject: [PATCH 05/86] fixed datatypes into datetime --- .../20191105211728_change_datatype_to_datetime.rb | 11 +++++++++++ db/schema.rb | 6 +++--- 2 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20191105211728_change_datatype_to_datetime.rb diff --git a/db/migrate/20191105211728_change_datatype_to_datetime.rb b/db/migrate/20191105211728_change_datatype_to_datetime.rb new file mode 100644 index 000000000..38aacdd5d --- /dev/null +++ b/db/migrate/20191105211728_change_datatype_to_datetime.rb @@ -0,0 +1,11 @@ +class ChangeDatatypeToDatetime < ActiveRecord::Migration[5.2] + def change + + remove_column :customers, :registered_at + remove_column :movies, :release_date + + add_column :customers, :registered_at, :datetime + add_column :movies, :release_date, :datetime + end +end + diff --git a/db/schema.rb b/db/schema.rb index af369d0f7..a3a7489cd 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,14 +10,13 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_11_05_210711) do +ActiveRecord::Schema.define(version: 2019_11_05_211728) 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 "registered_at" t.string "address" t.string "city" t.string "state" @@ -25,15 +24,16 @@ t.string "phone" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.datetime "registered_at" end create_table "movies", force: :cascade do |t| t.string "title" t.string "overview" - t.string "release_date" t.integer "inventory" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.datetime "release_date" end create_table "rentals", force: :cascade do |t| From d114e01d413400fa2a0ed6bb9b915f4b93161a10 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Tue, 5 Nov 2019 13:51:51 -0800 Subject: [PATCH 06/86] release_date from movies db supposed to be date and not datetime. fixed --- app/controllers/application_controller.rb | 5 +++++ config/routes.rb | 2 ++ db/migrate/20191105211728_change_datatype_to_datetime.rb | 2 +- db/schema.rb | 2 +- test/fixtures/movies.yml | 5 +++++ test/models/customer_test.rb | 8 +++++--- 6 files changed, 19 insertions(+), 5 deletions(-) create mode 100644 test/fixtures/movies.yml diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 4ac8823b0..4bed8b9db 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,2 +1,7 @@ class ApplicationController < ActionController::API + + def zomg + render json: { msgh: "yassss" }, status: :ok + end + end diff --git a/config/routes.rb b/config/routes.rb index 787824f88..424333cc6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,5 @@ Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html + get '/zomg', to: "application#zomg" + end diff --git a/db/migrate/20191105211728_change_datatype_to_datetime.rb b/db/migrate/20191105211728_change_datatype_to_datetime.rb index 38aacdd5d..9a9803432 100644 --- a/db/migrate/20191105211728_change_datatype_to_datetime.rb +++ b/db/migrate/20191105211728_change_datatype_to_datetime.rb @@ -5,7 +5,7 @@ def change remove_column :movies, :release_date add_column :customers, :registered_at, :datetime - add_column :movies, :release_date, :datetime + add_column :movies, :release_date, :date end end diff --git a/db/schema.rb b/db/schema.rb index a3a7489cd..0f7bd1ecc 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -33,7 +33,7 @@ t.integer "inventory" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.datetime "release_date" + t.date "release_date" end create_table "rentals", force: :cascade do |t| diff --git a/test/fixtures/movies.yml b/test/fixtures/movies.yml new file mode 100644 index 000000000..d78810ed7 --- /dev/null +++ b/test/fixtures/movies.yml @@ -0,0 +1,5 @@ +m1: + title: the shining + overview: scary movie about a hotel + integer: 5 + release_date: 1972-10-31 \ No newline at end of file diff --git a/test/models/customer_test.rb b/test/models/customer_test.rb index 30d5c5309..dec7e4297 100644 --- a/test/models/customer_test.rb +++ b/test/models/customer_test.rb @@ -1,7 +1,9 @@ require "test_helper" describe Customer do - # it "does a thing" do - # value(1+1).must_equal 2 - # end + it "does a thing" do + m1 = movies(:m1) + expect(Movie.count).must_equal 1 + p Movie.first + end end From 858f4f823f8c7f2782b60bae06331135eb4e9627 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Tue, 5 Nov 2019 14:32:13 -0800 Subject: [PATCH 07/86] added model relation tests for movies --- Gemfile.lock | 8 ++++++ test/fixtures/customers.yml | 17 ++++++++++++ test/fixtures/movies.yml | 10 ++++++-- test/fixtures/rentals.yml | 14 ++++++++++ test/models/customer_test.rb | 42 +++++++++++++++++++++++++++--- test/models/movie_test.rb | 50 +++++++++++++++++++++++++++++++++--- test/models/rental_test.rb | 42 +++++++++++++++++++++++++++--- test/test_helper.rb | 8 +++++- 8 files changed, 178 insertions(+), 13 deletions(-) create mode 100644 test/fixtures/customers.yml create mode 100644 test/fixtures/rentals.yml diff --git a/Gemfile.lock b/Gemfile.lock index 981f08e8a..82f09ef4c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -54,12 +54,14 @@ GEM debase (0.2.4.1) debase-ruby_core_source (>= 0.10.2) debase-ruby_core_source (0.10.6) + docile (1.3.2) erubi (1.9.0) ffi (1.11.1) globalid (0.4.2) activesupport (>= 4.2.0) i18n (1.7.0) concurrent-ruby (~> 1.0) + json (2.2.0) listen (3.1.5) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) @@ -130,6 +132,11 @@ GEM rake (>= 0.8.1) ruby-progressbar (1.10.1) ruby_dep (1.5.0) + simplecov (0.17.1) + docile (~> 1.1) + json (>= 1.8, < 3) + simplecov-html (~> 0.10.0) + simplecov-html (0.10.2) spring (2.1.0) spring-watcher-listen (2.0.1) listen (>= 2.7, < 4.0) @@ -164,6 +171,7 @@ DEPENDENCIES puma (~> 3.11) rails (~> 5.2.3) ruby-debug-ide (>= 0.7.0) + simplecov spring spring-watcher-listen (~> 2.0.0) tzinfo-data diff --git a/test/fixtures/customers.yml b/test/fixtures/customers.yml new file mode 100644 index 000000000..7dd0b2245 --- /dev/null +++ b/test/fixtures/customers.yml @@ -0,0 +1,17 @@ +c1: + name: bart simpson + address: 123 fake st + city: springfield + state: IL + postal_code: 12345 + phone: 1-800-whatevr + registered_at: 2019-01-01 + +c2: + name: ned flanders + address: 124 fake st + city: springfield + state: IL + postal_code: 12345 + phone: 1-800-diddly + registered_at: 2018-12-25 \ No newline at end of file diff --git a/test/fixtures/movies.yml b/test/fixtures/movies.yml index d78810ed7..12c57d1bc 100644 --- a/test/fixtures/movies.yml +++ b/test/fixtures/movies.yml @@ -1,5 +1,11 @@ m1: title: the shining overview: scary movie about a hotel - integer: 5 - release_date: 1972-10-31 \ No newline at end of file + inventory: 5 + release_date: 1972-10-31 + +m2: + title: a burns for all seasons + overview: mr. burns' propaganda film + inventory: 3 + release_date: 1990-01-01 \ No newline at end of file diff --git a/test/fixtures/rentals.yml b/test/fixtures/rentals.yml new file mode 100644 index 000000000..81004179e --- /dev/null +++ b/test/fixtures/rentals.yml @@ -0,0 +1,14 @@ +r1: + customer: c1 + movie: m1 + returned: true + checkout_date: 2019-10-03 + due_date: 2019-10-10 + +r2: + customer: c2 + movie: m1 + returned: false + checkout_date: 2019-11-01 + due_date: 2019-11-08 + diff --git a/test/models/customer_test.rb b/test/models/customer_test.rb index dec7e4297..89c224f63 100644 --- a/test/models/customer_test.rb +++ b/test/models/customer_test.rb @@ -1,9 +1,43 @@ require "test_helper" describe Customer do - it "does a thing" do - m1 = movies(:m1) - expect(Movie.count).must_equal 1 - p Movie.first + let (:m1) { movies(:m1) } + let (:c1) { customers(:c1) } + let (:r1) { rentals(:r1) } + + describe "RELATION" do + describe "has many rentals" do + it "nominal" do + end + + it "edge" do + end + end + + describe "has many movies, thru rentals" do + it "nominal" do + end + + it "edge" do + end + end + end + + describe "VALIDATION" do + ### NONE SO FAR + it "nominal" do + end + + it "edge" do + end + end + + describe "CUSTOM METHODS" do + ### NONE SO FAR + it "nominal" do + end + + it "edge" do + end end end diff --git a/test/models/movie_test.rb b/test/models/movie_test.rb index cd59ab14a..73a828c31 100644 --- a/test/models/movie_test.rb +++ b/test/models/movie_test.rb @@ -1,7 +1,51 @@ require "test_helper" describe Movie do - # it "does a thing" do - # value(1+1).must_equal 2 - # end + let (:m1) { movies(:m1) } + let (:m2) { movies(:m2) } + let (:c1) { customers(:c1) } + let (:c2) { customers(:c2) } + let (:r1) { rentals(:r1) } + let (:r2) { rentals(:r2) } + + describe "RELATION" do + describe "has many rentals" do + it "can get correct rentals count for valid movie" do + expect(m1.rentals.count).must_equal 2 + end + + it "if no rentals, return 0 rental count" do + expect(m2.rentals.count).must_equal 0 + end + end + + describe "has many customers, thru rentals" do + it "can get correct customers count for valid movie" do + expect(m1.customers.count).must_equal 2 + end + + it "if no rentals, return 0 customer count" do + expect(m2.rentals.count).must_equal 0 + end + end + end + + describe "VALIDATION" do + ### NONE SO FAR + it "nominal" do + end + + it "edge" do + end + end + + describe "CUSTOM METHODS" do + ### NONE SO FAR + it "nominal" do + end + + it "edge" do + end + end + end diff --git a/test/models/rental_test.rb b/test/models/rental_test.rb index 3e64959b4..a75d4206d 100644 --- a/test/models/rental_test.rb +++ b/test/models/rental_test.rb @@ -1,7 +1,43 @@ require "test_helper" describe Rental do - # it "does a thing" do - # value(1+1).must_equal 2 - # end + let (:m1) { movies(:m1) } + let (:c1) { customers(:c1) } + let (:r1) { rentals(:r1) } + + describe "RELATION" do + describe "has many movies" do + it "nominal" do + end + + it "edge" do + end + end + + describe "has many customers" do + it "nominal" do + end + + it "edge" do + end + end + end + + describe "VALIDATION" do + ### NONE SO FAR + it "nominal" do + end + + it "edge" do + end + end + + describe "CUSTOM METHODS" do + ### NONE SO FAR + it "nominal" do + end + + it "edge" do + end + end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 3ab84e3d1..830ce1575 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -2,9 +2,15 @@ require_relative '../config/environment' require 'rails/test_help' +# Adding from notes +require 'minitest/rails' +require 'minitest/autorun' +require 'minitest/reporters' +require "pry" + class ActiveSupport::TestCase # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. fixtures :all - + # Add more helper methods to be used by all tests here... end From 4d4d22eb998c4923074fb0c47b20326227bd6a0c Mon Sep 17 00:00:00 2001 From: stupendousC Date: Tue, 5 Nov 2019 14:34:53 -0800 Subject: [PATCH 08/86] working on custoemr test --- test/models/customer_test.rb | 9 +++++++-- test/models/rental_test.rb | 3 +++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/test/models/customer_test.rb b/test/models/customer_test.rb index 89c224f63..859e70d83 100644 --- a/test/models/customer_test.rb +++ b/test/models/customer_test.rb @@ -2,15 +2,20 @@ describe Customer do let (:m1) { movies(:m1) } + let (:m2) { movies(:m2) } let (:c1) { customers(:c1) } + let (:c2) { customers(:c2) } let (:r1) { rentals(:r1) } + let (:r2) { rentals(:r2) } describe "RELATION" do describe "has many rentals" do - it "nominal" do + it "can get correct rentals count for valid customer" do + expect(c1.rentals.count).must_equal ??? end - it "edge" do + it "if no rentals, return 0 rental count" do + expect(c2.rentals.count).must_equal ??? end end diff --git a/test/models/rental_test.rb b/test/models/rental_test.rb index a75d4206d..233848d05 100644 --- a/test/models/rental_test.rb +++ b/test/models/rental_test.rb @@ -2,8 +2,11 @@ describe Rental do let (:m1) { movies(:m1) } + let (:m2) { movies(:m2) } let (:c1) { customers(:c1) } + let (:c2) { customers(:c2) } let (:r1) { rentals(:r1) } + let (:r2) { rentals(:r2) } describe "RELATION" do describe "has many movies" do From 0d722ae480ae8f76123fcbbedeba5469d0d14ef8 Mon Sep 17 00:00:00 2001 From: Emily Vomacka Date: Tue, 5 Nov 2019 14:59:16 -0800 Subject: [PATCH 09/86] added validations --- app/models/customer.rb | 3 +++ app/models/movie.rb | 3 +++ db/schema.rb | 2 +- test/fixtures/customers.yml | 9 +++++++++ test/models/customer_test.rb | 5 +++-- 5 files changed, 19 insertions(+), 3 deletions(-) diff --git a/app/models/customer.rb b/app/models/customer.rb index 1d05ffeba..0680fe237 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -1,4 +1,7 @@ class Customer < ApplicationRecord has_many :rentals has_many :movies, through: :rentals + + validates :name, presence: :true + validates :phone, presence: :true, uniqueness: :true end diff --git a/app/models/movie.rb b/app/models/movie.rb index 6237ce84b..f1130ec77 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -1,4 +1,7 @@ class Movie < ApplicationRecord has_many :rentals has_many :customers, through: :rentals + + validates :title, presence: :true + validates :inventory, presence: :true, numericality: { only_integer: true, greater_than: -1 } end diff --git a/db/schema.rb b/db/schema.rb index 0f7bd1ecc..a3a7489cd 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -33,7 +33,7 @@ t.integer "inventory" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.date "release_date" + t.datetime "release_date" end create_table "rentals", force: :cascade do |t| diff --git a/test/fixtures/customers.yml b/test/fixtures/customers.yml index 7dd0b2245..a4d44db89 100644 --- a/test/fixtures/customers.yml +++ b/test/fixtures/customers.yml @@ -14,4 +14,13 @@ c2: state: IL postal_code: 12345 phone: 1-800-diddly + registered_at: 2018-12-25 + +c3: + name: ted flanders + address: 124 real st + city: springfield + state: IL + postal_code: 12345 + phone: 1-800-diddly registered_at: 2018-12-25 \ No newline at end of file diff --git a/test/models/customer_test.rb b/test/models/customer_test.rb index 859e70d83..cb67afc92 100644 --- a/test/models/customer_test.rb +++ b/test/models/customer_test.rb @@ -7,15 +7,16 @@ let (:c2) { customers(:c2) } let (:r1) { rentals(:r1) } let (:r2) { rentals(:r2) } + let (:c3) { customers(:c3) } describe "RELATION" do describe "has many rentals" do it "can get correct rentals count for valid customer" do - expect(c1.rentals.count).must_equal ??? + expect(c1.rentals.count).must_equal 1 end it "if no rentals, return 0 rental count" do - expect(c2.rentals.count).must_equal ??? + expect(c3.rentals.count).must_equal 0 end end From 0fd2d263289c3fbb329c22115d87aacc68205aff Mon Sep 17 00:00:00 2001 From: Emily Vomacka Date: Tue, 5 Nov 2019 15:09:35 -0800 Subject: [PATCH 10/86] actually adding tests --- test/models/movie_test.rb | 38 ++++++++++++++++++++++++++------------ test/models/rental_test.rb | 36 ++++++++++++++++++------------------ 2 files changed, 44 insertions(+), 30 deletions(-) diff --git a/test/models/movie_test.rb b/test/models/movie_test.rb index 73a828c31..ae099f905 100644 --- a/test/models/movie_test.rb +++ b/test/models/movie_test.rb @@ -32,20 +32,34 @@ describe "VALIDATION" do ### NONE SO FAR - it "nominal" do + describe "nominal" do + it "creates a movie when all required fields are present and correct" do + expect{Movie.create(title: "test", inventory: 3)}.must_change "Movie.count", 1 + end + + describe "edge" do + it "won't create a movie without a title" do + expect{Movie.create(title: nil, inventory: 3)}.wont_change "Movie.count" + end + + it "won't create a movie without an inventory" do + expect{Movie.create(title: "test", inventory: nil)}.wont_change "Movie.count" + end + + it "won't create movie with negative inventory" do + expect{Movie.create(title: "test", inventory: -25)}.wont_change "Movie.count" + end + end end - it "edge" do - end - end - - describe "CUSTOM METHODS" do - ### NONE SO FAR - it "nominal" do + describe "CUSTOM METHODS" do + ### NONE SO FAR + it "nominal" do + end + + it "edge" do + end end - it "edge" do - end end - -end +end \ No newline at end of file diff --git a/test/models/rental_test.rb b/test/models/rental_test.rb index 233848d05..4d64b8d0a 100644 --- a/test/models/rental_test.rb +++ b/test/models/rental_test.rb @@ -16,31 +16,31 @@ it "edge" do end end - - describe "has many customers" do - it "nominal" do - end - - it "edge" do - end - end - end - describe "VALIDATION" do - ### NONE SO FAR + describe "has many customers" do it "nominal" do end it "edge" do end end +end + +describe "VALIDATION" do + ### NONE SO FAR + it "nominal" do + end - describe "CUSTOM METHODS" do - ### NONE SO FAR - it "nominal" do - end - - it "edge" do - end + it "edge" do end end + +describe "CUSTOM METHODS" do + ### NONE SO FAR + it "nominal" do + end + + it "edge" do + end +end +end From 52a546d20beb2867e645f10b8bc35dfbfbddf899 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Tue, 5 Nov 2019 15:14:05 -0800 Subject: [PATCH 11/86] added routes --- app/controllers/application_controller.rb | 5 ----- config/routes.rb | 4 +++- test/models/movie_test.rb | 5 ++--- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 4bed8b9db..4ac8823b0 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,7 +1,2 @@ class ApplicationController < ActionController::API - - def zomg - render json: { msgh: "yassss" }, status: :ok - end - end diff --git a/config/routes.rb b/config/routes.rb index 424333cc6..5d56a6cd6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,7 @@ Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html - get '/zomg', to: "application#zomg" + + resources :movies, only: [:index, :show, :create] + resources :customers, only: [:index] end diff --git a/test/models/movie_test.rb b/test/models/movie_test.rb index ae099f905..a104ff850 100644 --- a/test/models/movie_test.rb +++ b/test/models/movie_test.rb @@ -31,7 +31,6 @@ end describe "VALIDATION" do - ### NONE SO FAR describe "nominal" do it "creates a movie when all required fields are present and correct" do expect{Movie.create(title: "test", inventory: 3)}.must_change "Movie.count", 1 @@ -41,11 +40,11 @@ it "won't create a movie without a title" do expect{Movie.create(title: nil, inventory: 3)}.wont_change "Movie.count" end - + it "won't create a movie without an inventory" do expect{Movie.create(title: "test", inventory: nil)}.wont_change "Movie.count" end - + it "won't create movie with negative inventory" do expect{Movie.create(title: "test", inventory: -25)}.wont_change "Movie.count" end From 6625079da25e1355a8c63e3bfe8001b7a1a68432 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Tue, 5 Nov 2019 15:30:16 -0800 Subject: [PATCH 12/86] added avail_inventory to movies database --- app/controllers/movies_controller.rb | 22 +++++++++++++++++++ ...105232104_add_avail_inventory_to_movies.rb | 5 +++++ db/schema.rb | 5 +++-- 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20191105232104_add_avail_inventory_to_movies.rb diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 6c4c51614..e0a691c3f 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -1,2 +1,24 @@ class MoviesController < ApplicationController + + def index + movies = Movie.all + + render json: movies.as_json( only: [:id, :title, :release_date]), status: :ok + end + + def show + movie = Movie.find_by(id: params[:id]) + + if movie + render json: movie.as_json( only: [:id, :title, :overview, :release_date, :inventory, :avail_inventory]), status: :ok + else + render json: { errors: "Movie not found" }, status: :ok + + end + end + + def create + + end + end diff --git a/db/migrate/20191105232104_add_avail_inventory_to_movies.rb b/db/migrate/20191105232104_add_avail_inventory_to_movies.rb new file mode 100644 index 000000000..e6f1037bf --- /dev/null +++ b/db/migrate/20191105232104_add_avail_inventory_to_movies.rb @@ -0,0 +1,5 @@ +class AddAvailInventoryToMovies < ActiveRecord::Migration[5.2] + def change + add_column :movies, :avail_inventory, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index a3a7489cd..5891ab88c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_11_05_211728) do +ActiveRecord::Schema.define(version: 2019_11_05_232104) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -33,7 +33,8 @@ t.integer "inventory" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.datetime "release_date" + t.date "release_date" + t.integer "avail_inventory" end create_table "rentals", force: :cascade do |t| From ab4deafde8097fc253ffd8dd707bd56a46824c40 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Tue, 5 Nov 2019 16:08:37 -0800 Subject: [PATCH 13/86] movies#create done --- app/controllers/movies_controller.rb | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index e0a691c3f..c28b063cc 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -1,5 +1,5 @@ class MoviesController < ApplicationController - + def index movies = Movie.all @@ -13,12 +13,24 @@ def show render json: movie.as_json( only: [:id, :title, :overview, :release_date, :inventory, :avail_inventory]), status: :ok else render json: { errors: "Movie not found" }, status: :ok - end end def create - + movie = Movie.new(new_params) + movie.avail_inventory = movie.inventory + + if movie.save + render json: { msg: "Movie #{movie.title.capitalize} added to database"}, status: :accepted + else + render json: { errors: "Cannot add movie", error_msgs: movie.errors.full_messages }, status: :bad_request + end + end + + private + + def new_params + return params.require(:movie).permit(:title, :overview, :release_date, :inventory) end end From b5a316f56a2b05b58ef16eeaa2bc219247361426 Mon Sep 17 00:00:00 2001 From: Emily Vomacka Date: Tue, 5 Nov 2019 16:08:44 -0800 Subject: [PATCH 14/86] customers index --- app/controllers/customers_controller.rb | 7 +++++++ app/models/customer.rb | 4 ++++ config/routes.rb | 2 -- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index ca3b6e024..f82bebd89 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -1,2 +1,9 @@ class CustomersController < ApplicationController +CUSTOMER_KEYS = [:id, :name, :registered_at, :postal_code, :phone] +#return to :movies_checked_out_count + + def index + customers = Customer.all + render json: customers.as_json( only: CUSTOMER_KEYS), status: :ok + end end diff --git a/app/models/customer.rb b/app/models/customer.rb index 0680fe237..f2fcb1247 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -4,4 +4,8 @@ class Customer < ApplicationRecord validates :name, presence: :true validates :phone, presence: :true, uniqueness: :true + + def movies_checked_out_count + return self.movies.count + end end diff --git a/config/routes.rb b/config/routes.rb index 5d56a6cd6..e67180710 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,5 @@ Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html - resources :movies, only: [:index, :show, :create] resources :customers, only: [:index] - end From 29f92044a5d8ed7642a87920d01700157398be2c Mon Sep 17 00:00:00 2001 From: Emily Vomacka Date: Tue, 5 Nov 2019 16:14:57 -0800 Subject: [PATCH 15/86] edited seed file --- db/seeds.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/db/seeds.rb b/db/seeds.rb index 5322340ba..0a7e25f67 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -3,5 +3,7 @@ end JSON.parse(File.read('db/seeds/movies.json')).each do |movie| - Movie.create!(movie) + new_movie = Movie.new(movie) + new_movie.avail_inventory = new_movie.inventory + new_movie.save! end From bcab12b75995c5793d34bd88d9f7afe71dca8546 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Tue, 5 Nov 2019 16:32:12 -0800 Subject: [PATCH 16/86] fixed available_inventory column name --- app/controllers/movies_controller.rb | 4 ++-- db/migrate/20191105232104_add_avail_inventory_to_movies.rb | 2 +- db/schema.rb | 2 +- db/seeds.rb | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index c28b063cc..7962554cd 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -10,7 +10,7 @@ def show movie = Movie.find_by(id: params[:id]) if movie - render json: movie.as_json( only: [:id, :title, :overview, :release_date, :inventory, :avail_inventory]), status: :ok + render json: movie.as_json( only: [:id, :title, :overview, :release_date, :inventory, :available_inventory]), status: :ok else render json: { errors: "Movie not found" }, status: :ok end @@ -18,7 +18,7 @@ def show def create movie = Movie.new(new_params) - movie.avail_inventory = movie.inventory + movie.available_inventory = movie.inventory if movie.save render json: { msg: "Movie #{movie.title.capitalize} added to database"}, status: :accepted diff --git a/db/migrate/20191105232104_add_avail_inventory_to_movies.rb b/db/migrate/20191105232104_add_avail_inventory_to_movies.rb index e6f1037bf..2f9895ef5 100644 --- a/db/migrate/20191105232104_add_avail_inventory_to_movies.rb +++ b/db/migrate/20191105232104_add_avail_inventory_to_movies.rb @@ -1,5 +1,5 @@ class AddAvailInventoryToMovies < ActiveRecord::Migration[5.2] def change - add_column :movies, :avail_inventory, :integer + add_column :movies, :available_inventory, :integer end end diff --git a/db/schema.rb b/db/schema.rb index 5891ab88c..6dfd1c5b8 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -34,7 +34,7 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.date "release_date" - t.integer "avail_inventory" + t.integer "available_inventory" end create_table "rentals", force: :cascade do |t| diff --git a/db/seeds.rb b/db/seeds.rb index 0a7e25f67..74ce791be 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -4,6 +4,6 @@ JSON.parse(File.read('db/seeds/movies.json')).each do |movie| new_movie = Movie.new(movie) - new_movie.avail_inventory = new_movie.inventory + new_movie.available_inventory = new_movie.inventory new_movie.save! end From 4dbafcab838cb3f0289f1d5dfaa5a6a85a290387 Mon Sep 17 00:00:00 2001 From: Emily Vomacka Date: Tue, 5 Nov 2019 16:33:14 -0800 Subject: [PATCH 17/86] edited movie show status code --- app/controllers/movies_controller.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index c28b063cc..682c13740 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -21,10 +21,11 @@ def create movie.avail_inventory = movie.inventory if movie.save - render json: { msg: "Movie #{movie.title.capitalize} added to database"}, status: :accepted + render json: { msg: "Movie #{movie.title.capitalize} added to database", id: "#{movie.id}" }, status: :ok else render json: { errors: "Cannot add movie", error_msgs: movie.errors.full_messages }, status: :bad_request end + return movie.id end private From 422614e459ed3acbd381b359b51e9ae6a1238016 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Tue, 5 Nov 2019 19:18:58 -0800 Subject: [PATCH 18/86] done w/ rental model relation tests --- app/controllers/movies_controller.rb | 2 +- test/models/rental_test.rb | 55 ++++++++++++++++------------ 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 7962554cd..c116ca407 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -30,7 +30,7 @@ def create private def new_params - return params.require(:movie).permit(:title, :overview, :release_date, :inventory) + return params.require(:movie).permit(:id, :title, :overview, :release_date, :inventory) end end diff --git a/test/models/rental_test.rb b/test/models/rental_test.rb index 4d64b8d0a..4f8108cb6 100644 --- a/test/models/rental_test.rb +++ b/test/models/rental_test.rb @@ -9,38 +9,45 @@ let (:r2) { rentals(:r2) } describe "RELATION" do - describe "has many movies" do - it "nominal" do - end + it "valid Rental instance belongs to 1 movie" do + expect(r1.movie).must_equal m1 - it "edge" do - end + # no such thing as r1.movieS + expect{r1.movies}.must_raise NoMethodError end - - describe "has many customers" do - it "nominal" do + + it "valid Rental instance belongs to 1 customer" do + expect(r1.customer).must_equal c1 + + # no such thing as r1.customerS + expect{r1.customers}.must_raise NoMethodError end - it "edge" do + it "edge cases?" do + # if invalid customer or movie given, Rental instance won't even add to database successfully + # therefore no edge case to test re: movie or customer associations + assert(true) end end -end - -describe "VALIDATION" do - ### NONE SO FAR - it "nominal" do - end - it "edge" do - end -end - -describe "CUSTOM METHODS" do - ### NONE SO FAR - it "nominal" do + describe "VALIDATION" do + it "can add new Rental instance, if given valid customer & movie instances" do + + end + + it "won't add new Rental instance if given bad customer instance" do + end + + it "won't add new Rental instance if given bad movie instance" do + end end - it "edge" do + describe "CUSTOM METHODS" do + ### NONE SO FAR + it "nominal" do + end + + it "edge" do + end end end -end From 80da4c3971dc35144f53a934acdd1f69018b84dd Mon Sep 17 00:00:00 2001 From: stupendousC Date: Tue, 5 Nov 2019 19:24:53 -0800 Subject: [PATCH 19/86] rental model validations tests done --- test/models/rental_test.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/test/models/rental_test.rb b/test/models/rental_test.rb index 4f8108cb6..789a9d01c 100644 --- a/test/models/rental_test.rb +++ b/test/models/rental_test.rb @@ -32,13 +32,19 @@ describe "VALIDATION" do it "can add new Rental instance, if given valid customer & movie instances" do - + expect{Rental.create(customer: Customer.first, movie: Movie.last)}.must_change "Rental.count", 1 end it "won't add new Rental instance if given bad customer instance" do + before_count = Rental.count + expect{Rental.create(customer: "garbage", movie: Movie.last)}.must_raise ActiveRecord::AssociationTypeMismatch + expect(Rental.count).must_equal before_count end it "won't add new Rental instance if given bad movie instance" do + before_count = Rental.count + expect{Rental.create(customer: Customer.first, movie: "garbage")}.must_raise ActiveRecord::AssociationTypeMismatch + expect(Rental.count).must_equal before_count end end From a2e8cb3d55342e00efd80cdf51459edc53076e93 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Tue, 5 Nov 2019 19:37:34 -0800 Subject: [PATCH 20/86] refined testing codes for movie model relations, to be safe from future fixture additions --- test/models/movie_test.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/test/models/movie_test.rb b/test/models/movie_test.rb index a104ff850..0ac14261f 100644 --- a/test/models/movie_test.rb +++ b/test/models/movie_test.rb @@ -7,25 +7,27 @@ let (:c2) { customers(:c2) } let (:r1) { rentals(:r1) } let (:r2) { rentals(:r2) } + let (:actual_m1_rentals) { rentals.select { |r| r.movie == m1 } } + let (:actual_m2_rentals) { rentals.select { |r| r.movie == m2 } } describe "RELATION" do describe "has many rentals" do it "can get correct rentals count for valid movie" do - expect(m1.rentals.count).must_equal 2 + expect(m1.rentals.count).must_equal actual_m1_rentals.count end it "if no rentals, return 0 rental count" do - expect(m2.rentals.count).must_equal 0 + expect(m2.rentals.count).must_equal actual_m2_rentals.count end end describe "has many customers, thru rentals" do it "can get correct customers count for valid movie" do - expect(m1.customers.count).must_equal 2 + expect(m1.customers.count).must_equal actual_m1_rentals.count end it "if no rentals, return 0 customer count" do - expect(m2.rentals.count).must_equal 0 + expect(m2.customers.count).must_equal actual_m2_rentals.count end end end From 716750b755f73c30825fb9a94f7876a1ceead915 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Tue, 5 Nov 2019 20:31:50 -0800 Subject: [PATCH 21/86] movies_ctrller test codes skeleton --- test/controllers/movies_controller_test.rb | 35 ++++++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/test/controllers/movies_controller_test.rb b/test/controllers/movies_controller_test.rb index b11d50cea..1134accf2 100644 --- a/test/controllers/movies_controller_test.rb +++ b/test/controllers/movies_controller_test.rb @@ -1,7 +1,36 @@ require "test_helper" describe MoviesController do - # it "does a thing" do - # value(1+1).must_equal 2 - # end + describe "INDEX" do + it "basic test w/ no parameters, if applicable" do + end + + it "nominal case: check status and request body" do + end + + it "edge case: check status and request body" do + end + end + + describe "SHOW" do + it "basic test w/ no parameters, if applicable" do + end + + it "nominal case: check status and request body" do + end + + it "edge case: check status and request body" do + end + end + + describe "CREATE" do + it "basic test w/ no parameters, if applicable" do + end + + it "nominal case: check status and request body" do + end + + it "edge case: check status and request body" do + end + end end From 23005295484169a0eb18d2e7edd36ce89d4217fa Mon Sep 17 00:00:00 2001 From: stupendousC Date: Tue, 5 Nov 2019 20:32:36 -0800 Subject: [PATCH 22/86] customers_ctrller test skeleton --- test/controllers/customers_controller_test.rb | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/test/controllers/customers_controller_test.rb b/test/controllers/customers_controller_test.rb index 61881e925..4b93facae 100644 --- a/test/controllers/customers_controller_test.rb +++ b/test/controllers/customers_controller_test.rb @@ -1,7 +1,16 @@ require "test_helper" describe CustomersController do - # it "does a thing" do - # value(1+1).must_equal 2 - # end + + describe "INDEX" do + it "basic test w/ no parameters, if applicable" do + end + + it "nominal case: check status and request body" do + end + + it "edge case: check status and request body" do + end + end + end From e303b5a9ad04bf558e5a8058d9164f76b912c1e5 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Tue, 5 Nov 2019 21:01:24 -0800 Subject: [PATCH 23/86] fixed movie_ctrller codes for postman tests --- app/controllers/movies_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index c116ca407..7b248fcd6 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -21,7 +21,7 @@ def create movie.available_inventory = movie.inventory if movie.save - render json: { msg: "Movie #{movie.title.capitalize} added to database"}, status: :accepted + render json: { id: Movie.last.id, msg: "Movie #{Movie.last.title.capitalize} added to database"}, status: :ok else render json: { errors: "Cannot add movie", error_msgs: movie.errors.full_messages }, status: :bad_request end @@ -30,7 +30,7 @@ def create private def new_params - return params.require(:movie).permit(:id, :title, :overview, :release_date, :inventory) + return params.permit(:id, :title, :overview, :release_date, :inventory) end end From 62a6b8ab60738d5ee3304e4bb52a0c30025a06a4 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Tue, 5 Nov 2019 21:15:50 -0800 Subject: [PATCH 24/86] refined movie new_params strong parameters --- app/controllers/movies_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 7b248fcd6..e268c23fd 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -30,7 +30,7 @@ def create private def new_params - return params.permit(:id, :title, :overview, :release_date, :inventory) + return params.permit(:title, :overview, :release_date, :inventory) end end From 7f4f675f1a5e04afb3e87bc38fea90cc05ba21a7 Mon Sep 17 00:00:00 2001 From: Emily Vomacka Date: Tue, 5 Nov 2019 21:21:56 -0800 Subject: [PATCH 25/86] movies_checked_out_count, all wave 2 tests passing --- app/controllers/movies_controller.rb | 8 ++++---- app/models/movie.rb | 5 +++-- ...106051902_add_movies_checked_out_count_to_customers.rb | 5 +++++ db/schema.rb | 3 ++- 4 files changed, 14 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20191106051902_add_movies_checked_out_count_to_customers.rb diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 8c0d2dd7b..46fa658f4 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -17,11 +17,11 @@ def show end def create - movie = Movie.new(new_params) + movie = Movie.new(movie_params) movie.available_inventory = movie.inventory if movie.save - render json: { msg: "Movie #{movie.title.capitalize} added to database", id: "#{movie.id}" }, status: :ok + render json: { msg: "Movie #{movie.title.capitalize} added to database", id: Movie.last.id }, status: :ok else render json: { errors: "Cannot add movie", error_msgs: movie.errors.full_messages }, status: :bad_request end @@ -30,8 +30,8 @@ def create private - def new_params - return params.require(:movie).permit(:title, :overview, :release_date, :inventory) + def movie_params + return params.permit(:id, :title, :overview, :release_date, :inventory) end end diff --git a/app/models/movie.rb b/app/models/movie.rb index f1130ec77..45553cd42 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -3,5 +3,6 @@ class Movie < ApplicationRecord has_many :customers, through: :rentals validates :title, presence: :true - validates :inventory, presence: :true, numericality: { only_integer: true, greater_than: -1 } -end + validates :inventory, presence: :true + # numericality: { only_integer: true, greater_than: -1 } +end \ No newline at end of file diff --git a/db/migrate/20191106051902_add_movies_checked_out_count_to_customers.rb b/db/migrate/20191106051902_add_movies_checked_out_count_to_customers.rb new file mode 100644 index 000000000..dac80cbe6 --- /dev/null +++ b/db/migrate/20191106051902_add_movies_checked_out_count_to_customers.rb @@ -0,0 +1,5 @@ +class AddMoviesCheckedOutCountToCustomers < ActiveRecord::Migration[5.2] + def change + add_column :customers, :movies_checked_out_count, :integer, default: 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index 6dfd1c5b8..e5f7c85ef 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_11_05_232104) do +ActiveRecord::Schema.define(version: 2019_11_06_051902) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -25,6 +25,7 @@ t.datetime "created_at", null: false t.datetime "updated_at", null: false t.datetime "registered_at" + t.integer "movies_checked_out_count", default: 0 end create_table "movies", force: :cascade do |t| From 50a4bf76f7ccb467541d8f7b17d4bbbfbb6748c5 Mon Sep 17 00:00:00 2001 From: Emily Vomacka Date: Tue, 5 Nov 2019 21:24:39 -0800 Subject: [PATCH 26/86] edited customer index to pass last test, oops --- app/controllers/customers_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index f82bebd89..519d60938 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -1,5 +1,5 @@ class CustomersController < ApplicationController -CUSTOMER_KEYS = [:id, :name, :registered_at, :postal_code, :phone] +CUSTOMER_KEYS = [:id, :name, :registered_at, :postal_code, :phone, :movies_checked_out_count] #return to :movies_checked_out_count def index From ea6c7e9d6050c095ddcacef0bc640bc8882d9eb0 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Tue, 5 Nov 2019 21:48:45 -0800 Subject: [PATCH 27/86] just some ideas on wave4 I dont' want to forget --- Caroline_thoughts.txt | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Caroline_thoughts.txt diff --git a/Caroline_thoughts.txt b/Caroline_thoughts.txt new file mode 100644 index 000000000..3b44c30c4 --- /dev/null +++ b/Caroline_thoughts.txt @@ -0,0 +1,22 @@ +###### THOUGHTS... +customerInstance.movies_checked_out_count + Go through customerInstance.rentals, count all the ones with returned: false + + +GET /rentals/overdue + Option A: go through each customer? find customerInstance.rentals where returned: false && due_date < today + Option B: go through each rental? + I think Option A is best, b/c with optionB we might repeat the iteration for the same customer(s), + whereas with optionA, as soon as that customer gets added to the JSON, we can move on to next customer + +GET /movies/:id/current + I vote for Option A, same reason + +GET /movies/:id/history + Go through movieInstance.rentals, where returned: true (bc IN THE PAST) + +GET /customers/:id/current + Go through customerInstance.rentals, get all the ones with returned: false + +GET /customers/:id/history + Go through customerInstance.rentals, get all the ones with returned: true \ No newline at end of file From 5d117a6f7e4e0364de15ee7ac32d64ba6aad4ece Mon Sep 17 00:00:00 2001 From: Emily Vomacka Date: Tue, 5 Nov 2019 21:55:02 -0800 Subject: [PATCH 28/86] finished customer testing --- app/models/customer.rb | 7 ++++--- app/models/movie.rb | 3 +-- test/models/customer_test.rb | 36 +++++++++++++++++++++++++++--------- test/models/movie_test.rb | 17 +++++++++-------- 4 files changed, 41 insertions(+), 22 deletions(-) diff --git a/app/models/customer.rb b/app/models/customer.rb index f2fcb1247..2f934c767 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -5,7 +5,8 @@ class Customer < ApplicationRecord validates :name, presence: :true validates :phone, presence: :true, uniqueness: :true - def movies_checked_out_count - return self.movies.count - end + #I don't think we need this method anymore! + # def movies_checked_out_count + # return self.movies.count + # end end diff --git a/app/models/movie.rb b/app/models/movie.rb index 45553cd42..afa79a251 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -3,6 +3,5 @@ class Movie < ApplicationRecord has_many :customers, through: :rentals validates :title, presence: :true - validates :inventory, presence: :true - # numericality: { only_integer: true, greater_than: -1 } + validates :inventory, presence: :true, numericality: { only_integer: true, greater_than: -1 } end \ No newline at end of file diff --git a/test/models/customer_test.rb b/test/models/customer_test.rb index cb67afc92..74c15bef2 100644 --- a/test/models/customer_test.rb +++ b/test/models/customer_test.rb @@ -20,22 +20,40 @@ end end - describe "has many movies, thru rentals" do - it "nominal" do + describe "has many movies" do + it "has many movies, thru rentals" do + expect(c2.movies.count).must_equal 1 end - it "edge" do + it "responds with an empty array if no movies have been rented" do + expect(c3.movies.count).must_equal 0 end - end + end end describe "VALIDATION" do - ### NONE SO FAR - it "nominal" do - end - it "edge" do - end + ### NONE SO FAR + it "creates a valid new customer with all required fields filled in" do + expect{Customer.create!(name: "test", phone: "333")}.must_change "Customer.count", 1 + + new_cust = Customer.last + expect(new_cust.phone).must_equal "333" + expect(new_cust.name).must_equal "test" + end + + it "fails to create a customer without name" do + expect{Customer.create(name: nil, phone: "333")}.wont_change "Customer.count" + end + + it "fails to create a new customer without phone number" do + expect{Customer.create(name: "test", phone: nil)}.wont_change "Customer.count" + end + + it "fails to create a new customer without unique phone number" do + Customer.create!(name: "test", phone: "333") + expect{Customer.create(name: "hi", phone: "333")}.wont_change "Customer.count" + end end describe "CUSTOM METHODS" do diff --git a/test/models/movie_test.rb b/test/models/movie_test.rb index 0ac14261f..2f23b66fe 100644 --- a/test/models/movie_test.rb +++ b/test/models/movie_test.rb @@ -52,15 +52,16 @@ end end end - - describe "CUSTOM METHODS" do - ### NONE SO FAR - it "nominal" do - end - - it "edge" do - end + end + + describe "CUSTOM METHODS" do + ### NONE SO FAR + it "nominal" do end + it "edge" do + end end + + end \ No newline at end of file From c4a39d830fd9bb60aac973e375a0155459c97f0a Mon Sep 17 00:00:00 2001 From: Emily Vomacka Date: Tue, 5 Nov 2019 22:07:00 -0800 Subject: [PATCH 29/86] final customer test update --- test/models/customer_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/models/customer_test.rb b/test/models/customer_test.rb index 74c15bef2..497bd58ca 100644 --- a/test/models/customer_test.rb +++ b/test/models/customer_test.rb @@ -46,11 +46,11 @@ expect{Customer.create(name: nil, phone: "333")}.wont_change "Customer.count" end - it "fails to create a new customer without phone number" do + it "fails to create a customer without phone number" do expect{Customer.create(name: "test", phone: nil)}.wont_change "Customer.count" end - it "fails to create a new customer without unique phone number" do + it "fails to create a customer without unique phone number" do Customer.create!(name: "test", phone: "333") expect{Customer.create(name: "hi", phone: "333")}.wont_change "Customer.count" end From bf83e488b39eb44ae829e68fba1f3b7734db51a9 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Wed, 6 Nov 2019 14:51:49 -0800 Subject: [PATCH 30/86] rental#check-out works via manual postman testing. now only 3 errors --- app/controllers/customers_controller.rb | 6 ++-- app/controllers/movies_controller.rb | 4 +-- app/controllers/rentals_controller.rb | 44 +++++++++++++++++++++++++ config/routes.rb | 3 ++ 4 files changed, 52 insertions(+), 5 deletions(-) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index 519d60938..45967ee58 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -1,7 +1,7 @@ class CustomersController < ApplicationController -CUSTOMER_KEYS = [:id, :name, :registered_at, :postal_code, :phone, :movies_checked_out_count] -#return to :movies_checked_out_count - + CUSTOMER_KEYS = [:id, :name, :registered_at, :postal_code, :phone, :movies_checked_out_count] + #return to :movies_checked_out_count + def index customers = Customer.all render json: customers.as_json( only: CUSTOMER_KEYS), status: :ok diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 3fb3700ff..46e2acd81 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -30,8 +30,8 @@ def create private - def new_params - return params.permit(:title, :overview, :release_date, :inventory) + def movie_params + return params.require(:movie).permit(:title, :overview, :release_date, :inventory) end end diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 58c72b791..9d8407855 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -1,2 +1,46 @@ class RentalsController < ApplicationController + + def check_out + ### CREATING a new rental record + new_rental = Rental.new(rental_params) + new_rental.checkout_date = Date.today + new_rental.due_date = Date.today + 7.days + new_rental.returned = false + + # does this movie even have available_inventory? + movie = new_rental.movie + if movie.available_inventory <= 0 + render json: { errors: "Cannot make a rental because movie #{movie.title.capitalize} ran out of copies"}, status: :bad_request + return + end + + if new_rental.save + this_rental = Rental.last + # update movie's avail count + movie.update(available_inventory: movie.available_inventory - 1) + + # update custoemr's checked_out_count + customer = this_rental.customer + customer.update(movies_checked_out_count: customer.movies_checked_out_count + 1) + + # prep API JSON + render json: { msg: "Rental id#{this_rental.id}: #{this_rental.movie.title} due on #{this_rental.due_date}"}, status: :ok + else + render json: { errors: "Cannot make a rental", error_msgs: new_rental.errors.full_messages }, status: :bad_request + end + + end + + def check_in + ### Modifying existing rental record flip status to returned: true + # CHRIS SAID... the client wanted a POST, even though we're doing UPDATE here, + # just stick to the client's specs... + + end + + private + + def rental_params + return params.require(:rental).permit(:movie_id, :customer_id) + end end diff --git a/config/routes.rb b/config/routes.rb index e67180710..93787db5a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,4 +2,7 @@ # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html resources :movies, only: [:index, :show, :create] resources :customers, only: [:index] + + post "/rentals/check-out", to: "rentals#check_out", as: "check_out" + post "/rentals/check-in", to: "rentals#check_in", as: "check_in" end From ccbcf8de363e922b8ec3411eb361f7e7b8f22df2 Mon Sep 17 00:00:00 2001 From: Emily Vomacka Date: Wed, 6 Nov 2019 15:39:32 -0800 Subject: [PATCH 31/86] check-in --- app/controllers/movies_controller.rb | 3 +-- app/controllers/rentals_controller.rb | 22 +++++++++++++++++----- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 46e2acd81..ad39bf861 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -31,7 +31,6 @@ def create private def movie_params - return params.require(:movie).permit(:title, :overview, :release_date, :inventory) + return params.permit(:title, :overview, :release_date, :inventory) end - end diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 9d8407855..f4dc92020 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -32,15 +32,27 @@ def check_out end def check_in - ### Modifying existing rental record flip status to returned: true - # CHRIS SAID... the client wanted a POST, even though we're doing UPDATE here, - # just stick to the client's specs... - + rental = Rental.find_by(movie_id: rental_params[:movie_id].to_i, customer_id: rental_params[:customer_id].to_i) + # render json: { msg: "Rental id#{rental.id}: #{rental.movie.title} has been rented by #{rental.customer.name}." }, status: :ok + # return + if rental.nil? + render json: { errors: "Rental doesn't exist" }, status: :bad_request + return + elsif rental.returned == true + render json: { errors: "Rental has already been returned" }, status: :bad_request + return + else + this_customer = rental.customer + this_customer.update(movies_checked_out_count: this_customer.movies_checked_out_count - 1) + this_movie = rental.movie + this_movie.update(available_inventory: this_movie.available_inventory + 1) + render json: { msg: "Rental id#{rental.id}: #{rental.movie.title} has been returned." }, status: :ok + end end private def rental_params - return params.require(:rental).permit(:movie_id, :customer_id) + return params.permit(:movie_id, :customer_id) end end From f5a5e46628dc6967b0acbfec4ab50a6ca8f5152b Mon Sep 17 00:00:00 2001 From: Emily Vomacka Date: Wed, 6 Nov 2019 15:45:01 -0800 Subject: [PATCH 32/86] returned: true on check-in --- app/controllers/rentals_controller.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index f4dc92020..5a655bb6c 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -46,6 +46,7 @@ def check_in this_customer.update(movies_checked_out_count: this_customer.movies_checked_out_count - 1) this_movie = rental.movie this_movie.update(available_inventory: this_movie.available_inventory + 1) + this_movie.update(returned: true) render json: { msg: "Rental id#{rental.id}: #{rental.movie.title} has been returned." }, status: :ok end end From e0a2a026d6e16d8ef269fd87ca6a84f67911033d Mon Sep 17 00:00:00 2001 From: stupendousC Date: Wed, 6 Nov 2019 20:49:31 -0800 Subject: [PATCH 33/86] customer ctrller tests done --- test/controllers/customers_controller_test.rb | 21 +++++++++++++++++-- test/test_helper.rb | 10 +++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/test/controllers/customers_controller_test.rb b/test/controllers/customers_controller_test.rb index 4b93facae..3e43cd1e0 100644 --- a/test/controllers/customers_controller_test.rb +++ b/test/controllers/customers_controller_test.rb @@ -1,15 +1,32 @@ require "test_helper" +CUSTOMER_KEYS = (%w[id name postal_code phone registered_at movies_checked_out_count]).sort + describe CustomersController do describe "INDEX" do - it "basic test w/ no parameters, if applicable" do + it "responds with JSON and success status" do + get customers_path + + check_response(expected_type: Array) end - it "nominal case: check status and request body" do + it "responds with an array of customer hashes" do + get customers_path + + body = JSON.parse(response.body) + + expect(body.count).must_equal Customer.count + + body.each do |customer| + expect(customer).must_be_instance_of Hash + expect(customer.keys.sort).must_equal CUSTOMER_KEYS + end + end it "edge case: check status and request body" do + # None for index end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 830ce1575..6f325f4f8 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -13,4 +13,14 @@ class ActiveSupport::TestCase fixtures :all # Add more helper methods to be used by all tests here... + + def check_response(expected_type:, expected_status: :success) + must_respond_with expected_status + expect(response.header['Content-Type']).must_include 'json' + + body = JSON.parse(response.body) + expect(body).must_be_kind_of expected_type + return body + end + end From faec3a3c476f7cf77dd7cce99f81d523ff94abf3 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Wed, 6 Nov 2019 20:53:39 -0800 Subject: [PATCH 34/86] setting up rentals#check_out skeleton --- test/controllers/rentals_controller_test.rb | 38 +++++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index dc043d4df..25ac9ca63 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -1,7 +1,39 @@ require "test_helper" describe RentalsController do - # it "does a thing" do - # value(1+1).must_equal 2 - # end + describe "CHECK_OUT" do + + describe "successful check_out with valid inputs" do + it "creates Rental instance with correct attribs" do + end + + it "updates movie's available_inventory correctly" do + end + + it "updates customer's movies_checked_out_count correctly" do + end + + it "returns JSON with success status" do + end + + end + + describe "edge" do + it "if movie's available_inventory = 0, return error msg in JSON and bad_request status" do + end + + + end + + end + + describe "CHECK_IN" do + + describe "nominal" do + end + + describe "edge" do + end + + end end From c176eac422c701855934392c9d77cbf88beab99e Mon Sep 17 00:00:00 2001 From: stupendousC Date: Wed, 6 Nov 2019 21:12:37 -0800 Subject: [PATCH 35/86] had to amend fixtures due to new schema additions --- test/fixtures/customers.yml | 5 ++++- test/fixtures/movies.yml | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/test/fixtures/customers.yml b/test/fixtures/customers.yml index a4d44db89..f74cb879e 100644 --- a/test/fixtures/customers.yml +++ b/test/fixtures/customers.yml @@ -6,6 +6,7 @@ c1: postal_code: 12345 phone: 1-800-whatevr registered_at: 2019-01-01 + movies_checked_out_count: 1 c2: name: ned flanders @@ -15,6 +16,7 @@ c2: postal_code: 12345 phone: 1-800-diddly registered_at: 2018-12-25 + movies_checked_out_count: 2 c3: name: ted flanders @@ -23,4 +25,5 @@ c3: state: IL postal_code: 12345 phone: 1-800-diddly - registered_at: 2018-12-25 \ No newline at end of file + registered_at: 2018-12-25 + movies_checked_out_count: 3 \ No newline at end of file diff --git a/test/fixtures/movies.yml b/test/fixtures/movies.yml index 12c57d1bc..cbf4e3723 100644 --- a/test/fixtures/movies.yml +++ b/test/fixtures/movies.yml @@ -2,10 +2,12 @@ m1: title: the shining overview: scary movie about a hotel inventory: 5 + available_inventory: 5 release_date: 1972-10-31 m2: title: a burns for all seasons overview: mr. burns' propaganda film inventory: 3 + available_inventory: 3 release_date: 1990-01-01 \ No newline at end of file From c66f230792fa37e9e3361c17232ff2c9698299bb Mon Sep 17 00:00:00 2001 From: stupendousC Date: Wed, 6 Nov 2019 21:40:04 -0800 Subject: [PATCH 36/86] typo fixed --- app/controllers/rentals_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 5a655bb6c..b498953f6 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -24,7 +24,7 @@ def check_out customer.update(movies_checked_out_count: customer.movies_checked_out_count + 1) # prep API JSON - render json: { msg: "Rental id#{this_rental.id}: #{this_rental.movie.title} due on #{this_rental.due_date}"}, status: :ok + render json: { msg: "Rental id #{this_rental.id}: #{this_rental.movie.title} due on #{this_rental.due_date}"}, status: :ok else render json: { errors: "Cannot make a rental", error_msgs: new_rental.errors.full_messages }, status: :bad_request end @@ -46,7 +46,7 @@ def check_in this_customer.update(movies_checked_out_count: this_customer.movies_checked_out_count - 1) this_movie = rental.movie this_movie.update(available_inventory: this_movie.available_inventory + 1) - this_movie.update(returned: true) + rental.update(returned: true) render json: { msg: "Rental id#{rental.id}: #{rental.movie.title} has been returned." }, status: :ok end end From 79f4572beaf162235b27ce1f87a71bfa769bbcfb Mon Sep 17 00:00:00 2001 From: stupendousC Date: Wed, 6 Nov 2019 21:40:32 -0800 Subject: [PATCH 37/86] trying to fix bugs in check_out test codes --- test/controllers/rentals_controller_test.rb | 56 ++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index 25ac9ca63..49a407bb3 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -3,25 +3,77 @@ describe RentalsController do describe "CHECK_OUT" do + let (:m1) { movies(:m1) } + let (:c3) { customers(:c3) } + describe "successful check_out with valid inputs" do + + before do + # is this better than having a huge IT block? + @rental_count_before = Rental.count + @m1_avail_inv_before = m1.available_inventory + @c3_movie_count_before = c3.movies_checked_out_count + puts "\nBEFORE" + p m1 + p c3 + post check_out_path, params: {movie_id: m1.id, customer_id: c3.id} + + m1B = Movie.find_by(id: m1.id) + c3B = Customer.find_by(id: c3.id) + puts "AFTER" + p m1B + p c3B + puts + end + it "creates Rental instance with correct attribs" do + expect(Rental.count).must_equal @rental_count_before+1 + + rental = Rental.last + expect(rental.movie).must_equal m1 + expect(rental.customer).must_equal c3 + expect(rental.checkout_date).must_equal Date.today + expect(rental.due_date).must_equal (Date.today + 7.days) + expect(rental.returned).must_equal false end it "updates movie's available_inventory correctly" do + expect(m1.available_inventory).must_equal m1.inventory - 1 + expect(m1.available_inventory).must_equal @m1_avail_inv_before - 1 end it "updates customer's movies_checked_out_count correctly" do + expect(c3.movies_checked_out_count).must_equal @c3_movie_count_before + 1 end - it "returns JSON with success status" do + it "returns success status & JSON with expected message" do + check_response(expected_type: Hash, expected_status: :success) + + body = JSON.parse(response.body) + expect(body.keys).must_equal ["msg"] + expect(body.values).must_equal ["Rental id #{Rental.last.id}: #{m1.title} due on #{Rental.last.due_date}"] end end describe "edge" do it "if movie's available_inventory = 0, return error msg in JSON and bad_request status" do + m1.update!(available_inventory: 0) + + expect{post check_out_path, params: {movie_id: m1.id, customer_id: c3.id}}.wont_change "Rental.count" + check_response(expected_type: Hash, expected_status: :bad_request) + + body = JSON.parse(response.body) + expect(body.keys).must_equal ["errors"] + expect(body.values).must_equal ["Cannot make a rental because movie #{m1.title.capitalize} ran out of copies"] end + it "if rental.save unsuccessful, return error msg in JSON and bad_request status" do + + + # render json: { errors: "Cannot make a rental", error_msgs: new_rental.errors.full_messages }, status: :bad_request + + end end @@ -33,6 +85,8 @@ end describe "edge" do + it "if movie has already been returned" do + end end end From 34c7f38db8b01f0b792e2b94778070ac8c692f3e Mon Sep 17 00:00:00 2001 From: stupendousC Date: Wed, 6 Nov 2019 22:07:01 -0800 Subject: [PATCH 38/86] fixed hole in logic, postman still 100% passing --- app/controllers/rentals_controller.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index b498953f6..168c4e90b 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -7,9 +7,9 @@ def check_out new_rental.due_date = Date.today + 7.days new_rental.returned = false - # does this movie even have available_inventory? + # does this movie (if existing) even have available_inventory? movie = new_rental.movie - if movie.available_inventory <= 0 + if movie && movie.available_inventory <= 0 render json: { errors: "Cannot make a rental because movie #{movie.title.capitalize} ran out of copies"}, status: :bad_request return end @@ -19,13 +19,14 @@ def check_out # update movie's avail count movie.update(available_inventory: movie.available_inventory - 1) - # update custoemr's checked_out_count + # update customer's checked_out_count customer = this_rental.customer customer.update(movies_checked_out_count: customer.movies_checked_out_count + 1) # prep API JSON render json: { msg: "Rental id #{this_rental.id}: #{this_rental.movie.title} due on #{this_rental.due_date}"}, status: :ok else + # if movie and/or customer don't exist render json: { errors: "Cannot make a rental", error_msgs: new_rental.errors.full_messages }, status: :bad_request end From d1861861d606620d14fc742afce7d8d54a07acfa Mon Sep 17 00:00:00 2001 From: stupendousC Date: Wed, 6 Nov 2019 22:09:36 -0800 Subject: [PATCH 39/86] added some check-out edge cases --- app/controllers/rentals_controller.rb | 3 +- test/controllers/rentals_controller_test.rb | 44 +++++++++++++++------ 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 168c4e90b..6dea0057b 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -8,6 +8,7 @@ def check_out new_rental.returned = false # does this movie (if existing) even have available_inventory? + ### EMILY!!! Can we make this part of Rental model validations? IDK if it's bad SRP practice... movie = new_rental.movie if movie && movie.available_inventory <= 0 render json: { errors: "Cannot make a rental because movie #{movie.title.capitalize} ran out of copies"}, status: :bad_request @@ -26,7 +27,7 @@ def check_out # prep API JSON render json: { msg: "Rental id #{this_rental.id}: #{this_rental.movie.title} due on #{this_rental.due_date}"}, status: :ok else - # if movie and/or customer don't exist + # failed Rental validations: if movie and/or customer don't exist render json: { errors: "Cannot make a rental", error_msgs: new_rental.errors.full_messages }, status: :bad_request end diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index 49a407bb3..36c07a45d 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -13,17 +13,17 @@ @rental_count_before = Rental.count @m1_avail_inv_before = m1.available_inventory @c3_movie_count_before = c3.movies_checked_out_count - puts "\nBEFORE" - p m1 - p c3 + # puts "\nBEFORE" + # p m1 + # p c3 post check_out_path, params: {movie_id: m1.id, customer_id: c3.id} - m1B = Movie.find_by(id: m1.id) - c3B = Customer.find_by(id: c3.id) - puts "AFTER" - p m1B - p c3B - puts + # m1B = Movie.find_by(id: m1.id) + # c3B = Customer.find_by(id: c3.id) + # puts "AFTER" + # p m1 + # p c3 + # puts end it "creates Rental instance with correct attribs" do @@ -68,13 +68,35 @@ expect(body.values).must_equal ["Cannot make a rental because movie #{m1.title.capitalize} ran out of copies"] end - it "if rental.save unsuccessful, return error msg in JSON and bad_request status" do + it "if rental.save unsuccessful due to nonexistent customer, return error msg in JSON and bad_request status" do + nil_customer_hash = {movie_id: m1.id, customer_id: nil} + bad_rental = Rental.create(nil_customer_hash) + post check_out_path, params: nil_customer_hash - # render json: { errors: "Cannot make a rental", error_msgs: new_rental.errors.full_messages }, status: :bad_request + check_response(expected_type: Hash, expected_status: :bad_request) + body = JSON.parse(response.body) + expect(body.keys.sort).must_equal ["error_msgs", "errors"] + expect(body["errors"]).must_equal "Cannot make a rental" + expect(body["error_msgs"]).must_equal bad_rental.errors.full_messages end + + it "if rental.save unsuccessful due to nonexistent movie, return error msg in JSON and bad_request status" do + nil_movie_hash = {movie_id: nil, customer_id: c3.id} + + bad_rental = Rental.create(nil_movie_hash) + + post check_out_path, params: nil_movie_hash + + check_response(expected_type: Hash, expected_status: :bad_request) + + body = JSON.parse(response.body) + expect(body.keys.sort).must_equal ["error_msgs", "errors"] + expect(body["errors"]).must_equal "Cannot make a rental" + expect(body["error_msgs"]).must_equal bad_rental.errors.full_messages + end end end From 3800a6dee8c7efd571843f4a3a40593cba0d0f4c Mon Sep 17 00:00:00 2001 From: stupendousC Date: Wed, 6 Nov 2019 22:15:56 -0800 Subject: [PATCH 40/86] finished customer_ctrller test codes, i think --- test/controllers/customers_controller_test.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/test/controllers/customers_controller_test.rb b/test/controllers/customers_controller_test.rb index 3e43cd1e0..bc4e89d29 100644 --- a/test/controllers/customers_controller_test.rb +++ b/test/controllers/customers_controller_test.rb @@ -25,8 +25,15 @@ end - it "edge case: check status and request body" do - # None for index + it "if no customers exist, responds with check status and request body" do + Customer.destroy_all + + get customers_path + + check_response(expected_type: Array) + body = JSON.parse(response.body) + expect(body.count).must_equal Customer.count + expect(body).must_equal [] end end From 2a56a4fe496f38cb1df62bf91e266acde0a30452 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Wed, 6 Nov 2019 22:18:08 -0800 Subject: [PATCH 41/86] clarified language in test arrangement --- test/controllers/customers_controller_test.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/controllers/customers_controller_test.rb b/test/controllers/customers_controller_test.rb index bc4e89d29..f5e8deed3 100644 --- a/test/controllers/customers_controller_test.rb +++ b/test/controllers/customers_controller_test.rb @@ -11,7 +11,7 @@ check_response(expected_type: Array) end - it "responds with an array of customer hashes" do + it "if customers exist, responds with an array of customer hashes" do get customers_path body = JSON.parse(response.body) @@ -30,7 +30,6 @@ get customers_path - check_response(expected_type: Array) body = JSON.parse(response.body) expect(body.count).must_equal Customer.count expect(body).must_equal [] From c63ae0001d578a1e9c49f9c964310925c3f160f6 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Wed, 6 Nov 2019 22:24:46 -0800 Subject: [PATCH 42/86] rentals#check_out testing almost done except for 1 weird bug --- app/controllers/rentals_controller.rb | 1 + test/controllers/rentals_controller_test.rb | 21 +++++++-------------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 6dea0057b..5cfcaff41 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -9,6 +9,7 @@ def check_out # does this movie (if existing) even have available_inventory? ### EMILY!!! Can we make this part of Rental model validations? IDK if it's bad SRP practice... + ### I think we'll have to look into custom model validations if we do that... movie = new_rental.movie if movie && movie.available_inventory <= 0 render json: { errors: "Cannot make a rental because movie #{movie.title.capitalize} ran out of copies"}, status: :bad_request diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index 36c07a45d..dda3fdc34 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -13,17 +13,10 @@ @rental_count_before = Rental.count @m1_avail_inv_before = m1.available_inventory @c3_movie_count_before = c3.movies_checked_out_count - # puts "\nBEFORE" - # p m1 - # p c3 post check_out_path, params: {movie_id: m1.id, customer_id: c3.id} - # m1B = Movie.find_by(id: m1.id) - # c3B = Customer.find_by(id: c3.id) - # puts "AFTER" - # p m1 - # p c3 - # puts + @m1_avail_inv_after = (Movie.find_by(id: m1.id)).available_inventory + @c3_movie_count_after = (Customer.find_by(id: c3.id)).movies_checked_out_count end it "creates Rental instance with correct attribs" do @@ -38,12 +31,12 @@ end it "updates movie's available_inventory correctly" do - expect(m1.available_inventory).must_equal m1.inventory - 1 - expect(m1.available_inventory).must_equal @m1_avail_inv_before - 1 + expect(@m1_avail_inv_after).must_equal @m1_avail_inv_before - 1 end it "updates customer's movies_checked_out_count correctly" do - expect(c3.movies_checked_out_count).must_equal @c3_movie_count_before + 1 + ### WHY???!!! Postman still passed! I also manually tested in rails console! + expect(@c3_movie_count_after).must_equal @c3_movie_count_before + 1 end it "returns success status & JSON with expected message" do @@ -82,10 +75,10 @@ expect(body["error_msgs"]).must_equal bad_rental.errors.full_messages end - + it "if rental.save unsuccessful due to nonexistent movie, return error msg in JSON and bad_request status" do nil_movie_hash = {movie_id: nil, customer_id: c3.id} - + bad_rental = Rental.create(nil_movie_hash) post check_out_path, params: nil_movie_hash From 55b965aa9935e8cfa68ba247bfc86ce2660d3bf6 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Wed, 6 Nov 2019 22:31:08 -0800 Subject: [PATCH 43/86] set up rentals#check_in test skeleton --- test/controllers/rentals_controller_test.rb | 34 ++++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index dda3fdc34..de48ea06f 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -49,7 +49,7 @@ end - describe "edge" do + describe "edge cases" do it "if movie's available_inventory = 0, return error msg in JSON and bad_request status" do m1.update!(available_inventory: 0) @@ -96,13 +96,39 @@ describe "CHECK_IN" do - describe "nominal" do + describe "successful check-ins with valid inputs" do + + it "will not add new rental instances" do + # TODO + end + + it "will increment movie.available_inventory by 1" do + # TODO + end + + it "will not decrease customer's movies_checked_out by 1" do + # TODO + end + + it "returns success status & JSON with expected message" do + # TODO + end + end - describe "edge" do + describe "edge cases" do + it "if rental doesn't exist, return error msg in JSON and bad_request status" do + # TODO + # render json: { errors: "Rental doesn't exist" }, status: :bad_request + + end + it "if movie has already been returned" do + # TODO + # render json: { errors: "Rental has already been returned" }, status: :bad_request + end + end - end end From ea741b4825f9dbf55da59dbbbb918348cd9463e6 Mon Sep 17 00:00:00 2001 From: Emily Vomacka Date: Thu, 7 Nov 2019 09:55:29 -0800 Subject: [PATCH 44/86] test_helper --- test/controllers/customers_controller_test.rb | 21 +++++++++++++++++-- test/test_helper.rb | 15 ++++++++++++- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/test/controllers/customers_controller_test.rb b/test/controllers/customers_controller_test.rb index 4b93facae..579974b85 100644 --- a/test/controllers/customers_controller_test.rb +++ b/test/controllers/customers_controller_test.rb @@ -1,12 +1,29 @@ require "test_helper" +CUSTOMER_FIELDS = %w(id name phone postal_code registered_at movies_checked_out_count).sort + describe CustomersController do describe "INDEX" do - it "basic test w/ no parameters, if applicable" do + it "responds with JSON, success, and an array of customer hashes" do + get customers_path + body = check_response(expected_type: Array) + + body.each do |customer| + expect(customer).must_be_instance_of Hash + expect(customer.keys.sort).must_equal CUSTOMER_FIELDS + end + end + + it "responds with an empty array when there are no customers" do + Customer.destroy_all + get customers_path + body = check_response(expected_type: Array) + expect(body).must_be_empty end - it "nominal case: check status and request body" do + it "displays list of customers" do + end it "edge case: check status and request body" do diff --git a/test/test_helper.rb b/test/test_helper.rb index 830ce1575..820a1995a 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -8,9 +8,22 @@ require 'minitest/reporters' require "pry" +Minitest::Reporters.use!( + Minitest::Reporters::SpecReporter.new, + ENV, + Minitest.backtrace_filter +) + class ActiveSupport::TestCase # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. fixtures :all - # Add more helper methods to be used by all tests here... + def check_response(expected_type:, expected_status: :success) + must_respond_with expected_status + expect(response.header['Content-Type']).must_include 'json' + + body = JSON.parse(response.body) + expect(body).must_be_kind_of expected_type + return body + end end From 95ac7dc97a745a0365385ad4bba0499f06aa0f3d Mon Sep 17 00:00:00 2001 From: stupendousC Date: Thu, 7 Nov 2019 10:05:34 -0800 Subject: [PATCH 45/86] fixed yml seed data discrepancy --- test/fixtures/customers.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/fixtures/customers.yml b/test/fixtures/customers.yml index f74cb879e..8262bd189 100644 --- a/test/fixtures/customers.yml +++ b/test/fixtures/customers.yml @@ -16,7 +16,7 @@ c2: postal_code: 12345 phone: 1-800-diddly registered_at: 2018-12-25 - movies_checked_out_count: 2 + movies_checked_out_count: 1 c3: name: ted flanders @@ -26,4 +26,4 @@ c3: postal_code: 12345 phone: 1-800-diddly registered_at: 2018-12-25 - movies_checked_out_count: 3 \ No newline at end of file + movies_checked_out_count: 0 \ No newline at end of file From 01cea085b77974fff6f7eb76f6663c0ecd0f1a7b Mon Sep 17 00:00:00 2001 From: stupendousC Date: Thu, 7 Nov 2019 10:06:22 -0800 Subject: [PATCH 46/86] fixed more yml errors --- test/fixtures/customers.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/fixtures/customers.yml b/test/fixtures/customers.yml index 8262bd189..ad054f8e8 100644 --- a/test/fixtures/customers.yml +++ b/test/fixtures/customers.yml @@ -6,7 +6,7 @@ c1: postal_code: 12345 phone: 1-800-whatevr registered_at: 2019-01-01 - movies_checked_out_count: 1 + movies_checked_out_count: 0 c2: name: ned flanders From d8e4489364a50da33bb185e97012a104eaf01227 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Thu, 7 Nov 2019 10:41:55 -0800 Subject: [PATCH 47/86] fixed yml, thereby fixing test codes, yay --- test/controllers/rentals_controller_test.rb | 8 ++++---- test/fixtures/customers.yml | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index de48ea06f..7d5f8b962 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -1,4 +1,5 @@ require "test_helper" +require 'pry' describe RentalsController do describe "CHECK_OUT" do @@ -9,14 +10,11 @@ describe "successful check_out with valid inputs" do before do - # is this better than having a huge IT block? @rental_count_before = Rental.count @m1_avail_inv_before = m1.available_inventory @c3_movie_count_before = c3.movies_checked_out_count + post check_out_path, params: {movie_id: m1.id, customer_id: c3.id} - - @m1_avail_inv_after = (Movie.find_by(id: m1.id)).available_inventory - @c3_movie_count_after = (Customer.find_by(id: c3.id)).movies_checked_out_count end it "creates Rental instance with correct attribs" do @@ -31,10 +29,12 @@ end it "updates movie's available_inventory correctly" do + @m1_avail_inv_after = (Movie.find_by(id: m1.id)).available_inventory expect(@m1_avail_inv_after).must_equal @m1_avail_inv_before - 1 end it "updates customer's movies_checked_out_count correctly" do + @c3_movie_count_after = (Customer.find_by(id: c3.id)).movies_checked_out_count ### WHY???!!! Postman still passed! I also manually tested in rails console! expect(@c3_movie_count_after).must_equal @c3_movie_count_before + 1 end diff --git a/test/fixtures/customers.yml b/test/fixtures/customers.yml index ad054f8e8..a936c8477 100644 --- a/test/fixtures/customers.yml +++ b/test/fixtures/customers.yml @@ -19,11 +19,11 @@ c2: movies_checked_out_count: 1 c3: - name: ted flanders + name: principal skinners address: 124 real st city: springfield state: IL postal_code: 12345 - phone: 1-800-diddly + phone: 1-800-school1 registered_at: 2018-12-25 movies_checked_out_count: 0 \ No newline at end of file From 5b1a6c573d49494b308b2668eb94e40809c0cd70 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Thu, 7 Nov 2019 13:55:12 -0800 Subject: [PATCH 48/86] took out unnecessary comment --- app/models/customer.rb | 8 ++------ test/controllers/rentals_controller_test.rb | 3 +-- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/app/models/customer.rb b/app/models/customer.rb index 2f934c767..113ce72cb 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -1,12 +1,8 @@ class Customer < ApplicationRecord has_many :rentals has_many :movies, through: :rentals - + validates :name, presence: :true validates :phone, presence: :true, uniqueness: :true - - #I don't think we need this method anymore! - # def movies_checked_out_count - # return self.movies.count - # end + end diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index 7d5f8b962..1a7d1e4be 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -13,7 +13,7 @@ @rental_count_before = Rental.count @m1_avail_inv_before = m1.available_inventory @c3_movie_count_before = c3.movies_checked_out_count - + post check_out_path, params: {movie_id: m1.id, customer_id: c3.id} end @@ -35,7 +35,6 @@ it "updates customer's movies_checked_out_count correctly" do @c3_movie_count_after = (Customer.find_by(id: c3.id)).movies_checked_out_count - ### WHY???!!! Postman still passed! I also manually tested in rails console! expect(@c3_movie_count_after).must_equal @c3_movie_count_before + 1 end From 7236314bab69dd62bf7c0391e85269f2da975570 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Thu, 7 Nov 2019 14:23:13 -0800 Subject: [PATCH 49/86] sorting by name/registered_at/postal_code works for Customers#index --- app/controllers/customers_controller.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index 45967ee58..a08a7e659 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -4,6 +4,18 @@ class CustomersController < ApplicationController def index customers = Customer.all + + if ["name", "registered_at", "postal_code"].include? params[:sort] + choice = params[:sort].to_sym + customers = Customer.all.order(choice => :asc) + end + + if params[:n] && params[:p] + # I think these 2 query params are used together? otherwise p by itself makes no sense + # TODO + end + render json: customers.as_json( only: CUSTOMER_KEYS), status: :ok end + end From 720ad9838b5145065e3c8efbaa5333bfc2481609 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Thu, 7 Nov 2019 14:41:53 -0800 Subject: [PATCH 50/86] able to use query params sort and/or n&&p, but no validations --- app/controllers/customers_controller.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index a08a7e659..2b2c95f6a 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -11,10 +11,15 @@ def index end if params[:n] && params[:p] - # I think these 2 query params are used together? otherwise p by itself makes no sense - # TODO + limit_amt = params[:n].to_i + offset_amt = (params[:p].to_i - 1) * limit_amt + + customers = customers.limit(limit_amt).offset(offset_amt) end + # validations on query parameters? none... + # will just generate regular Customer.all if bogus params given for sort/n/p + render json: customers.as_json( only: CUSTOMER_KEYS), status: :ok end From af09103a0f63a76e71707a9b88a4080160716224 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Thu, 7 Nov 2019 14:46:49 -0800 Subject: [PATCH 51/86] skeletons of customer ctrller test for query parameter extras --- test/controllers/customers_controller_test.rb | 73 ++++++++++++++----- 1 file changed, 53 insertions(+), 20 deletions(-) diff --git a/test/controllers/customers_controller_test.rb b/test/controllers/customers_controller_test.rb index f5e8deed3..f5ae9cb60 100644 --- a/test/controllers/customers_controller_test.rb +++ b/test/controllers/customers_controller_test.rb @@ -5,34 +5,67 @@ describe CustomersController do describe "INDEX" do - it "responds with JSON and success status" do - get customers_path + describe "basic index" do + it "responds with JSON and success status" do + get customers_path + + check_response(expected_type: Array) + end + + it "if customers exist, responds with an array of customer hashes" do + get customers_path + + body = JSON.parse(response.body) + + expect(body.count).must_equal Customer.count + + body.each do |customer| + expect(customer).must_be_instance_of Hash + expect(customer.keys.sort).must_equal CUSTOMER_KEYS + end + + end - check_response(expected_type: Array) + it "if no customers exist, responds with check status and request body" do + Customer.destroy_all + + get customers_path + + body = JSON.parse(response.body) + expect(body.count).must_equal Customer.count + expect(body).must_equal [] + end end - it "if customers exist, responds with an array of customer hashes" do - get customers_path - - body = JSON.parse(response.body) - - expect(body.count).must_equal Customer.count + describe "additional query parameters" do + describe "does query parameter sort work?" do + it "with valid input, respond w/ correct JSON and status" do + end + + it "with invalid inputs, respond w/ default JSON of all customers and success status" do + end + + end - body.each do |customer| - expect(customer).must_be_instance_of Hash - expect(customer.keys.sort).must_equal CUSTOMER_KEYS + describe "does query parameter combo n&p work?" do + it "with valid input, respond w/ correct JSON and status" do + end + + it "with invalid inputs, respond w/ default JSON of all customers and success status" do + end + end - end - - it "if no customers exist, responds with check status and request body" do - Customer.destroy_all + describe "does query parameter super combo of sort AND n&p work?" do + it "with valid input, respond w/ correct JSON and status" do + end + + it "with invalid inputs, respond w/ default JSON of all customers and success status" do + end + + end - get customers_path - body = JSON.parse(response.body) - expect(body.count).must_equal Customer.count - expect(body).must_equal [] end end From e7de0fa5604551864804b107e30adcb66083934a Mon Sep 17 00:00:00 2001 From: stupendousC Date: Thu, 7 Nov 2019 15:31:15 -0800 Subject: [PATCH 52/86] almost done with tetsing on query parameter :sort --- test/controllers/customers_controller_test.rb | 42 ++++++++++++++++++- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/test/controllers/customers_controller_test.rb b/test/controllers/customers_controller_test.rb index f5ae9cb60..ee686674f 100644 --- a/test/controllers/customers_controller_test.rb +++ b/test/controllers/customers_controller_test.rb @@ -40,11 +40,49 @@ describe "additional query parameters" do describe "does query parameter sort work?" do it "with valid input, respond w/ correct JSON and status" do + get customers_path, params: { sort: "name" } + + check_response(expected_type: Array) + + body = JSON.parse(response.body) + expect(body.count).must_equal Customer.count + + body.each do |customer| + expect(customer).must_be_instance_of Hash + expect(customer.keys.sort).must_equal CUSTOMER_KEYS + end + + # checking alphabetical order + (body.count-1).times do |i| + assert(body[i]["name"] < body[i+1]["name"]) + end end it "with invalid inputs, respond w/ default JSON of all customers and success status" do + # I did NOT do input validation, I could've ... but I didn't lol + # this is expecting the same result as the describe "basic index" block + get customers_path, params: { sort: "garbage" } + + body = JSON.parse(response.body) + + expect(body.count).must_equal Customer.count + + body.each do |customer| + expect(customer).must_be_instance_of Hash + expect(customer.keys.sort).must_equal CUSTOMER_KEYS + end end + it "if no customers exist, responds with check status and request body" do + Customer.destroy_all + + get customers_path, params: { sort: "garbage"} + + body = JSON.parse(response.body) + expect(body.count).must_equal Customer.count + expect(body).must_equal [] + end + end describe "does query parameter combo n&p work?" do @@ -53,7 +91,7 @@ it "with invalid inputs, respond w/ default JSON of all customers and success status" do end - + end describe "does query parameter super combo of sort AND n&p work?" do @@ -62,7 +100,7 @@ it "with invalid inputs, respond w/ default JSON of all customers and success status" do end - + end From b50bc27b8ea5797a92eb3d514f3c72ded5ed5b0d Mon Sep 17 00:00:00 2001 From: stupendousC Date: Thu, 7 Nov 2019 15:43:47 -0800 Subject: [PATCH 53/86] finished sort query parameter testing --- test/controllers/customers_controller_test.rb | 53 ++++++++++++------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/test/controllers/customers_controller_test.rb b/test/controllers/customers_controller_test.rb index ee686674f..06db0e97f 100644 --- a/test/controllers/customers_controller_test.rb +++ b/test/controllers/customers_controller_test.rb @@ -39,22 +39,28 @@ describe "additional query parameters" do describe "does query parameter sort work?" do - it "with valid input, respond w/ correct JSON and status" do - get customers_path, params: { sort: "name" } - - check_response(expected_type: Array) - - body = JSON.parse(response.body) - expect(body.count).must_equal Customer.count - - body.each do |customer| - expect(customer).must_be_instance_of Hash - expect(customer.keys.sort).must_equal CUSTOMER_KEYS - end + it "with each valid input, respond w/ correct JSON and status" do + # testing all 3 of these choices + choices = %w[name registered_at postal_code] - # checking alphabetical order - (body.count-1).times do |i| - assert(body[i]["name"] < body[i+1]["name"]) + choices.each do |choice| + get customers_path, params: { sort: choice } + + check_response(expected_type: Array) + + body = JSON.parse(response.body) + expect(body.count).must_equal Customer.count + + body.each do |customer| + expect(customer).must_be_instance_of Hash + expect(customer.keys.sort).must_equal CUSTOMER_KEYS + end + + # checking order of name/registerd_at/postal_code + # why <= and not just Date: Thu, 7 Nov 2019 16:36:16 -0800 Subject: [PATCH 54/86] huge chunk of n&p qurey parameters edge cases testing finished --- app/controllers/customers_controller.rb | 34 +++++++--- test/controllers/customers_controller_test.rb | 62 +++++++++++++++---- 2 files changed, 75 insertions(+), 21 deletions(-) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index 2b2c95f6a..5980d35b7 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -5,22 +5,36 @@ class CustomersController < ApplicationController def index customers = Customer.all - if ["name", "registered_at", "postal_code"].include? params[:sort] - choice = params[:sort].to_sym - customers = Customer.all.order(choice => :asc) + if params[:sort] + if ["name", "registered_at", "postal_code"].include? params[:sort] + choice = params[:sort].to_sym + customers = Customer.all.order(choice => :asc) + else + render json: { errors: "Can't sort anything with that key" }, status: :bad_request + return + end end if params[:n] && params[:p] - limit_amt = params[:n].to_i - offset_amt = (params[:p].to_i - 1) * limit_amt - - customers = customers.limit(limit_amt).offset(offset_amt) + # validation not complete + if (params[:n].respond_to? :to_i) && (params[:p].respond_to? :to_i) + limit_amt = params[:n].to_i + offset_amt = (params[:p].to_i - 1) * limit_amt + + if (limit_amt > 0) && (offset_amt > 0) + customers = customers.limit(limit_amt).offset(offset_amt) + else + render json: { errors: "Invalid n & p combo" }, status: :bad_request + return + end + end + elsif params[:n] || params[:p] + render json: { errors: "We require both n and p, not just one of them" }, status: :bad_request + return end - # validations on query parameters? none... - # will just generate regular Customer.all if bogus params given for sort/n/p - render json: customers.as_json( only: CUSTOMER_KEYS), status: :ok + return end end diff --git a/test/controllers/customers_controller_test.rb b/test/controllers/customers_controller_test.rb index 06db0e97f..cf30671bd 100644 --- a/test/controllers/customers_controller_test.rb +++ b/test/controllers/customers_controller_test.rb @@ -4,6 +4,8 @@ describe CustomersController do + let(:c2) { customers(:c2) } + describe "INDEX" do describe "basic index" do it "responds with JSON and success status" do @@ -65,18 +67,12 @@ end it "with invalid inputs, respond w/ default JSON of all customers and success status" do - # I did NOT do input validation, I could've ... but I didn't lol - # this is expecting the same result as the describe "basic index" block get customers_path, params: { sort: "garbage" } - body = JSON.parse(response.body) - - expect(body.count).must_equal Customer.count + check_response(expected_type: Hash, expected_status: :bad_request) - body.each do |customer| - expect(customer).must_be_instance_of Hash - expect(customer.keys.sort).must_equal CUSTOMER_KEYS - end + body = JSON.parse(response.body) + expect(body["errors"]).must_equal "Can't sort anything with that key" end it "if no customers exist but valid input, responds with check status and request body" do @@ -93,9 +89,54 @@ describe "does query parameter combo n&p work?" do it "with valid inputs, respond w/ correct JSON and status" do + params = { n: "1", p: "2" } + get customers_path, params: params + + check_response(expected_type: Array) + + body = JSON.parse(response.body) + expect(body.count).must_equal params[:n].to_i + + # hash in body.first should match those of c2 + customer_json = body.first + expect(customer_json["id"]).must_equal c2.id + expect(customer_json["name"]).must_equal c2.name + expect(customer_json["postal_code"]).must_equal c2.postal_code + expect(customer_json["phone"]).must_equal c2.phone + expect(customer_json["registered_at"]).must_equal c2.registered_at + expect(customer_json["movies_checked_out_count"]).must_equal c2.movies_checked_out_count end - it "with invalid inputs, respond w/ default JSON of all customers and success status" do + it "with invalid combo of single input, respond w/ JSON error msg and bad_request status" do + bad_params1 = { n: "5" } + bad_params2 = { p: "10" } + all_bad_params = [bad_params1, bad_params2] + + all_bad_params.each do |bad_param| + get customers_path, params: bad_param + + check_response(expected_type: Hash, expected_status: :bad_request) + + body = JSON.parse(response.body) + expect(body["errors"]).must_equal "We require both n and p, not just one of them" + end + end + + it "with invalid combo of bad n & p values, respond w/ JSON error msg and bad_request status" do + bad_params1 = { n: "5", p: "garbage" } + bad_params2 = { n: "garbage", p: "10" } + bad_params3 = { n: "", p: "garbage" } + bad_params4 = { n: "0", p: "-3" } + all_bad_params = [bad_params1, bad_params2, bad_params3, bad_params4] + + all_bad_params.each do |bad_param| + get customers_path, params: bad_param + + check_response(expected_type: Hash, expected_status: :bad_request) + + body = JSON.parse(response.body) + expect(body["errors"]).must_equal "Invalid n & p combo" + end end it "if no customers exist but valid input, responds with check status and request body" do @@ -118,7 +159,6 @@ end - end end From 6ac38f2a414cc3caa4d37ee6a40264f206ca8f85 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Thu, 7 Nov 2019 16:39:00 -0800 Subject: [PATCH 55/86] finished testing for query parameter sort by itself, and n&p combo by itself, but NOT the supercombo of all 3 --- test/controllers/customers_controller_test.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/controllers/customers_controller_test.rb b/test/controllers/customers_controller_test.rb index cf30671bd..f52a1d23c 100644 --- a/test/controllers/customers_controller_test.rb +++ b/test/controllers/customers_controller_test.rb @@ -152,9 +152,11 @@ describe "does query parameter super combo of sort AND n&p work?" do it "with valid input, respond w/ correct JSON and status" do + # TODO, wanted to focus on making other extra functionalities instead end it "with invalid inputs, respond w/ default JSON of all customers and success status" do + # TODO, wanted to focus on making other extra functionalities instead end end From 85a3db6a32848844747eeb13efc74b789dd64df2 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Thu, 7 Nov 2019 17:10:40 -0800 Subject: [PATCH 56/86] attempting rentals/overdue --- app/controllers/customers_controller.rb | 2 +- app/controllers/rentals_controller.rb | 10 ++++++++++ config/routes.rb | 1 + 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index 5980d35b7..38792e077 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -16,7 +16,6 @@ def index end if params[:n] && params[:p] - # validation not complete if (params[:n].respond_to? :to_i) && (params[:p].respond_to? :to_i) limit_amt = params[:n].to_i offset_amt = (params[:p].to_i - 1) * limit_amt @@ -37,4 +36,5 @@ def index return end + end diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 5cfcaff41..74213dc62 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -27,9 +27,11 @@ def check_out # prep API JSON render json: { msg: "Rental id #{this_rental.id}: #{this_rental.movie.title} due on #{this_rental.due_date}"}, status: :ok + return else # failed Rental validations: if movie and/or customer don't exist render json: { errors: "Cannot make a rental", error_msgs: new_rental.errors.full_messages }, status: :bad_request + return end end @@ -51,9 +53,17 @@ def check_in this_movie.update(available_inventory: this_movie.available_inventory + 1) rental.update(returned: true) render json: { msg: "Rental id#{rental.id}: #{rental.movie.title} has been returned." }, status: :ok + return end end + def overdue + affected_customers = Customers.where(movies_checked_out_count: 1) + + render json: affected_customers.as_json ( only: :name), status: :ok + return + end + private def rental_params diff --git a/config/routes.rb b/config/routes.rb index 93787db5a..c508a2847 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,4 +5,5 @@ post "/rentals/check-out", to: "rentals#check_out", as: "check_out" post "/rentals/check-in", to: "rentals#check_in", as: "check_in" + get "/rentals/overdue", to: "rentals#overdue", as: "overdue" end From 182dc7269a9e7262d994bbdae89071949cb12a42 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Thu, 7 Nov 2019 20:13:20 -0800 Subject: [PATCH 57/86] /rentals/overdue does NOT work yet... --- Caroline_thoughts.txt | 9 +++++---- app/controllers/rentals_controller.rb | 20 +++++++++++++++++--- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/Caroline_thoughts.txt b/Caroline_thoughts.txt index 3b44c30c4..5066ba3b0 100644 --- a/Caroline_thoughts.txt +++ b/Caroline_thoughts.txt @@ -4,10 +4,11 @@ customerInstance.movies_checked_out_count GET /rentals/overdue - Option A: go through each customer? find customerInstance.rentals where returned: false && due_date < today - Option B: go through each rental? - I think Option A is best, b/c with optionB we might repeat the iteration for the same customer(s), - whereas with optionA, as soon as that customer gets added to the JSON, we can move on to next customer + Option A: go through each affected customer? + Option B: go through each rental? No b/c worse time complexity + + with optionA, I search Customer for all those who has movies_checked_out_count > 0, + then iterate thru and find customerInstance.rentals where returned: false && due_date < today GET /movies/:id/current I vote for Option A, same reason diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 74213dc62..1a394b284 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -58,10 +58,24 @@ def check_in end def overdue - affected_customers = Customers.where(movies_checked_out_count: 1) + # HOW DO I SEARCH FOR >0 ???? + possible_customers = Customer.where(movies_checked_out_count: 1) + overdue_customers = [] - render json: affected_customers.as_json ( only: :name), status: :ok - return + ### ALSO this doesn't work lol + possible_customers.each do |customer| + customer.rentals.each do |rental| + unless rental.returned + if rental.due_date.to_date < Date.today + overdue_customers << customer + break + end + end + end + end + + render json: overdue_customers, status: :ok + return end private From 9a397c0cd03a79bc8e7d3cf60376056fd8e99fb0 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Thu, 7 Nov 2019 20:22:38 -0800 Subject: [PATCH 58/86] set up routes with temporary default JSON msg --- Caroline_thoughts.txt | 1 + app/controllers/customers_controller.rb | 11 +++++++++++ app/controllers/movies_controller.rb | 6 ++++++ app/controllers/rentals_controller.rb | 3 +++ config/routes.rb | 4 ++++ 5 files changed, 25 insertions(+) diff --git a/Caroline_thoughts.txt b/Caroline_thoughts.txt index 5066ba3b0..0ecbd438c 100644 --- a/Caroline_thoughts.txt +++ b/Caroline_thoughts.txt @@ -15,6 +15,7 @@ GET /movies/:id/current GET /movies/:id/history Go through movieInstance.rentals, where returned: true (bc IN THE PAST) + WAIT... no I think it's BOTH returned: true/false... it doesn't matter if it's been returned or not GET /customers/:id/current Go through customerInstance.rentals, get all the ones with returned: false diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index 38792e077..0cd2e122f 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -36,5 +36,16 @@ def index return end + def current + # Go through customerInstance.rentals, get all the ones with returned: false + render json: { msg: "TODO" }, status: :ok + + end + + def history + # Go through customerInstance.rentals, get all the ones with returned: true + render json: { msg: "TODO" }, status: :ok + + end end diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index ad39bf861..13d3a8312 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -28,6 +28,12 @@ def create return movie.id end + def history + # sends list of customers who've checked out a specific movie in the past + render json: { msg: "TODO" }, status: :ok + + end + private def movie_params diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 1a394b284..e5f3477b7 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -61,6 +61,9 @@ def overdue # HOW DO I SEARCH FOR >0 ???? possible_customers = Customer.where(movies_checked_out_count: 1) overdue_customers = [] + render json: { msg: "Still working out the kinks" }, status: :ok + return + ### ALSO this doesn't work lol possible_customers.each do |customer| diff --git a/config/routes.rb b/config/routes.rb index c508a2847..d6b532411 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,11 @@ Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html resources :movies, only: [:index, :show, :create] + get "/movies/:id/history", to: "movies#history", as: "movie_hx" + resources :customers, only: [:index] + get "/customers/:id/current", to: "customers#current", as: "customer_current" + get "/customers/:id/history", to: "customers#history", as: "customer_hx" post "/rentals/check-out", to: "rentals#check_out", as: "check_out" post "/rentals/check-in", to: "rentals#check_in", as: "check_in" From fe48355fdcc4cf7e54cd491331d460bb8b3dc0e7 Mon Sep 17 00:00:00 2001 From: Emily Vomacka Date: Thu, 7 Nov 2019 21:19:29 -0800 Subject: [PATCH 59/86] movies controller testing --- app/controllers/movies_controller.rb | 6 ++- test/controllers/customers_controller_test.rb | 1 - test/controllers/movies_controller_test.rb | 39 +++++++++++++++---- test/controllers/rentals_controller_test.rb | 1 + test/fixtures/customers.yml | 2 +- 5 files changed, 37 insertions(+), 12 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index ad39bf861..ef43c0106 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -1,5 +1,7 @@ class MoviesController < ApplicationController - + + MOVIE_KEYS = [:id, :title, :overview, :release_date, :inventory, :available_inventory] + def index movies = Movie.all @@ -10,7 +12,7 @@ def show movie = Movie.find_by(id: params[:id]) if movie - render json: movie.as_json( only: [:id, :title, :overview, :release_date, :inventory, :available_inventory]), status: :ok + render json: movie.as_json( only: MOVIE_KEYS), status: :ok else render json: { errors: "Movie not found" }, status: :ok end diff --git a/test/controllers/customers_controller_test.rb b/test/controllers/customers_controller_test.rb index f5e8deed3..108aa3415 100644 --- a/test/controllers/customers_controller_test.rb +++ b/test/controllers/customers_controller_test.rb @@ -7,7 +7,6 @@ describe "INDEX" do it "responds with JSON and success status" do get customers_path - check_response(expected_type: Array) end diff --git a/test/controllers/movies_controller_test.rb b/test/controllers/movies_controller_test.rb index 1134accf2..99fbf644c 100644 --- a/test/controllers/movies_controller_test.rb +++ b/test/controllers/movies_controller_test.rb @@ -1,17 +1,40 @@ require "test_helper" +MOVIE_KEYS = (%w[id name postal_code phone registered_at movies_checked_out_count]).sort + describe MoviesController do describe "INDEX" do - it "basic test w/ no parameters, if applicable" do - end - - it "nominal case: check status and request body" do + describe "INDEX" do + it "responds with JSON and success status" do + get movies_path + check_response(expected_type: Array) + end + + it "if movies exist, responds with an array of movie hashes" do + get movies_path + + body = JSON.parse(response.body) + + expect(body.count).must_equal Movie.count + + body.each do |movie| + expect(movie).must_be_instance_of Hash + expect(movie.keys.sort).must_equal CUSTOMER_KEYS + end + + end + + it "if no movies exist, responds with check status and request body" do + Movie.destroy_all + + get movies_path + + body = JSON.parse(response.body) + expect(body.count).must_equal Movie.count + expect(body).must_equal [] + end end - it "edge case: check status and request body" do - end - end - describe "SHOW" do it "basic test w/ no parameters, if applicable" do end diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index de48ea06f..de5440de5 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -1,4 +1,5 @@ require "test_helper" +require 'pry' describe RentalsController do describe "CHECK_OUT" do diff --git a/test/fixtures/customers.yml b/test/fixtures/customers.yml index f74cb879e..c6c0a687d 100644 --- a/test/fixtures/customers.yml +++ b/test/fixtures/customers.yml @@ -24,6 +24,6 @@ c3: city: springfield state: IL postal_code: 12345 - phone: 1-800-diddly + phone: 1-800-fiddly registered_at: 2018-12-25 movies_checked_out_count: 3 \ No newline at end of file From 747d634ba0020bb202380ac77d6129194db74ff1 Mon Sep 17 00:00:00 2001 From: Emily Vomacka Date: Thu, 7 Nov 2019 21:22:10 -0800 Subject: [PATCH 60/86] movies controller test --- test/controllers/movies_controller_test.rb | 66 +++++++++++----------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/test/controllers/movies_controller_test.rb b/test/controllers/movies_controller_test.rb index 99fbf644c..a61b44b83 100644 --- a/test/controllers/movies_controller_test.rb +++ b/test/controllers/movies_controller_test.rb @@ -1,58 +1,58 @@ require "test_helper" -MOVIE_KEYS = (%w[id name postal_code phone registered_at movies_checked_out_count]).sort +MOVIE_SHOW_KEYS = (%w[id name postal_code phone registered_at movies_checked_out_count]).sort +MOVIE_INDEX_KEYS = (%w[id title release_date]).sort describe MoviesController do describe "INDEX" do - describe "INDEX" do - it "responds with JSON and success status" do - get movies_path - check_response(expected_type: Array) - end + it "responds with JSON and success status" do + get movies_path + check_response(expected_type: Array) + end + + it "if movies exist, responds with an array of movie hashes" do + get movies_path - it "if movies exist, responds with an array of movie hashes" do - get movies_path - - body = JSON.parse(response.body) - - expect(body.count).must_equal Movie.count - - body.each do |movie| - expect(movie).must_be_instance_of Hash - expect(movie.keys.sort).must_equal CUSTOMER_KEYS - end - - end + body = JSON.parse(response.body) + + expect(body.count).must_equal Movie.count - it "if no movies exist, responds with check status and request body" do - Movie.destroy_all - - get movies_path - - body = JSON.parse(response.body) - expect(body.count).must_equal Movie.count - expect(body).must_equal [] + body.each do |movie| + expect(movie).must_be_instance_of Hash + expect(movie.keys.sort).must_equal MOVIE_INDEX_KEYS end + end - + + it "if no movies exist, responds with check status and request body" do + Movie.destroy_all + + get movies_path + + body = JSON.parse(response.body) + expect(body.count).must_equal Movie.count + expect(body).must_equal [] + end + end + describe "SHOW" do it "basic test w/ no parameters, if applicable" do end - + it "nominal case: check status and request body" do end - + it "edge case: check status and request body" do end end - + describe "CREATE" do it "basic test w/ no parameters, if applicable" do end - + it "nominal case: check status and request body" do end - + it "edge case: check status and request body" do end end From 373fb8ebe2aee7c6081238aa182e2a537439e5b5 Mon Sep 17 00:00:00 2001 From: Emily Vomacka Date: Thu, 7 Nov 2019 21:59:11 -0800 Subject: [PATCH 61/86] finished movie testing --- test/controllers/movies_controller_test.rb | 45 ++++++++++++++++------ 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/test/controllers/movies_controller_test.rb b/test/controllers/movies_controller_test.rb index a61b44b83..cea41b336 100644 --- a/test/controllers/movies_controller_test.rb +++ b/test/controllers/movies_controller_test.rb @@ -1,6 +1,6 @@ require "test_helper" -MOVIE_SHOW_KEYS = (%w[id name postal_code phone registered_at movies_checked_out_count]).sort +MOVIE_SHOW_KEYS = (%w[id title overview release_date inventory available_inventory]).sort MOVIE_INDEX_KEYS = (%w[id title release_date]).sort describe MoviesController do @@ -36,24 +36,47 @@ end describe "SHOW" do - it "basic test w/ no parameters, if applicable" do - end - it "nominal case: check status and request body" do + let (:m1) {movies(:m1) } + + it "responds with JSON, success, and a movie hash" do + get movie_path(m1) + body = check_response(expected_type: Hash) + expect(body.keys.sort).must_equal MOVIE_SHOW_KEYS end - it "edge case: check status and request body" do + it "will respond ok with an error JSON when movie not found" do + Movie.destroy_all + get movie_path(-1) + body = check_response(expected_type: Hash) + expect(body.keys).must_equal ["errors"] + expect(body.values).must_equal ["Movie not found"] + must_respond_with :success end end describe "CREATE" do - it "basic test w/ no parameters, if applicable" do - end - it "nominal case: check status and request body" do - end + it "creates a movie with correct params" do + prev_movie_count = Movie.count + movie_params = { title: "test movie", inventory: 10, overview: "fascinating", release_date: "2019-10-01" } + post movies_path, params: movie_params + must_respond_with :success + + expect(Movie.count).must_equal prev_movie_count + 1 + + new_movie = Movie.last + expect(new_movie.title).must_equal "test movie" + expect(new_movie.inventory).must_equal 10 + expect(new_movie.overview).must_equal "fascinating" + expect(new_movie.release_date.to_s).must_equal "2019-10-01" + expect(new_movie.available_inventory).must_equal 10 + end - it "edge case: check status and request body" do - end + it "returns bad_request when movie cannot be created" do + bad_params = { movie: { title: "bad movie", inventory: -24 } } + expect{post movies_path, params: bad_params}.wont_change 'Movie.count' + must_respond_with :bad_request + end end end From 8abd9be0f6318b3e1b29464ce13a2fc123c2d16e Mon Sep 17 00:00:00 2001 From: stupendousC Date: Thu, 7 Nov 2019 22:02:11 -0800 Subject: [PATCH 62/86] /customers/:id/current and /customers/:id/history both work --- app/controllers/application_controller.rb | 4 +-- app/controllers/customers_controller.rb | 39 ++++++++++++++++++++--- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index b5d44fda3..0f94056b7 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,6 +1,6 @@ class ApplicationController < ActionController::API - + def apply_query_params - + # refactor the query params into here... end end diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index 0cd2e122f..d7cca0cb3 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -1,6 +1,7 @@ class CustomersController < ApplicationController CUSTOMER_KEYS = [:id, :name, :registered_at, :postal_code, :phone, :movies_checked_out_count] - #return to :movies_checked_out_count + + before_action :get_customer, only: [:current, :history] def index customers = Customer.all @@ -37,15 +38,43 @@ def index end def current - # Go through customerInstance.rentals, get all the ones with returned: false - render json: { msg: "TODO" }, status: :ok + # Go through customerInstance.rentals, get all the ones with returned: false + # @customer is from before_action get_customer() + current_rentals = @customer.rentals.select { |rental| + !rental.returned + } + render json: current_rentals, status: :ok + return end def history # Go through customerInstance.rentals, get all the ones with returned: true - render json: { msg: "TODO" }, status: :ok - + # @customer is from before_action get_customer() + past_rentals = @customer.rentals.select { |rental| + rental.returned + } + render json: past_rentals, status: :ok + return + end + + private + def get_customer + # gets & evals the params[:id] from URL request, returns either a JSON error msg or an actual customer instance + ### in order for the found customer to be accessible, I *HAD* to add the @ in front of it, idk why + if params[:id].match? (/^\d+$/) # only accept chars of 0..9 + @customer = Customer.find_by(id: params[:id].to_i) + + if @customer.nil? + render json: { error: "No customer match for id# #{params[:id]}" }, status: :bad_request + return + end + + else + # regex match failed, params[:id] can't even be an integer + render json: { error: "That id# #{params[:id]} is not even valid"}, status: :bad_request + return + end end end From 35e73463fb3cf9c3f54594c27be9a2cfdb13406a Mon Sep 17 00:00:00 2001 From: stupendousC Date: Thu, 7 Nov 2019 22:06:36 -0800 Subject: [PATCH 63/86] setting up movies/current & movies/history --- Caroline_thoughts.txt | 11 ++++++++--- app/controllers/movies_controller.rb | 11 +++++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Caroline_thoughts.txt b/Caroline_thoughts.txt index 0ecbd438c..66fd1aa32 100644 --- a/Caroline_thoughts.txt +++ b/Caroline_thoughts.txt @@ -10,15 +10,20 @@ GET /rentals/overdue with optionA, I search Customer for all those who has movies_checked_out_count > 0, then iterate thru and find customerInstance.rentals where returned: false && due_date < today +### GET /movies/:id/current I vote for Option A, same reason + Go through movieInstance.rentals, where returned: false GET /movies/:id/history - Go through movieInstance.rentals, where returned: true (bc IN THE PAST) - WAIT... no I think it's BOTH returned: true/false... it doesn't matter if it's been returned or not + Go through movieInstance.rentals, where returned: true +### + +### GET /customers/:id/current Go through customerInstance.rentals, get all the ones with returned: false GET /customers/:id/history - Go through customerInstance.rentals, get all the ones with returned: true \ No newline at end of file + Go through customerInstance.rentals, get all the ones with returned: true +### \ No newline at end of file diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 343a6321e..5775be2af 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -1,7 +1,7 @@ class MoviesController < ApplicationController - + MOVIE_KEYS = [:id, :title, :overview, :release_date, :inventory, :available_inventory] - + def index movies = Movie.all @@ -30,6 +30,13 @@ def create return movie.id end + def current + # sends list of customers who've checked out a specific movie right now + render json: { msg: "TODO" }, status: :ok + + end + + def history # sends list of customers who've checked out a specific movie in the past render json: { msg: "TODO" }, status: :ok From 0725a1922ed1539570bdc23e87a6638bb0766809 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Thu, 7 Nov 2019 22:07:52 -0800 Subject: [PATCH 64/86] forgot to add 1 more route for /movies/:id/current --- config/routes.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/config/routes.rb b/config/routes.rb index d6b532411..d60cdf820 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,7 @@ Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html resources :movies, only: [:index, :show, :create] + get "/movies/:id/current", to: "movies#current", as: "movie_current" get "/movies/:id/history", to: "movies#history", as: "movie_hx" resources :customers, only: [:index] From a8a13f6e3ae60cd43a4aeb067e99780b0e40102f Mon Sep 17 00:00:00 2001 From: stupendousC Date: Thu, 7 Nov 2019 22:16:29 -0800 Subject: [PATCH 65/86] /movies/:id/history and /movies/:id/current both work now. can probably stand to DRY out some... --- app/controllers/movies_controller.rb | 35 ++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 5775be2af..385a8ff3e 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -2,6 +2,8 @@ class MoviesController < ApplicationController MOVIE_KEYS = [:id, :title, :overview, :release_date, :inventory, :available_inventory] + before_action :get_movie, only: [:current, :history] + def index movies = Movie.all @@ -32,15 +34,20 @@ def create def current # sends list of customers who've checked out a specific movie right now - render json: { msg: "TODO" }, status: :ok - + current_rentals = @movie.rentals.select { |rental| + !rental.returned + } + render json: current_rentals, status: :ok + return end - def history # sends list of customers who've checked out a specific movie in the past - render json: { msg: "TODO" }, status: :ok - + past_rentals = @movie.rentals.select { |rental| + rental.returned + } + render json: past_rentals, status: :ok + return end private @@ -48,4 +55,22 @@ def history def movie_params return params.permit(:title, :overview, :release_date, :inventory) end + + def get_movie + # gets & evals the params[:id] from URL request, returns either a JSON error msg or an actual movie instance + ### in order for the found movie to be accessible, I *HAD* to add the @ in front of it, idk why + if params[:id].match? (/^\d+$/) # only accept chars of 0..9 + @movie = Movie.find_by(id: params[:id].to_i) + + if @movie.nil? + render json: { error: "No movie match for id# #{params[:id]}" }, status: :bad_request + return + end + + else + # regex match failed, params[:id] can't even be an integer + render json: { error: "That id# #{params[:id]} is not even valid"}, status: :bad_request + return + end + end end From 98e5fcc8ca476481bf4a4d8067173cd117401c9d Mon Sep 17 00:00:00 2001 From: stupendousC Date: Thu, 7 Nov 2019 22:45:21 -0800 Subject: [PATCH 66/86] rentals/overdue works --- Caroline_thoughts.txt | 7 +++---- app/controllers/rentals_controller.rb | 8 ++------ 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/Caroline_thoughts.txt b/Caroline_thoughts.txt index 66fd1aa32..c9db48da0 100644 --- a/Caroline_thoughts.txt +++ b/Caroline_thoughts.txt @@ -1,7 +1,4 @@ ###### THOUGHTS... -customerInstance.movies_checked_out_count - Go through customerInstance.rentals, count all the ones with returned: false - GET /rentals/overdue Option A: go through each affected customer? @@ -26,4 +23,6 @@ GET /customers/:id/current GET /customers/:id/history Go through customerInstance.rentals, get all the ones with returned: true -### \ No newline at end of file +### + +! Can probably refactor out some more between the prev 4 GET lines... \ No newline at end of file diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index e5f3477b7..d388ac3ff 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -58,19 +58,15 @@ def check_in end def overdue - # HOW DO I SEARCH FOR >0 ???? - possible_customers = Customer.where(movies_checked_out_count: 1) + possible_customers = Customer.where("movies_checked_out_count > ?", 0) overdue_customers = [] - render json: { msg: "Still working out the kinks" }, status: :ok - return - - ### ALSO this doesn't work lol possible_customers.each do |customer| customer.rentals.each do |rental| unless rental.returned if rental.due_date.to_date < Date.today overdue_customers << customer + # break out of this enumerable loop & move on to next customer break end end From 7136d1338588fc0e3733a9910603103672e32395 Mon Sep 17 00:00:00 2001 From: Emily Vomacka Date: Thu, 7 Nov 2019 22:53:28 -0800 Subject: [PATCH 67/86] more rentals test --- app/controllers/rentals_controller.rb | 3 +- app/models/customer.rb | 5 -- test/controllers/rentals_controller_test.rb | 84 +++++++++++---------- test/fixtures/customers.yml | 2 +- 4 files changed, 47 insertions(+), 47 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 5cfcaff41..c1bc59857 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -29,6 +29,7 @@ def check_out render json: { msg: "Rental id #{this_rental.id}: #{this_rental.movie.title} due on #{this_rental.due_date}"}, status: :ok else # failed Rental validations: if movie and/or customer don't exist + # Caroline! I like the idea of putting these messages up front. :) render json: { errors: "Cannot make a rental", error_msgs: new_rental.errors.full_messages }, status: :bad_request end @@ -50,7 +51,7 @@ def check_in this_movie = rental.movie this_movie.update(available_inventory: this_movie.available_inventory + 1) rental.update(returned: true) - render json: { msg: "Rental id#{rental.id}: #{rental.movie.title} has been returned." }, status: :ok + render json: { msg: "Rental id #{rental.id}: #{rental.movie.title} has been returned." }, status: :ok end end diff --git a/app/models/customer.rb b/app/models/customer.rb index 2f934c767..0680fe237 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -4,9 +4,4 @@ class Customer < ApplicationRecord validates :name, presence: :true validates :phone, presence: :true, uniqueness: :true - - #I don't think we need this method anymore! - # def movies_checked_out_count - # return self.movies.count - # end end diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index de5440de5..ed8bac5d4 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -10,7 +10,6 @@ describe "successful check_out with valid inputs" do before do - # is this better than having a huge IT block? @rental_count_before = Rental.count @m1_avail_inv_before = m1.available_inventory @c3_movie_count_before = c3.movies_checked_out_count @@ -36,14 +35,12 @@ end it "updates customer's movies_checked_out_count correctly" do - ### WHY???!!! Postman still passed! I also manually tested in rails console! expect(@c3_movie_count_after).must_equal @c3_movie_count_before + 1 end it "returns success status & JSON with expected message" do - check_response(expected_type: Hash, expected_status: :success) + body = check_response(expected_type: Hash, expected_status: :success) - body = JSON.parse(response.body) expect(body.keys).must_equal ["msg"] expect(body.values).must_equal ["Rental id #{Rental.last.id}: #{m1.title} due on #{Rental.last.due_date}"] end @@ -55,9 +52,8 @@ m1.update!(available_inventory: 0) expect{post check_out_path, params: {movie_id: m1.id, customer_id: c3.id}}.wont_change "Rental.count" - check_response(expected_type: Hash, expected_status: :bad_request) - - body = JSON.parse(response.body) + body = check_response(expected_type: Hash, expected_status: :bad_request) + expect(body.keys).must_equal ["errors"] expect(body.values).must_equal ["Cannot make a rental because movie #{m1.title.capitalize} ran out of copies"] end @@ -68,9 +64,8 @@ post check_out_path, params: nil_customer_hash - check_response(expected_type: Hash, expected_status: :bad_request) - - body = JSON.parse(response.body) + body = check_response(expected_type: Hash, expected_status: :bad_request) + expect(body.keys.sort).must_equal ["error_msgs", "errors"] expect(body["errors"]).must_equal "Cannot make a rental" expect(body["error_msgs"]).must_equal bad_rental.errors.full_messages @@ -96,40 +91,49 @@ end describe "CHECK_IN" do - - describe "successful check-ins with valid inputs" do - - it "will not add new rental instances" do - # TODO - end - - it "will increment movie.available_inventory by 1" do - # TODO - end - - it "will not decrease customer's movies_checked_out by 1" do - # TODO - end - - it "returns success status & JSON with expected message" do - # TODO + let (:c1) { customers(:c1) } + let (:c2) { customers(:c2) } + let (:m1) { movies(:m1) } + let (:m2) { movies(:m2) } + let (:r2) { rentals(:r2) } + let (:check_in_params) { {movie_id: m1.id, customer_id: c2.id} } + + it "successfully checks in a checked-out movie" do + skip + assert_difference ->{ c2.movies_checked_out_count } => -1, ->{ m1.available_inventory } => 1, ->{ Rental.count } => 0 do + post check_in_path, params: check_in_params end - + + expect(r2.returned).must_equal true + end + + it "returns success status & JSON with expected message" do + post check_in_path, params: check_in_params + body = check_response(expected_type: Hash) + expect(body.values).must_equal ["Rental id #{r2.id}: #{r2.movie.title} has been returned."] end - describe "edge cases" do - it "if rental doesn't exist, return error msg in JSON and bad_request status" do - # TODO - # render json: { errors: "Rental doesn't exist" }, status: :bad_request - - end - - it "if movie has already been returned" do - # TODO - # render json: { errors: "Rental has already been returned" }, status: :bad_request - - end + it "if rental has already been returned, return error msg in JSON with :bad_request status" do + bad_params = { movie_id: m1.id, customer_id: c1.id} + + post check_in_path, params: bad_params + body = check_response(expected_type: Hash, expected_status: :bad_request) + + expect(body.keys).must_equal ["errors"] + expect(body.values).must_equal ["Rental has already been returned"] + end + + it "if rental doesn't exist, return error msg in JSON with :bad_request status" do + bad_params = { movie_id: m2.id, customer_id: c2.id } + + post check_in_path, params: bad_params + + body = check_response(expected_type: Hash, expected_status: :bad_request) + + expect(body.keys).must_equal ["errors"] + expect(body.values).must_equal ["Rental doesn't exist"] end end end + diff --git a/test/fixtures/customers.yml b/test/fixtures/customers.yml index c6c0a687d..f226b1dc1 100644 --- a/test/fixtures/customers.yml +++ b/test/fixtures/customers.yml @@ -14,7 +14,7 @@ c2: city: springfield state: IL postal_code: 12345 - phone: 1-800-diddly + phone: 1-800-4444 registered_at: 2018-12-25 movies_checked_out_count: 2 From b5d82ffe36ba74fb1ceb2d976e5ce71dbe9754ca Mon Sep 17 00:00:00 2001 From: stupendousC Date: Thu, 7 Nov 2019 22:56:52 -0800 Subject: [PATCH 68/86] successful refactoring of get_db_object --- app/controllers/application_controller.rb | 19 +++++++++++++++++++ app/controllers/customers_controller.rb | 15 +-------------- app/controllers/movies_controller.rb | 17 ++--------------- 3 files changed, 22 insertions(+), 29 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0f94056b7..cf6db562a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,4 +3,23 @@ class ApplicationController < ActionController::API def apply_query_params # refactor the query params into here... end + + def get_db_object(model:) + # gets & evals the params[:id] from URL request, returns either a JSON error msg or an actual customer instance + if params[:id].match? (/^\d+$/) # only accept chars of 0..9 + object = model.find_by(id: params[:id].to_i) + + if object.nil? + render json: { error: "No #{model} match for id# #{params[:id]}" }, status: :bad_request + return + else + return object + end + + else + # regex match failed, params[:id] can't even be an integer + render json: { error: "That id# #{params[:id]} is not even valid"}, status: :bad_request + return + end + end end diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index d7cca0cb3..dd5a26ebd 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -60,21 +60,8 @@ def history private def get_customer - # gets & evals the params[:id] from URL request, returns either a JSON error msg or an actual customer instance ### in order for the found customer to be accessible, I *HAD* to add the @ in front of it, idk why - if params[:id].match? (/^\d+$/) # only accept chars of 0..9 - @customer = Customer.find_by(id: params[:id].to_i) - - if @customer.nil? - render json: { error: "No customer match for id# #{params[:id]}" }, status: :bad_request - return - end - - else - # regex match failed, params[:id] can't even be an integer - render json: { error: "That id# #{params[:id]} is not even valid"}, status: :bad_request - return - end + @customer = get_db_object(model: Customer) end end diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 385a8ff3e..736688c0f 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -57,20 +57,7 @@ def movie_params end def get_movie - # gets & evals the params[:id] from URL request, returns either a JSON error msg or an actual movie instance ### in order for the found movie to be accessible, I *HAD* to add the @ in front of it, idk why - if params[:id].match? (/^\d+$/) # only accept chars of 0..9 - @movie = Movie.find_by(id: params[:id].to_i) - - if @movie.nil? - render json: { error: "No movie match for id# #{params[:id]}" }, status: :bad_request - return - end - - else - # regex match failed, params[:id] can't even be an integer - render json: { error: "That id# #{params[:id]} is not even valid"}, status: :bad_request - return - end + @movie = get_db_object(model: Movie) end -end +end \ No newline at end of file From eec01b313c486602304386b010e4e0c27f325897 Mon Sep 17 00:00:00 2001 From: Emily Vomacka Date: Thu, 7 Nov 2019 23:03:56 -0800 Subject: [PATCH 69/86] still refactoring --- test/controllers/rentals_controller_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index ed8bac5d4..8d140b17e 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -99,7 +99,7 @@ let (:check_in_params) { {movie_id: m1.id, customer_id: c2.id} } it "successfully checks in a checked-out movie" do - skip + # skip assert_difference ->{ c2.movies_checked_out_count } => -1, ->{ m1.available_inventory } => 1, ->{ Rental.count } => 0 do post check_in_path, params: check_in_params end From 9bfcb6c3c771394d37e8fed57856bfed1ac76275 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Thu, 7 Nov 2019 23:05:53 -0800 Subject: [PATCH 70/86] successfully refactored out get_active_rentals_from() and get_past_rentals_from() --- app/controllers/application_controller.rb | 8 ++++++++ app/controllers/customers_controller.rb | 9 ++------- app/controllers/movies_controller.rb | 8 ++------ 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index cf6db562a..8f492bfd3 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -22,4 +22,12 @@ def get_db_object(model:) return end end + + def get_active_rentals_from(instance:) + return instance.rentals.select { |rental| !rental.returned } + end + + def get_past_rentals_from(instance:) + return instance.rentals.select { |rental| rental.returned } + end end diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index dd5a26ebd..20fc80f86 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -40,10 +40,7 @@ def index def current # Go through customerInstance.rentals, get all the ones with returned: false # @customer is from before_action get_customer() - - current_rentals = @customer.rentals.select { |rental| - !rental.returned - } + current_rentals = get_active_rentals_from(instance: @customer) render json: current_rentals, status: :ok return end @@ -51,9 +48,7 @@ def current def history # Go through customerInstance.rentals, get all the ones with returned: true # @customer is from before_action get_customer() - past_rentals = @customer.rentals.select { |rental| - rental.returned - } + past_rentals = get_past_rentals_from(instance: @customer) render json: past_rentals, status: :ok return end diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 736688c0f..591c568ca 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -34,18 +34,14 @@ def create def current # sends list of customers who've checked out a specific movie right now - current_rentals = @movie.rentals.select { |rental| - !rental.returned - } + current_rentals = get_active_rentals_from(instance: @movie) render json: current_rentals, status: :ok return end def history # sends list of customers who've checked out a specific movie in the past - past_rentals = @movie.rentals.select { |rental| - rental.returned - } + past_rentals = get_past_rentals_from(instance: @movie) render json: past_rentals, status: :ok return end From 46124288b06a0fc29046d7e295b2e62c2941b439 Mon Sep 17 00:00:00 2001 From: Emily Vomacka Date: Thu, 7 Nov 2019 23:49:38 -0800 Subject: [PATCH 71/86] ALL RENTAL TESTS PASSING --- test/controllers/rentals_controller_test.rb | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index 8d140b17e..585185890 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -98,12 +98,18 @@ let (:r2) { rentals(:r2) } let (:check_in_params) { {movie_id: m1.id, customer_id: c2.id} } + before do + @m1_starting_avail_inventory = m1.available_inventory + @c2_starting_movies_co_count = c2.movies_checked_out_count + end + it "successfully checks in a checked-out movie" do # skip - assert_difference ->{ c2.movies_checked_out_count } => -1, ->{ m1.available_inventory } => 1, ->{ Rental.count } => 0 do - post check_in_path, params: check_in_params - end - + expect{ post check_in_path, params: check_in_params }.wont_change Rental.count + c2.reload + expect(c2.movies_checked_out_count).must_equal @c2_starting_movies_co_count - 1 + m1.reload + expect(m1.available_inventory).must_equal @m1_starting_avail_inventory + 1 expect(r2.returned).must_equal true end From 1a1d93fa8dbb5f03e11ea1e0319177e3f9374d6e Mon Sep 17 00:00:00 2001 From: stupendousC Date: Thu, 7 Nov 2019 23:52:55 -0800 Subject: [PATCH 72/86] trying out query params all over Movies_ctroller --- app/controllers/application_controller.rb | 50 ++++++++++++++++++++++- app/controllers/movies_controller.rb | 13 +++++- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 8f492bfd3..c4ab77a27 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,7 +1,53 @@ class ApplicationController < ActionController::API - def apply_query_params - # refactor the query params into here... + def validate_query_params(acceptable_keys:) + approved_params = {} + + if params[:sort] + if acceptable_keys.include? params[:sort] + approved_params[:sort] = params[:sort] + else + render json: { errors: "Can't sort anything with key of #{params[:sort]}" }, status: :bad_request + return + end + end + + if params[:n] && params[:p] + if (params[:n].match? (/^\d+$/)) && (params[:p].match? (/^\d+$/)) + # only valid integers will make it to this point + limit_amt = params[:n].to_i + offset_amt = (params[:p].to_i - 1) * limit_amt + + if (limit_amt > 0) && (offset_amt >= 0) + approved_params[:limit_amt] = limit_amt + approved_params[:offset_amt] = offset_amt + else + render json: { errors: "Invalid n & p combo" }, status: :bad_request + return + end + end + elsif params[:n] || params[:p] + render json: { errors: "We require both n and p, not just one of them" }, status: :bad_request + return + end + + return approved_params + end + + def apply_query_params(array_of_objs:, approved_params:) + + # do this first to lower amount to sort later + if approved_params[:limit_amt] && approved_params[:offset_amt] + limit_amt = approved_params[:limit_amt] + offset_amt = approved_params[:offset_amt] + array_of_objs = array_of_objs.limit(limit_amt).offset(offset_amt) + end + + if approved_params[:sort] + array_of_objs = array_of_objs.sort_by{ |obj| obj[approved_params[:sort].to_s] } + end + + return array_of_objs end def get_db_object(model:) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 591c568ca..de4fc60c2 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -3,11 +3,18 @@ class MoviesController < ApplicationController MOVIE_KEYS = [:id, :title, :overview, :release_date, :inventory, :available_inventory] before_action :get_movie, only: [:current, :history] + before_action :get_query_params, only: [:index, :current, :history] def index - movies = Movie.all + + if @approved_params + movies = apply_query_params(array_of_objs: Movie.all, approved_params: @approved_params) + else + movies = Movie.all + end render json: movies.as_json( only: [:id, :title, :release_date]), status: :ok + return end def show @@ -56,4 +63,8 @@ def get_movie ### in order for the found movie to be accessible, I *HAD* to add the @ in front of it, idk why @movie = get_db_object(model: Movie) end + + def get_query_params + @approved_params = validate_query_params(acceptable_keys: ["title", "release_date"]) + end end \ No newline at end of file From b9c85c2e1ec69cd7dc386d112f0aa4a0f6cd397f Mon Sep 17 00:00:00 2001 From: stupendousC Date: Thu, 7 Nov 2019 23:58:29 -0800 Subject: [PATCH 73/86] Movies#index query params work, but not in movies#current & movies#history, fix later --- app/controllers/movies_controller.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index de4fc60c2..6aa9a0a86 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -42,6 +42,12 @@ def create def current # sends list of customers who've checked out a specific movie right now current_rentals = get_active_rentals_from(instance: @movie) + + ### NOT SURE WHAT I MESSED UP HERE... + # if @approved_params + # current_rentals = apply_query_params(array_of_objs: current_rentals, approved_params: @approved_params) + # end + render json: current_rentals, status: :ok return end @@ -49,6 +55,12 @@ def current def history # sends list of customers who've checked out a specific movie in the past past_rentals = get_past_rentals_from(instance: @movie) + + ### NOT SURE WHAT I MESSED UP HERE... + # if @approved_params + # past_rentals = apply_query_params(array_of_objs: past_rentals, approved_params: @approved_params) + # end + render json: past_rentals, status: :ok return end From 3d93280abb53b40e5eb476c8d1810232d56b4ca9 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Fri, 8 Nov 2019 11:24:09 -0800 Subject: [PATCH 74/86] query parameters sort/n/p successfully implemented in movies#history and movies#current --- Gemfile | 3 ++ Gemfile.lock | 10 ++----- app/controllers/application_controller.rb | 22 +++++++++------ app/controllers/movies_controller.rb | 34 +++++++++++++++-------- 4 files changed, 40 insertions(+), 29 deletions(-) diff --git a/Gemfile b/Gemfile index 8143da43e..867d77608 100644 --- a/Gemfile +++ b/Gemfile @@ -57,3 +57,6 @@ group :test do gem 'minitest-rails' gem 'minitest-reporters' end + +# Supposed to help, but couldn't get it to work on stuff retrieved from db +gem 'will_paginate', '~> 3.1.0' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 82f09ef4c..92abfa80d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -54,14 +54,12 @@ GEM debase (0.2.4.1) debase-ruby_core_source (>= 0.10.2) debase-ruby_core_source (0.10.6) - docile (1.3.2) erubi (1.9.0) ffi (1.11.1) globalid (0.4.2) activesupport (>= 4.2.0) i18n (1.7.0) concurrent-ruby (~> 1.0) - json (2.2.0) listen (3.1.5) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) @@ -132,11 +130,6 @@ GEM rake (>= 0.8.1) ruby-progressbar (1.10.1) ruby_dep (1.5.0) - simplecov (0.17.1) - docile (~> 1.1) - json (>= 1.8, < 3) - simplecov-html (~> 0.10.0) - simplecov-html (0.10.2) spring (2.1.0) spring-watcher-listen (2.0.1) listen (>= 2.7, < 4.0) @@ -155,6 +148,7 @@ GEM websocket-driver (0.7.1) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.4) + will_paginate (3.1.8) PLATFORMS ruby @@ -171,10 +165,10 @@ DEPENDENCIES puma (~> 3.11) rails (~> 5.2.3) ruby-debug-ide (>= 0.7.0) - simplecov spring spring-watcher-listen (~> 2.0.0) tzinfo-data + will_paginate (~> 3.1.0) RUBY VERSION ruby 2.5.5p157 diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c4ab77a27..a4aa47e8e 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -15,12 +15,12 @@ def validate_query_params(acceptable_keys:) if params[:n] && params[:p] if (params[:n].match? (/^\d+$/)) && (params[:p].match? (/^\d+$/)) # only valid integers will make it to this point - limit_amt = params[:n].to_i - offset_amt = (params[:p].to_i - 1) * limit_amt + per_page = params[:n].to_i + page = params[:p].to_i - if (limit_amt > 0) && (offset_amt >= 0) - approved_params[:limit_amt] = limit_amt - approved_params[:offset_amt] = offset_amt + if (per_page > 0) && (page > 0) + approved_params[:per_page] = per_page + approved_params[:page] = page else render json: { errors: "Invalid n & p combo" }, status: :bad_request return @@ -37,10 +37,14 @@ def validate_query_params(acceptable_keys:) def apply_query_params(array_of_objs:, approved_params:) # do this first to lower amount to sort later - if approved_params[:limit_amt] && approved_params[:offset_amt] - limit_amt = approved_params[:limit_amt] - offset_amt = approved_params[:offset_amt] - array_of_objs = array_of_objs.limit(limit_amt).offset(offset_amt) + if approved_params[:per_page] && approved_params[:page] + # could NOT get will_paginate gem to work, does it only talk directly to db? and not on stuff retrieved from db? + # array_of_objs = array_of_objs.paginate(page: approved_params[:page], per_page: approved_params[:per_page]) + + # doing it old school + index_of_first_target = approved_params[:per_page] * (approved_params[:page]-1) + index_of_last_target = index_of_first_target + approved_params[:per_page] - 1 + array_of_objs = array_of_objs[index_of_first_target..index_of_last_target] end if approved_params[:sort] diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 6aa9a0a86..9f55c7e08 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -43,26 +43,36 @@ def current # sends list of customers who've checked out a specific movie right now current_rentals = get_active_rentals_from(instance: @movie) - ### NOT SURE WHAT I MESSED UP HERE... - # if @approved_params - # current_rentals = apply_query_params(array_of_objs: current_rentals, approved_params: @approved_params) - # end + if @approved_params + # any kind of params[:sort] declared by user would just be redundant and have zero effect + current_rentals = apply_query_params(array_of_objs: current_rentals, approved_params: @approved_params) + end - render json: current_rentals, status: :ok - return + if current_rentals + render json: current_rentals, status: :ok + return + else + render json: { error: "Nothing to show you b/c of bad query parameters"}, status: :bad_request + return + end end def history # sends list of customers who've checked out a specific movie in the past past_rentals = get_past_rentals_from(instance: @movie) - ### NOT SURE WHAT I MESSED UP HERE... - # if @approved_params - # past_rentals = apply_query_params(array_of_objs: past_rentals, approved_params: @approved_params) - # end + if @approved_params + # any kind of params[:sort] declared by user would just be redundant and have zero effect + past_rentals = apply_query_params(array_of_objs: past_rentals, approved_params: @approved_params) + end - render json: past_rentals, status: :ok - return + if past_rentals + render json: past_rentals, status: :ok + return + else + render json: { error: "Nothing to show you b/c of bad query parameters"}, status: :bad_request + return + end end private From 3fd7e5f8fde71dff523214cffe0defac54c2590b Mon Sep 17 00:00:00 2001 From: stupendousC Date: Fri, 8 Nov 2019 11:32:43 -0800 Subject: [PATCH 75/86] refactored out render_error_json_query_params --- app/controllers/application_controller.rb | 10 ++++++++++ app/controllers/movies_controller.rb | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index a4aa47e8e..a842aaa68 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -54,6 +54,16 @@ def apply_query_params(array_of_objs:, approved_params:) return array_of_objs end + def render_error_json(msg: "An error has occurred, please call customer service 1-800-LOL-WHAT") + render json: { error: msg}, status: :bad_request + return + end + + def render_error_json_bad_query_params + render_error_json(msg: "Nothing to show you b/c of bad query parameters") + return + end + def get_db_object(model:) # gets & evals the params[:id] from URL request, returns either a JSON error msg or an actual customer instance if params[:id].match? (/^\d+$/) # only accept chars of 0..9 diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 9f55c7e08..ab3e0130b 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -52,7 +52,7 @@ def current render json: current_rentals, status: :ok return else - render json: { error: "Nothing to show you b/c of bad query parameters"}, status: :bad_request + render_error_json_bad_query_params return end end @@ -70,7 +70,7 @@ def history render json: past_rentals, status: :ok return else - render json: { error: "Nothing to show you b/c of bad query parameters"}, status: :bad_request + render_error_json_bad_query_params return end end From c3c9acd87cad01e39fb63f202a2c6e4ff52e5e3d Mon Sep 17 00:00:00 2001 From: stupendousC Date: Fri, 8 Nov 2019 11:56:44 -0800 Subject: [PATCH 76/86] more refactoring on render_error_json --- app/controllers/application_controller.rb | 13 +++++++++---- app/controllers/movies_controller.rb | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index a842aaa68..21b09c4a2 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -54,13 +54,18 @@ def apply_query_params(array_of_objs:, approved_params:) return array_of_objs end - def render_error_json(msg: "An error has occurred, please call customer service 1-800-LOL-WHAT") - render json: { error: msg}, status: :bad_request - return + def render_error_json(error: "An error has occurred, please call customer service 1-800-LOL-WHAT", error_msgs: nil) + if error_msgs + render json: { error: error, error_msgs: error_msgs }, status: :bad_request + return + else + render json: { error: error }, status: :bad_request + return + end end def render_error_json_bad_query_params - render_error_json(msg: "Nothing to show you b/c of bad query parameters") + render_error_json(error: "Nothing to show you b/c of bad query parameters") return end diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index ab3e0130b..143b35e3f 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -23,7 +23,7 @@ def show if movie render json: movie.as_json( only: MOVIE_KEYS), status: :ok else - render json: { errors: "Movie not found" }, status: :ok + render_error_json(error: "Movie not found") end end From 29d74b2db882dc5a8ece9fb43358406758426dda Mon Sep 17 00:00:00 2001 From: stupendousC Date: Fri, 8 Nov 2019 12:39:40 -0800 Subject: [PATCH 77/86] satisfied w/ amount of refactoring done, moving on --- app/controllers/movies_controller.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 143b35e3f..671f4e8c1 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -22,8 +22,10 @@ def show if movie render json: movie.as_json( only: MOVIE_KEYS), status: :ok + return else render_error_json(error: "Movie not found") + return end end @@ -33,10 +35,14 @@ def create if movie.save render json: { msg: "Movie #{movie.title.capitalize} added to database", id: Movie.last.id }, status: :ok + return else - render json: { errors: "Cannot add movie", error_msgs: movie.errors.full_messages }, status: :bad_request + render_error_json(errors: "Cannot add movie", error_msgs: movie.errors.full_messages) + return end - return movie.id + + ## is this line necessary? + # return movie.id end def current From 13524a36c918bfa0050cc64d861750b1581676a3 Mon Sep 17 00:00:00 2001 From: Emily Vomacka Date: Fri, 8 Nov 2019 14:07:02 -0800 Subject: [PATCH 78/86] test fixes --- app/controllers/application_controller.rb | 11 ++--------- app/controllers/movies_controller.rb | 7 +------ app/controllers/rentals_controller.rb | 16 ++++------------ 3 files changed, 7 insertions(+), 27 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 21b09c4a2..3c5224f73 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -36,12 +36,7 @@ def validate_query_params(acceptable_keys:) def apply_query_params(array_of_objs:, approved_params:) - # do this first to lower amount to sort later if approved_params[:per_page] && approved_params[:page] - # could NOT get will_paginate gem to work, does it only talk directly to db? and not on stuff retrieved from db? - # array_of_objs = array_of_objs.paginate(page: approved_params[:page], per_page: approved_params[:per_page]) - - # doing it old school index_of_first_target = approved_params[:per_page] * (approved_params[:page]-1) index_of_last_target = index_of_first_target + approved_params[:per_page] - 1 array_of_objs = array_of_objs[index_of_first_target..index_of_last_target] @@ -54,7 +49,7 @@ def apply_query_params(array_of_objs:, approved_params:) return array_of_objs end - def render_error_json(error: "An error has occurred, please call customer service 1-800-LOL-WHAT", error_msgs: nil) + def render_errors_json(error: "An error has occurred, please call customer service 1-800-LOL-WHAT", error_msgs: nil) if error_msgs render json: { error: error, error_msgs: error_msgs }, status: :bad_request return @@ -70,8 +65,7 @@ def render_error_json_bad_query_params end def get_db_object(model:) - # gets & evals the params[:id] from URL request, returns either a JSON error msg or an actual customer instance - if params[:id].match? (/^\d+$/) # only accept chars of 0..9 + if params[:id].match? (/^\d+$/) object = model.find_by(id: params[:id].to_i) if object.nil? @@ -82,7 +76,6 @@ def get_db_object(model:) end else - # regex match failed, params[:id] can't even be an integer render json: { error: "That id# #{params[:id]} is not even valid"}, status: :bad_request return end diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 671f4e8c1..678df8ac3 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -24,7 +24,7 @@ def show render json: movie.as_json( only: MOVIE_KEYS), status: :ok return else - render_error_json(error: "Movie not found") + render_errors_json(error: "Movie not found") return end end @@ -40,17 +40,12 @@ def create render_error_json(errors: "Cannot add movie", error_msgs: movie.errors.full_messages) return end - - ## is this line necessary? - # return movie.id end def current - # sends list of customers who've checked out a specific movie right now current_rentals = get_active_rentals_from(instance: @movie) if @approved_params - # any kind of params[:sort] declared by user would just be redundant and have zero effect current_rentals = apply_query_params(array_of_objs: current_rentals, approved_params: @approved_params) end diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 80e6f8ee8..4b78c2255 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -11,14 +11,13 @@ def check_out ### EMILY!!! Can we make this part of Rental model validations? IDK if it's bad SRP practice... ### I think we'll have to look into custom model validations if we do that... movie = new_rental.movie - if movie && movie.available_inventory <= 0 + if movie.nil? || movie.available_inventory <= 0 render json: { errors: "Cannot make a rental because movie #{movie.title.capitalize} ran out of copies"}, status: :bad_request return end if new_rental.save this_rental = Rental.last - # update movie's avail count movie.update(available_inventory: movie.available_inventory - 1) # update customer's checked_out_count @@ -29,8 +28,6 @@ def check_out render json: { msg: "Rental id #{this_rental.id}: #{this_rental.movie.title} due on #{this_rental.due_date}"}, status: :ok return else - # failed Rental validations: if movie and/or customer don't exist - # Caroline! I like the idea of putting these messages up front. :) render json: { errors: "Cannot make a rental", error_msgs: new_rental.errors.full_messages }, status: :bad_request return end @@ -39,8 +36,6 @@ def check_out def check_in rental = Rental.find_by(movie_id: rental_params[:movie_id].to_i, customer_id: rental_params[:customer_id].to_i) - # render json: { msg: "Rental id#{rental.id}: #{rental.movie.title} has been rented by #{rental.customer.name}." }, status: :ok - # return if rental.nil? render json: { errors: "Rental doesn't exist" }, status: :bad_request return @@ -48,13 +43,10 @@ def check_in render json: { errors: "Rental has already been returned" }, status: :bad_request return else - this_customer = rental.customer - this_customer.update(movies_checked_out_count: this_customer.movies_checked_out_count - 1) - this_movie = rental.movie - this_movie.update(available_inventory: this_movie.available_inventory + 1) + rental.customer.update(movies_checked_out_count: rental.customer.movies_checked_out_count - 1) + rental.movie.update(available_inventory: rental.movie.available_inventory + 1) rental.update(returned: true) - - render json: { msg: "Rental id#{rental.id}: #{rental.movie.title} has been returned." }, status: :ok + render json: { msg: "Rental id #{rental.id}: #{rental.movie.title} has been returned." }, status: :ok return end end From c48913827a540021d2034a7c4c42f4973eec6a46 Mon Sep 17 00:00:00 2001 From: Emily Vomacka Date: Fri, 8 Nov 2019 14:24:17 -0800 Subject: [PATCH 79/86] test fixes --- app/controllers/movies_controller.rb | 2 +- app/controllers/rentals_controller.rb | 23 +++++++-------------- test/controllers/movies_controller_test.rb | 5 ++--- test/controllers/rentals_controller_test.rb | 5 ++--- 4 files changed, 13 insertions(+), 22 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 678df8ac3..cc15f2281 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -37,7 +37,7 @@ def create render json: { msg: "Movie #{movie.title.capitalize} added to database", id: Movie.last.id }, status: :ok return else - render_error_json(errors: "Cannot add movie", error_msgs: movie.errors.full_messages) + render_errors_json(error: "Cannot add movie", error_msgs: movie.errors.full_messages) return end end diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 4b78c2255..b497d711e 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -7,31 +7,24 @@ def check_out new_rental.due_date = Date.today + 7.days new_rental.returned = false - # does this movie (if existing) even have available_inventory? - ### EMILY!!! Can we make this part of Rental model validations? IDK if it's bad SRP practice... - ### I think we'll have to look into custom model validations if we do that... - movie = new_rental.movie - if movie.nil? || movie.available_inventory <= 0 - render json: { errors: "Cannot make a rental because movie #{movie.title.capitalize} ran out of copies"}, status: :bad_request + if new_rental.movie.nil? || new_rental.customer.nil? + render json: { errors: "Cannot make a rental", error_msgs: new_rental.errors.full_messages }, status: :bad_request + return + elsif new_rental.movie.available_inventory <= 0 + render json: { errors: "Cannot make a rental because movie #{new_rental.movie.title.capitalize} ran out of copies"}, status: :bad_request return end - + if new_rental.save this_rental = Rental.last - movie.update(available_inventory: movie.available_inventory - 1) - - # update customer's checked_out_count - customer = this_rental.customer - customer.update(movies_checked_out_count: customer.movies_checked_out_count + 1) - - # prep API JSON + this_rental.movie.update(available_inventory: this_rental.movie.available_inventory - 1) + this_rental.customer.update(movies_checked_out_count: this_rental.customer.movies_checked_out_count + 1) render json: { msg: "Rental id #{this_rental.id}: #{this_rental.movie.title} due on #{this_rental.due_date}"}, status: :ok return else render json: { errors: "Cannot make a rental", error_msgs: new_rental.errors.full_messages }, status: :bad_request return end - end def check_in diff --git a/test/controllers/movies_controller_test.rb b/test/controllers/movies_controller_test.rb index cea41b336..0d98ac72d 100644 --- a/test/controllers/movies_controller_test.rb +++ b/test/controllers/movies_controller_test.rb @@ -48,10 +48,9 @@ it "will respond ok with an error JSON when movie not found" do Movie.destroy_all get movie_path(-1) - body = check_response(expected_type: Hash) - expect(body.keys).must_equal ["errors"] + body = check_response(expected_type: Hash, expected_status: :bad_request) + expect(body.keys).must_equal ["error"] expect(body.values).must_equal ["Movie not found"] - must_respond_with :success end end diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index 9d95a5c74..ca76fc85e 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -72,7 +72,7 @@ it "if rental.save unsuccessful due to nonexistent movie, return error msg in JSON and bad_request status" do - nil_movie_hash = {movie_id: nil, customer_id: c3.id} + nil_movie_hash = {movie_id: -2, customer_id: c3.id} bad_rental = Rental.create(nil_movie_hash) @@ -102,8 +102,7 @@ @c2_starting_movies_co_count = c2.movies_checked_out_count end - it "successfully checks in a checked-out movie" do - # skip + it "successfully checks in a checked-out movie" do expect{ post check_in_path, params: check_in_params }.wont_change Rental.count c2.reload expect(c2.movies_checked_out_count).must_equal @c2_starting_movies_co_count - 1 From 4f0ae5d3350b98455c14ee3a51a19d6c6932474a Mon Sep 17 00:00:00 2001 From: Emily Vomacka Date: Fri, 8 Nov 2019 14:29:03 -0800 Subject: [PATCH 80/86] nominal rental checkout working --- test/controllers/rentals_controller_test.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index ca76fc85e..090b43e9d 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -13,8 +13,9 @@ @rental_count_before = Rental.count @m1_avail_inv_before = m1.available_inventory @c3_movie_count_before = c3.movies_checked_out_count - post check_out_path, params: {movie_id: m1.id, customer_id: c3.id} + c3.reload + @c3_movie_count_after = c3.movies_checked_out_count end it "creates Rental instance with correct attribs" do @@ -34,6 +35,7 @@ end it "updates customer's movies_checked_out_count correctly" do + expect(@c3_movie_count_after).must_equal @c3_movie_count_before + 1 end From d27f77eb443a998631f6c0462e16c4197471583d Mon Sep 17 00:00:00 2001 From: stupendousC Date: Fri, 8 Nov 2019 14:31:02 -0800 Subject: [PATCH 81/86] merging --- test/controllers/rentals_controller_test.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index ca76fc85e..ecc9e6da0 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -52,7 +52,7 @@ expect{post check_out_path, params: {movie_id: m1.id, customer_id: c3.id}}.wont_change "Rental.count" body = check_response(expected_type: Hash, expected_status: :bad_request) - + expect(body.keys).must_equal ["errors"] expect(body.values).must_equal ["Cannot make a rental because movie #{m1.title.capitalize} ran out of copies"] end @@ -64,7 +64,7 @@ post check_out_path, params: nil_customer_hash body = check_response(expected_type: Hash, expected_status: :bad_request) - + expect(body.keys.sort).must_equal ["error_msgs", "errors"] expect(body["errors"]).must_equal "Cannot make a rental" expect(body["error_msgs"]).must_equal bad_rental.errors.full_messages @@ -96,12 +96,12 @@ let (:m2) { movies(:m2) } let (:r2) { rentals(:r2) } let (:check_in_params) { {movie_id: m1.id, customer_id: c2.id} } - + before do @m1_starting_avail_inventory = m1.available_inventory @c2_starting_movies_co_count = c2.movies_checked_out_count end - + it "successfully checks in a checked-out movie" do expect{ post check_in_path, params: check_in_params }.wont_change Rental.count c2.reload @@ -119,22 +119,22 @@ it "if rental has already been returned, return error msg in JSON with :bad_request status" do bad_params = { movie_id: m1.id, customer_id: c1.id} - + post check_in_path, params: bad_params body = check_response(expected_type: Hash, expected_status: :bad_request) - + expect(body.keys).must_equal ["errors"] expect(body.values).must_equal ["Rental has already been returned"] end it "if rental doesn't exist, return error msg in JSON with :bad_request status" do bad_params = { movie_id: m2.id, customer_id: c2.id } - + post check_in_path, params: bad_params - + body = check_response(expected_type: Hash, expected_status: :bad_request) - + expect(body.keys).must_equal ["errors"] expect(body.values).must_equal ["Rental doesn't exist"] end From df3832e402ddeaef031190604b4619d6982937b3 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Fri, 8 Nov 2019 14:41:59 -0800 Subject: [PATCH 82/86] fixed bugs in test codes --- app/controllers/rentals_controller.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index b497d711e..e73c0ccfb 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -8,13 +8,14 @@ def check_out new_rental.returned = false if new_rental.movie.nil? || new_rental.customer.nil? + new_rental.valid? render json: { errors: "Cannot make a rental", error_msgs: new_rental.errors.full_messages }, status: :bad_request return elsif new_rental.movie.available_inventory <= 0 render json: { errors: "Cannot make a rental because movie #{new_rental.movie.title.capitalize} ran out of copies"}, status: :bad_request return end - + if new_rental.save this_rental = Rental.last this_rental.movie.update(available_inventory: this_rental.movie.available_inventory - 1) From 169d99ea10a1d8a02d88e3c0011debdcf00f8bd1 Mon Sep 17 00:00:00 2001 From: Emily Vomacka Date: Fri, 8 Nov 2019 15:02:05 -0800 Subject: [PATCH 83/86] checkout/in methods --- app/controllers/rentals_controller.rb | 8 ++++---- app/models/customer.rb | 3 +++ app/models/movie.rb | 4 ++++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index e73c0ccfb..c8b595f1a 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -18,8 +18,8 @@ def check_out if new_rental.save this_rental = Rental.last - this_rental.movie.update(available_inventory: this_rental.movie.available_inventory - 1) - this_rental.customer.update(movies_checked_out_count: this_rental.customer.movies_checked_out_count + 1) + this_rental.movie.available_inventory_update(-1) + this_rental.customer.movies_checked_out_update(1) render json: { msg: "Rental id #{this_rental.id}: #{this_rental.movie.title} due on #{this_rental.due_date}"}, status: :ok return else @@ -37,8 +37,8 @@ def check_in render json: { errors: "Rental has already been returned" }, status: :bad_request return else - rental.customer.update(movies_checked_out_count: rental.customer.movies_checked_out_count - 1) - rental.movie.update(available_inventory: rental.movie.available_inventory + 1) + rental.customer.movies_checked_out_update(-1) + rental.movie.available_inventory_update(1) rental.update(returned: true) render json: { msg: "Rental id #{rental.id}: #{rental.movie.title} has been returned." }, status: :ok return diff --git a/app/models/customer.rb b/app/models/customer.rb index 7596cc304..2b438c779 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -5,4 +5,7 @@ class Customer < ApplicationRecord validates :name, presence: :true validates :phone, presence: :true, uniqueness: :true + def movies_checked_out_update(quantity) + self.update(movies_checked_out_count: self.movies_checked_out_count + quantity) + end end diff --git a/app/models/movie.rb b/app/models/movie.rb index afa79a251..ee4f0af66 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -4,4 +4,8 @@ class Movie < ApplicationRecord validates :title, presence: :true validates :inventory, presence: :true, numericality: { only_integer: true, greater_than: -1 } + + def available_inventory_update(quantity) + self.update(available_inventory: self.available_inventory + quantity) + end end \ No newline at end of file From 7438ace47af397cfb3cf55f59ef6534060664e57 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Fri, 8 Nov 2019 15:16:23 -0800 Subject: [PATCH 84/86] applied newly refactored method render_errors_json( ) to customers_ctrller --- app/controllers/rentals_controller.rb | 10 +++++----- test/controllers/rentals_controller_test.rb | 16 ++++++++-------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index c8b595f1a..b088a6a99 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -9,10 +9,10 @@ def check_out if new_rental.movie.nil? || new_rental.customer.nil? new_rental.valid? - render json: { errors: "Cannot make a rental", error_msgs: new_rental.errors.full_messages }, status: :bad_request + render_errors_json(error: "Cannot make a rental", error_msgs: new_rental.errors.full_messages ) return elsif new_rental.movie.available_inventory <= 0 - render json: { errors: "Cannot make a rental because movie #{new_rental.movie.title.capitalize} ran out of copies"}, status: :bad_request + render_errors_json(error: "Cannot make a rental because movie #{new_rental.movie.title.capitalize} ran out of copies") return end @@ -23,7 +23,7 @@ def check_out render json: { msg: "Rental id #{this_rental.id}: #{this_rental.movie.title} due on #{this_rental.due_date}"}, status: :ok return else - render json: { errors: "Cannot make a rental", error_msgs: new_rental.errors.full_messages }, status: :bad_request + render_errors_json(error: "Cannot make a rental", error_msgs: new_rental.errors.full_messages) return end end @@ -31,10 +31,10 @@ def check_out def check_in rental = Rental.find_by(movie_id: rental_params[:movie_id].to_i, customer_id: rental_params[:customer_id].to_i) if rental.nil? - render json: { errors: "Rental doesn't exist" }, status: :bad_request + render_errors_json(error: "Rental doesn't exist" ) return elsif rental.returned == true - render json: { errors: "Rental has already been returned" }, status: :bad_request + render_errors_json(error: "Rental has already been returned" ) return else rental.customer.movies_checked_out_update(-1) diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index 61ac51727..7e15e10b3 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -35,7 +35,7 @@ end it "updates customer's movies_checked_out_count correctly" do - + expect(@c3_movie_count_after).must_equal @c3_movie_count_before + 1 end @@ -55,7 +55,7 @@ expect{post check_out_path, params: {movie_id: m1.id, customer_id: c3.id}}.wont_change "Rental.count" body = check_response(expected_type: Hash, expected_status: :bad_request) - expect(body.keys).must_equal ["errors"] + expect(body.keys).must_equal ["error"] expect(body.values).must_equal ["Cannot make a rental because movie #{m1.title.capitalize} ran out of copies"] end @@ -67,8 +67,8 @@ body = check_response(expected_type: Hash, expected_status: :bad_request) - expect(body.keys.sort).must_equal ["error_msgs", "errors"] - expect(body["errors"]).must_equal "Cannot make a rental" + expect(body.keys.sort).must_equal ["error", "error_msgs"] + expect(body["error"]).must_equal "Cannot make a rental" expect(body["error_msgs"]).must_equal bad_rental.errors.full_messages end @@ -83,8 +83,8 @@ check_response(expected_type: Hash, expected_status: :bad_request) body = JSON.parse(response.body) - expect(body.keys.sort).must_equal ["error_msgs", "errors"] - expect(body["errors"]).must_equal "Cannot make a rental" + expect(body.keys.sort).must_equal ["error", "error_msgs"] + expect(body["error"]).must_equal "Cannot make a rental" expect(body["error_msgs"]).must_equal bad_rental.errors.full_messages end end @@ -126,7 +126,7 @@ body = check_response(expected_type: Hash, expected_status: :bad_request) - expect(body.keys).must_equal ["errors"] + expect(body.keys).must_equal ["error"] expect(body.values).must_equal ["Rental has already been returned"] end @@ -137,7 +137,7 @@ body = check_response(expected_type: Hash, expected_status: :bad_request) - expect(body.keys).must_equal ["errors"] + expect(body.keys).must_equal ["error"] expect(body.values).must_equal ["Rental doesn't exist"] end end From 8298eee4f6ef1509975303ff2f47e2879f2de8dc Mon Sep 17 00:00:00 2001 From: stupendousC Date: Fri, 8 Nov 2019 15:18:03 -0800 Subject: [PATCH 85/86] applied newly refactored render_errors_json( ) to customers_ctrller --- app/controllers/customers_controller.rb | 6 +++--- test/controllers/customers_controller_test.rb | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index 20fc80f86..b7006cfbb 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -11,7 +11,7 @@ def index choice = params[:sort].to_sym customers = Customer.all.order(choice => :asc) else - render json: { errors: "Can't sort anything with that key" }, status: :bad_request + render_errors_json(error: "Can't sort anything with that key") return end end @@ -24,12 +24,12 @@ def index if (limit_amt > 0) && (offset_amt > 0) customers = customers.limit(limit_amt).offset(offset_amt) else - render json: { errors: "Invalid n & p combo" }, status: :bad_request + render_errors_json(error: "Invalid n & p combo" ) return end end elsif params[:n] || params[:p] - render json: { errors: "We require both n and p, not just one of them" }, status: :bad_request + render_errors_json(error: "We require both n and p, not just one of them" ) return end diff --git a/test/controllers/customers_controller_test.rb b/test/controllers/customers_controller_test.rb index f52a1d23c..6e7bb06eb 100644 --- a/test/controllers/customers_controller_test.rb +++ b/test/controllers/customers_controller_test.rb @@ -72,7 +72,7 @@ check_response(expected_type: Hash, expected_status: :bad_request) body = JSON.parse(response.body) - expect(body["errors"]).must_equal "Can't sort anything with that key" + expect(body["error"]).must_equal "Can't sort anything with that key" end it "if no customers exist but valid input, responds with check status and request body" do @@ -118,7 +118,7 @@ check_response(expected_type: Hash, expected_status: :bad_request) body = JSON.parse(response.body) - expect(body["errors"]).must_equal "We require both n and p, not just one of them" + expect(body["error"]).must_equal "We require both n and p, not just one of them" end end @@ -135,7 +135,7 @@ check_response(expected_type: Hash, expected_status: :bad_request) body = JSON.parse(response.body) - expect(body["errors"]).must_equal "Invalid n & p combo" + expect(body["error"]).must_equal "Invalid n & p combo" end end From 03fc1a4c044636ab82f0075c6a50dd86a702c798 Mon Sep 17 00:00:00 2001 From: stupendousC Date: Fri, 8 Nov 2019 16:39:30 -0800 Subject: [PATCH 86/86] fixed query parameter bug --- app/controllers/application_controller.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 3c5224f73..5d4b5a4fb 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -25,6 +25,8 @@ def validate_query_params(acceptable_keys:) render json: { errors: "Invalid n & p combo" }, status: :bad_request return end + else + render json: { errors: "Invalid n or p query parameter(s)" }, status: :bad_request end elsif params[:n] || params[:p] render json: { errors: "We require both n and p, not just one of them" }, status: :bad_request