From c9197099d2a53e660d741e2a12ec837c07bd2064 Mon Sep 17 00:00:00 2001 From: Dikla Date: Mon, 7 May 2018 12:01:35 -0700 Subject: [PATCH 01/57] rails new --- .gitignore | 16 + Gemfile | 50 +++ Gemfile.lock | 154 +++++++++ README.md | 312 +----------------- 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 | 35 ++ bin/spring | 17 + bin/update | 29 ++ config.ru | 5 + config/application.rb | 40 +++ config/boot.rb | 3 + config/cable.yml | 10 + config/database.yml | 85 +++++ config/environment.rb | 5 + config/environments/development.rb | 47 +++ config/environments/production.rb | 83 +++++ config/environments/test.rb | 42 +++ .../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 | 56 ++++ config/routes.rb | 3 + config/secrets.yml | 32 ++ config/spring.rb | 6 + db/seeds.rb | 14 +- lib/tasks/.keep | 0 log/.keep | 0 public/robots.txt | 1 + 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 | 26 ++ tmp/.keep | 0 vendor/.keep | 0 55 files changed, 927 insertions(+), 306 deletions(-) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 Gemfile.lock 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/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/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/secrets.yml create mode 100644 config/spring.rb create mode 100644 lib/tasks/.keep create mode 100644 log/.keep create mode 100644 public/robots.txt 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..68ac019ec --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +# 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 + +.byebug_history diff --git a/Gemfile b/Gemfile new file mode 100644 index 000000000..78bff8d26 --- /dev/null +++ b/Gemfile @@ -0,0 +1,50 @@ +source 'https://rubygems.org' + +git_source(:github) do |repo_name| + repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/") + "https://github.com/#{repo_name}.git" +end + + +# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' +gem 'rails', '~> 5.1.6' +# Use postgresql as the database for Active Record +gem 'pg', '>= 0.18', '< 2.0' +# Use Puma as the app server +gem 'puma', '~> 3.7' +# 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 Capistrano for deployment +# gem 'capistrano-rails', group: :development + +# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible +# gem 'rack-cors' + +group :development, :test do + # Call 'byebug' anywhere in the code to stop execution and get a debugger console + gem 'byebug', 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 :test do + gem 'minitest-rails' + gem 'minitest-reporters' +end diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 000000000..e547d2449 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,154 @@ +GEM + remote: https://rubygems.org/ + specs: + actioncable (5.1.6) + actionpack (= 5.1.6) + nio4r (~> 2.0) + websocket-driver (~> 0.6.1) + actionmailer (5.1.6) + actionpack (= 5.1.6) + actionview (= 5.1.6) + activejob (= 5.1.6) + mail (~> 2.5, >= 2.5.4) + rails-dom-testing (~> 2.0) + actionpack (5.1.6) + actionview (= 5.1.6) + activesupport (= 5.1.6) + rack (~> 2.0) + rack-test (>= 0.6.3) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.0, >= 1.0.2) + actionview (5.1.6) + activesupport (= 5.1.6) + builder (~> 3.1) + erubi (~> 1.4) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.0, >= 1.0.3) + activejob (5.1.6) + activesupport (= 5.1.6) + globalid (>= 0.3.6) + activemodel (5.1.6) + activesupport (= 5.1.6) + activerecord (5.1.6) + activemodel (= 5.1.6) + activesupport (= 5.1.6) + arel (~> 8.0) + activesupport (5.1.6) + concurrent-ruby (~> 1.0, >= 1.0.2) + i18n (>= 0.7, < 2) + minitest (~> 5.1) + tzinfo (~> 1.1) + ansi (1.5.0) + arel (8.0.0) + builder (3.2.3) + byebug (10.0.2) + coderay (1.1.2) + concurrent-ruby (1.0.5) + crass (1.0.4) + erubi (1.7.1) + ffi (1.9.23) + globalid (0.4.1) + activesupport (>= 4.2.0) + i18n (1.0.1) + 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.2.2) + crass (~> 1.0.2) + nokogiri (>= 1.5.9) + mail (2.7.0) + mini_mime (>= 0.1.1) + method_source (0.9.0) + mini_mime (1.0.0) + mini_portile2 (2.3.0) + minitest (5.11.3) + minitest-rails (3.0.0) + minitest (~> 5.8) + railties (~> 5.0) + minitest-reporters (1.2.0) + ansi + builder + minitest (>= 5.0) + ruby-progressbar + nio4r (2.3.1) + nokogiri (1.8.2) + mini_portile2 (~> 2.3.0) + pg (1.0.0) + pry (0.11.3) + coderay (~> 1.1.0) + method_source (~> 0.9.0) + pry-rails (0.3.6) + pry (>= 0.10.4) + puma (3.11.4) + rack (2.0.5) + rack-test (1.0.0) + rack (>= 1.0, < 3) + rails (5.1.6) + actioncable (= 5.1.6) + actionmailer (= 5.1.6) + actionpack (= 5.1.6) + actionview (= 5.1.6) + activejob (= 5.1.6) + activemodel (= 5.1.6) + activerecord (= 5.1.6) + activesupport (= 5.1.6) + bundler (>= 1.3.0) + railties (= 5.1.6) + sprockets-rails (>= 2.0.0) + rails-dom-testing (2.0.3) + activesupport (>= 4.2.0) + nokogiri (>= 1.6) + rails-html-sanitizer (1.0.4) + loofah (~> 2.2, >= 2.2.2) + railties (5.1.6) + actionpack (= 5.1.6) + activesupport (= 5.1.6) + method_source + rake (>= 0.8.7) + thor (>= 0.18.1, < 2.0) + rake (12.3.1) + rb-fsevent (0.10.3) + rb-inotify (0.9.10) + ffi (>= 0.5.0, < 2) + ruby-progressbar (1.9.0) + ruby_dep (1.5.0) + spring (2.0.2) + activesupport (>= 4.2) + spring-watcher-listen (2.0.1) + listen (>= 2.7, < 4.0) + spring (>= 1.2, < 3.0) + sprockets (3.7.1) + concurrent-ruby (~> 1.0) + rack (> 1, < 3) + sprockets-rails (3.2.1) + actionpack (>= 4.0) + activesupport (>= 4.0) + sprockets (>= 3.0.0) + thor (0.20.0) + thread_safe (0.3.6) + tzinfo (1.2.5) + thread_safe (~> 0.1) + websocket-driver (0.6.5) + websocket-extensions (>= 0.1.0) + websocket-extensions (0.1.3) + +PLATFORMS + ruby + +DEPENDENCIES + byebug + listen (>= 3.0.5, < 3.2) + minitest-rails + minitest-reporters + pg (>= 0.18, < 2.0) + pry-rails + puma (~> 3.7) + rails (~> 5.1.6) + spring + spring-watcher-listen (~> 2.0.0) + tzinfo-data + +BUNDLED WITH + 1.16.1 diff --git a/README.md b/README.md index 59f4e3757..7db80e4ca 100644 --- a/README.md +++ b/README.md @@ -1,310 +1,24 @@ -# Project: VideoStoreAPI +# README -**Due:** EOD Friday May 11 +This README would normally document whatever steps are necessary to get the +application up and running. -The goal of this project is to create a system that a video store (remember those?) could use to track their inventory of rental videos and their list of customers. +Things you may want to cover: -We will use Rails to construct a RESTful API. The purpose of this API is to quickly serve information about the store's video collection, customer information, and to update rental status. This repository provides two JSON datafiles to serve as the initial seeds for this system. +* Ruby version -This is a pair project. You and your partner should use all the techniques we've learned so far to keep yourselves organized and on track, and ensure that no requirements slip through the cracks. +* System dependencies -## Learning Goals -Upon completing this project, students should be able to: +* Configuration -- Build an ERD and set up ActiveRecord models for a given dataset / use-case -- Expose database contents through a web API -- Respond reasonably to bad user data in the context of an API -- Verify the correctness of an API using controller tests +* Database creation -This is a [stage 2](https://github.com/Ada-Developers-Academy/pedagogy/blob/master/rule-of-three.md) project. +* Database initialization -## Success Criteria -Your project will be evaluated against the following requirements: +* How to run the test suite -- API conformance - - The provided smoke tests should pass (see the subfolder) - - Bad data sent to the API should result in an appropriate status code and helpful error -- Test coverage - - Models: All relations, validations, and custom model methods should include at least one positive and one negative test case - - Controllers: Every API endpoint should include at least one positive and one negative test case -- Style and Organization - - Everything we've learned so far about how to design and build a Rails app still applies! +* Services (job queues, cache servers, search engines, etc.) -## Project Baseline -- Read the API Requirements below and create a pseudo-code "routes" file that specifies - - The _endpoints_ your API will need - - The _HTTP verbs_ each endpoint will use - - Any data that must be provided to the endpoint in order for it to do its work -- Read the Seed Data description below and, bearing in mind the API Requirements, create an ERD for your database that specifies - - The _models_ your database will require - - The _attributes_ for each model - - Any _relationships_ between models -- Create a new Rails app to serve as the API - - **Create the rails app with:** `$ rails new . --api` -- Create a route that responds to `/zomg` that serves a json-encoded "it works!" +* Deployment instructions -## Wave 1: Database Models, Tables, & Seeds -- Generate Rails models and associations to match your ERD -- Use the provided seed script `db/seeds.rb` to import the provided JSON data into your database - -In the past, many students have spent lots of time writing and testing validations for these models. Because project time is limited and validations are not an important learning objective this week, we do not recommend this. Instead, validate only those fields that, if they are absent, will break your API. - -### Seed Data -`movies.json` contains information about the videos available to rent at the store. The data is presented as an array of objects, with each object having the following key-value pairs: - -| Field | Datatype | Description -|----------------|----------|------------ -| `title` | string | The title of the film -| `overview` | string | A short plot synopsis -| `release_date` | date | `YYYY-MM-DD`, Day the film was originally released -| `inventory` | integer | How many copies of the film the video store owns - -`customers.json` contains information about the customers that have rented with the store in the past. The data is presented as, you guessed it, an array of objects, with each object have the following key-value pairs: - -| Field | Datatype | Description -|------------------|----------|------------ -| `name` | string | The customer's name -| `registered_at` | datetime | `Wed, 29 Apr 2015 07:54:14 -0700`, When the customer first visited the store -| `address` | string | Street address -| `city` | string |   -| `state` | string |   -| `postal_code` | string |   -| `phone` | string | Primary contact phone number - -### Testing -As with all Rails projects, model testing is a requirement. You should have _at least_ one positive and one negative test case for each relation, validation, and custom function you add to your models. - -Use good TDD practices, and test _before_ you code. Remember: red-green-refactor. - -## Waves 2 and 3: Coding The API -In this wave, you will implement the API described below. The endpoints are described more-or-less in order of complexity, and we recommend you build them in that order. Every endpoint must serve JSON data, and must use HTTP response codes to indicate the status of the request. - -The schema of your database and the structure of your rails app are completely up to you, so long as the API conforms to the description and provided script. - -### Error Handling -If something goes wrong, your API should return an appropriate [HTTP status code](http://billpatrianakos.me/blog/2013/10/13/list-of-rails-status-code-symbols/), as well as a list of errors. The list should be formatted like this: - -```json -{ - "errors": { - "title": ["Movie 'Revenge of the Gnomes' not found"] - } -} -``` - -All errors your API can return should be covered by at least one test case. - -### Testing -Because APIs are often open to the public, thorough testing is essential. For a Rails API, that means controller testing. - -For each API endpoint, you should have _at least_: -- A basic test with no parameters, if applicable -- Positive and negative tests for any URI parameters (user ID, movie title) -- Testing around any data in the request body - -Use good TDD practices, and test _before_ you code. Remember: red-green-refactor. - -#### Smoke Tests -Because this API will be used as the backend for a future project, there are strict requirements about how it should be structured. To this end, we have provided a set of [smoke tests](http://softwaretestingfundamentals.com/smoke-testing/) written in Postman to exercise all the endpoints. - -The smoke tests will verify that your API looks correct to the outside world, by sending actual HTTP requests to your running server and checking the results. They test things like: - -- Did I get a success response for a valid request? -- Did the API return JSON? -- Does the JSON contain the expected property names? - -**The smoke tests are not a substitute for writing your own tests!!!!!** They do **not** check that the content is _correct_, nor do they cover any negative or edge cases. Verifying correctness in these cases is **your** responsibility. - -The smoke tests live in the file [`test/VideoStoreAPI_smoke_tests.postman_collection.json`](test/VideoStoreAPI_smoke_tests.postman_collection.json). To run them: - -1. Open Postman -1. Click `Import` in the top left -1. Drag-and-drop the file into the box -1. In the left sidebar, click on the `Collections` tab -1. There should now be an entry for the smoke tests. Hover over it and click the `>` icon for a detail view. You will notice they are in the format `{{url}}/movies`. `{{url}}` is a key which you can give a value on your computer. -1. To do so go to the Gearbox in the top-right and select `Manage Environments` -![Manage Environments](images/manage-environment.png) -1. Then Select `Add` -![add button](images/add-btn.png) -1. Lastly add a key `url` and value `http://localhost:3000` -![Key & Value](images/key-value.png) -1. Click the blue `Run` button. This will launch the collection runner. -1. In the collection runner, scroll down in the center pane and click the blue `Start Test` button - -## API Description - -### Wave 2: Customers and Movies - -#### `GET /customers` -List all customers - -Fields to return: -- `id` -- `name` -- `registered_at` -- `postal_code` -- `phone` -- `movies_checked_out_count` - - This will be 0 unless you've completed optional requirements - -#### `GET /movies` -List all movies - -Fields to return: -- `id` -- `title` -- `release_date` - -#### `GET /movies/:id` -Look a movie up by `id` - -URI parameters: -- `id`: Movie identifier - -Fields to return: -- `title` -- `overview` -- `release_date` -- `inventory` (total) -- `available_inventory` (not currently checked-out to a customer) - - This will be the same as `inventory` unless you've completed the optional endpoints. - -#### `POST /movies` -Create a new movie in the video store inventory. - -Upon success, this request should return the `id` of the movie created. - -Request body: - -| Field | Datatype | Description -|---------------|---------------------|------------ -| `title` | string | Title of the movie -| `overview` | string | Descriptive summary of the movie -| `release_date` | string `YYYY-MM-DD` | Date the movie was released -| `inventory` | integer | Quantity available in the video store - -### Wave 3: Rentals - -Wave 2 focused on working with customers and movies. With these endpoints you can extend the functionality of your API to allow managing the rental process. - -#### `POST /rentals/check-out` -Check out one of the movie's inventory to the customer. The rental's check-out date should be set to today, and the due date should be set to a week from today. - -**Note:** Some of the fields from wave 2 should now have interesting values. Good thing you wrote tests for them, right... right? - -Request body: - -| Field | Datatype | Description -|---------------|---------------------|------------ -| `customer_id` | integer | ID of the customer checking out this film -| `movie_id` | integer | ID of the movie to be checked out - -#### `POST /rentals/check-in` -Check in one of a customer's rentals - -Request body: - -| Field | Datatype | Description -|---------------|----------|------------ -| `customer_id` | integer | ID of the customer checking in this film -| `movie_id` | integer | ID of the movie to be checked in - -## Optional Enhancements -These really are **optional** - if you've gotten here and you have time left, that means you're moving speedy fast! - -### Query Parameters -Any endpoint that returns a list should accept 3 _optional_ [query parameters](http://guides.rubyonrails.org/action_controller_overview.html#parameters): - -| Name | Value | Description -|--------|---------|------------ -| `sort` | string | Sort objects by this field, in ascending order -| `n` | integer | Number of responses to return per page -| `p` | integer | Page of responses to return - -So, for an API endpoint like `GET /customers`, the following requests should be valid: -- `GET /customers`: All customers, sorted by ID -- `GET /customers?sort=name`: All customers, sorted by name -- `GET /customers?n=10&p=2`: Customers 10-19, sorted by ID -- `GET /customers?sort=name&n=10&p=2`: Customers 10-19, sorted by name - -Of course, adding new features means you should be adding new controller tests to verify them. - -Things to note: -- Sorting by ID is the rails default -- Possible sort fields: - - Customers can be sorted by `name`, `registered_at` and `postal_code` - - Movies can be sorted by `title` and `release_date` - - Overdue rentals can be sorted by `title`, `name`, `checkout_date` and `due_date` -- If the client requests both sorting and pagination, pagination should be relative to the sorted order -- Check out the [will_paginate gem](https://github.com/mislav/will_paginate) - -### More Endpoints: Inventory Management -All these endpoints should support all 3 query parameters. All fields are sortable. - -#### `GET /rentals/overdue` -List all customers with overdue movies - -Fields to return: -- `movie_id` -- `title` -- `customer_id` -- `name` -- `postal_code` -- `checkout_date` -- `due_date` - -#### `GET /movies/:id/current` -List customers that have _currently_ checked out a copy of the film - -URI parameters: -- `id`: Movie identifier - -Fields to return: -- `customer_id` -- `name` -- `postal_code` -- `checkout_date` -- `due_date` - -#### `GET /movies/:id/history` -List customers that have checked out a copy of the film _in the past_ - -URI parameters: -- `id`: Movie identifier - -Fields to return: -- `customer_id` -- `name` -- `postal_code` -- `checkout_date` -- `due_date` - -#### `GET /customers/:id/current` -List the movies a customer _currently_ has checked out - -URI parameters: -- `id`: Customer ID - -Fields to return: -- `title` -- `checkout_date` -- `due_date` - -#### `GET /customers/:id/history` -List the movies a customer has checked out _in the past_ - -URI parameters: -- `id`: Customer ID - -Fields to return: -- `title` -- `checkout_date` -- `due_date` - - -## Reference -- [Postman on Environments](https://www.getpostman.com/docs/environments) - -## What We're Looking For - -Check the [feedback template](./feedback.md) to see how we will evaluate your project. +* ... 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..66e9889e8 --- /dev/null +++ b/bin/bundle @@ -0,0 +1,3 @@ +#!/usr/bin/env ruby +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) +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..104e40c1c --- /dev/null +++ b/bin/setup @@ -0,0 +1,35 @@ +#!/usr/bin/env ruby +require 'pathname' +require 'fileutils' +include FileUtils + +# path to your application root. +APP_ROOT = Pathname.new File.expand_path('../../', __FILE__) + +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..fb2ec2ebb --- /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..a8e4462f2 --- /dev/null +++ b/bin/update @@ -0,0 +1,29 @@ +#!/usr/bin/env ruby +require 'pathname' +require 'fileutils' +include FileUtils + +# path to your application root. +APP_ROOT = Pathname.new File.expand_path('../../', __FILE__) + +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..2fe7718ea --- /dev/null +++ b/config/application.rb @@ -0,0 +1,40 @@ +require_relative 'boot' + +require "rails" +# Pick the frameworks you want: +require "active_model/railtie" +require "active_job/railtie" +require "active_record/railtie" +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.1 + + # Settings in config/environments/* take precedence over those specified here. + # Application configuration should go into files in config/initializers + # -- all .rb files in that directory are automatically loaded. + + # 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..30f5120df --- /dev/null +++ b/config/boot.rb @@ -0,0 +1,3 @@ +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__) + +require 'bundler/setup' # Set up gems listed in the Gemfile. diff --git a/config/cable.yml b/config/cable.yml new file mode 100644 index 000000000..ad59bcd88 --- /dev/null +++ b/config/cable.yml @@ -0,0 +1,10 @@ +development: + adapter: async + +test: + adapter: async + +production: + adapter: redis + url: redis://localhost:6379/1 + channel_prefix: VideoStoreAPI_production diff --git a/config/database.yml b/config/database.yml new file mode 100644 index 000000000..720570700 --- /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: VideoStoreAPI_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: VideoStoreAPI + + # 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: VideoStoreAPI_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: VideoStoreAPI_production + username: VideoStoreAPI + password: <%= ENV['VIDEOSTOREAPI_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..abc82221c --- /dev/null +++ b/config/environments/development.rb @@ -0,0 +1,47 @@ +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. + 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.seconds.to_i}" + } + else + config.action_controller.perform_caching = false + + config.cache_store = :null_store + end + + # 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 + + + # 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..3bd8115ea --- /dev/null +++ b/config/environments/production.rb @@ -0,0 +1,83 @@ +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 + + # Attempt to read encrypted secrets from `config/secrets.yml.enc`. + # Requires an encryption key in `ENV["RAILS_MASTER_KEY"]` or + # `config/secrets.yml.key`. + config.read_encrypted_secrets = 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 + + # 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 = "VideoStoreAPI_#{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..8e5cbde53 --- /dev/null +++ b/config/environments/test.rb @@ -0,0 +1,42 @@ +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.seconds.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 + 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/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..1e19380dc --- /dev/null +++ b/config/puma.rb @@ -0,0 +1,56 @@ +# 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. If you use this option +# you need to make sure to reconnect any threads in the `on_worker_boot` +# block. +# +# preload_app! + +# If you are preloading your application and using Active Record, it's +# recommended that you close any connections to the database before workers +# are forked to prevent connection leakage. +# +# before_fork do +# ActiveRecord::Base.connection_pool.disconnect! if defined?(ActiveRecord) +# end + +# The code in the `on_worker_boot` will be called if you are using +# clustered mode by specifying a number of `workers`. After each worker +# process is booted, this block will be run. If you are using the `preload_app!` +# option, you will want to use this block to reconnect to any threads +# or connections that may have been created at application boot, as Ruby +# cannot share connections between processes. +# +# on_worker_boot do +# ActiveRecord::Base.establish_connection if defined?(ActiveRecord) +# end +# + +# 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/secrets.yml b/config/secrets.yml new file mode 100644 index 000000000..2faf06cb9 --- /dev/null +++ b/config/secrets.yml @@ -0,0 +1,32 @@ +# Be sure to restart your server when you modify this file. + +# Your secret key is used for verifying the integrity of signed cookies. +# If you change this key, all old signed cookies will become invalid! + +# Make sure the secret is at least 30 characters and all random, +# no regular words or you'll be exposed to dictionary attacks. +# You can use `rails secret` to generate a secure secret key. + +# Make sure the secrets in this file are kept private +# if you're sharing your code publicly. + +# Shared secrets are available across all environments. + +# shared: +# api_key: a1B2c3D4e5F6 + +# Environmental secrets are only available for that specific environment. + +development: + secret_key_base: 0b54282873d7d2127fcb0c9df774966859852037931f6a5192a4122bc396517a7ff646017719d6f7ea639a9a9d1572e85dfe2dca986717843b4c95fc57aa16e2 + +test: + secret_key_base: a1f1b17d220dd226cd88706eba5ac1ca3916f519aaa4f92acb89ae51b20e6d142f25e4fe2be6d3fe812be146b35e0addd5983d19c6470b27edf8c9dcb9faab8a + +# Do not keep production secrets in the unencrypted secrets file. +# Instead, either read values from the environment. +# Or, use `bin/rails secrets:setup` to configure encrypted secrets +# and move the `production:` environment over there. + +production: + secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> diff --git a/config/spring.rb b/config/spring.rb new file mode 100644 index 000000000..c9119b40c --- /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/db/seeds.rb b/db/seeds.rb index 5322340ba..1beea2acc 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,7 +1,7 @@ -JSON.parse(File.read('db/seeds/customers.json')).each do |customer| - Customer.create!(customer) -end - -JSON.parse(File.read('db/seeds/movies.json')).each do |movie| - Movie.create!(movie) -end +# This file should contain all the record creation needed to seed the database with its default values. +# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup). +# +# Examples: +# +# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) +# Character.create(name: 'Luke', movie: movies.first) 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/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..10594a324 --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,26 @@ +ENV["RAILS_ENV"] = "test" +require File.expand_path("../../config/environment", __FILE__) +require "rails/test_help" +require "minitest/rails" +require "minitest/reporters" # for Colorized output + +# For colorful output! +Minitest::Reporters.use!( + Minitest::Reporters::SpecReporter.new, + ENV, + Minitest.backtrace_filter +) + + +# To add Capybara feature tests add `gem "minitest-rails-capybara"` +# to the test group in the Gemfile and uncomment the following: +# require "minitest/rails/capybara" + +# Uncomment for awesome colorful output +# require "minitest/pride" + +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 35b2b973c5ea208676564ca359007f4283d4d8fb Mon Sep 17 00:00:00 2001 From: Dikla Date: Mon, 7 May 2018 12:22:13 -0700 Subject: [PATCH 02/57] generated movie model generated customer model --- app/models/customer.rb | 2 + app/models/movie.rb | 2 + db/migrate/20180507191825_create_movies.rb | 12 ++++++ db/migrate/20180507192054_create_customers.rb | 15 +++++++ db/schema.rb | 39 +++++++++++++++++++ test/fixtures/customers.yml | 19 +++++++++ test/fixtures/movies.yml | 13 +++++++ test/models/customer_test.rb | 9 +++++ test/models/movie_test.rb | 9 +++++ 9 files changed, 120 insertions(+) create mode 100644 app/models/customer.rb create mode 100644 app/models/movie.rb create mode 100644 db/migrate/20180507191825_create_movies.rb create mode 100644 db/migrate/20180507192054_create_customers.rb create mode 100644 db/schema.rb create mode 100644 test/fixtures/customers.yml create mode 100644 test/fixtures/movies.yml create mode 100644 test/models/customer_test.rb create mode 100644 test/models/movie_test.rb diff --git a/app/models/customer.rb b/app/models/customer.rb new file mode 100644 index 000000000..0b5277335 --- /dev/null +++ b/app/models/customer.rb @@ -0,0 +1,2 @@ +class Customer < ApplicationRecord +end diff --git a/app/models/movie.rb b/app/models/movie.rb new file mode 100644 index 000000000..dc614df15 --- /dev/null +++ b/app/models/movie.rb @@ -0,0 +1,2 @@ +class Movie < ApplicationRecord +end diff --git a/db/migrate/20180507191825_create_movies.rb b/db/migrate/20180507191825_create_movies.rb new file mode 100644 index 000000000..62885a98f --- /dev/null +++ b/db/migrate/20180507191825_create_movies.rb @@ -0,0 +1,12 @@ +class CreateMovies < ActiveRecord::Migration[5.1] + def change + create_table :movies do |t| + t.string :title + t.string :overview + t.date :release_date + t.integer :inventory + + t.timestamps + end + end +end diff --git a/db/migrate/20180507192054_create_customers.rb b/db/migrate/20180507192054_create_customers.rb new file mode 100644 index 000000000..23316d24a --- /dev/null +++ b/db/migrate/20180507192054_create_customers.rb @@ -0,0 +1,15 @@ +class CreateCustomers < ActiveRecord::Migration[5.1] + def change + create_table :customers do |t| + t.string :name + t.datetime :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/schema.rb b/db/schema.rb new file mode 100644 index 000000000..9020054d7 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,39 @@ +# 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: 20180507192054) 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.datetime "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.date "release_date" + t.integer "inventory" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + +end diff --git a/test/fixtures/customers.yml b/test/fixtures/customers.yml new file mode 100644 index 000000000..2aa69d31e --- /dev/null +++ b/test/fixtures/customers.yml @@ -0,0 +1,19 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + name: MyString + registered_at: 2018-05-07 12:20:54 + address: MyString + city: MyString + state: MyString + postal_code: MyString + phone: MyString + +two: + name: MyString + registered_at: 2018-05-07 12:20:54 + address: MyString + city: MyString + state: MyString + postal_code: MyString + phone: MyString diff --git a/test/fixtures/movies.yml b/test/fixtures/movies.yml new file mode 100644 index 000000000..090b19e62 --- /dev/null +++ b/test/fixtures/movies.yml @@ -0,0 +1,13 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + title: MyString + overview: MyString + release_date: 2018-05-07 + inventory: 1 + +two: + title: MyString + overview: MyString + release_date: 2018-05-07 + inventory: 1 diff --git a/test/models/customer_test.rb b/test/models/customer_test.rb new file mode 100644 index 000000000..5ebc5c850 --- /dev/null +++ b/test/models/customer_test.rb @@ -0,0 +1,9 @@ +require "test_helper" + +describe Customer do + let(:customer) { Customer.new } + + it "must be valid" do + value(customer).must_be :valid? + end +end diff --git a/test/models/movie_test.rb b/test/models/movie_test.rb new file mode 100644 index 000000000..34d1d30a5 --- /dev/null +++ b/test/models/movie_test.rb @@ -0,0 +1,9 @@ +require "test_helper" + +describe Movie do + let(:movie) { Movie.new } + + it "must be valid" do + value(movie).must_be :valid? + end +end From c6fa949a497296aed2bcf325497d271c47bcb33c Mon Sep 17 00:00:00 2001 From: Anne Date: Mon, 7 May 2018 12:35:19 -0700 Subject: [PATCH 03/57] controller for movies --- app/controllers/movies_controller.rb | 2 ++ test/controllers/movies_controller_test.rb | 7 +++++++ 2 files changed, 9 insertions(+) create mode 100644 app/controllers/movies_controller.rb create mode 100644 test/controllers/movies_controller_test.rb 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/test/controllers/movies_controller_test.rb b/test/controllers/movies_controller_test.rb new file mode 100644 index 000000000..67fabbcfb --- /dev/null +++ b/test/controllers/movies_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +describe MoviesController do + # it "must be a real test" do + # flunk "Need real tests" + # end +end From 3ccaa1c523eb24b998cf64aa7c85ec5d8ba98cdc Mon Sep 17 00:00:00 2001 From: Anne Date: Mon, 7 May 2018 12:42:04 -0700 Subject: [PATCH 04/57] customer scontroller --- app/controllers/customers_controller.rb | 2 ++ test/controllers/customers_controller_test.rb | 7 +++++++ 2 files changed, 9 insertions(+) create mode 100644 app/controllers/customers_controller.rb create mode 100644 test/controllers/customers_controller_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/test/controllers/customers_controller_test.rb b/test/controllers/customers_controller_test.rb new file mode 100644 index 000000000..5e123f6cd --- /dev/null +++ b/test/controllers/customers_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +describe CustomersController do + # it "must be a real test" do + # flunk "Need real tests" + # end +end From 9a0fa37487573cbf2bb30d735cc599c27c1536a5 Mon Sep 17 00:00:00 2001 From: Dikla Date: Mon, 7 May 2018 13:53:00 -0700 Subject: [PATCH 05/57] Added index route and index method in customer controller --- app/controllers/customers_controller.rb | 10 ++++++++++ config/routes.rb | 3 +++ 2 files changed, 13 insertions(+) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index ca3b6e024..2f81fa707 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -1,2 +1,12 @@ class CustomersController < ApplicationController + + def index + customers = Customers.all + render json: @customers.as_json(only: [:name, :registered_at, :address, :city, :state, :postal_code, :phone]), status: :ok + end + + def zomg + render json: {"it works"} + end + end diff --git a/config/routes.rb b/config/routes.rb index 787824f88..2298d2cfd 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,6 @@ Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html + resources :customerss, only: [:index] + + get '/zomg', to: "customers#zomg", as: 'zomg' end From 3ff55aacc7520c8e98b35645defd197f4d8dfc63 Mon Sep 17 00:00:00 2001 From: Dikla Date: Mon, 7 May 2018 13:59:20 -0700 Subject: [PATCH 06/57] Added the zomg method and route --- app/controllers/customers_controller.rb | 10 +++++++--- config/routes.rb | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index 2f81fa707..33c49edcf 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -5,8 +5,12 @@ def index render json: @customers.as_json(only: [:name, :registered_at, :address, :city, :state, :postal_code, :phone]), status: :ok end - def zomg - render json: {"it works"} - end + # def zomg + # render json: { + # "customer": { + # "name": "it works" + # } + # } + # end end diff --git a/config/routes.rb b/config/routes.rb index 2298d2cfd..97dc8ace3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,5 +2,5 @@ # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html resources :customerss, only: [:index] - get '/zomg', to: "customers#zomg", as: 'zomg' + # get '/customers/zomg', to: "customers#zomg", as: 'zomg' end From 2d0b5c0a082cc97b321a21292ae95e51df5eee58 Mon Sep 17 00:00:00 2001 From: Anne Date: Mon, 7 May 2018 14:03:58 -0700 Subject: [PATCH 07/57] rental model and relations --- app/models/customer.rb | 1 + app/models/movie.rb | 1 + app/models/rental.rb | 2 ++ db/migrate/20180507210143_create_rentals.rb | 8 ++++++++ test/fixtures/rentals.yml | 11 +++++++++++ test/models/rental_test.rb | 9 +++++++++ 6 files changed, 32 insertions(+) create mode 100644 app/models/rental.rb create mode 100644 db/migrate/20180507210143_create_rentals.rb create mode 100644 test/fixtures/rentals.yml create mode 100644 test/models/rental_test.rb diff --git a/app/models/customer.rb b/app/models/customer.rb index 0b5277335..d2533dbf9 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -1,2 +1,3 @@ class Customer < ApplicationRecord + has_many :rentals end diff --git a/app/models/movie.rb b/app/models/movie.rb index dc614df15..b8b339cbc 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -1,2 +1,3 @@ class Movie < ApplicationRecord + has_many :rentals end diff --git a/app/models/rental.rb b/app/models/rental.rb new file mode 100644 index 000000000..79e3a65ca --- /dev/null +++ b/app/models/rental.rb @@ -0,0 +1,2 @@ +class Rental < ApplicationRecord +end diff --git a/db/migrate/20180507210143_create_rentals.rb b/db/migrate/20180507210143_create_rentals.rb new file mode 100644 index 000000000..fcd36af2d --- /dev/null +++ b/db/migrate/20180507210143_create_rentals.rb @@ -0,0 +1,8 @@ +class CreateRentals < ActiveRecord::Migration[5.1] + def change + create_table :rentals do |t| + + t.timestamps + end + end +end diff --git a/test/fixtures/rentals.yml b/test/fixtures/rentals.yml new file mode 100644 index 000000000..dc3ee79b5 --- /dev/null +++ b/test/fixtures/rentals.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the "{}" from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/test/models/rental_test.rb b/test/models/rental_test.rb new file mode 100644 index 000000000..6ea53d94f --- /dev/null +++ b/test/models/rental_test.rb @@ -0,0 +1,9 @@ +require "test_helper" + +describe Rental do + let(:rental) { Rental.new } + + it "must be valid" do + value(rental).must_be :valid? + end +end From 4111a91f5b7d64613f6ad06121f19df5ef462a6b Mon Sep 17 00:00:00 2001 From: Anne Date: Mon, 7 May 2018 14:16:20 -0700 Subject: [PATCH 08/57] foreign keys for rentals on customer and movie --- app/models/rental.rb | 2 ++ db/migrate/20180507210818_add_rentals_join.rb | 10 ++++++++++ db/schema.rb | 16 +++++++++++++++- 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20180507210818_add_rentals_join.rb diff --git a/app/models/rental.rb b/app/models/rental.rb index 79e3a65ca..e001ff37c 100644 --- a/app/models/rental.rb +++ b/app/models/rental.rb @@ -1,2 +1,4 @@ class Rental < ApplicationRecord + belongs_to :customer + belongs_to :movie end diff --git a/db/migrate/20180507210818_add_rentals_join.rb b/db/migrate/20180507210818_add_rentals_join.rb new file mode 100644 index 000000000..45c3255b7 --- /dev/null +++ b/db/migrate/20180507210818_add_rentals_join.rb @@ -0,0 +1,10 @@ +class AddRentalsJoin < ActiveRecord::Migration[5.1] + def change + create_table :customers_rentals do |t| + t.belongs_to :customer, index: true + t.belongs_to :movie, index: true + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 9020054d7..ae803defa 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: 20180507192054) do +ActiveRecord::Schema.define(version: 20180507210818) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -27,6 +27,15 @@ t.datetime "updated_at", null: false end + create_table "customers_rentals", force: :cascade do |t| + t.bigint "customer_id" + t.bigint "movie_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["customer_id"], name: "index_customers_rentals_on_customer_id" + t.index ["movie_id"], name: "index_customers_rentals_on_movie_id" + end + create_table "movies", force: :cascade do |t| t.string "title" t.string "overview" @@ -36,4 +45,9 @@ t.datetime "updated_at", null: false end + create_table "rentals", force: :cascade do |t| + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + end From 7947ee0e84b7324dc921fdd7c672504d3672a241 Mon Sep 17 00:00:00 2001 From: Anne Date: Mon, 7 May 2018 14:38:00 -0700 Subject: [PATCH 09/57] readded the seed snippet back in because it disappeared --- app/controllers/customers_controller.rb | 4 +- config/routes.rb | 2 +- db/seeds.rb | 8 +++ ...VideoStoreAPI Wave 2.postman_test_run.json | 53 +++++++++++++++++++ test/controllers/customers_controller_test.rb | 7 +-- 5 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 test/VideoStoreAPI Wave 2.postman_test_run.json diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index 33c49edcf..574ab2a17 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -1,8 +1,8 @@ class CustomersController < ApplicationController def index - customers = Customers.all - render json: @customers.as_json(only: [:name, :registered_at, :address, :city, :state, :postal_code, :phone]), status: :ok + customers = Customer.all + render json: customers.as_json(only: [:name, :registered_at, :address, :city, :state, :postal_code, :phone]), status: :ok end # def zomg diff --git a/config/routes.rb b/config/routes.rb index 97dc8ace3..3ba3267e0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,6 @@ Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html - resources :customerss, only: [:index] + resources :customers, only: [:index] # get '/customers/zomg', to: "customers#zomg", as: 'zomg' end diff --git a/db/seeds.rb b/db/seeds.rb index 1beea2acc..0e8274fca 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -5,3 +5,11 @@ # # movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) # Character.create(name: 'Luke', movie: movies.first) + +JSON.parse(File.read('db/seeds/customers.json')).each do |customer| + Customer.create!(customer) +end + +JSON.parse(File.read('db/seeds/movies.json')).each do |movie| + Movie.create!(movie) +end diff --git a/test/VideoStoreAPI Wave 2.postman_test_run.json b/test/VideoStoreAPI Wave 2.postman_test_run.json new file mode 100644 index 000000000..6cc21040c --- /dev/null +++ b/test/VideoStoreAPI Wave 2.postman_test_run.json @@ -0,0 +1,53 @@ +{ + "id": "4737232f-60c2-4e4b-8dfc-601e9035aafe", + "name": "VideoStoreAPI Wave 2", + "allTests": [], + "timestamp": "2018-05-07T21:23:59.099Z", + "collection_id": "34f1a741-7a64-4906-b500-f4eeeac84012", + "folder_id": 0, + "target_type": "collection", + "environment_id": "0", + "data": [], + "delay": 0, + "count": 1, + "collection": { + "id": "34f1a741-7a64-4906-b500-f4eeeac84012", + "name": "VideoStoreAPI Wave 2", + "description": null, + "auth": null, + "events": null, + "variables": null, + "order": [ + "7588c537-c438-4bab-bcdd-87bbcceec047", + "b2d4237b-2cbd-4d81-96d7-09680957f968", + "287ea268-dddf-4ba3-b22f-9ad76df9383d", + "34551a30-0a55-480f-9146-0d28018d7ba0", + "1170b97b-79c3-428e-bbfe-02d5e55e79e2" + ], + "folders_order": [], + "owner": "0", + "permissions": {}, + "favorite": false, + "shared": false, + "type": "collection", + "depth": 0 + }, + "folder": null, + "environment": null, + "globals": null, + "results": [], + "totalPass": 0, + "totalFail": 0, + "totalTime": 0, + "lifecycle": "done", + "requests": [ + { + "name": "List Customers", + "id": "7588c537-c438-4bab-bcdd-87bbcceec047", + "url": "{{url}}/customers", + "time": "2018-05-07T21:23:59.099Z", + "testPassFailCounts": {} + } + ], + "synced": false +} \ No newline at end of file diff --git a/test/controllers/customers_controller_test.rb b/test/controllers/customers_controller_test.rb index 5e123f6cd..e0cbda0b0 100644 --- a/test/controllers/customers_controller_test.rb +++ b/test/controllers/customers_controller_test.rb @@ -1,7 +1,8 @@ require "test_helper" describe CustomersController do - # it "must be a real test" do - # flunk "Need real tests" - # end + it "is a real working route" do + get customers_url + must_respond_with :success + end end From 7062f43281ad62b09d67f877eb314e172207ec7a Mon Sep 17 00:00:00 2001 From: Dikla Date: Mon, 7 May 2018 15:05:28 -0700 Subject: [PATCH 10/57] Added index method to movies controoler and created a route --- app/controllers/movies_controller.rb | 6 ++++++ config/routes.rb | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 6c4c51614..1be15eec4 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -1,2 +1,8 @@ class MoviesController < ApplicationController + + def index + movies = Movie.all + render json: customers.as_json(only: [:title, :overview, :release_date, :inventory]), status: :ok + end + end diff --git a/config/routes.rb b/config/routes.rb index 3ba3267e0..aa0cfc137 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,6 @@ Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html resources :customers, only: [:index] - + resources :movies, only: [:index] # get '/customers/zomg', to: "customers#zomg", as: 'zomg' end From f5980253815ee2c83d47e70ea5deb550340aa8e3 Mon Sep 17 00:00:00 2001 From: Dikla Date: Mon, 7 May 2018 15:06:35 -0700 Subject: [PATCH 11/57] Fixed typo --- 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 1be15eec4..321e9315d 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -2,7 +2,7 @@ class MoviesController < ApplicationController def index movies = Movie.all - render json: customers.as_json(only: [:title, :overview, :release_date, :inventory]), status: :ok + render json: movies.as_json(only: [:title, :overview, :release_date, :inventory]), status: :ok end end From 793906e63879de224ea1324cf822452e2ac7baf9 Mon Sep 17 00:00:00 2001 From: Anne Date: Mon, 7 May 2018 15:08:43 -0700 Subject: [PATCH 12/57] index customers controller test --- test/controllers/customers_controller_test.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/controllers/customers_controller_test.rb b/test/controllers/customers_controller_test.rb index e0cbda0b0..a53a4d6fb 100644 --- a/test/controllers/customers_controller_test.rb +++ b/test/controllers/customers_controller_test.rb @@ -3,6 +3,22 @@ describe CustomersController do it "is a real working route" do get customers_url + must_respond_with :success + response.header['Content-Type'].must_include 'json' + body = JSON.parse(response.body) + body.must_be_kind_of Array + end + + it "returns all of the customers" do + get customers_url + + body = JSON.parse(response.body) + body.length.must_equal Customer.count + end + + it "returns customers with exactly the required fields" do + end + end From 85716c8c5af1b21a91269ef8ff1530a8e68369c9 Mon Sep 17 00:00:00 2001 From: Dikla Date: Mon, 7 May 2018 15:10:50 -0700 Subject: [PATCH 13/57] Added tests for movie model --- test/controllers/movies_controller_test.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/controllers/movies_controller_test.rb b/test/controllers/movies_controller_test.rb index 67fabbcfb..1bced8179 100644 --- a/test/controllers/movies_controller_test.rb +++ b/test/controllers/movies_controller_test.rb @@ -4,4 +4,23 @@ # it "must be a real test" do # flunk "Need real tests" # end + it "is a real working route" do + get movies_url + + must_respond_with :success + response.header['Content-Type'].must_include 'json' + body = JSON.parse(response.body) + body.must_be_kind_of Array + end + + it "returns all of the movies" do + get movies_url + + body = JSON.parse(response.body) + body.length.must_equal Customer.count + end + + it "returns movies with exactly the required fields" do + + end end From 0acfd34b56fc3c30174212e1425d6001b7b3852c Mon Sep 17 00:00:00 2001 From: Anne Date: Mon, 7 May 2018 15:31:41 -0700 Subject: [PATCH 14/57] stubs for customer test --- test/models/customer_test.rb | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/test/models/customer_test.rb b/test/models/customer_test.rb index 5ebc5c850..7c74b2489 100644 --- a/test/models/customer_test.rb +++ b/test/models/customer_test.rb @@ -1,9 +1,24 @@ require "test_helper" describe Customer do - let(:customer) { Customer.new } + describe "validations" do - it "must be valid" do - value(customer).must_be :valid? + end + + describe "relations" do + before do + @customer = Customer.new( + name: "Movie Watcher", + address: "2015-04-29T14:54:14.000Z", + city: "Seattle", + state: "WA", + postal_code: "98109", + phone: "555-555-5555" + ) + end + + it "has movies" do + + end end end From b81e1d90d0893bf3da8ffcb0489e5f74d5372737 Mon Sep 17 00:00:00 2001 From: Dikla Date: Mon, 7 May 2018 15:41:24 -0700 Subject: [PATCH 15/57] Added route for movie show and the show method for movie controller --- app/controllers/movies_controller.rb | 13 +++++++++++++ config/routes.rb | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 321e9315d..2bc64f4f5 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -5,4 +5,17 @@ def index render json: movies.as_json(only: [:title, :overview, :release_date, :inventory]), status: :ok end + def show + movie = Movie.find_by(id: params[:id]) + if movie.nil? + render json: { + errors: { + id: ["No movie with ID #{params[:id]}"] + } + }, status: :not_found + else + render json: movie.as_json(only: [:id, :title, :overview, :release_date, :inventory]), status: :ok + end + end + end diff --git a/config/routes.rb b/config/routes.rb index aa0cfc137..d700517ce 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,6 @@ Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html resources :customers, only: [:index] - resources :movies, only: [:index] + resources :movies, only: [:index, :show] # get '/customers/zomg', to: "customers#zomg", as: 'zomg' end From 4c1ced345cd1b9462a1bb8e2a247b4c8eb2708a9 Mon Sep 17 00:00:00 2001 From: Anne Date: Mon, 7 May 2018 15:49:17 -0700 Subject: [PATCH 16/57] create a new movie test and method --- app/controllers/movies_controller.rb | 13 ++++++ config/routes.rb | 2 +- test/controllers/movies_controller_test.rb | 51 +++++++++++++++++----- 3 files changed, 53 insertions(+), 13 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 2bc64f4f5..744ee852b 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -18,4 +18,17 @@ def show end end + def create + movie = Movie.new(movie_params) + if movie.save + render json: { id: movie.id }, status: :created + else + render json: { errors: movie.errors.messages }, status: :bad_request + end + end + + private + def movie_params + params.require(:movie).permit(:title, :overview, :release_date, :inventory) + end end diff --git a/config/routes.rb b/config/routes.rb index d700517ce..4c6d69802 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,6 @@ Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html resources :customers, only: [:index] - resources :movies, only: [:index, :show] + resources :movies, only: [:index, :show, :create] # get '/customers/zomg', to: "customers#zomg", as: 'zomg' end diff --git a/test/controllers/movies_controller_test.rb b/test/controllers/movies_controller_test.rb index 1bced8179..50c8c9b46 100644 --- a/test/controllers/movies_controller_test.rb +++ b/test/controllers/movies_controller_test.rb @@ -4,23 +4,50 @@ # it "must be a real test" do # flunk "Need real tests" # end - it "is a real working route" do - get movies_url + describe "index" do + it "is a real working route" do + get movies_url - must_respond_with :success - response.header['Content-Type'].must_include 'json' - body = JSON.parse(response.body) - body.must_be_kind_of Array - end + must_respond_with :success + response.header['Content-Type'].must_include 'json' + body = JSON.parse(response.body) + body.must_be_kind_of Array + end + + it "returns all of the movies" do + get movies_url + + body = JSON.parse(response.body) + body.length.must_equal Customer.count + end - it "returns all of the movies" do - get movies_url + it "returns movies with exactly the required fields" do - body = JSON.parse(response.body) - body.length.must_equal Customer.count + end end - it "returns movies with exactly the required fields" do + describe "create" do + let(:movie_data) { + { + title: "Best Movie", + overview: "Stuff happens", + release_date: "2000-01-01", + inventory: 10 + } + } + + it "creates a new movie" do + before_movie_count = Movie.count + post movies_url, params: { movie: movie_data } + must_respond_with :success + + Movie.count.must_equal before_movie_count + 1 + + body = JSON.parse(response.body) + body.must_be_kind_of Hash + body.must_include "id" + Movie.find(body["id"]).title.must_equal movie_data[:title] + end end end From 2459e1be16250f13806a729ac472bc58785fb911 Mon Sep 17 00:00:00 2001 From: Dikla Date: Mon, 7 May 2018 15:56:01 -0700 Subject: [PATCH 17/57] Added tests for the show method in the movie controller test file --- test/controllers/movies_controller_test.rb | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/test/controllers/movies_controller_test.rb b/test/controllers/movies_controller_test.rb index 50c8c9b46..efb47b702 100644 --- a/test/controllers/movies_controller_test.rb +++ b/test/controllers/movies_controller_test.rb @@ -50,4 +50,30 @@ Movie.find(body["id"]).title.must_equal movie_data[:title] end end + + describe "show" do + # This bit is up to you! + it "can get a movie" do + keys = %w(id inventory overview release_date title) + movie = movies(:two) + get movie_path(movie.id) + must_respond_with :success + + response.header['Content-Type'].must_include 'json' + body = JSON.parse(response.body) + body.must_be_kind_of Hash + body.keys.sort.must_equal keys + body["id"].must_equal movie.id + end + + it "yields a not found status and also return some error text if the movie D.N.E" do + movie_id = Movie.last.id + 1 + get movie_path(movie_id) + must_respond_with :not_found + body = JSON.parse(response.body) + body.must_be_kind_of Hash + body.must_include "errors" + body["errors"].must_include "id" + end + end end From f9eae11a70f59c91d2d5b0b816aca6e0860e14d1 Mon Sep 17 00:00:00 2001 From: Dikla Date: Mon, 7 May 2018 16:46:04 -0700 Subject: [PATCH 18/57] Added validation for movie title and validation tests --- app/models/movie.rb | 1 + test/models/movie_test.rb | 49 +++++++++++++++++++++++++++++++++++---- 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/app/models/movie.rb b/app/models/movie.rb index b8b339cbc..0dc4c3cca 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -1,3 +1,4 @@ class Movie < ApplicationRecord has_many :rentals + validates :title, presence: true, uniqueness: true end diff --git a/test/models/movie_test.rb b/test/models/movie_test.rb index 34d1d30a5..9891b8188 100644 --- a/test/models/movie_test.rb +++ b/test/models/movie_test.rb @@ -1,9 +1,50 @@ require "test_helper" describe Movie do - let(:movie) { Movie.new } + describe "validations" do + # all validations pass + before do + # Arrange + + + @movie = Movie.new( + title: "test movie", + overview: "The unexciting life of a boy", + release_date: "1979-01-18", + inventory: "10" + ) + end + + it "can be created will all required fields" do + # Act + result = @movie.valid? + + # Assert + result.must_equal true + + + end + + # no title -> fail + it "is invalid without a title" do + @movie.title = nil + + result = @movie.valid? + + result.must_equal false + @movie.errors.messages.must_include :title + end + + # duplicate title -> fail + it "is invalid with a duplicate title" do + dup_movie = Movie.first + @movie.title = dup_movie.title + + result = @movie.valid? + + result.must_equal false + @movie.errors.messages.must_include :title + end + end - it "must be valid" do - value(movie).must_be :valid? end -end From 3766bf4745b5a6b4c3b3d37ec223065ee9a6fbcf Mon Sep 17 00:00:00 2001 From: Dikla Date: Mon, 7 May 2018 16:46:25 -0700 Subject: [PATCH 19/57] Added validation for customer name and validation tests --- app/models/customer.rb | 1 + test/models/customer_test.rb | 57 +++++++++++++++++++++++++++++++----- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/app/models/customer.rb b/app/models/customer.rb index d2533dbf9..453c4c065 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -1,3 +1,4 @@ class Customer < ApplicationRecord has_many :rentals + validates :name, presence: true end diff --git a/test/models/customer_test.rb b/test/models/customer_test.rb index 7c74b2489..6fe196fe4 100644 --- a/test/models/customer_test.rb +++ b/test/models/customer_test.rb @@ -2,23 +2,64 @@ describe Customer do describe "validations" do + # all validations pass + before do + customer = Customer.first - end - describe "relations" do - before do @customer = Customer.new( name: "Movie Watcher", address: "2015-04-29T14:54:14.000Z", city: "Seattle", state: "WA", postal_code: "98109", - phone: "555-555-5555" - ) + phone: "555-555-5555") + end + + it "can be created will all required fields" do + # Act + result = @customer.valid? + + # Assert + result.must_equal true + + end + + it "is invalid without a name" do + @customer.name = nil + + result = @customer.valid? + + result.must_equal false + @customer.errors.messages.must_include :name + end + + # duplicate title -> fail + # it "is invalid with a duplicate title" do + # dup_customer = Customer.first + # @customer.title = dup_customer.title + # + # result = @customer.valid? + # + # result.must_equal false + # @customer.errors.messages.must_include :title + # end end - it "has movies" do - + describe "relations" do + before do + @customer = Customer.new( + name: "Movie Watcher", + address: "2015-04-29T14:54:14.000Z", + city: "Seattle", + state: "WA", + postal_code: "98109", + phone: "555-555-5555" + ) + end + + it "has movies" do + + end end end -end From 50c033e34c872fa2191f186488abf66d949c3459 Mon Sep 17 00:00:00 2001 From: Dikla Date: Mon, 7 May 2018 16:50:14 -0700 Subject: [PATCH 20/57] Added before block to customer model tests --- test/models/customer_test.rb | 10 ---------- test/models/movie_test.rb | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/test/models/customer_test.rb b/test/models/customer_test.rb index 6fe196fe4..152e895d1 100644 --- a/test/models/customer_test.rb +++ b/test/models/customer_test.rb @@ -34,16 +34,6 @@ @customer.errors.messages.must_include :name end - # duplicate title -> fail - # it "is invalid with a duplicate title" do - # dup_customer = Customer.first - # @customer.title = dup_customer.title - # - # result = @customer.valid? - # - # result.must_equal false - # @customer.errors.messages.must_include :title - # end end describe "relations" do diff --git a/test/models/movie_test.rb b/test/models/movie_test.rb index 9891b8188..d5ef5c5af 100644 --- a/test/models/movie_test.rb +++ b/test/models/movie_test.rb @@ -47,4 +47,19 @@ end end + describe "relations" do + before do + @movie = Movie.new( + title: "test movie", + overview: "The unexciting life of a boy", + release_date: "1979-01-18", + inventory: "10" + ) + end + + it "has customers" do + + end + end + end From 6b0c4f3f996b595f86cf1a7f30eb3c658fc9d6c9 Mon Sep 17 00:00:00 2001 From: Anne Date: Tue, 8 May 2018 09:35:26 -0700 Subject: [PATCH 21/57] controller test --- config/routes.rb | 2 +- test/controllers/customers_controller_test.rb | 71 +++++++++++++++---- 2 files changed, 59 insertions(+), 14 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index 4c6d69802..4cbc64a86 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,6 @@ Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html - resources :customers, only: [:index] + resources :customers, only: [:index, :show, :create] resources :movies, only: [:index, :show, :create] # get '/customers/zomg', to: "customers#zomg", as: 'zomg' end diff --git a/test/controllers/customers_controller_test.rb b/test/controllers/customers_controller_test.rb index a53a4d6fb..033c06364 100644 --- a/test/controllers/customers_controller_test.rb +++ b/test/controllers/customers_controller_test.rb @@ -1,24 +1,69 @@ require "test_helper" describe CustomersController do - it "is a real working route" do - get customers_url + describe "index" do + it "is a real working route" do + get customers_url - must_respond_with :success - response.header['Content-Type'].must_include 'json' - body = JSON.parse(response.body) - body.must_be_kind_of Array - end + must_respond_with :success + response.header['Content-Type'].must_include 'json' + body = JSON.parse(response.body) + body.must_be_kind_of Array + end + + it "returns all of the customers" do + get customers_url + + body = JSON.parse(response.body) + body.length.must_equal Customer.count + end + + it "returns customers with exactly the required fields" do + + end - it "returns all of the customers" do - get customers_url + it "returns an empty array when there are no customers" do - body = JSON.parse(response.body) - body.length.must_equal Customer.count + end end - it "returns customers with exactly the required fields" do - + describe "show" do + it "can get a customer" do + get customer_path(customers(:one).id) + + must_respond_with :success + end + + it "returns the customer info for the selected customer" do + get customer_path(customers(:one).id) + + body = JSON.parse(response.body) + + body.must_be_kind_of Hash + body["phone"].must_equal customers(:one).phone + end + + it "returns not_found when the customer DNE" do + not_real_customer_id = Customer.last.id + 1 + + get customer_path(not_real_customer_id) + + must_respond_with :not_found + + #assert for whatever we are putting for the body response message with not found + end end + describe "create" do + #like let(:pet_data) + + it "creates a new customer" do + + end + + it "returns an error for an invalid customer" do + + end + + end end From f52adc528448c61f176f11932c6a1778307a8402 Mon Sep 17 00:00:00 2001 From: Dikla Date: Tue, 8 May 2018 09:41:28 -0700 Subject: [PATCH 22/57] Generate rental controller --- app/controllers/rental_controller.rb | 2 ++ test/controllers/rental_controller_test.rb | 7 +++++++ 2 files changed, 9 insertions(+) create mode 100644 app/controllers/rental_controller.rb create mode 100644 test/controllers/rental_controller_test.rb diff --git a/app/controllers/rental_controller.rb b/app/controllers/rental_controller.rb new file mode 100644 index 000000000..55344c535 --- /dev/null +++ b/app/controllers/rental_controller.rb @@ -0,0 +1,2 @@ +class RentalController < ApplicationController +end diff --git a/test/controllers/rental_controller_test.rb b/test/controllers/rental_controller_test.rb new file mode 100644 index 000000000..4e8af52d0 --- /dev/null +++ b/test/controllers/rental_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +describe RentalController do + # it "must be a real test" do + # flunk "Need real tests" + # end +end From f568c8748ca7d3b2cf00fc73d14fff308e91565c Mon Sep 17 00:00:00 2001 From: Anne Date: Tue, 8 May 2018 09:42:58 -0700 Subject: [PATCH 23/57] through relations --- app/models/customer.rb | 2 ++ app/models/movie.rb | 2 ++ 2 files changed, 4 insertions(+) diff --git a/app/models/customer.rb b/app/models/customer.rb index 453c4c065..566ae02a3 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -1,4 +1,6 @@ class Customer < ApplicationRecord has_many :rentals + has_many :movies, :through => :rentals + validates :name, presence: true end diff --git a/app/models/movie.rb b/app/models/movie.rb index 0dc4c3cca..aa520e165 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -1,4 +1,6 @@ class Movie < ApplicationRecord has_many :rentals + has_many :customers, :through => :rentals + validates :title, presence: true, uniqueness: true end From a0cdbb8e7dc03c92f58a3e886a63aadf665571d1 Mon Sep 17 00:00:00 2001 From: Anne Date: Tue, 8 May 2018 09:50:03 -0700 Subject: [PATCH 24/57] two new columns --- db/migrate/20180508164652_add_columns_to_customer_movie.rb | 7 +++++++ db/schema.rb | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20180508164652_add_columns_to_customer_movie.rb diff --git a/db/migrate/20180508164652_add_columns_to_customer_movie.rb b/db/migrate/20180508164652_add_columns_to_customer_movie.rb new file mode 100644 index 000000000..1858274a2 --- /dev/null +++ b/db/migrate/20180508164652_add_columns_to_customer_movie.rb @@ -0,0 +1,7 @@ +class AddColumnsToCustomerMovie < ActiveRecord::Migration[5.1] + def change + add_column :customers, :movies_checked_out_count, :integer + + add_column :movies, :available_inventory, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index ae803defa..3d89606d2 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: 20180507210818) do +ActiveRecord::Schema.define(version: 20180508164652) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -25,6 +25,7 @@ t.string "phone" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "movies_checked_out_count" end create_table "customers_rentals", force: :cascade do |t| @@ -43,6 +44,7 @@ t.integer "inventory" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "available_inventory" end create_table "rentals", force: :cascade do |t| From a73e28753e496748126d4ec692644031daee3295 Mon Sep 17 00:00:00 2001 From: Dikla Date: Tue, 8 May 2018 09:57:44 -0700 Subject: [PATCH 25/57] Added create and show methods and tests for customers controller --- app/controllers/customers_controller.rb | 28 ++++++++++++++++++- test/controllers/customers_controller_test.rb | 23 ++++++++++++--- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index 574ab2a17..51fb74b53 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -13,4 +13,30 @@ def index # } # end -end + def show + customer = Customer.find_by(id: params[:id]) + if customer.nil? + render json: { + errors: { + id: ["No customer with ID #{params[:id]}"] + } + }, status: :not_found + else + render json: customer.as_json(only: [:name, :registered_at, :address, :city, :state, :postal_code, :phone]), status: :ok + end + end + + def create + customer = Customer.new(customer_params) + if customer.save + render json: { id: customer.id }, status: :created + else + render json: { errors: customer.errors.messages }, status: :bad_request + end + end + + private + def customer_params + params.require(:customer).permit(:name, :registered_at, :address, :city, :state, :postal_code, :phone) + end + end diff --git a/test/controllers/customers_controller_test.rb b/test/controllers/customers_controller_test.rb index 033c06364..2c301af64 100644 --- a/test/controllers/customers_controller_test.rb +++ b/test/controllers/customers_controller_test.rb @@ -55,15 +55,30 @@ end describe "create" do - #like let(:pet_data) + let(:customer_data) { + { + name: "Shelley Rocha", + registered_at: "2015-04-29T14:54:14.000Z", + address: "Ap #292-5216 Ipsum Rd.", + city: "Hillsboro", + state: "OR", + postal_code: "24309", + phone: "(322) 510-8695" + } + } it "creates a new customer" do + before_customer_count = Customer.count + post customers_url, params: { customer: customer_data } + must_respond_with :success - end + Customer.count.must_equal before_customer_count + 1 - it "returns an error for an invalid customer" do + body = JSON.parse(response.body) + body.must_be_kind_of Hash + body.must_include "id" + Customer.find(body["id"]).name.must_equal customer_data[:name] end - end end From 480ead316f6fe9796d7805eb240536c777a343dc Mon Sep 17 00:00:00 2001 From: Anne Date: Tue, 8 May 2018 09:59:37 -0700 Subject: [PATCH 26/57] added new fields to controllers --- app/controllers/customers_controller.rb | 2 +- app/controllers/movies_controller.rb | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index 574ab2a17..8da958e1d 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -2,7 +2,7 @@ class CustomersController < ApplicationController def index customers = Customer.all - render json: customers.as_json(only: [:name, :registered_at, :address, :city, :state, :postal_code, :phone]), status: :ok + render json: customers.as_json(only: [:name, :registered_at, :address, :city, :state, :postal_code, :phone, :movies_checked_out_count]), status: :ok end # def zomg diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 744ee852b..b89be2aae 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -2,7 +2,7 @@ class MoviesController < ApplicationController def index movies = Movie.all - render json: movies.as_json(only: [:title, :overview, :release_date, :inventory]), status: :ok + render json: movies.as_json(only: [:title, :overview, :release_date, :inventory, :available_inventory]), status: :ok end def show @@ -14,7 +14,7 @@ def show } }, status: :not_found else - render json: movie.as_json(only: [:id, :title, :overview, :release_date, :inventory]), status: :ok + render json: movie.as_json(only: [:id, :title, :overview, :release_date, :inventory, :available_inventory]), status: :ok end end @@ -29,6 +29,6 @@ def create private def movie_params - params.require(:movie).permit(:title, :overview, :release_date, :inventory) + params.require(:movie).permit(:title, :overview, :release_date, :inventory, :available_inventory) end end From 0881dec9c53cf20437c9ccd39e09281c4bbdb5d4 Mon Sep 17 00:00:00 2001 From: Dikla Date: Tue, 8 May 2018 10:06:12 -0700 Subject: [PATCH 27/57] Added routes for rentals check-in and rentals check-out --- config/routes.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/routes.rb b/config/routes.rb index 4cbc64a86..2b9077347 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,5 +2,7 @@ # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html resources :customers, only: [:index, :show, :create] resources :movies, only: [:index, :show, :create] + post "/rentals", to: "rentals#check-in", as: "check-in" + post "/rentals", to: "rentals#check-out", as: "check-out" # get '/customers/zomg', to: "customers#zomg", as: 'zomg' end From dd5e071c6915eab596ab7215f109f19513eac0e6 Mon Sep 17 00:00:00 2001 From: Dikla Date: Tue, 8 May 2018 10:09:56 -0700 Subject: [PATCH 28/57] rename rental controller as rentals --- app/controllers/rental_controller.rb | 2 -- app/controllers/rentals_controller.rb | 2 ++ .../{rental_controller_test.rb => rentals_controller_test.rb} | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) delete mode 100644 app/controllers/rental_controller.rb create mode 100644 app/controllers/rentals_controller.rb rename test/controllers/{rental_controller_test.rb => rentals_controller_test.rb} (76%) diff --git a/app/controllers/rental_controller.rb b/app/controllers/rental_controller.rb deleted file mode 100644 index 55344c535..000000000 --- a/app/controllers/rental_controller.rb +++ /dev/null @@ -1,2 +0,0 @@ -class RentalController < 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/test/controllers/rental_controller_test.rb b/test/controllers/rentals_controller_test.rb similarity index 76% rename from test/controllers/rental_controller_test.rb rename to test/controllers/rentals_controller_test.rb index 4e8af52d0..f0227216c 100644 --- a/test/controllers/rental_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -1,6 +1,6 @@ require "test_helper" -describe RentalController do +describe RentalsController do # it "must be a real test" do # flunk "Need real tests" # end From 2e19a1074f27ae9a9c4f7f0b3ced04ee580681a6 Mon Sep 17 00:00:00 2001 From: Dikla Date: Tue, 8 May 2018 10:46:32 -0700 Subject: [PATCH 29/57] Added create method for rentals controller Updated the routes for create rental --- app/controllers/rentals_controller.rb | 17 +++++++++++++++++ config/routes.rb | 4 ++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 58c72b791..98a3be39e 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -1,2 +1,19 @@ class RentalsController < ApplicationController + + def create + movie = Movie.find_by(id: params[:movie_id]) + customer = Customer.find_by(id: params[:customer_id]) + new_rental_data = { + customer_id: customer.id, + movie_id: movie.id + } + rental = Rental.new(new_rental_data) + + if rental.save + render json: { errors: rental.errors.messages }, status: :bad_request + else + render :new + end + end + end diff --git a/config/routes.rb b/config/routes.rb index 2b9077347..1fe10f939 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,7 +2,7 @@ # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html resources :customers, only: [:index, :show, :create] resources :movies, only: [:index, :show, :create] - post "/rentals", to: "rentals#check-in", as: "check-in" - post "/rentals", to: "rentals#check-out", as: "check-out" + post "/rentals", to: "rentals#create", as: "check-in" + post "/rentals", to: "rentals#create", as: "check-out" # get '/customers/zomg', to: "customers#zomg", as: 'zomg' end From b50e2211029b599c0f5170ca886d77f01c842077 Mon Sep 17 00:00:00 2001 From: Dikla Date: Tue, 8 May 2018 11:24:19 -0700 Subject: [PATCH 30/57] Added create method and test for rental controller --- app/controllers/rentals_controller.rb | 6 +++++- test/controllers/movies_controller_test.rb | 2 +- test/controllers/rentals_controller_test.rb | 23 +++++++++++++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 98a3be39e..e95b2746a 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -15,5 +15,9 @@ def create render :new end end - + + private + def rental_params + params.require(:rental).permit(:movie_id, :customer_id) + end end diff --git a/test/controllers/movies_controller_test.rb b/test/controllers/movies_controller_test.rb index efb47b702..3c6fb9b60 100644 --- a/test/controllers/movies_controller_test.rb +++ b/test/controllers/movies_controller_test.rb @@ -54,7 +54,7 @@ describe "show" do # This bit is up to you! it "can get a movie" do - keys = %w(id inventory overview release_date title) + keys = %w(available_inventory id inventory overview release_date title) movie = movies(:two) get movie_path(movie.id) must_respond_with :success diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index f0227216c..4acc9d1ba 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -4,4 +4,27 @@ # it "must be a real test" do # flunk "Need real tests" # end + describe "create" do + let(:rental_data) { + { + customer_id: rentals(:one).id, + movie_id: movies(:one).id + } + } + + it "creates a new rental" do + before_rental_count = Rental.count + post check-in_url, params: { rental: rental_data } + must_respond_with :success + + Rental.count.must_equal before_rental_count + 1 + + body = JSON.parse(response.body) + body.must_be_kind_of Hash + body.must_include "id" + + Rental.find(body["customer_id"]).must_equal rental_data[:customer_id] + Rental.find(body["movie_id"]).must_equal rental_data[:movie_id] + end + end end From 6829d452c5cc320677ca73fc6f2a99de79efbb51 Mon Sep 17 00:00:00 2001 From: Dikla Date: Tue, 8 May 2018 13:42:06 -0700 Subject: [PATCH 31/57] Updated the routes --- app/controllers/rentals_controller.rb | 4 ++-- config/routes.rb | 4 ++-- test/controllers/rentals_controller_test.rb | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index e95b2746a..0061b180b 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -1,6 +1,6 @@ class RentalsController < ApplicationController - def create + def check_in movie = Movie.find_by(id: params[:movie_id]) customer = Customer.find_by(id: params[:customer_id]) new_rental_data = { @@ -15,7 +15,7 @@ def create render :new end end - + private def rental_params params.require(:rental).permit(:movie_id, :customer_id) diff --git a/config/routes.rb b/config/routes.rb index 1fe10f939..643b5339e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,7 +2,7 @@ # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html resources :customers, only: [:index, :show, :create] resources :movies, only: [:index, :show, :create] - post "/rentals", to: "rentals#create", as: "check-in" - post "/rentals", to: "rentals#create", as: "check-out" + post "/rentals", to: "rentals#check_in", as: "checkin" + post "/rentals", to: "rentals#check_out", as: "checkout" # get '/customers/zomg', to: "customers#zomg", as: 'zomg' end diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index 4acc9d1ba..7e6a522a6 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -4,17 +4,17 @@ # it "must be a real test" do # flunk "Need real tests" # end - describe "create" do + describe "check_in" do let(:rental_data) { { - customer_id: rentals(:one).id, + customer_id: customers(:one).id, movie_id: movies(:one).id } } it "creates a new rental" do before_rental_count = Rental.count - post check-in_url, params: { rental: rental_data } + post checkin_url, params: { rental: rental_data } must_respond_with :success Rental.count.must_equal before_rental_count + 1 From fe21dfee6631dd75212dbd013e73dba2d34d097e Mon Sep 17 00:00:00 2001 From: Anne Date: Tue, 8 May 2018 13:44:04 -0700 Subject: [PATCH 32/57] added date columns on rental --- db/migrate/20180508204140_add_date_columns.rb | 7 +++++++ db/schema.rb | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20180508204140_add_date_columns.rb diff --git a/db/migrate/20180508204140_add_date_columns.rb b/db/migrate/20180508204140_add_date_columns.rb new file mode 100644 index 000000000..c11eeaaa5 --- /dev/null +++ b/db/migrate/20180508204140_add_date_columns.rb @@ -0,0 +1,7 @@ +class AddDateColumns < ActiveRecord::Migration[5.1] + def change + add_column :rentals, :check_out_date, :date + + add_column :rentals, :due_date, :date + end +end diff --git a/db/schema.rb b/db/schema.rb index 3d89606d2..c738a9c7e 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: 20180508164652) do +ActiveRecord::Schema.define(version: 20180508204140) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -50,6 +50,8 @@ create_table "rentals", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.date "check_out_date" + t.date "due_date" end end From 71b0876d08409590e41df1d617ace713fb4d20bc Mon Sep 17 00:00:00 2001 From: Dikla Date: Tue, 8 May 2018 14:11:20 -0700 Subject: [PATCH 33/57] Skiped a test --- test/models/rental_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/models/rental_test.rb b/test/models/rental_test.rb index 6ea53d94f..83818d73c 100644 --- a/test/models/rental_test.rb +++ b/test/models/rental_test.rb @@ -2,8 +2,8 @@ describe Rental do let(:rental) { Rental.new } - it "must be valid" do + skip value(rental).must_be :valid? end end From b8d78c0f7e6294ffb45e12c11dd04b7171ed2b02 Mon Sep 17 00:00:00 2001 From: Dikla Date: Tue, 8 May 2018 15:08:00 -0700 Subject: [PATCH 34/57] Updated the schema --- app/controllers/rentals_controller.rb | 18 +++++++++--------- .../20180508215010_add_column_rentals.rb | 7 +++++++ db/migrate/20180508220005_drop_table.rb | 5 +++++ .../20180508220130_drop_table_rentals.rb | 6 ++++++ db/migrate/20180508220245_rentals.rb | 9 +++++++++ db/schema.rb | 17 +++++------------ 6 files changed, 41 insertions(+), 21 deletions(-) create mode 100644 db/migrate/20180508215010_add_column_rentals.rb create mode 100644 db/migrate/20180508220005_drop_table.rb create mode 100644 db/migrate/20180508220130_drop_table_rentals.rb create mode 100644 db/migrate/20180508220245_rentals.rb diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 0061b180b..967d3072a 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -1,18 +1,18 @@ class RentalsController < ApplicationController def check_in - movie = Movie.find_by(id: params[:movie_id]) - customer = Customer.find_by(id: params[:customer_id]) - new_rental_data = { - customer_id: customer.id, - movie_id: movie.id - } - rental = Rental.new(new_rental_data) + # movie = Movie.find_by(id: rental_params[:movie_id]) + # customer = Customer.find_by(id: rental_params[:customer_id]) + # new_rental_data = { + # customer_id: customer.id, + # movie_id: movie.id + # } + rental = Rental.new(rental_params) if rental.save - render json: { errors: rental.errors.messages }, status: :bad_request + render json: { id: rental.id }, status: :created else - render :new + render json: { errors: rental.errors.messages }, status: :bad_request end end diff --git a/db/migrate/20180508215010_add_column_rentals.rb b/db/migrate/20180508215010_add_column_rentals.rb new file mode 100644 index 000000000..aeb449d60 --- /dev/null +++ b/db/migrate/20180508215010_add_column_rentals.rb @@ -0,0 +1,7 @@ +class AddColumnRentals < ActiveRecord::Migration[5.1] + def change + add_column :rentals, :movie_id, :integer + + add_column :rentals, :customer_id, :integer + end +end diff --git a/db/migrate/20180508220005_drop_table.rb b/db/migrate/20180508220005_drop_table.rb new file mode 100644 index 000000000..b035011c5 --- /dev/null +++ b/db/migrate/20180508220005_drop_table.rb @@ -0,0 +1,5 @@ +class DropTable < ActiveRecord::Migration[5.1] + def change + drop_table :customers_rentals + end +end diff --git a/db/migrate/20180508220130_drop_table_rentals.rb b/db/migrate/20180508220130_drop_table_rentals.rb new file mode 100644 index 000000000..5233ef0c7 --- /dev/null +++ b/db/migrate/20180508220130_drop_table_rentals.rb @@ -0,0 +1,6 @@ +class DropTableRentals < ActiveRecord::Migration[5.1] + def change + drop_table :rentals + + end +end diff --git a/db/migrate/20180508220245_rentals.rb b/db/migrate/20180508220245_rentals.rb new file mode 100644 index 000000000..3a85852f8 --- /dev/null +++ b/db/migrate/20180508220245_rentals.rb @@ -0,0 +1,9 @@ +class Rentals < ActiveRecord::Migration[5.1] + def change + create_table :rentals do |t| + t.belongs_to :movie, index: true + t.belongs_to :customer, index: true + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index c738a9c7e..2e875c971 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: 20180508204140) do +ActiveRecord::Schema.define(version: 20180508220245) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -28,15 +28,6 @@ t.integer "movies_checked_out_count" end - create_table "customers_rentals", force: :cascade do |t| - t.bigint "customer_id" - t.bigint "movie_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.index ["customer_id"], name: "index_customers_rentals_on_customer_id" - t.index ["movie_id"], name: "index_customers_rentals_on_movie_id" - end - create_table "movies", force: :cascade do |t| t.string "title" t.string "overview" @@ -48,10 +39,12 @@ end create_table "rentals", force: :cascade do |t| + t.bigint "movie_id" + t.bigint "customer_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.date "check_out_date" - t.date "due_date" + t.index ["customer_id"], name: "index_rentals_on_customer_id" + t.index ["movie_id"], name: "index_rentals_on_movie_id" end end From dd7c16a55c5669d9b731e170192467a862dbf513 Mon Sep 17 00:00:00 2001 From: Anne Date: Tue, 8 May 2018 15:12:21 -0700 Subject: [PATCH 35/57] fields migration --- db/migrate/20180508221126_add_columns_again.rb | 7 +++++++ db/schema.rb | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20180508221126_add_columns_again.rb diff --git a/db/migrate/20180508221126_add_columns_again.rb b/db/migrate/20180508221126_add_columns_again.rb new file mode 100644 index 000000000..81bc83d24 --- /dev/null +++ b/db/migrate/20180508221126_add_columns_again.rb @@ -0,0 +1,7 @@ +class AddColumnsAgain < ActiveRecord::Migration[5.1] + def change + add_column :rentals, :check_out_date, :date + + add_column :rentals, :due_date, :date + end +end diff --git a/db/schema.rb b/db/schema.rb index 2e875c971..87821e091 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: 20180508220245) do +ActiveRecord::Schema.define(version: 20180508221126) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -43,6 +43,8 @@ t.bigint "customer_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.date "check_out_date" + t.date "due_date" t.index ["customer_id"], name: "index_rentals_on_customer_id" t.index ["movie_id"], name: "index_rentals_on_movie_id" end From 8eaaf44b822537fe6ded6f76b00124a7fe29051c Mon Sep 17 00:00:00 2001 From: Dikla Date: Tue, 8 May 2018 15:39:25 -0700 Subject: [PATCH 36/57] passed rentals controller test and successfully pass customer_id and movie_id on rental --- app/controllers/rentals_controller.rb | 2 +- test/controllers/rentals_controller_test.rb | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 967d3072a..8a2c306cf 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -10,7 +10,7 @@ def check_in rental = Rental.new(rental_params) if rental.save - render json: { id: rental.id }, status: :created + render json: { id: rental.id, customer_id: rental.customer_id, movie_id: rental.movie_id }, status: :created else render json: { errors: rental.errors.messages }, status: :bad_request end diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index 7e6a522a6..3701c4b56 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -22,9 +22,10 @@ body = JSON.parse(response.body) body.must_be_kind_of Hash body.must_include "id" + puts body - Rental.find(body["customer_id"]).must_equal rental_data[:customer_id] - Rental.find(body["movie_id"]).must_equal rental_data[:movie_id] + Rental.find(body["id"]).customer.id.must_equal rental_data[:customer_id] + Rental.find(body["id"]).movie.id.must_equal rental_data[:movie_id] end end end From eebef0fe0c761a873c3a9f4c6fbd2f3637f5c327 Mon Sep 17 00:00:00 2001 From: Dikla Date: Tue, 8 May 2018 15:48:08 -0700 Subject: [PATCH 37/57] Updated the routes --- app/controllers/rentals_controller.rb | 9 ++------- config/routes.rb | 5 +++-- test/controllers/rentals_controller_test.rb | 5 ++--- 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 8a2c306cf..b3a5e6fbd 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -1,12 +1,7 @@ class RentalsController < ApplicationController - def check_in - # movie = Movie.find_by(id: rental_params[:movie_id]) - # customer = Customer.find_by(id: rental_params[:customer_id]) - # new_rental_data = { - # customer_id: customer.id, - # movie_id: movie.id - # } + def check_out + rental = Rental.new(rental_params) if rental.save diff --git a/config/routes.rb b/config/routes.rb index 643b5339e..f103f2ad3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,7 +2,8 @@ # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html resources :customers, only: [:index, :show, :create] resources :movies, only: [:index, :show, :create] - post "/rentals", to: "rentals#check_in", as: "checkin" - post "/rentals", to: "rentals#check_out", as: "checkout" + post "/rentals/checkin", to: "rentals#check_in", as: "checkin" + post "/rentals/checkout", to: "rentals#check_out", as: "checkout" + # get '/customers/zomg', to: "customers#zomg", as: 'zomg' end diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index 3701c4b56..3222a9828 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -4,7 +4,7 @@ # it "must be a real test" do # flunk "Need real tests" # end - describe "check_in" do + describe "check_out" do let(:rental_data) { { customer_id: customers(:one).id, @@ -14,7 +14,7 @@ it "creates a new rental" do before_rental_count = Rental.count - post checkin_url, params: { rental: rental_data } + post checkout_url, params: { rental: rental_data } must_respond_with :success Rental.count.must_equal before_rental_count + 1 @@ -22,7 +22,6 @@ body = JSON.parse(response.body) body.must_be_kind_of Hash body.must_include "id" - puts body Rental.find(body["id"]).customer.id.must_equal rental_data[:customer_id] Rental.find(body["id"]).movie.id.must_equal rental_data[:movie_id] From 4d2103821ae9121d3955eb4de5da2f09c5b40c33 Mon Sep 17 00:00:00 2001 From: Anne Date: Tue, 8 May 2018 16:07:44 -0700 Subject: [PATCH 38/57] relations test for customer rentals --- test/models/customer_test.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/test/models/customer_test.rb b/test/models/customer_test.rb index 152e895d1..4330c4afa 100644 --- a/test/models/customer_test.rb +++ b/test/models/customer_test.rb @@ -38,7 +38,7 @@ describe "relations" do before do - @customer = Customer.new( + @customer = Customer.create( name: "Movie Watcher", address: "2015-04-29T14:54:14.000Z", city: "Seattle", @@ -46,10 +46,16 @@ postal_code: "98109", phone: "555-555-5555" ) + + movie = Movie.first + rental_data = { movie_id: movie.id, customer_id: @customer.id} + + rental = Rental.create(rental_data) + end it "has movies" do - + @customer.rentals.count.must_equal 1 end end end From 9ca99edafe5b4995295427d4c7f08a21b1f728a1 Mon Sep 17 00:00:00 2001 From: Dikla Date: Tue, 8 May 2018 16:20:29 -0700 Subject: [PATCH 39/57] Added relation tests for movie model --- test/models/movie_test.rb | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/test/models/movie_test.rb b/test/models/movie_test.rb index d5ef5c5af..98f81b923 100644 --- a/test/models/movie_test.rb +++ b/test/models/movie_test.rb @@ -13,6 +13,11 @@ release_date: "1979-01-18", inventory: "10" ) + + customer = Customer.first + rental_data = { movie_id: @movie.id, customer_id: customer.id} + + rental = Rental.create(rental_data) end it "can be created will all required fields" do @@ -49,16 +54,21 @@ describe "relations" do before do - @movie = Movie.new( + @movie = Movie.create( title: "test movie", overview: "The unexciting life of a boy", release_date: "1979-01-18", inventory: "10" ) - end - it "has customers" do + customer = Customer.first + rental_data = { movie_id: @movie.id, customer_id: customer.id} + + rental = Rental.create(rental_data) + end + it "has rentals" do + @movie.rentals.count.must_equal 1 end end From 5d3e8f97d01c82a60d7f6ed2ca6d9f8c6376af3a Mon Sep 17 00:00:00 2001 From: Anne Date: Tue, 8 May 2018 18:47:58 -0700 Subject: [PATCH 40/57] added after_initialize on customer and movie to set default field values upon creation --- app/controllers/customers_controller.rb | 8 -------- app/models/customer.rb | 9 +++++++++ app/models/movie.rb | 9 +++++++++ app/models/rental.rb | 10 ++++++++++ test/models/rental_test.rb | 6 ++++-- 5 files changed, 32 insertions(+), 10 deletions(-) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index fc08e2adf..9216673c4 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -5,14 +5,6 @@ def index render json: customers.as_json(only: [:name, :registered_at, :address, :city, :state, :postal_code, :phone, :movies_checked_out_count]), status: :ok end - # def zomg - # render json: { - # "customer": { - # "name": "it works" - # } - # } - # end - def show customer = Customer.find_by(id: params[:id]) if customer.nil? diff --git a/app/models/customer.rb b/app/models/customer.rb index 566ae02a3..1b917c525 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -3,4 +3,13 @@ class Customer < ApplicationRecord has_many :movies, :through => :rentals validates :name, presence: true + + #https://stackoverflow.com/questions/29575259/default-values-for-models-in-rails + + after_initialize :set_defaults, unless: :persisted? + + private + def set_defaults + self.movies_checked_out_count ||= 0 + end end diff --git a/app/models/movie.rb b/app/models/movie.rb index aa520e165..f4d60b447 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -3,4 +3,13 @@ class Movie < ApplicationRecord has_many :customers, :through => :rentals validates :title, presence: true, uniqueness: true + + #https://stackoverflow.com/questions/29575259/default-values-for-models-in-rails + + private + after_initialize :set_defaults, unless: :persisted? + + def set_defaults + self.available_inventory ||= self.inventory + end end diff --git a/app/models/rental.rb b/app/models/rental.rb index e001ff37c..3d9544bb2 100644 --- a/app/models/rental.rb +++ b/app/models/rental.rb @@ -1,4 +1,14 @@ class Rental < ApplicationRecord belongs_to :customer belongs_to :movie + + #https://stackoverflow.com/questions/29575259/default-values-for-models-in-rails + + after_initialize :set_defaults, unless: :persisted? + + private + def set_defaults + self.check_out_date ||= Date.today + self.due_date ||= Date.today + 7 + end end diff --git a/test/models/rental_test.rb b/test/models/rental_test.rb index 83818d73c..febf49b48 100644 --- a/test/models/rental_test.rb +++ b/test/models/rental_test.rb @@ -1,9 +1,11 @@ require "test_helper" describe Rental do - let(:rental) { Rental.new } + movie = Movie.first + customer = Customer.first + let(:rental) { Rental.new(movie_id: movie.id, customer_id: customer.id) } + it "must be valid" do - skip value(rental).must_be :valid? end end From d4bb46c8a554e682bf21e96c6f4f370cd3cf822a Mon Sep 17 00:00:00 2001 From: Anne Date: Tue, 8 May 2018 18:52:58 -0700 Subject: [PATCH 41/57] rental model test --- test/models/rental_test.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/test/models/rental_test.rb b/test/models/rental_test.rb index febf49b48..a20038f20 100644 --- a/test/models/rental_test.rb +++ b/test/models/rental_test.rb @@ -1,11 +1,22 @@ require "test_helper" describe Rental do + movie = Movie.first customer = Customer.first let(:rental) { Rental.new(movie_id: movie.id, customer_id: customer.id) } - + it "must be valid" do value(rental).must_be :valid? end + + describe "relationship" do + it "belongs to movie" do + rental.must_respond_to :movie + end + + it "belongs to customer" do + rental.must_respond_to :customer + end + end end From 4118031baaea7f9f4f9f27116ab2991beaf9d7b6 Mon Sep 17 00:00:00 2001 From: Anne Date: Tue, 8 May 2018 20:06:58 -0700 Subject: [PATCH 42/57] available to rent test --- app/controllers/rentals_controller.rb | 9 ++++++--- app/models/movie.rb | 8 +++++++- test/controllers/rentals_controller_test.rb | 12 ++++++++++++ test/fixtures/movies.yml | 2 ++ 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index b3a5e6fbd..f3eb1d08f 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -1,11 +1,14 @@ class RentalsController < ApplicationController def check_out - rental = Rental.new(rental_params) - if rental.save - render json: { id: rental.id, customer_id: rental.customer_id, movie_id: rental.movie_id }, status: :created + if rental.movie.available_to_rent? + if rental.save + render json: { id: rental.id, customer_id: rental.customer_id, movie_id: rental.movie_id }, status: :created + else + render json: { errors: rental.errors.messages }, status: :bad_request + end else render json: { errors: rental.errors.messages }, status: :bad_request end diff --git a/app/models/movie.rb b/app/models/movie.rb index f4d60b447..2cd96e7d8 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -4,9 +4,15 @@ class Movie < ApplicationRecord validates :title, presence: true, uniqueness: true - #https://stackoverflow.com/questions/29575259/default-values-for-models-in-rails + def available_to_rent? + return self.available_inventory >= 1 + end + + + private + #https://stackoverflow.com/questions/29575259/default-values-for-models-in-rails after_initialize :set_defaults, unless: :persisted? def set_defaults diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index 3222a9828..9723b8340 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -26,5 +26,17 @@ Rental.find(body["id"]).customer.id.must_equal rental_data[:customer_id] Rental.find(body["id"]).movie.id.must_equal rental_data[:movie_id] end + + it "will respond with bad_request if the movie is not available to rent" do + before_rental_count = Rental.count + movie = Movie.create(title: "Unavailable", inventory: 1, available_inventory: 0) + + unavailable_data = { customer_id: customers(:one).id, movie_id: movie.id} + + post checkout_url, params: { rental: unavailable_data } + + must_respond_with :bad_request + Rental.count.must_equal before_rental_count + end end end diff --git a/test/fixtures/movies.yml b/test/fixtures/movies.yml index 090b19e62..b997d3e77 100644 --- a/test/fixtures/movies.yml +++ b/test/fixtures/movies.yml @@ -5,9 +5,11 @@ one: overview: MyString release_date: 2018-05-07 inventory: 1 + available_inventory: 1 two: title: MyString overview: MyString release_date: 2018-05-07 inventory: 1 + available_inventory: 1 From 2a592985ace7fa3fbd961ebd6db6b2ab611ab0d5 Mon Sep 17 00:00:00 2001 From: Anne Date: Tue, 8 May 2018 20:19:50 -0700 Subject: [PATCH 43/57] still need available inventory decrement test to pass --- app/controllers/rentals_controller.rb | 1 + app/models/movie.rb | 5 ++++- test/controllers/rentals_controller_test.rb | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index f3eb1d08f..f80e70e3d 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -5,6 +5,7 @@ def check_out if rental.movie.available_to_rent? if rental.save + rental.movie.reduce_available_inventory render json: { id: rental.id, customer_id: rental.customer_id, movie_id: rental.movie_id }, status: :created else render json: { errors: rental.errors.messages }, status: :bad_request diff --git a/app/models/movie.rb b/app/models/movie.rb index 2cd96e7d8..96a020880 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -9,7 +9,10 @@ def available_to_rent? end - + def reduce_available_inventory + self.available_inventory -= 1 + self.save + end private #https://stackoverflow.com/questions/29575259/default-values-for-models-in-rails diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index 9723b8340..afb49c40b 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -38,5 +38,19 @@ must_respond_with :bad_request Rental.count.must_equal before_rental_count end + + it "reduces_available_inventory on movie in the rental" do + # movie = Movie.create(title: "Fake Movie", inventory: 10, available_inventory: 10) + # + # before_availability = movie.available_inventory + # + # puts before_availability + # + # available_data = { customer_id: customers(:one).id, movie_id: movie.id } + # + # post checkout_url, params: { rental: available_data } + # + # movie.available_inventory.must_equal before_availability - 1 + end end end From ba34b9bda907f68a14238eeebcee657b784d4b78 Mon Sep 17 00:00:00 2001 From: Dikla Date: Wed, 9 May 2018 09:10:56 -0700 Subject: [PATCH 44/57] Fixed code to make the postman test to pass --- .DS_Store | Bin 0 -> 6148 bytes app/controllers/customers_controller.rb | 2 +- app/controllers/movies_controller.rb | 7 ++++--- app/controllers/rentals_controller.rb | 3 ++- test/controllers/movies_controller_test.rb | 5 ++--- 5 files changed, 9 insertions(+), 8 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..f3c7967df78c7bd8f70ef9e574abcbe0f5fed333 GIT binary patch literal 6148 zcmeHK-EPw`6h3ZBmoN~OKq{BHLE>UU-I$mVLMo*b+65A#2rd9^NmIILX;QW63Y4PW z@DAesKTZvVp z9%Yo$J8%akS2RjougIl4G^T0E<4eh-LJ@@}&d(BO6La_R0~2xe=maAVDKfo?qdO}^ zw?R+fGnDI2s9$1pltuOxlZ_b?M2)#jKN)e+uLf0ySk(Y?O-fvjwa9faX1q4qGAk=9 z&CvtgN5c;v=bsjz&#;>?fpbyXHI3)+6@?2OPP^kY z$6_D>DA3E@R zqNQ=EP`)~l$twV`ie_mj%a4Dc&kewq#-&2Ez=U!IDp#Se7((A2gmOoJOY@fsl{*PJ zGse*~3w=Wo`syKkNhi@#XmhK8RlrohQPn!1|NDQw|C>&>W)-js{8tKyYNy+2BP4yc zt^~(tt&ens#Kyd(Lg|7`AIGx5NAXQ0Y3TF00BmVoDntv+eh5ezY-Sbss|x%ErIsLe literal 0 HcmV?d00001 diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index fc08e2adf..b49c60892 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -2,7 +2,7 @@ class CustomersController < ApplicationController def index customers = Customer.all - render json: customers.as_json(only: [:name, :registered_at, :address, :city, :state, :postal_code, :phone, :movies_checked_out_count]), status: :ok + render json: customers.as_json(only: [:id, :name, :registered_at, :address, :city, :state, :postal_code, :phone, :movies_checked_out_count]), status: :ok end # def zomg diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index b89be2aae..b5d4d7d28 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -2,7 +2,7 @@ class MoviesController < ApplicationController def index movies = Movie.all - render json: movies.as_json(only: [:title, :overview, :release_date, :inventory, :available_inventory]), status: :ok + render json: movies.as_json(only: [:id, :title, :overview, :release_date, :inventory, :available_inventory]), status: :ok end def show @@ -14,12 +14,13 @@ def show } }, status: :not_found else - render json: movie.as_json(only: [:id, :title, :overview, :release_date, :inventory, :available_inventory]), status: :ok + render json: movie.as_json(only: [:title, :overview, :release_date, :inventory, :available_inventory]), status: :ok end end def create movie = Movie.new(movie_params) + #TODO set available_inventory to inventory before saving if movie.save render json: { id: movie.id }, status: :created else @@ -29,6 +30,6 @@ def create private def movie_params - params.require(:movie).permit(:title, :overview, :release_date, :inventory, :available_inventory) + params.permit(:title, :overview, :release_date, :inventory, :available_inventory) end end diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index b3a5e6fbd..ff2fe2a65 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -1,10 +1,11 @@ class RentalsController < ApplicationController def check_out - + rental = Rental.new(rental_params) if rental.save + #TODO call the method to decrease movie available_inventory render json: { id: rental.id, customer_id: rental.customer_id, movie_id: rental.movie_id }, status: :created else render json: { errors: rental.errors.messages }, status: :bad_request diff --git a/test/controllers/movies_controller_test.rb b/test/controllers/movies_controller_test.rb index 3c6fb9b60..44d518ab5 100644 --- a/test/controllers/movies_controller_test.rb +++ b/test/controllers/movies_controller_test.rb @@ -38,7 +38,7 @@ it "creates a new movie" do before_movie_count = Movie.count - post movies_url, params: { movie: movie_data } + post movies_url, params: movie_data must_respond_with :success Movie.count.must_equal before_movie_count + 1 @@ -54,7 +54,7 @@ describe "show" do # This bit is up to you! it "can get a movie" do - keys = %w(available_inventory id inventory overview release_date title) + keys = %w(available_inventory inventory overview release_date title) movie = movies(:two) get movie_path(movie.id) must_respond_with :success @@ -63,7 +63,6 @@ body = JSON.parse(response.body) body.must_be_kind_of Hash body.keys.sort.must_equal keys - body["id"].must_equal movie.id end it "yields a not found status and also return some error text if the movie D.N.E" do From 1d1d1e7be1a33fcf5d72753ad3360c8dbce88cdf Mon Sep 17 00:00:00 2001 From: Anne Date: Wed, 9 May 2018 09:11:42 -0700 Subject: [PATCH 45/57] future model method commented out --- app/models/movie.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/models/movie.rb b/app/models/movie.rb index 96a020880..85867e368 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -14,6 +14,11 @@ def reduce_available_inventory self.save end + #def increase_available_inventory + # self.available_inventory += 1 + # self.save + #end + private #https://stackoverflow.com/questions/29575259/default-values-for-models-in-rails after_initialize :set_defaults, unless: :persisted? From f038daefecacbf2b4878825c619bb1e473a4de9b Mon Sep 17 00:00:00 2001 From: Anne Date: Wed, 9 May 2018 10:22:50 -0700 Subject: [PATCH 46/57] rental checkout test passing --- app/controllers/rentals_controller.rb | 1 + ...Wave_3_smoke_tests.postman_collection.json | 6 ++-- test/controllers/rentals_controller_test.rb | 31 ++++++++++++------- test/fixtures/movies.yml | 8 ++--- 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 6c1c4f2a2..f2b1f32f2 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -7,6 +7,7 @@ def check_out if rental.movie.available_to_rent? if rental.save rental.movie.reduce_available_inventory + #rental.customer.increase_movies_checked_out_count render json: { id: rental.id, customer_id: rental.customer_id, movie_id: rental.movie_id }, status: :created else render json: { errors: rental.errors.messages }, status: :bad_request diff --git a/test/VideoStoreAPI_Wave_3_smoke_tests.postman_collection.json b/test/VideoStoreAPI_Wave_3_smoke_tests.postman_collection.json index 9f640d98c..51f4b6dc5 100644 --- a/test/VideoStoreAPI_Wave_3_smoke_tests.postman_collection.json +++ b/test/VideoStoreAPI_Wave_3_smoke_tests.postman_collection.json @@ -221,13 +221,13 @@ "raw": "{\n\t\"movie_id\": {{movie_id}},\n\t\"customer_id\": {{customer_id}}\n}" }, "url": { - "raw": "{{url}}/rentals/check-out", + "raw": "{{url}}/rentals/checkout", "host": [ "{{url}}" ], "path": [ "rentals", - "check-out" + "checkout" ] } }, @@ -479,4 +479,4 @@ "response": [] } ] -} \ No newline at end of file +} diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index afb49c40b..a55610f9e 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -40,17 +40,26 @@ end it "reduces_available_inventory on movie in the rental" do - # movie = Movie.create(title: "Fake Movie", inventory: 10, available_inventory: 10) - # - # before_availability = movie.available_inventory - # - # puts before_availability - # - # available_data = { customer_id: customers(:one).id, movie_id: movie.id } - # - # post checkout_url, params: { rental: available_data } - # - # movie.available_inventory.must_equal before_availability - 1 + movie = movies(:two) + + before_availability = movie.available_inventory + + puts "before availability #{before_availability}" + + available_data = { customer_id: customers(:one).id, movie_id: movies(:two).id } + + post checkout_url, params: { rental: available_data } + + body = JSON.parse(response.body) + + puts "body: #{body}" + + + puts movie.title + movie.reload + + puts "movie availability after post: #{movie.available_inventory}" + movie.available_inventory.must_equal before_availability - 1 end end end diff --git a/test/fixtures/movies.yml b/test/fixtures/movies.yml index b997d3e77..682290670 100644 --- a/test/fixtures/movies.yml +++ b/test/fixtures/movies.yml @@ -8,8 +8,8 @@ one: available_inventory: 1 two: - title: MyString - overview: MyString + title: SecondMovie + overview: SecondMovieOverview release_date: 2018-05-07 - inventory: 1 - available_inventory: 1 + inventory: 3 + available_inventory: 2 From 6d25a396e0208d7ea7dc28ebf24d457cb6643719 Mon Sep 17 00:00:00 2001 From: Dikla Date: Wed, 9 May 2018 10:25:22 -0700 Subject: [PATCH 47/57] Added check_in method to rental controller --- app/controllers/movies_controller.rb | 1 - app/controllers/rentals_controller.rb | 13 +++++++++++++ app/models/movie.rb | 8 ++++---- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index b5d4d7d28..f5cf4c023 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -20,7 +20,6 @@ def show def create movie = Movie.new(movie_params) - #TODO set available_inventory to inventory before saving if movie.save render json: { id: movie.id }, status: :created else diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 6c1c4f2a2..78f35b7f6 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -16,6 +16,19 @@ def check_out end end + def check_in + + rental = Rental.find_by_id(rental_params[:id]) + if rental.save + rental.movie.increase_available_inventory + render json: { id: rental.id, customer_id: rental.customer_id, movie_id: rental.movie_id }, status: :created + else + render json: { errors: rental.errors.messages }, status: :bad_request + end + + end + + private def rental_params params.require(:rental).permit(:movie_id, :customer_id) diff --git a/app/models/movie.rb b/app/models/movie.rb index 85867e368..51056f4ea 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -14,10 +14,10 @@ def reduce_available_inventory self.save end - #def increase_available_inventory - # self.available_inventory += 1 - # self.save - #end + def increase_available_inventory + self.available_inventory += 1 + self.save + end private #https://stackoverflow.com/questions/29575259/default-values-for-models-in-rails From 6a0fd5cbe9a5c8e4e6e30233dde0bc87399677d8 Mon Sep 17 00:00:00 2001 From: Dikla Date: Wed, 9 May 2018 10:49:32 -0700 Subject: [PATCH 48/57] Added test for chek_in method --- app/controllers/rentals_controller.rb | 3 +- test/controllers/rentals_controller_test.rb | 33 +++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 2fdc60dae..e616de9ef 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -19,7 +19,8 @@ def check_out def check_in - rental = Rental.find_by_id(rental_params[:id]) + rental = Rental.find_by(rental_params[:id]) + puts rental.customer if rental.save rental.movie.increase_available_inventory render json: { id: rental.id, customer_id: rental.customer_id, movie_id: rental.movie_id }, status: :created diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index a55610f9e..cdb32ba9d 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -62,4 +62,37 @@ movie.available_inventory.must_equal before_availability - 1 end end + + + describe "check_in" do + let(:rental_data) { + { + customer_id: customers(:one).id, + movie_id: movies(:one).id + } + } + it "increase_available_inventory on movie in the rental" do + movie = movies(:one) + before_availability = movie.available_inventory + + puts "before availability #{before_availability}" + + available_data = { customer_id: customers(:one).id, movie_id: movies(:two).id } + # post checkout_url, params: { rental: available_data } + # movie.reload + post checkin_url, params: { rental: available_data } + + body = JSON.parse(response.body) + + puts "body: #{body}" + + + puts movie.title + movie.reload + + puts "movie availability after post: #{movie.available_inventory}" + movie.available_inventory.must_equal before_availability + 1 + end + end + end From 648827e9cdb30eba13747633dd94bd446b6e4526 Mon Sep 17 00:00:00 2001 From: Anne Date: Wed, 9 May 2018 10:50:18 -0700 Subject: [PATCH 49/57] update smoke test --- test/VideoStoreAPI_Wave_3_smoke_tests.postman_collection.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/VideoStoreAPI_Wave_3_smoke_tests.postman_collection.json b/test/VideoStoreAPI_Wave_3_smoke_tests.postman_collection.json index 51f4b6dc5..745786797 100644 --- a/test/VideoStoreAPI_Wave_3_smoke_tests.postman_collection.json +++ b/test/VideoStoreAPI_Wave_3_smoke_tests.postman_collection.json @@ -370,13 +370,13 @@ "raw": "{\n\t\"customer_id\": {{customer_id}},\n\t\"movie_id\": {{movie_id}}\n}" }, "url": { - "raw": "{{url}}/rentals/check-in", + "raw": "{{url}}/rentals/checkin", "host": [ "{{url}}" ], "path": [ "rentals", - "check-in" + "checkin" ] } }, From 8ccb977cdf67c47fb7dc30db0305ab9d2cfa415a Mon Sep 17 00:00:00 2001 From: Dikla Date: Wed, 9 May 2018 11:29:54 -0700 Subject: [PATCH 50/57] Updated the check_in method and test --- app/controllers/rentals_controller.rb | 3 +-- test/controllers/rentals_controller_test.rb | 23 +++++---------------- 2 files changed, 6 insertions(+), 20 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index e616de9ef..763b536eb 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -19,8 +19,7 @@ def check_out def check_in - rental = Rental.find_by(rental_params[:id]) - puts rental.customer + rental = Rental.find_by(rental_params) if rental.save rental.movie.increase_available_inventory render json: { id: rental.id, customer_id: rental.customer_id, movie_id: rental.movie_id }, status: :created diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index cdb32ba9d..7894a0b97 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -44,21 +44,14 @@ before_availability = movie.available_inventory - puts "before availability #{before_availability}" - available_data = { customer_id: customers(:one).id, movie_id: movies(:two).id } post checkout_url, params: { rental: available_data } body = JSON.parse(response.body) - puts "body: #{body}" - - - puts movie.title movie.reload - puts "movie availability after post: #{movie.available_inventory}" movie.available_inventory.must_equal before_availability - 1 end end @@ -68,30 +61,24 @@ let(:rental_data) { { customer_id: customers(:one).id, - movie_id: movies(:one).id + movie_id: movies(:two).id } } it "increase_available_inventory on movie in the rental" do - movie = movies(:one) + movie = movies(:two) before_availability = movie.available_inventory - puts "before availability #{before_availability}" available_data = { customer_id: customers(:one).id, movie_id: movies(:two).id } - # post checkout_url, params: { rental: available_data } - # movie.reload + post checkout_url, params: { rental: available_data } + movie.reload post checkin_url, params: { rental: available_data } body = JSON.parse(response.body) - puts "body: #{body}" - - - puts movie.title movie.reload - puts "movie availability after post: #{movie.available_inventory}" - movie.available_inventory.must_equal before_availability + 1 + movie.available_inventory.must_equal before_availability end end From ca6941b2d71b022ebcd5cbcd230f9e2a104e0e84 Mon Sep 17 00:00:00 2001 From: Dikla Date: Wed, 9 May 2018 11:41:51 -0700 Subject: [PATCH 51/57] updated the smoke tests --- .DS_Store | Bin 6148 -> 6148 bytes ...Wave_3_smoke_tests.postman_collection.json | 4 ++-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.DS_Store b/.DS_Store index f3c7967df78c7bd8f70ef9e574abcbe0f5fed333..475fb3349f7cce212760c5bf68d4266dcff1401f 100644 GIT binary patch delta 96 zcmZoMXffEZn~m}0=E3MR&8wY8ia;;M$Wo(Z{? gRn;}Mbu$^jfRPbGGw?%c7&U8h3wzUMc8 Date: Wed, 9 May 2018 12:04:53 -0700 Subject: [PATCH 52/57] available to rent tests on movie --- test/models/movie_test.rb | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/test/models/movie_test.rb b/test/models/movie_test.rb index 98f81b923..2d6777917 100644 --- a/test/models/movie_test.rb +++ b/test/models/movie_test.rb @@ -72,4 +72,39 @@ end end + describe "available_to_rent?" do + before do + @movie = Movie.create( + title: "test movie", + overview: "The unexciting life of a boy", + release_date: "1979-01-18", + inventory: "10", + available_inventory: "10" + ) + + customer = Customer.first + rental_data = { movie_id: @movie.id, customer_id: customer.id} + + rental = Rental.create(rental_data) + end + + it "returns true if the movie has available inventory" do + @movie.available_inventory.must_be :>, 0 + + @movie.available_to_rent?.must_equal true + end + + it "returns false if the movie does not have available inventory" do + @movie.inventory.times do + @movie.reduce_available_inventory + end + + @movie.reload + puts @movie.available_inventory + @movie.available_inventory.must_equal 0 + + @movie.available_to_rent?.must_equal false + end + end + end From fc5e53ae2bd2200d41788c372513889ebd567859 Mon Sep 17 00:00:00 2001 From: Dikla Date: Wed, 9 May 2018 12:11:49 -0700 Subject: [PATCH 53/57] Added test for reduce_available_inventory method --- test/models/movie_test.rb | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/test/models/movie_test.rb b/test/models/movie_test.rb index 98f81b923..ea67766ba 100644 --- a/test/models/movie_test.rb +++ b/test/models/movie_test.rb @@ -58,7 +58,8 @@ title: "test movie", overview: "The unexciting life of a boy", release_date: "1979-01-18", - inventory: "10" + inventory: "10", + available_inventory: "8" ) customer = Customer.first @@ -72,4 +73,25 @@ end end + describe "reduce_available_inventory" do + it "reduce_available_inventory" do + @movie = Movie.create( + title: "test movie", + overview: "The unexciting life of a boy", + release_date: "1979-01-18", + inventory: "10", + available_inventory: "8" + ) + + available_inventory = @movie.available_inventory + puts available_inventory + customer = Customer.first + rental_data = { movie_id: @movie.id, customer_id: customer.id} + rental = Rental.create(rental_data) + @movie.reduce_available_inventory + @movie.available_inventory.must_equal available_inventory - 1 + + end + end + end From e7befc6321a36ca9d4b85a7d9a40d6a452aa7e86 Mon Sep 17 00:00:00 2001 From: Anne Date: Wed, 9 May 2018 12:18:22 -0700 Subject: [PATCH 54/57] adding back available to rent tests on movie --- test/models/movie_test.rb | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/test/models/movie_test.rb b/test/models/movie_test.rb index 6069a4eeb..12b8ed44b 100644 --- a/test/models/movie_test.rb +++ b/test/models/movie_test.rb @@ -73,6 +73,42 @@ end end + describe "available_to_rent?" do + before do + @movie = Movie.create( + title: "test movie", + overview: "The unexciting life of a boy", + release_date: "1979-01-18", + inventory: "10", + available_inventory: "10" + ) + + customer = Customer.first + rental_data = { movie_id: @movie.id, customer_id: customer.id} + + rental = Rental.create(rental_data) + end + + it "returns true if the movie has available inventory" do + @movie.available_inventory.must_be :>, 0 + + @movie.available_to_rent?.must_equal true + end + + it "returns false if the movie does not have available inventory" do + @movie.inventory.times do + @movie.reduce_available_inventory + end + + @movie.reload + puts @movie.available_inventory + @movie.available_inventory.must_equal 0 + + @movie.available_to_rent?.must_equal false + end + end + + describe "reduce_available_inventory" do it "reduce_available_inventory" do @movie = Movie.create( From a44fce730fca25aafde7128b1719f0a2d71471ba Mon Sep 17 00:00:00 2001 From: Dikla Date: Wed, 9 May 2018 12:20:42 -0700 Subject: [PATCH 55/57] Added test for increase_available_inventory --- test/models/movie_test.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/models/movie_test.rb b/test/models/movie_test.rb index 12b8ed44b..bc565ebba 100644 --- a/test/models/movie_test.rb +++ b/test/models/movie_test.rb @@ -129,4 +129,24 @@ end end + describe "increase_available_inventory" do + it "increase_available_inventory" do + @movie = Movie.create( + title: "test movie", + overview: "The unexciting life of a boy", + release_date: "1979-01-18", + inventory: "10", + available_inventory: "8" + ) + + available_inventory = @movie.available_inventory + customer = Customer.first + rental_data = { movie_id: @movie.id, customer_id: customer.id} + rental = Rental.create(rental_data) + @movie.increase_available_inventory + @movie.available_inventory.must_equal available_inventory + 1 + + end + end + end From 36c73fa21086b2b194cf34a70388840218e17e4b Mon Sep 17 00:00:00 2001 From: Dikla Date: Wed, 9 May 2018 14:55:42 -0700 Subject: [PATCH 56/57] change the status from created to ok --- app/controllers/movies_controller.rb | 2 +- app/controllers/rentals_controller.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index f5cf4c023..4b84a1a63 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -21,7 +21,7 @@ def show def create movie = Movie.new(movie_params) if movie.save - render json: { id: movie.id }, status: :created + render json: { id: movie.id }, status: :ok else render json: { errors: movie.errors.messages }, status: :bad_request end diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 763b536eb..d35eb2325 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -8,7 +8,7 @@ def check_out if rental.save rental.movie.reduce_available_inventory #rental.customer.increase_movies_checked_out_count - render json: { id: rental.id, customer_id: rental.customer_id, movie_id: rental.movie_id }, status: :created + render json: { id: rental.id, customer_id: rental.customer_id, movie_id: rental.movie_id }, status: :ok else render json: { errors: rental.errors.messages }, status: :bad_request end @@ -22,7 +22,7 @@ def check_in rental = Rental.find_by(rental_params) if rental.save rental.movie.increase_available_inventory - render json: { id: rental.id, customer_id: rental.customer_id, movie_id: rental.movie_id }, status: :created + render json: { id: rental.id, customer_id: rental.customer_id, movie_id: rental.movie_id }, status: :ok else render json: { errors: rental.errors.messages }, status: :bad_request end From 84c1c7554b400b6c24ef367125885c141953324a Mon Sep 17 00:00:00 2001 From: Dikla Date: Thu, 10 May 2018 14:46:37 -0700 Subject: [PATCH 57/57] Added the movies_checked_out_count method for the customer --- .DS_Store | Bin 6148 -> 6148 bytes app/controllers/customers_controller.rb | 4 +- app/controllers/rentals_controller.rb | 4 +- app/models/customer.rb | 11 +++++ test/controllers/rentals_controller_test.rb | 12 +++++- test/fixtures/customers.yml | 3 +- test/models/customer_test.rb | 45 ++++++++++++++++++++ test/models/movie_test.rb | 1 - 8 files changed, 73 insertions(+), 7 deletions(-) diff --git a/.DS_Store b/.DS_Store index 475fb3349f7cce212760c5bf68d4266dcff1401f..5f0558c7422bbab08c79ddf88accbb853ad83f2a 100644 GIT binary patch delta 21 ccmZoMXffDe&(2|JYO14PY-YLHoqewW06_=^z5oCK delta 21 ccmZoMXffDe&(5K1WUixNY-YaMoqewW06@A0w*UYD diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index 880e474ae..b4badd640 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -14,7 +14,7 @@ def show } }, status: :not_found else - render json: customer.as_json(only: [:name, :registered_at, :address, :city, :state, :postal_code, :phone]), status: :ok + render json: customer.as_json(only: [:name, :registered_at, :address, :city, :state, :postal_code, :phone, :movies_checked_out_count]), status: :ok end end @@ -29,6 +29,6 @@ def create private def customer_params - params.require(:customer).permit(:name, :registered_at, :address, :city, :state, :postal_code, :phone) + params.require(:customer).permit(:name, :registered_at, :address, :city, :state, :postal_code, :phone, :movies_checked_out_count) end end diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index d35eb2325..aa473ab7a 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -7,7 +7,8 @@ def check_out if rental.movie.available_to_rent? if rental.save rental.movie.reduce_available_inventory - #rental.customer.increase_movies_checked_out_count + puts rental.customer.movies_checked_out_count + rental.customer.increase_movies_checked_out_count render json: { id: rental.id, customer_id: rental.customer_id, movie_id: rental.movie_id }, status: :ok else render json: { errors: rental.errors.messages }, status: :bad_request @@ -22,6 +23,7 @@ def check_in rental = Rental.find_by(rental_params) if rental.save rental.movie.increase_available_inventory + rental.customer.decrease_movies_checked_out_count render json: { id: rental.id, customer_id: rental.customer_id, movie_id: rental.movie_id }, status: :ok else render json: { errors: rental.errors.messages }, status: :bad_request diff --git a/app/models/customer.rb b/app/models/customer.rb index 1b917c525..ea316b2c3 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -8,6 +8,17 @@ class Customer < ApplicationRecord after_initialize :set_defaults, unless: :persisted? + def increase_movies_checked_out_count + self.movies_checked_out_count += 1 + self.save + end + + def decrease_movies_checked_out_count + self.movies_checked_out_count -= 1 + self.save + end + + private def set_defaults self.movies_checked_out_count ||= 0 diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index 7894a0b97..92901e282 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -14,10 +14,14 @@ it "creates a new rental" do before_rental_count = Rental.count + customer = customers(:one) + before_movie_check_out = customer.movies_checked_out_count post checkout_url, params: { rental: rental_data } must_respond_with :success Rental.count.must_equal before_rental_count + 1 + customer.reload + customer.movies_checked_out_count.must_equal before_movie_check_out + 1 body = JSON.parse(response.body) body.must_be_kind_of Hash @@ -67,13 +71,17 @@ it "increase_available_inventory on movie in the rental" do movie = movies(:two) before_availability = movie.available_inventory + customer = customers(:one) - available_data = { customer_id: customers(:one).id, movie_id: movies(:two).id } + available_data = { customer_id: customer.id, movie_id: movies(:two).id } post checkout_url, params: { rental: available_data } + customer.reload movie.reload + before_movie_check_in = customer.movies_checked_out_count post checkin_url, params: { rental: available_data } - + customer.reload + customer.movies_checked_out_count.must_equal before_movie_check_in - 1 body = JSON.parse(response.body) movie.reload diff --git a/test/fixtures/customers.yml b/test/fixtures/customers.yml index 2aa69d31e..303bcc9fb 100644 --- a/test/fixtures/customers.yml +++ b/test/fixtures/customers.yml @@ -8,7 +8,7 @@ one: state: MyString postal_code: MyString phone: MyString - + movies_checked_out_count: 0 two: name: MyString registered_at: 2018-05-07 12:20:54 @@ -17,3 +17,4 @@ two: state: MyString postal_code: MyString phone: MyString + movies_checked_out_count: 0 diff --git a/test/models/customer_test.rb b/test/models/customer_test.rb index 4330c4afa..90a73795d 100644 --- a/test/models/customer_test.rb +++ b/test/models/customer_test.rb @@ -58,4 +58,49 @@ @customer.rentals.count.must_equal 1 end end + + describe "decrease_movies_checked_out_count" do + it "decrease_movies_checked_out_count" do + @customer = Customer.create( + name: "Movie Watcher", + address: "2015-04-29T14:54:14.000Z", + city: "Seattle", + state: "WA", + postal_code: "98109", + phone: "555-555-5555", + movies_checked_out_count: 3 + ) + + customer_movies_checked_out_count = @customer.movies_checked_out_count + movie = Movie.first + rental_data = { movie_id: movie.id, customer_id: @customer.id } + rental = Rental.create(rental_data) + @customer.decrease_movies_checked_out_count + @customer.movies_checked_out_count.must_equal customer_movies_checked_out_count - 1 + + end + end + + describe "increase_movies_checked_out_count" do + it "increase_movies_checked_out_count" do + @customer = Customer.create( + name: "Movie Watcher", + address: "2015-04-29T14:54:14.000Z", + city: "Seattle", + state: "WA", + postal_code: "98109", + phone: "555-555-5555", + movies_checked_out_count: 3 + ) + + customer_movies_checked_out_count = @customer.movies_checked_out_count + movie = Movie.first + rental_data = { movie_id: movie.id, customer_id: @customer.id } + rental = Rental.create(rental_data) + @customer.increase_movies_checked_out_count + @customer.movies_checked_out_count.must_equal customer_movies_checked_out_count + 1 + + end + end + end diff --git a/test/models/movie_test.rb b/test/models/movie_test.rb index bc565ebba..0d7f2e2d0 100644 --- a/test/models/movie_test.rb +++ b/test/models/movie_test.rb @@ -101,7 +101,6 @@ end @movie.reload - puts @movie.available_inventory @movie.available_inventory.must_equal 0 @movie.available_to_rent?.must_equal false