From 8d4699ef6032da5fb4696b6c2500e8de3a8a7000 Mon Sep 17 00:00:00 2001 From: Risha Date: Tue, 14 Jun 2016 15:38:02 -0700 Subject: [PATCH 001/118] added route zomg --- routes/customers.js | 0 routes/index.js | 6 +++++- routes/movies.js | 49 +++++++++++++++++++++++++++++++++++++++++++++ routes/rentals.js | 0 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 routes/customers.js create mode 100644 routes/movies.js create mode 100644 routes/rentals.js diff --git a/routes/customers.js b/routes/customers.js new file mode 100644 index 000000000..e69de29bb diff --git a/routes/index.js b/routes/index.js index 06cfc1137..38f8e423e 100644 --- a/routes/index.js +++ b/routes/index.js @@ -3,7 +3,11 @@ var router = express.Router(); /* GET home page. */ router.get('/', function(req, res, next) { - res.status(200).json({whatevs: 'whatevs!!!'}) + res.status(200).json({whatevs: 'whatevs!!!'}); +}); + +router.get('/zomg', function(req, res, next) { + res.status(200).json({it_works: 'it works!'}); }); module.exports = router; diff --git a/routes/movies.js b/routes/movies.js new file mode 100644 index 000000000..ebc873527 --- /dev/null +++ b/routes/movies.js @@ -0,0 +1,49 @@ +// - GET a list of all movies (/movies) +// * display a list to the customers. +// * in JSON +// +// - GET a subset of movies +// * make a dynamic route(s) +// * provide this path: +// 1. release_date: ("/movies/sort/release-date?n=5&p=1") +// 2. title: ("/movies/sort/title?n=5&p=1") +// - Given a sort column, return n movie records, offset by p records (this will be used to create "pages" of movies) +// - Sort columns are +// - title +// - release_date +// +// - Given a movies title... +// (employees are getting the information) +// - GET a list of customers that have currently checked out a copy of the film: +// get request (/movies/Jaws/current) +// connect = link movie to the rental, check status +// if status == active(checked out), +// return customer(s) +// a collection of customers (each): +// - name, +// - phone number, +// - account credit +// +// - GET a list of customers that have checked out a copy in the past route the employee will use: (/movies/Jaws/history/sort/name) +// intermediate table (history) +// keeps all movies rented +// pull out the records that have the movie id, +// then associte the id with the customer id +// then send the collection of customers, +// if +// return customers +// - include each customers +// name, +// phone number, +// account credit +// - ordered by customer name or +// - ordered by check out date +// +// cust_id movie_id +// 2 3 +// 4 3 +// 7 1 +// +// SELECT cust_id +// WHERE movie_id = 3 +// => [2,4] diff --git a/routes/rentals.js b/routes/rentals.js new file mode 100644 index 000000000..e69de29bb From 502a5534df93d081e209f30dd983437b2936d239 Mon Sep 17 00:00:00 2001 From: Risha Date: Tue, 14 Jun 2016 16:33:16 -0700 Subject: [PATCH 002/118] added massive dependency along with the customized script for the database actions. --- package.json | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index d39b26403..62cf52fd5 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,12 @@ "private": true, "scripts": { "start": "nodemon ./bin/www", - "test": "clear; jasmine-node --verbose spec/" + "test": "clear; jasmine-node --verbose spec/", + "db:drop": "dropdb videoStore", + "db:create": "createdb videoStore", + "db:schema": "node tasks/load_schema.js", + "db:seed": "", + "db:reset": "npm run db:drop; npm run db:create; npm run db:schema" }, "dependencies": { "body-parser": "~1.13.2", @@ -12,6 +17,7 @@ "debug": "~2.2.0", "express": "~4.13.1", "jade": "~1.11.0", + "massive": "^2.3.0", "morgan": "~1.6.1", "sequelize": "^3.23.3", "serve-favicon": "~2.3.0" From 99d09db8078de6f5325c0ecd3819ecdc9267c138 Mon Sep 17 00:00:00 2001 From: Risha Date: Tue, 14 Jun 2016 16:34:29 -0700 Subject: [PATCH 003/118] created a schema as a part of the customized scripts. --- db/setup/schema.sql | 24 ++++++++++++++++++++++++ tasks/load_schema.js | 13 +++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 db/setup/schema.sql create mode 100644 tasks/load_schema.js diff --git a/db/setup/schema.sql b/db/setup/schema.sql new file mode 100644 index 000000000..a1220b116 --- /dev/null +++ b/db/setup/schema.sql @@ -0,0 +1,24 @@ +DROP TABLE IF EXISTS movies; +CREATE TABLE movies( + id serial PRIMARY KEY, + title text, + overview text, + release_date text, + inventory integer +); + +CREATE INDEX movies_title ON movies (title); +CREATE INDEX movies_release_date ON movies (release_date); + +DROP TABLE IF EXISTS customers; +CREATE TABLE customers( + id serial PRIMARY KEY, + name text, + registered_at text, + address text, + city text, + state text, + postal_code text, + phone text, + account_credit integer +); diff --git a/tasks/load_schema.js b/tasks/load_schema.js new file mode 100644 index 000000000..fbb9352a8 --- /dev/null +++ b/tasks/load_schema.js @@ -0,0 +1,13 @@ +var massive = require('massive'); +var connectionString = "postgres://localhost/videoStore"; + +var db = massive.connectSync({connectionString : connectionString}); + +db.setup.schema([], function(err, result) { + if (err) { + throw(new Error(err.message)); + } + + console.log("yay schema!"); + process.exit(); +}); From c9fc56f9c841530576ce52e9c571798611457729 Mon Sep 17 00:00:00 2001 From: Risha Date: Tue, 14 Jun 2016 16:37:08 -0700 Subject: [PATCH 004/118] added '*.log' to gitignore. --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 646ac519e..879df49d1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .DS_Store node_modules/ +*.log From fe633893474fdb0fa92b495a4aa90f03168d77bd Mon Sep 17 00:00:00 2001 From: Risha Date: Wed, 15 Jun 2016 09:55:29 -0700 Subject: [PATCH 005/118] created two tables, history and rentals. --- db/setup/schema.sql | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/db/setup/schema.sql b/db/setup/schema.sql index a1220b116..a978c7201 100644 --- a/db/setup/schema.sql +++ b/db/setup/schema.sql @@ -22,3 +22,25 @@ CREATE TABLE customers( phone text, account_credit integer ); + + +DROP TABLE IF EXISTS rentals; +CREATE TABLE rentals( + id serial PRIMARY KEY, + movie_id integer, + customer_id integer, + status text +); + +CREATE INDEX rentals_customer_id ON rentals (customer_id); + +DROP TABLE IF EXISTS history; +CREATE TABLE history( + id serial PRIMARY KEY, + rental_id integer, + customer_id integer, + checkout_date text, + return_date text +); + +CREATE INDEX history_customer_id ON history (customer_id); From 2a146018bcbc7fa343b822fb31bffbfc06570928 Mon Sep 17 00:00:00 2001 From: Risha Date: Wed, 15 Jun 2016 15:22:00 -0700 Subject: [PATCH 006/118] adjusted value type in customer table from integer to deciaml for accoutnt credit. --- db/setup/schema.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/setup/schema.sql b/db/setup/schema.sql index a978c7201..6988b9534 100644 --- a/db/setup/schema.sql +++ b/db/setup/schema.sql @@ -20,7 +20,7 @@ CREATE TABLE customers( state text, postal_code text, phone text, - account_credit integer + account_credit decimal ); From 1c488256198de2215966d6838bff8420bbb5efdf Mon Sep 17 00:00:00 2001 From: Risha Date: Wed, 15 Jun 2016 15:23:09 -0700 Subject: [PATCH 007/118] we updated the script seed task and added it to the reset script. --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 62cf52fd5..8b1f9d715 100644 --- a/package.json +++ b/package.json @@ -8,8 +8,8 @@ "db:drop": "dropdb videoStore", "db:create": "createdb videoStore", "db:schema": "node tasks/load_schema.js", - "db:seed": "", - "db:reset": "npm run db:drop; npm run db:create; npm run db:schema" + "db:seed": "node tasks/seed.js", + "db:reset": "npm run db:drop; npm run db:create; npm run db:schema; npm run db:seed" }, "dependencies": { "body-parser": "~1.13.2", From 6d442570c0eb7e85602be0da2d752217a38384fa Mon Sep 17 00:00:00 2001 From: Risha Date: Wed, 15 Jun 2016 15:24:16 -0700 Subject: [PATCH 008/118] created an algorithm for seeding the data tables. --- tasks/seed.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 tasks/seed.js diff --git a/tasks/seed.js b/tasks/seed.js new file mode 100644 index 000000000..caf4f9424 --- /dev/null +++ b/tasks/seed.js @@ -0,0 +1,15 @@ +var massive = require('massive'); +var connectionString = "postgres://localhost/videoStore"; +var db = massive.connectSync({connectionString : connectionString}); +var data_movies = require('../db/seeds/movies.json'); +var data_customers = require('../db/seeds/customers.json'); + + +for (var record of data_movies) { + db.movies.saveSync(record) +} + +for (var record of data_customers) { + db.customers.saveSync(record) +} +process.exit( ); From 0133392633d0f8b9dcf9e5776c01d941f3a8459b Mon Sep 17 00:00:00 2001 From: melissajimison Date: Wed, 15 Jun 2016 15:46:34 -0700 Subject: [PATCH 009/118] seeded the Rentals table --- tasks/seed.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/tasks/seed.js b/tasks/seed.js index caf4f9424..8fef0354b 100644 --- a/tasks/seed.js +++ b/tasks/seed.js @@ -4,12 +4,16 @@ var db = massive.connectSync({connectionString : connectionString}); var data_movies = require('../db/seeds/movies.json'); var data_customers = require('../db/seeds/customers.json'); - for (var record of data_movies) { db.movies.saveSync(record) -} + + for (i = 0; i < record.inventory; i++) { + db.rentals.saveSync({movie_id: record.id }) + }; +}; for (var record of data_customers) { db.customers.saveSync(record) -} +}; + process.exit( ); From c5cb407a6d29cadf00bca2d27629e60483f6b40e Mon Sep 17 00:00:00 2001 From: melissajimison Date: Wed, 15 Jun 2016 15:47:07 -0700 Subject: [PATCH 010/118] Added 'status' as INDEX in the rentals schema --- db/setup/schema.sql | 1 + 1 file changed, 1 insertion(+) diff --git a/db/setup/schema.sql b/db/setup/schema.sql index 6988b9534..5b0c54e78 100644 --- a/db/setup/schema.sql +++ b/db/setup/schema.sql @@ -33,6 +33,7 @@ CREATE TABLE rentals( ); CREATE INDEX rentals_customer_id ON rentals (customer_id); +CREATE INDEX rentals_status ON rentals (status); DROP TABLE IF EXISTS history; CREATE TABLE history( From fc9706b11d77ed5647c05ae09ce10ac2e17a7cd8 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Wed, 15 Jun 2016 17:30:14 -0700 Subject: [PATCH 011/118] fixed a bug that was not capturing the id of the movie to populate into the rentals table when seeding. --- tasks/seed.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/tasks/seed.js b/tasks/seed.js index 8fef0354b..e47a9f1c2 100644 --- a/tasks/seed.js +++ b/tasks/seed.js @@ -5,15 +5,18 @@ var data_movies = require('../db/seeds/movies.json'); var data_customers = require('../db/seeds/customers.json'); for (var record of data_movies) { - db.movies.saveSync(record) - - for (i = 0; i < record.inventory; i++) { - db.rentals.saveSync({movie_id: record.id }) - }; + db.movies.saveSync(record); + db.movies.find({title: record.title}, function(err, res){ + for (i = 0; i < record.inventory; i++) { + db.rentals.saveSync({movie_id: res[0].id }) + // console.log(record.title + i); + console.log(res[0].id); + }; + }); }; for (var record of data_customers) { db.customers.saveSync(record) }; -process.exit( ); +process.exit( ); \ No newline at end of file From 065202ac958e611a71aec931ab7e2b0eedd30c7a Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Wed, 15 Jun 2016 17:33:32 -0700 Subject: [PATCH 012/118] removed unnecessary console.logs used to test seeding --- tasks/seed.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/tasks/seed.js b/tasks/seed.js index e47a9f1c2..50d3199d7 100644 --- a/tasks/seed.js +++ b/tasks/seed.js @@ -9,8 +9,6 @@ for (var record of data_movies) { db.movies.find({title: record.title}, function(err, res){ for (i = 0; i < record.inventory; i++) { db.rentals.saveSync({movie_id: res[0].id }) - // console.log(record.title + i); - console.log(res[0].id); }; }); }; From 8b2857233e3141d5ed8bf4dcca754f154ba76ab9 Mon Sep 17 00:00:00 2001 From: Risha Date: Thu, 16 Jun 2016 13:59:18 -0700 Subject: [PATCH 013/118] made notes to identify the steps taken in the request process. --- tasks/seed.js | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/tasks/seed.js b/tasks/seed.js index 50d3199d7..c81a524ef 100644 --- a/tasks/seed.js +++ b/tasks/seed.js @@ -5,16 +5,38 @@ var data_movies = require('../db/seeds/movies.json'); var data_customers = require('../db/seeds/customers.json'); for (var record of data_movies) { + + // this is a single record: + // "title": "Psycho", + // "overview": "When larcenous real estate clerk Marion Crane goes on the lam with a wad of cash and hopes of starting a new life, she ends up at the notorious Bates Motel, where manager Norman Bates cares for his housebound mother. The place seems quirky, but fine… until Marion decides to take a shower.", + // "release_date": "1960-06-16", + // "inventory": 8 + // }, + + // saving the instance of movie to the database + // once saved, its given an id db.movies.saveSync(record); - db.movies.find({title: record.title}, function(err, res){ + db.movies.find({title: record.title}, function(err, res) { for (i = 0; i < record.inventory; i++) { - db.rentals.saveSync({movie_id: res[0].id }) - }; + db.rentals.saveSync({movie_id: res[0].id }); + } }); -}; +} for (var record of data_customers) { - db.customers.saveSync(record) -}; -process.exit( ); \ No newline at end of file + // this is a record: + // { + // "name": "Shelley Rocha", + // "registered_at": "Wed, 29 Apr 2015 07:54:14 -0700", + // "address": "Ap #292-5216 Ipsum Rd.", + // "city": "Hillsboro", + // "state": "OR", + // "postal_code": "24309", + // "phone": "(322) 510-8695", + // "account_credit": 13.15 + // }, + db.customers.saveSync(record); +} + +process.exit( ); From 4693e9ef1e42f335034139805f686a97c32a268f Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Thu, 16 Jun 2016 14:07:50 -0700 Subject: [PATCH 014/118] created controllers and models for movies, customers, and rentals. defined routes. --- app.js | 9 +++++++++ controllers/customers_controller.js | 0 controllers/movies_controller.js | 0 controllers/rentals_controller.js | 0 models/customers.js | 0 models/movies.js | 0 models/rentals.js | 0 7 files changed, 9 insertions(+) create mode 100644 controllers/customers_controller.js create mode 100644 controllers/movies_controller.js create mode 100644 controllers/rentals_controller.js create mode 100644 models/customers.js create mode 100644 models/movies.js create mode 100644 models/rentals.js diff --git a/app.js b/app.js index f0579b1dc..047b6df35 100644 --- a/app.js +++ b/app.js @@ -23,6 +23,15 @@ app.use(express.static(path.join(__dirname, 'public'))); app.use('/', routes); +var moviesRoutes = require('./routes/movies'); +app.use('/movies', moviesRoutes); + +var customersRoutes = require('./routes/customers'); +app.use('/customers', customersRoutes); + +var rentalsRoutes = require('./routes/rentals'); +app.use('/rentals', rentalsRoutes); + // catch 404 and forward to error handler app.use(function(req, res, next) { var err = new Error('Not Found'); diff --git a/controllers/customers_controller.js b/controllers/customers_controller.js new file mode 100644 index 000000000..e69de29bb diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js new file mode 100644 index 000000000..e69de29bb diff --git a/controllers/rentals_controller.js b/controllers/rentals_controller.js new file mode 100644 index 000000000..e69de29bb diff --git a/models/customers.js b/models/customers.js new file mode 100644 index 000000000..e69de29bb diff --git a/models/movies.js b/models/movies.js new file mode 100644 index 000000000..e69de29bb diff --git a/models/rentals.js b/models/rentals.js new file mode 100644 index 000000000..e69de29bb From 0b18c5cd8d9ccce94959bc584acf6d0d7c0ebb07 Mon Sep 17 00:00:00 2001 From: Risha Date: Thu, 16 Jun 2016 15:27:54 -0700 Subject: [PATCH 015/118] added massive, connection, and db setup to this file. Changed the location of the route path. --- app.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/app.js b/app.js index 047b6df35..7670ce93d 100644 --- a/app.js +++ b/app.js @@ -4,10 +4,14 @@ var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); +var massive = require('massive'); -var routes = require('./routes/index'); +var app = module.exports = express(); -var app = express(); +// database setup +var connectionString = "postgres://localhost/videoStore"; +var db = massive.connectSync({connectionString: connectionString}); +app.set('db', db); // view engine setup app.set('views', path.join(__dirname, 'views')); @@ -21,16 +25,17 @@ app.use(bodyParser.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); +var routes = require('./routes/index'); app.use('/', routes); -var moviesRoutes = require('./routes/movies'); -app.use('/movies', moviesRoutes); +// var moviesRoutes = require('./routes/movies'); +// app.use('/movies', moviesRoutes); var customersRoutes = require('./routes/customers'); app.use('/customers', customersRoutes); -var rentalsRoutes = require('./routes/rentals'); -app.use('/rentals', rentalsRoutes); +// var rentalsRoutes = require('./routes/rentals'); +// app.use('/rentals', rentalsRoutes); // catch 404 and forward to error handler app.use(function(req, res, next) { From 38247902b3560b43115c954a7a57845821ef620f Mon Sep 17 00:00:00 2001 From: Risha Date: Thu, 16 Jun 2016 15:29:00 -0700 Subject: [PATCH 016/118] created the customers.all function and flow. --- controllers/customers_controller.js | 19 ++++++++++++++++++ models/customers.js | 31 +++++++++++++++++++++++++++++ routes/customers.js | 11 ++++++++++ 3 files changed, 61 insertions(+) diff --git a/controllers/customers_controller.js b/controllers/customers_controller.js index e69de29bb..52130e05d 100644 --- a/controllers/customers_controller.js +++ b/controllers/customers_controller.js @@ -0,0 +1,19 @@ +var Customers = require("../models/customers"); + +var CustomersController = { + index: function(req, res, next) { + Customers.all(function(error, customers) { // this is a callback + if(error) { + var err = new Error("Error retrieving customers list:\n" + error.message); + err.status = 500; + next(err); + } else { + res.json(customers); + // var locals = { accounts : accounts}; + // res.render("accounts/index",locals); + } + }); + } +}; + +module.exports = CustomersController; diff --git a/models/customers.js b/models/customers.js index e69de29bb..3b976550f 100644 --- a/models/customers.js +++ b/models/customers.js @@ -0,0 +1,31 @@ +var app = require("../app"); +var db = app.get("db"); + +// Constructor function +var Customers = function(customer) { + this.id = customer.id; + this.name = customer.name; + this.registered_at = customer.registered_at; + this.address = customer.address; + this.city = customer.city; + this.state = customer.state; + this.postal_code = customer.postal_code; + this.phone = customer.phone; + this.account_credit = customer.account_credit; +}; + +Customers.all = function(callback) { + db.customers.find(function(error, customers) { + if(error || !customers) { + callback(error || new Error("Could not retrieve customers"), undefined); + } else { + callback(null, customers.map(function(customer) { + return new Customers(customer); + })); + } + // callback(null, "melissa"); + }); +}; + + +module.exports = Customers; diff --git a/routes/customers.js b/routes/customers.js index e69de29bb..740bddf82 100644 --- a/routes/customers.js +++ b/routes/customers.js @@ -0,0 +1,11 @@ +var express = require('express'); +var router = express.Router(); +var CustomersController = require('../controllers/customers_controller.js'); + +/* GET customers listing. */ +router.get('/', CustomersController.index); + +/* GET customers details. */ +// router.get('/:id', CustomersController.show); + +module.exports = router; From b5bc62a927fdea43c22bee3fe76f26118d0fc3bf Mon Sep 17 00:00:00 2001 From: Risha Date: Thu, 16 Jun 2016 15:30:19 -0700 Subject: [PATCH 017/118] set basic functionality of the routes and models for movies and rentals. --- controllers/movies_controller.js | 1 + models/movies.js | 22 ++++++++++++++++++++++ models/rentals.js | 22 ++++++++++++++++++++++ routes/movies.js | 14 ++++++++++++++ routes/rentals.js | 11 +++++++++++ 5 files changed, 70 insertions(+) diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index e69de29bb..8b1378917 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -0,0 +1 @@ + diff --git a/models/movies.js b/models/movies.js index e69de29bb..52bedecff 100644 --- a/models/movies.js +++ b/models/movies.js @@ -0,0 +1,22 @@ +var app = require("../app"); +var db = app.get("db"); + +// Constructor function +var Movies= function(id) { + this.id = id; +}; + +// Customers.all = function(callback) { +// db.customers.find(function(error, customers) { +// if(error || !customers) { +// callback(error || new Error("Could not retrieve customers"), undefined); +// } else { +// callback(null, customers.map(function(customer) { +// return new Customers(customer.id); +// })); +// } +// }); +// }; + + +module.exports = Movies; diff --git a/models/rentals.js b/models/rentals.js index e69de29bb..5b6947396 100644 --- a/models/rentals.js +++ b/models/rentals.js @@ -0,0 +1,22 @@ +var app = require("../app"); +var db = app.get("db"); + +// Constructor function +var Rentals= function(id) { + this.id = id; +}; + +// Customers.all = function(callback) { +// db.customers.find(function(error, customers) { +// if(error || !customers) { +// callback(error || new Error("Could not retrieve customers"), undefined); +// } else { +// callback(null, customers.map(function(customer) { +// return new Customers(customer.id); +// })); +// } +// }); +// }; + + +module.exports = Rentals; diff --git a/routes/movies.js b/routes/movies.js index ebc873527..1b0fcd434 100644 --- a/routes/movies.js +++ b/routes/movies.js @@ -1,3 +1,17 @@ +var express = require('express'); +var router = express.Router(); +var MoviesController = require('../controllers/movies_controller.js'); + +/* GET customers listing. */ +router.get('/', MoviesController.index); + +/* GET customers details. */ +// router.get('/:id', MoviesController.show); + +module.exports = router; + + + // - GET a list of all movies (/movies) // * display a list to the customers. // * in JSON diff --git a/routes/rentals.js b/routes/rentals.js index e69de29bb..cb652e28d 100644 --- a/routes/rentals.js +++ b/routes/rentals.js @@ -0,0 +1,11 @@ +var express = require('express'); +var router = express.Router(); +var RentalsController = require('../controllers/rentals_controller.js'); + +/* GET customers listing. */ +router.get('/', RentalsController.index); + +/* GET customers details. */ +// router.get('/:id', RentalsController.show); + +module.exports = router; From 95a5171f576a59a60e33ccaf8986769f3069b59f Mon Sep 17 00:00:00 2001 From: Risha Date: Thu, 16 Jun 2016 15:32:16 -0700 Subject: [PATCH 018/118] added semicolons --- spec/controllers/index.spec.js | 36 +++++++++++++++++----------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/spec/controllers/index.spec.js b/spec/controllers/index.spec.js index e4151d267..ee9e394a2 100644 --- a/spec/controllers/index.spec.js +++ b/spec/controllers/index.spec.js @@ -1,29 +1,29 @@ -var request = require('request') -var base_url = "http://localhost:3000/" +var request = require('request'); +var base_url = "http://localhost:3000/"; describe("Endpoint at /", function () { it('responds with a 200 status code', function (done) { request.get(base_url, function(error, response, body) { - expect(response.statusCode).toEqual(200) - done() - }) - }) + expect(response.statusCode).toEqual(200); + done(); + }); + }); describe("the returned json data", function() { it('has the right keys', function(done) { request.get(base_url, function(error, response, body) { - var data = JSON.parse(body) - expect(Object.keys(data)).toEqual(['whatevs']) - done() - }) - }) + var data = JSON.parse(body); + expect(Object.keys(data)).toEqual(['whatevs']); + done(); + }); + }); it('has the right values for the keys', function(done) { request.get(base_url, function(error, response, body) { - var data = JSON.parse(body) - expect(data.whatevs).toEqual('whatevs!!!') - done() - }) - }) - }) -}) + var data = JSON.parse(body); + expect(data.whatevs).toEqual('whatevs!!!'); + done(); + }); + }); + }); +}); From 8162a9446ca711d7573ae07ead5a5e2cf26f551d Mon Sep 17 00:00:00 2001 From: Risha Date: Thu, 16 Jun 2016 15:34:10 -0700 Subject: [PATCH 019/118] requiring app and setting app for the database. --- tasks/load_schema.js | 6 ++---- tasks/seed.js | 5 ++--- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/tasks/load_schema.js b/tasks/load_schema.js index fbb9352a8..8becaa659 100644 --- a/tasks/load_schema.js +++ b/tasks/load_schema.js @@ -1,7 +1,5 @@ -var massive = require('massive'); -var connectionString = "postgres://localhost/videoStore"; - -var db = massive.connectSync({connectionString : connectionString}); +var app = require("../app"); +var db = app.get("db"); db.setup.schema([], function(err, result) { if (err) { diff --git a/tasks/seed.js b/tasks/seed.js index c81a524ef..84c15c401 100644 --- a/tasks/seed.js +++ b/tasks/seed.js @@ -1,6 +1,5 @@ -var massive = require('massive'); -var connectionString = "postgres://localhost/videoStore"; -var db = massive.connectSync({connectionString : connectionString}); +var app = require("../app"); +var db = app.get("db"); var data_movies = require('../db/seeds/movies.json'); var data_customers = require('../db/seeds/customers.json'); From 7319eb2f4dc6d38504eeae61de4be29b42deb780 Mon Sep 17 00:00:00 2001 From: Risha Date: Thu, 16 Jun 2016 15:34:52 -0700 Subject: [PATCH 020/118] created test --- spec/controllers/customers.spec.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 spec/controllers/customers.spec.js diff --git a/spec/controllers/customers.spec.js b/spec/controllers/customers.spec.js new file mode 100644 index 000000000..c00e9d175 --- /dev/null +++ b/spec/controllers/customers.spec.js @@ -0,0 +1,15 @@ +var request = require('request'); +var base_url = "http://localhost:3000"; + +describe("Endpoint at /", function () { + var url = function(endpoint) { + return base_url + "customers" + endpoint; + }; + + it('responds with a 200 status code', function (done) { + request.get(url('/'), function(error, response, body) { + expect(response.statusCode).toEqual(200); + done(); + }); + }); +}); From ed2ab857798fa48fd09d72c26815e8593d4fed17 Mon Sep 17 00:00:00 2001 From: Risha Date: Thu, 16 Jun 2016 15:51:26 -0700 Subject: [PATCH 021/118] added the movies flow for .all request and started on rentals show. --- app.js | 4 ++-- controllers/movies_controller.js | 18 ++++++++++++++++++ controllers/rentals_controller.js | 22 ++++++++++++++++++++++ models/movies.js | 30 +++++++++++++++++------------- routes/rentals.js | 2 +- spec/controllers/movies.spec.js | 16 +++++++++++++--- 6 files changed, 73 insertions(+), 19 deletions(-) diff --git a/app.js b/app.js index 7670ce93d..d111495fa 100644 --- a/app.js +++ b/app.js @@ -28,8 +28,8 @@ app.use(express.static(path.join(__dirname, 'public'))); var routes = require('./routes/index'); app.use('/', routes); -// var moviesRoutes = require('./routes/movies'); -// app.use('/movies', moviesRoutes); +var moviesRoutes = require('./routes/movies'); +app.use('/movies', moviesRoutes); var customersRoutes = require('./routes/customers'); app.use('/customers', customersRoutes); diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index 8b1378917..79d2dc786 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -1 +1,19 @@ +var Movies = require("../models/movies"); +var MoviesController = { + index: function(req, res, next) { + Movies.all(function(error, movies) { // this is a callback + if(error) { + var err = new Error("Error retrieving movies list:\n" + error.message); + err.status = 500; + next(err); + } else { + res.json(movies); + // var locals = { accounts : accounts}; + // res.render("accounts/index",locals); + } + }); + } +}; + +module.exports = MoviesController; diff --git a/controllers/rentals_controller.js b/controllers/rentals_controller.js index e69de29bb..b3a8c4bab 100644 --- a/controllers/rentals_controller.js +++ b/controllers/rentals_controller.js @@ -0,0 +1,22 @@ +var Rentals = require("../models/movies"); + +show: function(req, res, next) { + Rental.find(req.params.id, function(error, account) { + if(error) { + var err = new Error("No such account"); + err.status = 404; + next(err); + } else { + account.getBalance(function(error, balance) { + res.render("accounts/show", { + account: { + id: account.id, + balance: balance + } + }); + }); + } + }); +} +}; +module.exports = RentalsController; diff --git a/models/movies.js b/models/movies.js index 52bedecff..cf18872bb 100644 --- a/models/movies.js +++ b/models/movies.js @@ -2,21 +2,25 @@ var app = require("../app"); var db = app.get("db"); // Constructor function -var Movies= function(id) { - this.id = id; +var Movies = function(movie) { + this.id = movie.id; + this.title = movie.title; + this.overview = movie.overview; + this.release_date = movie.release_date; + this.inventory = movie.inventory; }; -// Customers.all = function(callback) { -// db.customers.find(function(error, customers) { -// if(error || !customers) { -// callback(error || new Error("Could not retrieve customers"), undefined); -// } else { -// callback(null, customers.map(function(customer) { -// return new Customers(customer.id); -// })); -// } -// }); -// }; +Movies.all = function(callback) { + db.movies.find(function(error, movies) { + if(error || !movies) { + callback(error || new Error("Could not retrieve movies"), undefined); + } else { + callback(null, movies.map(function(movie) { + return new Movies(movie); + })); + } + }); +}; module.exports = Movies; diff --git a/routes/rentals.js b/routes/rentals.js index cb652e28d..0e4db02c5 100644 --- a/routes/rentals.js +++ b/routes/rentals.js @@ -3,7 +3,7 @@ var router = express.Router(); var RentalsController = require('../controllers/rentals_controller.js'); /* GET customers listing. */ -router.get('/', RentalsController.index); +router.get('/:title', RentalsController.show); /* GET customers details. */ // router.get('/:id', RentalsController.show); diff --git a/spec/controllers/movies.spec.js b/spec/controllers/movies.spec.js index ddcaf2f68..ca123e751 100644 --- a/spec/controllers/movies.spec.js +++ b/spec/controllers/movies.spec.js @@ -1,5 +1,15 @@ var request = require('request'); +var base_url = "http://localhost:3000"; -describe("Endpoints under /movies", function() { - -}) +describe("Endpoint at /", function () { + var url = function(endpoint) { + return base_url + "movies" + endpoint; + }; + + it('responds with a 200 status code', function (done) { + request.get(url('/'), function(error, response, body) { + expect(response.statusCode).toEqual(200); + done(); + }); + }); +}); From def45beb7a08fb250c711982e48097689765defe Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Fri, 17 Jun 2016 10:03:50 -0700 Subject: [PATCH 022/118] added sort route and functionality to movies --- controllers/movies_controller.js | 24 ++++++++++++++++++++++++ models/movies.js | 11 +++++++++++ routes/movies.js | 6 +++--- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index 79d2dc786..109b7bbbd 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -13,6 +13,30 @@ var MoviesController = { // res.render("accounts/index",locals); } }); + }, + + sort: function(req, res, next) { + // // GET /search?q=tobi+ferret + // req.query.q + // // => "tobi ferret" + + //Send in an ORDER clause and a LIMIT with OFFSET + var options = { + limit : req.query.n, + order : req.params.column, + offset: req.query.p + } + + // products ordered in descending fashion + Movies.sort_by_day(options, function(error, movies) { // this is a callback + if(error) { + var err = new Error("No such movie"); + err.status = 404; + next(err); + } else { + res.json(movies) + } + }); } }; diff --git a/models/movies.js b/models/movies.js index cf18872bb..4fa8649ff 100644 --- a/models/movies.js +++ b/models/movies.js @@ -22,5 +22,16 @@ Movies.all = function(callback) { }); }; +Movies.sort_by_day = function(options, callback) { + db.movies.find({}, options, function(error, movies){ + if(error || !movies) { + callback(error || new Error("Could not retrieve movies"), undefined); + } else { + callback(null, movies.map(function(movie) { + return new Movies(movie); + })); + } + }); +}; module.exports = Movies; diff --git a/routes/movies.js b/routes/movies.js index 1b0fcd434..f054870f6 100644 --- a/routes/movies.js +++ b/routes/movies.js @@ -2,11 +2,11 @@ var express = require('express'); var router = express.Router(); var MoviesController = require('../controllers/movies_controller.js'); -/* GET customers listing. */ +/* GET movies listing. */ router.get('/', MoviesController.index); -/* GET customers details. */ -// router.get('/:id', MoviesController.show); +/* GET movies sorted by column details. */ +router.get('/sort/:column', MoviesController.sort); module.exports = router; From 1eeb9075b4aa207a720928fe3267d947ee4acdcf Mon Sep 17 00:00:00 2001 From: melissajimison Date: Fri, 17 Jun 2016 10:22:09 -0700 Subject: [PATCH 023/118] created tests for sort by column --- spec/controllers/movies.spec.js | 29 +++++++++++++++++++++++- tasks/seed.js | 39 ++++++++------------------------- 2 files changed, 37 insertions(+), 31 deletions(-) diff --git a/spec/controllers/movies.spec.js b/spec/controllers/movies.spec.js index ca123e751..67fcd26ae 100644 --- a/spec/controllers/movies.spec.js +++ b/spec/controllers/movies.spec.js @@ -1,5 +1,5 @@ var request = require('request'); -var base_url = "http://localhost:3000"; +var base_url = "http://localhost:3000/"; describe("Endpoint at /", function () { var url = function(endpoint) { @@ -13,3 +13,30 @@ describe("Endpoint at /", function () { }); }); }); + +describe("Endpoint at /sort", function () { + var url = function(endpoint) { + return base_url + "movies/sort" + endpoint; + }; + + it('responds with a 200 status code', function (done) { + request.get(url('/title?n=10&p=1'), function(error, response, body) { + expect(response.statusCode).toEqual(200); + done(); + }); + }); + + it('responds with a 200 status code released_date', function (done) { + request.get(url('/release_date?n=6&p=1'), function(error, response, body) { + expect(response.statusCode).toEqual(200); + done(); + }); + }); + + it('responds with a 200 status code for mispelling', function (done) { + request.get(url('/released-date?n=10&p=1'), function(error, response, body) { + expect(response.statusCode).toEqual(200); + done(); + }); + }); +}); diff --git a/tasks/seed.js b/tasks/seed.js index 84c15c401..32640771f 100644 --- a/tasks/seed.js +++ b/tasks/seed.js @@ -1,41 +1,20 @@ -var app = require("../app"); -var db = app.get("db"); +var massive = require('massive'); +var connectionString = "postgres://localhost/videoStore"; +var db = massive.connectSync({connectionString : connectionString}); var data_movies = require('../db/seeds/movies.json'); var data_customers = require('../db/seeds/customers.json'); for (var record of data_movies) { - - // this is a single record: - // "title": "Psycho", - // "overview": "When larcenous real estate clerk Marion Crane goes on the lam with a wad of cash and hopes of starting a new life, she ends up at the notorious Bates Motel, where manager Norman Bates cares for his housebound mother. The place seems quirky, but fine… until Marion decides to take a shower.", - // "release_date": "1960-06-16", - // "inventory": 8 - // }, - - // saving the instance of movie to the database - // once saved, its given an id db.movies.saveSync(record); - db.movies.find({title: record.title}, function(err, res) { + db.movies.find({title: record.title}, function(err, res){ for (i = 0; i < record.inventory; i++) { - db.rentals.saveSync({movie_id: res[0].id }); - } + db.rentals.saveSync({movie_id: res[0].id }) + }; }); -} +}; for (var record of data_customers) { - - // this is a record: - // { - // "name": "Shelley Rocha", - // "registered_at": "Wed, 29 Apr 2015 07:54:14 -0700", - // "address": "Ap #292-5216 Ipsum Rd.", - // "city": "Hillsboro", - // "state": "OR", - // "postal_code": "24309", - // "phone": "(322) 510-8695", - // "account_credit": 13.15 - // }, - db.customers.saveSync(record); -} + db.customers.saveSync(record) +}; process.exit( ); From 4d837aca7b23a0498cf3de58de1019f4940eac18 Mon Sep 17 00:00:00 2001 From: Risha Date: Fri, 17 Jun 2016 10:50:09 -0700 Subject: [PATCH 024/118] added the flow to send requests and retrieve json response. --- controllers/customers_controller.js | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/controllers/customers_controller.js b/controllers/customers_controller.js index 52130e05d..462c88e76 100644 --- a/controllers/customers_controller.js +++ b/controllers/customers_controller.js @@ -13,7 +13,30 @@ var CustomersController = { // res.render("accounts/index",locals); } }); - } -}; + }, + +sort: function(req, res, next) { + // // GET /search?q=tobi+ferret + // req.query.q + // // => "tobi ferret" + //Send in an ORDER clause and a LIMIT with OFFSET + var options = { + limit : req.query.n, + order : req.params.column, + offset: req.query.p + }; + + // products ordered in descending fashion + Customers.sort_by(options, function(error, customers) { // this is a callback + if(error) { + var err = new Error("No such customer"); + err.status = 404; + next(err); + } else { + res.json(customers); + } + }); +} +}; module.exports = CustomersController; From 23280a72a443d1fbc629838f52b970355c32d863 Mon Sep 17 00:00:00 2001 From: Risha Date: Fri, 17 Jun 2016 10:51:05 -0700 Subject: [PATCH 025/118] apart of the flow request and response to json. --- models/customers.js | 15 ++++++++++++++- routes/customers.js | 5 ++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/models/customers.js b/models/customers.js index 3b976550f..3acc76e3b 100644 --- a/models/customers.js +++ b/models/customers.js @@ -23,9 +23,22 @@ Customers.all = function(callback) { return new Customers(customer); })); } - // callback(null, "melissa"); }); }; +Customers.sort_by = function(options, callback) { + db.customers.find({}, options, function(error, customers){ + if(error || !customers) { + callback(error || new Error("Could not retrieve customers"), undefined); + } else { + callback(null, customers.map(function(customer) { + return new Customers(customer); + })); + } + }); +}; + + + module.exports = Customers; diff --git a/routes/customers.js b/routes/customers.js index 740bddf82..b03185b4d 100644 --- a/routes/customers.js +++ b/routes/customers.js @@ -5,7 +5,10 @@ var CustomersController = require('../controllers/customers_controller.js'); /* GET customers listing. */ router.get('/', CustomersController.index); -/* GET customers details. */ +// /* GET customers details. */ // router.get('/:id', CustomersController.show); +/* GET customers sorted by column details. */ +router.get('/sort/:column', CustomersController.sort); + module.exports = router; From 119ceb526ef5af7cee849471afd18e956824744f Mon Sep 17 00:00:00 2001 From: melissajimison Date: Fri, 17 Jun 2016 11:13:23 -0700 Subject: [PATCH 026/118] added rentals seeds --- db/seeds/rentals.json | 157 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 db/seeds/rentals.json diff --git a/db/seeds/rentals.json b/db/seeds/rentals.json new file mode 100644 index 000000000..32f927d68 --- /dev/null +++ b/db/seeds/rentals.json @@ -0,0 +1,157 @@ + +{ +"movie_id":116, +"customer_id": 8, +"status": "rented", +}, +{ +"movie_id":119, +"customer_id": 6, +"status": "rented", +}, +{ +"movie_id":126, +"customer_id": 7, +"status": "rented", +}, +{ +"movie_id":117, +"customer_id": 10, +"status": "rented", +}, +{ +"movie_id":114, +"customer_id": 3, +"status": "rented", +}, +{ +"movie_id":125, +"customer_id": 4, +"status": "available", +}, +{ +"movie_id":128, +"customer_id": 1, +"status": "available", +}, +{ +"movie_id":107, +"customer_id": 8, +"status": "available", +}, +{ +"movie_id":112, +"customer_id": 2, +"status": "available", +}, +{ +"movie_id":112, +"customer_id": 9, +"status": "available", +}, +{ +"movie_id":115, +"customer_id": 5, +"status": "available", +}, +{ +"movie_id":102, +"customer_id": 10, +"status": "available", +}, +{ +"movie_id":104, +"customer_id": 4, +"status": "available", +}, +{ +"movie_id":101, +"customer_id": 10, +"status": "rented", +}, +{ +"movie_id":130, +"customer_id": 2, +"status": "rented", +}, +{ +"movie_id":120, +"customer_id": 6, +"status": "rented", +}, +{ +"movie_id":124, +"customer_id": 1, +"status": "rented", +}, +{ +"movie_id":128, +"customer_id": 10, +"status": "rented", +}, +{ +"movie_id":104, +"customer_id": 4, +"status": "rented", +}, +{ +"movie_id":124, +"customer_id": 4, +"status": "rented", +}, +{ +"movie_id":118, +"customer_id": 4, +"status": "rented", +}, +{ +"movie_id":108, +"customer_id": 4, +"status": "rented", +}, +{ +"movie_id":110, +"customer_id": 6, +"status": "rented", +}, +{ +"movie_id":124, +"customer_id": 5, +"status": "rented", +}, +{ +"movie_id":119, +"customer_id": 5, +"status": "rented", +}, +{ +"movie_id":118, +"customer_id": 2, +"status": "rented", +}, +{ +"movie_id":125, +"customer_id": 2, +"status": "rented", +}, +{ +"movie_id":111, +"customer_id": 8, +"status": "rented", +}, +{ +"movie_id":122, +"customer_id": 3, +"status": "rented", +}, +{ +"movie_id":108, +"customer_id": 4, +"status": "rented", +}, +{ +"movie_id":116, +"customer_id": 3, +"status": "rented", +} +] From 1dc3d4a9c9dfe6a7359397e9857e39e9c41fbdb6 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Fri, 17 Jun 2016 13:38:54 -0700 Subject: [PATCH 027/118] removed seed file for rentals. unnecessary --- db/seeds/rentals.json | 157 ------------------------------------------ 1 file changed, 157 deletions(-) delete mode 100644 db/seeds/rentals.json diff --git a/db/seeds/rentals.json b/db/seeds/rentals.json deleted file mode 100644 index 32f927d68..000000000 --- a/db/seeds/rentals.json +++ /dev/null @@ -1,157 +0,0 @@ - -{ -"movie_id":116, -"customer_id": 8, -"status": "rented", -}, -{ -"movie_id":119, -"customer_id": 6, -"status": "rented", -}, -{ -"movie_id":126, -"customer_id": 7, -"status": "rented", -}, -{ -"movie_id":117, -"customer_id": 10, -"status": "rented", -}, -{ -"movie_id":114, -"customer_id": 3, -"status": "rented", -}, -{ -"movie_id":125, -"customer_id": 4, -"status": "available", -}, -{ -"movie_id":128, -"customer_id": 1, -"status": "available", -}, -{ -"movie_id":107, -"customer_id": 8, -"status": "available", -}, -{ -"movie_id":112, -"customer_id": 2, -"status": "available", -}, -{ -"movie_id":112, -"customer_id": 9, -"status": "available", -}, -{ -"movie_id":115, -"customer_id": 5, -"status": "available", -}, -{ -"movie_id":102, -"customer_id": 10, -"status": "available", -}, -{ -"movie_id":104, -"customer_id": 4, -"status": "available", -}, -{ -"movie_id":101, -"customer_id": 10, -"status": "rented", -}, -{ -"movie_id":130, -"customer_id": 2, -"status": "rented", -}, -{ -"movie_id":120, -"customer_id": 6, -"status": "rented", -}, -{ -"movie_id":124, -"customer_id": 1, -"status": "rented", -}, -{ -"movie_id":128, -"customer_id": 10, -"status": "rented", -}, -{ -"movie_id":104, -"customer_id": 4, -"status": "rented", -}, -{ -"movie_id":124, -"customer_id": 4, -"status": "rented", -}, -{ -"movie_id":118, -"customer_id": 4, -"status": "rented", -}, -{ -"movie_id":108, -"customer_id": 4, -"status": "rented", -}, -{ -"movie_id":110, -"customer_id": 6, -"status": "rented", -}, -{ -"movie_id":124, -"customer_id": 5, -"status": "rented", -}, -{ -"movie_id":119, -"customer_id": 5, -"status": "rented", -}, -{ -"movie_id":118, -"customer_id": 2, -"status": "rented", -}, -{ -"movie_id":125, -"customer_id": 2, -"status": "rented", -}, -{ -"movie_id":111, -"customer_id": 8, -"status": "rented", -}, -{ -"movie_id":122, -"customer_id": 3, -"status": "rented", -}, -{ -"movie_id":108, -"customer_id": 4, -"status": "rented", -}, -{ -"movie_id":116, -"customer_id": 3, -"status": "rented", -} -] From a86b2330571a868df4793c1a6788d927f820f0dd Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Fri, 17 Jun 2016 13:39:21 -0700 Subject: [PATCH 028/118] fixed bug with populating rentals table. assign a few customers to rentals. --- tasks/seed.js | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/tasks/seed.js b/tasks/seed.js index 32640771f..c9ef78581 100644 --- a/tasks/seed.js +++ b/tasks/seed.js @@ -3,18 +3,28 @@ var connectionString = "postgres://localhost/videoStore"; var db = massive.connectSync({connectionString : connectionString}); var data_movies = require('../db/seeds/movies.json'); var data_customers = require('../db/seeds/customers.json'); +var data_rentals = require('../db/seeds/rentals.json'); for (var record of data_movies) { db.movies.saveSync(record); - db.movies.find({title: record.title}, function(err, res){ - for (i = 0; i < record.inventory; i++) { - db.rentals.saveSync({movie_id: res[0].id }) - }; - }); }; + +db.movies.find(function(err, res){ + for (var movie of res) { + for (i = 0; i < movie.inventory; i++) { + db.rentals.saveSync({movie_id: movie.id, status: 'available' }); + }; + } +}); + for (var record of data_customers) { - db.customers.saveSync(record) + db.customers.saveSync(record); }; +db.rentals.saveSync({id: [10, 11, 12], status: 'rented', customer_id: 2}) +db.rentals.saveSync({id: [13, 14, 15], status: 'rented', customer_id: 4}) + + + process.exit( ); From 6909f39a9b0f9c2e2b9b92476e9e46a3a9d7bb30 Mon Sep 17 00:00:00 2001 From: Risha Date: Fri, 17 Jun 2016 13:43:58 -0700 Subject: [PATCH 029/118] working on the customers movie call --- controllers/customers_controller.js | 55 ++++++++++++++++++----------- models/customers.js | 1 - models/rentals.js | 15 ++++++++ routes/customers.js | 6 ++-- 4 files changed, 52 insertions(+), 25 deletions(-) diff --git a/controllers/customers_controller.js b/controllers/customers_controller.js index 462c88e76..a06686228 100644 --- a/controllers/customers_controller.js +++ b/controllers/customers_controller.js @@ -15,28 +15,41 @@ var CustomersController = { }); }, -sort: function(req, res, next) { - // // GET /search?q=tobi+ferret - // req.query.q - // // => "tobi ferret" + sort: function(req, res, next) { + //Send in an ORDER clause and a LIMIT with OFFSET + var options = { + limit : req.query.n, + order : req.params.column, + offset: req.query.p + }; + // products ordered in descending fashion + // takes 2 parameters + // options and the callback fucntion + Customers.sort_by(options, function(error, customers) { // this will return and array of customers + if(error) { + var err = new Error("No such customer"); + err.status = 404; + next(err); + } else { + res.json(customers); + } + }); + }, - //Send in an ORDER clause and a LIMIT with OFFSET - var options = { - limit : req.query.n, - order : req.params.column, - offset: req.query.p - }; - // products ordered in descending fashion - Customers.sort_by(options, function(error, customers) { // this is a callback - if(error) { - var err = new Error("No such customer"); - err.status = 404; - next(err); - } else { - res.json(customers); - } - }); + current: function(req, res, next) { + // passed the callback + Customers.find(req.params.id, function(error, customers) { // calling the find method on customers + if(error) { + var err = new Error ("No checkout information"); + err.status = 404; + next(err); + } else { + res.json(movies); + } + }); + }; } -}; + + module.exports = CustomersController; diff --git a/models/customers.js b/models/customers.js index 3acc76e3b..3a0392909 100644 --- a/models/customers.js +++ b/models/customers.js @@ -40,5 +40,4 @@ Customers.sort_by = function(options, callback) { - module.exports = Customers; diff --git a/models/rentals.js b/models/rentals.js index 5b6947396..3e69276c6 100644 --- a/models/rentals.js +++ b/models/rentals.js @@ -6,6 +6,21 @@ var Rentals= function(id) { this.id = id; }; +// find the movies by customer id +// an array of customer ids +Rentals.sort_by = function(options, callback) { + db.rentals.find({}, options, function(error, rentals){ + if(error || !rentals) { + callback(error || new Error("Could not retrieve your movies"), undefined); + } else { + callback(null, customers.map(function(rental) { + return new Movies(rentals.movie.id); + })); + } + }); +}; + + // Customers.all = function(callback) { // db.customers.find(function(error, customers) { // if(error || !customers) { diff --git a/routes/customers.js b/routes/customers.js index b03185b4d..450d5df57 100644 --- a/routes/customers.js +++ b/routes/customers.js @@ -5,10 +5,10 @@ var CustomersController = require('../controllers/customers_controller.js'); /* GET customers listing. */ router.get('/', CustomersController.index); -// /* GET customers details. */ -// router.get('/:id', CustomersController.show); - /* GET customers sorted by column details. */ router.get('/sort/:column', CustomersController.sort); +/* GET customers details. */ +router.get('/:id/current', CustomersController.current); + module.exports = router; From 03fb9104b220a480991c5e9475b46c97e240c637 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Fri, 17 Jun 2016 13:48:22 -0700 Subject: [PATCH 030/118] deleted reference to rental seed file since it was removed --- tasks/seed.js | 1 - 1 file changed, 1 deletion(-) diff --git a/tasks/seed.js b/tasks/seed.js index c9ef78581..b78d56c7b 100644 --- a/tasks/seed.js +++ b/tasks/seed.js @@ -3,7 +3,6 @@ var connectionString = "postgres://localhost/videoStore"; var db = massive.connectSync({connectionString : connectionString}); var data_movies = require('../db/seeds/movies.json'); var data_customers = require('../db/seeds/customers.json'); -var data_rentals = require('../db/seeds/rentals.json'); for (var record of data_movies) { db.movies.saveSync(record); From 7d8f6d816a63a5eb2567f0ce675e94c887b1bb06 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Fri, 17 Jun 2016 14:58:41 -0700 Subject: [PATCH 031/118] when given a movie title in movies/:title/current, gives a list of customers who currently have that movie checked out --- controllers/movies_controller.js | 35 ++++++++++++++++++++++++++++++++ models/customers.js | 13 +++++++++++- models/movies.js | 11 ++++++++++ models/rentals.js | 30 +++++++++++++++------------ routes/movies.js | 3 +++ 5 files changed, 78 insertions(+), 14 deletions(-) diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index 109b7bbbd..e1d95bcde 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -1,4 +1,6 @@ var Movies = require("../models/movies"); +var Rentals = require('../models/rentals'); +var Customers = require('../models/customers'); var MoviesController = { index: function(req, res, next) { @@ -37,6 +39,39 @@ var MoviesController = { res.json(movies) } }); + }, + + current: function(req, res, next) { + var movie = req.params.title; + + Movies.find(movie, function(error, found_movie) { + if(error) { + var err = new Error("No such movie"); + err.status = 404; + next(err); + } else { + Rentals.get_rentals(found_movie.id, function(error, found_rentals) { + if(error) { + var err = new Error("No such rentals"); + err.status = 404; + next(err); + } else { + + Customers.find(found_rentals, function(error, customers) { + if(error) { + var err = new Error("No such customers"); + err.status = 404; + next(err); + } else { + res.json(customers); + } + }) + } + }) + } + }) + + } }; diff --git a/models/customers.js b/models/customers.js index 3b976550f..c76e9d9bc 100644 --- a/models/customers.js +++ b/models/customers.js @@ -23,9 +23,20 @@ Customers.all = function(callback) { return new Customers(customer); })); } - // callback(null, "melissa"); }); }; +Customers.find = function(ids, callback) { + db.customers.find({id: ids}, function(error, customers) { + if(error || !customers) { + callback(error || new Error("Could not retrieve customers"), undefined); + } else { + callback(null, customers.map(function(customer) { + return new Customers(customer); + })); + } + }) +} + module.exports = Customers; diff --git a/models/movies.js b/models/movies.js index 4fa8649ff..176ca1185 100644 --- a/models/movies.js +++ b/models/movies.js @@ -34,4 +34,15 @@ Movies.sort_by_day = function(options, callback) { }); }; +Movies.find = function(title, callback) { + db.movies.findOne({title: title}, function(error, movie) { + if(error || !movie) { + callback(error || new Error("Could not retrieve movie"), undefined); + } else { + + callback(null, new Movies(movie)); + } + }); +} + module.exports = Movies; diff --git a/models/rentals.js b/models/rentals.js index 5b6947396..b82c64a6c 100644 --- a/models/rentals.js +++ b/models/rentals.js @@ -2,21 +2,25 @@ var app = require("../app"); var db = app.get("db"); // Constructor function -var Rentals= function(id) { - this.id = id; +var Rentals = function(rental) { + this.id = rental.id; + this.movie_id = rental.movie_id; + this.customer_id = rental.customer_id; + this.status = rental.status; }; -// Customers.all = function(callback) { -// db.customers.find(function(error, customers) { -// if(error || !customers) { -// callback(error || new Error("Could not retrieve customers"), undefined); -// } else { -// callback(null, customers.map(function(customer) { -// return new Customers(customer.id); -// })); -// } -// }); -// }; +Rentals.get_rentals = function(movie_id, callback) { + db.rentals.find({movie_id: movie_id}, function(error, rentals) { + if(error || !rentals) { + callback(error || new Error("Could not retrieve rentals"), undefined); + } else { + callback(null, rentals.map(function(rental) { + var rental = new Rentals(rental); + return rental.customer_id; + })); + } + }) +} module.exports = Rentals; diff --git a/routes/movies.js b/routes/movies.js index f054870f6..930fd140b 100644 --- a/routes/movies.js +++ b/routes/movies.js @@ -8,6 +8,9 @@ router.get('/', MoviesController.index); /* GET movies sorted by column details. */ router.get('/sort/:column', MoviesController.sort); +/* GET customers that currently have the film */ +router.get('/:title/current', MoviesController.current) + module.exports = router; From 41ba9d0029ab48947c4036866e6c3bbda7bdac38 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Fri, 17 Jun 2016 15:08:02 -0700 Subject: [PATCH 032/118] added some history seed data --- db/seeds/history.json | 38 ++++++++++++++++++++++++++++++++++++++ tasks/seed.js | 6 ++++-- 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 db/seeds/history.json diff --git a/db/seeds/history.json b/db/seeds/history.json new file mode 100644 index 000000000..1aea4f103 --- /dev/null +++ b/db/seeds/history.json @@ -0,0 +1,38 @@ +[ +{ +"rental_id": 1, +"customer_id": 1, +"checkout_date": "1960-06-16", +"return_date": "1960-06-26" +}, +{ +"rental_id": 7, +"customer_id": 3, +"checkout_date": "1990-06-16", +"return_date": "1990-06-26" +}, +{ +"rental_id": 12, +"customer_id": 2, +"checkout_date": "1999-06-16", +"return_date": "1999-06-26" +}, +{ +"rental_id": 4, +"customer_id": 1, +"checkout_date": "1998-06-16", +"return_date": "1998-06-26" +}, +{ +"rental_id": 9, +"customer_id": 11, +"checkout_date": "2002-06-16", +"return_date": "2002-06-26" +}, +{ +"rental_id": 14, +"customer_id": 2, +"checkout_date": "2004-06-16", +"return_date": "2004-06-26" +} +] \ No newline at end of file diff --git a/tasks/seed.js b/tasks/seed.js index b78d56c7b..3dc3c4b27 100644 --- a/tasks/seed.js +++ b/tasks/seed.js @@ -3,11 +3,15 @@ var connectionString = "postgres://localhost/videoStore"; var db = massive.connectSync({connectionString : connectionString}); var data_movies = require('../db/seeds/movies.json'); var data_customers = require('../db/seeds/customers.json'); +var data_history = require('../db/seeds/history.json'); for (var record of data_movies) { db.movies.saveSync(record); }; +for (var record of data_history) { + db.history.saveSync(record); +}; db.movies.find(function(err, res){ for (var movie of res) { @@ -24,6 +28,4 @@ for (var record of data_customers) { db.rentals.saveSync({id: [10, 11, 12], status: 'rented', customer_id: 2}) db.rentals.saveSync({id: [13, 14, 15], status: 'rented', customer_id: 4}) - - process.exit( ); From 0d3b09b3aaf4728584c291f513cb5c2aa6c68acc Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Fri, 17 Jun 2016 15:24:56 -0700 Subject: [PATCH 033/118] changed method and variable names to be more descriptive --- controllers/movies_controller.js | 9 +++------ models/rentals.js | 3 +-- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index e1d95bcde..6d7bc3c81 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -50,14 +50,13 @@ var MoviesController = { err.status = 404; next(err); } else { - Rentals.get_rentals(found_movie.id, function(error, found_rentals) { + Rentals.get_customer_ids(found_movie.id, function(error, customer_ids) { if(error) { var err = new Error("No such rentals"); err.status = 404; next(err); } else { - - Customers.find(found_rentals, function(error, customers) { + Customers.find(customer_ids, function(error, customers) { if(error) { var err = new Error("No such customers"); err.status = 404; @@ -70,9 +69,7 @@ var MoviesController = { }) } }) - - - } + }, }; module.exports = MoviesController; diff --git a/models/rentals.js b/models/rentals.js index b82c64a6c..8419cf32e 100644 --- a/models/rentals.js +++ b/models/rentals.js @@ -9,7 +9,7 @@ var Rentals = function(rental) { this.status = rental.status; }; -Rentals.get_rentals = function(movie_id, callback) { +Rentals.get_customer_ids = function(movie_id, callback) { db.rentals.find({movie_id: movie_id}, function(error, rentals) { if(error || !rentals) { callback(error || new Error("Could not retrieve rentals"), undefined); @@ -22,5 +22,4 @@ Rentals.get_rentals = function(movie_id, callback) { }) } - module.exports = Rentals; From 2b6f758338d85d8c539e44cd9619debb43c5bb55 Mon Sep 17 00:00:00 2001 From: Risha Date: Fri, 17 Jun 2016 15:37:42 -0700 Subject: [PATCH 034/118] added a method, current to find the rentals of a single customer id and added a route to access the rentals by customer id. --- app.js | 2 ++ controllers/customers_controller.js | 18 +++++++++--------- controllers/rentals_controller.js | 25 ++++++------------------- models/rentals.js | 19 ++++++++++++------- routes/rentals.js | 2 +- tasks/seed.js | 10 +++++----- 6 files changed, 35 insertions(+), 41 deletions(-) diff --git a/app.js b/app.js index d111495fa..e57ba6ed0 100644 --- a/app.js +++ b/app.js @@ -10,7 +10,9 @@ var app = module.exports = express(); // database setup var connectionString = "postgres://localhost/videoStore"; +// the node module that is used to access the database var db = massive.connectSync({connectionString: connectionString}); +// this gives me a way to grab the database from the code, like in the model app.set('db', db); // view engine setup diff --git a/controllers/customers_controller.js b/controllers/customers_controller.js index a06686228..3a71154c4 100644 --- a/controllers/customers_controller.js +++ b/controllers/customers_controller.js @@ -1,4 +1,5 @@ var Customers = require("../models/customers"); +var Rentals = require("../models/rentals"); var CustomersController = { index: function(req, res, next) { @@ -35,21 +36,20 @@ var CustomersController = { } }); }, - - + // this is a property whose value is a function (this is now a method) + // this is the response from node current: function(req, res, next) { - // passed the callback - Customers.find(req.params.id, function(error, customers) { // calling the find method on customers + //model has a find method that takes in 2 arguments (id, callback function) + Rentals.find_by_customer(req.params.id, function(error, rentals) { if(error) { - var err = new Error ("No checkout information"); + var err = new Error("No such account"); err.status = 404; next(err); } else { - res.json(movies); + res.json(rentals); // write out the rentals as json } }); - }; -} - + }, +}; module.exports = CustomersController; diff --git a/controllers/rentals_controller.js b/controllers/rentals_controller.js index b3a8c4bab..3bdd625fb 100644 --- a/controllers/rentals_controller.js +++ b/controllers/rentals_controller.js @@ -1,22 +1,9 @@ var Rentals = require("../models/movies"); -show: function(req, res, next) { - Rental.find(req.params.id, function(error, account) { - if(error) { - var err = new Error("No such account"); - err.status = 404; - next(err); - } else { - account.getBalance(function(error, balance) { - res.render("accounts/show", { - account: { - id: account.id, - balance: balance - } - }); - }); - } - }); -} -}; +// declaring as a javascript object +var RentalsController = {}; + + + + module.exports = RentalsController; diff --git a/models/rentals.js b/models/rentals.js index 3e69276c6..b487fb41f 100644 --- a/models/rentals.js +++ b/models/rentals.js @@ -2,24 +2,29 @@ var app = require("../app"); var db = app.get("db"); // Constructor function -var Rentals= function(id) { - this.id = id; +var Rentals = function(rentals) { + this.id = rentals.id; + this.customer_id = rentals.customer_id; + this.movie_id = rentals.movie_id; + this.status = rentals.status; }; - // find the movies by customer id // an array of customer ids -Rentals.sort_by = function(options, callback) { - db.rentals.find({}, options, function(error, rentals){ +Rentals.find_by_customer = function(customer_id, callback) { + // key value that matches one of the column names in the rentals TABLE + // value is the specific value that we want to look for in the table + db.rentals.find({customer_id : customer_id}, function(error, rentals) { if(error || !rentals) { callback(error || new Error("Could not retrieve your movies"), undefined); } else { - callback(null, customers.map(function(rental) { - return new Movies(rentals.movie.id); + callback(null, rentals.map(function(rental) { + return new Rentals(rental); })); } }); }; +// when movies are returned the customer id will be deleted from the rentals table // Customers.all = function(callback) { // db.customers.find(function(error, customers) { diff --git a/routes/rentals.js b/routes/rentals.js index 0e4db02c5..1d1ef1dd4 100644 --- a/routes/rentals.js +++ b/routes/rentals.js @@ -6,6 +6,6 @@ var RentalsController = require('../controllers/rentals_controller.js'); router.get('/:title', RentalsController.show); /* GET customers details. */ -// router.get('/:id', RentalsController.show); +router.get('/:id/current', RentalsController.current); module.exports = router; diff --git a/tasks/seed.js b/tasks/seed.js index b78d56c7b..73ceebeef 100644 --- a/tasks/seed.js +++ b/tasks/seed.js @@ -6,23 +6,23 @@ var data_customers = require('../db/seeds/customers.json'); for (var record of data_movies) { db.movies.saveSync(record); -}; +} db.movies.find(function(err, res){ for (var movie of res) { for (i = 0; i < movie.inventory; i++) { db.rentals.saveSync({movie_id: movie.id, status: 'available' }); - }; + } } }); for (var record of data_customers) { db.customers.saveSync(record); -}; +} -db.rentals.saveSync({id: [10, 11, 12], status: 'rented', customer_id: 2}) -db.rentals.saveSync({id: [13, 14, 15], status: 'rented', customer_id: 4}) +db.rentals.saveSync({id: [10, 11, 12], status: 'rented', customer_id: 2}); +db.rentals.saveSync({id: [13, 14, 15], status: 'rented', customer_id: 4}); From 3b20c69f7de885a5a9ec29c8cf9d61fc93b54269 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Fri, 17 Jun 2016 15:42:10 -0700 Subject: [PATCH 035/118] can find history of all past rentals for a specific movie --- controllers/movies_controller.js | 41 ++++++++++++++++++++++++++++++++ models/history.js | 27 +++++++++++++++++++++ models/rentals.js | 14 +++++++++++ routes/movies.js | 6 ++++- 4 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 models/history.js diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index 6d7bc3c81..6a626c3fe 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -1,6 +1,7 @@ var Movies = require("../models/movies"); var Rentals = require('../models/rentals'); var Customers = require('../models/customers'); +var History = require('../models/history'); var MoviesController = { index: function(req, res, next) { @@ -70,6 +71,46 @@ var MoviesController = { } }) }, + + history: function(req, res, next) { + var movie = req.params.title; + var column = req.params.column; + + Movies.find(movie, function(error, found_movie) { + if(error) { + var err = new Error("No such movie"); + err.status = 404; + next(err); + } else { + Rentals.get_rental_ids(found_movie.id, function(error, rental_ids) { + if(error) { + var err = new Error("No such rental"); + err.status = 404; + next(err); + } else { + History.get_customer_ids(rental_ids, function(error, customer_ids) { + if(error) { + var err = new Error("No such history"); + err.status = 404; + next(err); + } else { + Customers.find(customer_ids, function(error, customers) { + if(error) { + var err = new Error("No such customers"); + err.status = 404; + next(err); + } else { + res.json(customers); + } + }) + } + }) + } + }) + } + }) + } }; module.exports = MoviesController; + diff --git a/models/history.js b/models/history.js new file mode 100644 index 000000000..1d7cfdb1b --- /dev/null +++ b/models/history.js @@ -0,0 +1,27 @@ +var app = require("../app"); +var db = app.get("db"); + +// Constructor function +var History = function(history) { + this.id = history.id; + this.rental_id = history.rental_id; + this.customer_id = history.customer_id; + this.checkout_date = history.checkout_date; + this.return_date = history.return_date; +}; + +History.get_customer_ids = function(rental_ids, callback) { + db.history.find({rental_id: rental_ids}, function(error, histories) { + if(error || !histories) { + callback(error || new Error("Could not retrieve histories"), undefined); + } else { + callback(null, histories.map(function(history) { + var history = new History(history); + return history.customer_id; + })); + } + }) +} + + +module.exports = History; diff --git a/models/rentals.js b/models/rentals.js index 8419cf32e..7ecbbf965 100644 --- a/models/rentals.js +++ b/models/rentals.js @@ -22,4 +22,18 @@ Rentals.get_customer_ids = function(movie_id, callback) { }) } +Rentals.get_rental_ids = function(movie_id, callback) { + db.rentals.find({movie_id: movie_id}, function(error, rentals) { + if(error || !rentals) { + callback(error || new Error("Could not retrieve rentals"), undefined); + } else { + callback(null, rentals.map(function(rental) { + var rental = new Rentals(rental); + return rental.id; + })); + } + }) +} + + module.exports = Rentals; diff --git a/routes/movies.js b/routes/movies.js index 930fd140b..850356f1b 100644 --- a/routes/movies.js +++ b/routes/movies.js @@ -9,7 +9,11 @@ router.get('/', MoviesController.index); router.get('/sort/:column', MoviesController.sort); /* GET customers that currently have the film */ -router.get('/:title/current', MoviesController.current) +router.get('/:title/current', MoviesController.current); + +// /movies/Jaws/history/sort/name +/* GET customers that have checkout out the film in the past */ +router.get('/:title/history/sort/:column', MoviesController.history); module.exports = router; From b89b146979651d8eb7af629351d356db1cf94a32 Mon Sep 17 00:00:00 2001 From: Risha Date: Fri, 17 Jun 2016 15:57:32 -0700 Subject: [PATCH 036/118] fixed colon error --- controllers/customers_controller.js | 14 ++++++++++++++ controllers/movies_controller.js | 10 +++++----- models/customers.js | 20 ++++++++++++-------- routes/customers.js | 2 ++ 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/controllers/customers_controller.js b/controllers/customers_controller.js index 3a71154c4..daaa83c97 100644 --- a/controllers/customers_controller.js +++ b/controllers/customers_controller.js @@ -50,6 +50,20 @@ var CustomersController = { } }); }, + + history: function(req, res, next) { + //model has a find method that takes in 2 arguments (id, callback function) + Rentals.find_by_customer(req.params.id, function(error, history) { + if(error) { + var err = new Error("No history at this store"); + err.status = 404; + next(err); + } else { + res.json(history); // write out the history as json + } + }); + }, }; + module.exports = CustomersController; diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index 6d7bc3c81..9ff138fb0 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -27,7 +27,7 @@ var MoviesController = { limit : req.query.n, order : req.params.column, offset: req.query.p - } + }; // products ordered in descending fashion Movies.sort_by_day(options, function(error, movies) { // this is a callback @@ -36,7 +36,7 @@ var MoviesController = { err.status = 404; next(err); } else { - res.json(movies) + res.json(movies); } }); }, @@ -64,11 +64,11 @@ var MoviesController = { } else { res.json(customers); } - }) + }); } - }) + }); } - }) + }); }, }; diff --git a/models/customers.js b/models/customers.js index 919e3090e..f011eed46 100644 --- a/models/customers.js +++ b/models/customers.js @@ -26,13 +26,20 @@ Customers.all = function(callback) { }); }; -<<<<<<< HEAD Customers.sort_by = function(options, callback) { db.customers.find({}, options, function(error, customers){ -======= + if(error || !customers) { + callback(error || new Error("Could not retrieve customers"), undefined); + } else { + callback(null, customers.map(function(customer) { + return new Customers(customer); + })); + } + }); +}; + Customers.find = function(ids, callback) { db.customers.find({id: ids}, function(error, customers) { ->>>>>>> 0d3b09b3aaf4728584c291f513cb5c2aa6c68acc if(error || !customers) { callback(error || new Error("Could not retrieve customers"), undefined); } else { @@ -40,14 +47,11 @@ Customers.find = function(ids, callback) { return new Customers(customer); })); } -<<<<<<< HEAD }); }; -======= - }) -} ->>>>>>> 0d3b09b3aaf4728584c291f513cb5c2aa6c68acc + + module.exports = Customers; diff --git a/routes/customers.js b/routes/customers.js index 450d5df57..69b3ab1b8 100644 --- a/routes/customers.js +++ b/routes/customers.js @@ -11,4 +11,6 @@ router.get('/sort/:column', CustomersController.sort); /* GET customers details. */ router.get('/:id/current', CustomersController.current); +router.get('/:id/history', CustomersController.history); + module.exports = router; From 51252e27e3ae8194aecf2b187341067b568d6230 Mon Sep 17 00:00:00 2001 From: melissajimison Date: Fri, 17 Jun 2016 16:09:50 -0700 Subject: [PATCH 037/118] uncommment routes for Rantals --- app.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app.js b/app.js index d111495fa..151c05546 100644 --- a/app.js +++ b/app.js @@ -34,8 +34,8 @@ app.use('/movies', moviesRoutes); var customersRoutes = require('./routes/customers'); app.use('/customers', customersRoutes); -// var rentalsRoutes = require('./routes/rentals'); -// app.use('/rentals', rentalsRoutes); +var rentalsRoutes = require('./routes/rentals'); +app.use('/rentals', rentalsRoutes); // catch 404 and forward to error handler app.use(function(req, res, next) { From 4cc46536fa09dfbe3a6e1a2c7cde54dab9774621 Mon Sep 17 00:00:00 2001 From: melissajimison Date: Fri, 17 Jun 2016 16:11:20 -0700 Subject: [PATCH 038/118] added '/:title endpoint --- routes/rentals.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/routes/rentals.js b/routes/rentals.js index 0e4db02c5..b065e37d2 100644 --- a/routes/rentals.js +++ b/routes/rentals.js @@ -2,10 +2,9 @@ var express = require('express'); var router = express.Router(); var RentalsController = require('../controllers/rentals_controller.js'); -/* GET customers listing. */ -router.get('/:title', RentalsController.show); +/* GET rentals listing by title. */ +// Look a movie up by title to see (/rentals/Jaws) -/* GET customers details. */ -// router.get('/:id', RentalsController.show); +router.get('/:title', RentalsController.find_movie); module.exports = router; From 0f40cf68cd2a02ff2d50d0df61b13a9969164574 Mon Sep 17 00:00:00 2001 From: melissajimison Date: Fri, 17 Jun 2016 16:13:15 -0700 Subject: [PATCH 039/118] added find_movie method that calls Movies.find, Rentals.available, and render an object --- controllers/rentals_controller.js | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/controllers/rentals_controller.js b/controllers/rentals_controller.js index b3a8c4bab..11eeb8e7a 100644 --- a/controllers/rentals_controller.js +++ b/controllers/rentals_controller.js @@ -1,19 +1,24 @@ -var Rentals = require("../models/movies"); +var Rentals = require('../models/rentals'); +var Movies = require("../models/movies"); -show: function(req, res, next) { - Rental.find(req.params.id, function(error, account) { +var RentalsController = { + +find_movie: function(req, res, next) { + var movie = req.params.title; + + Movies.find(movie, function(error, found_movie) { if(error) { - var err = new Error("No such account"); + var err = new Error("No such movie"); err.status = 404; next(err); } else { - account.getBalance(function(error, balance) { - res.render("accounts/show", { - account: { - id: account.id, - balance: balance - } - }); + obj = {} + obj['Synopsis'] = found_movie.overview; + obj['Release Date'] = found_movie.release_date; + obj['Total Inventory'] = found_movie.inventory; + Rentals.available(found_movie.id, function(err, number){ + obj['Available Inventory'] = number + res.json(obj); }); } }); From d51dbda3e7dee3e743c0de6677917adc4b30a100 Mon Sep 17 00:00:00 2001 From: melissajimison Date: Fri, 17 Jun 2016 16:20:03 -0700 Subject: [PATCH 040/118] created Rentals.available that returns a nummber, the length of the array that matches movies_id and status 'available' --- models/rentals.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/models/rentals.js b/models/rentals.js index b82c64a6c..f5feb7812 100644 --- a/models/rentals.js +++ b/models/rentals.js @@ -22,5 +22,14 @@ Rentals.get_rentals = function(movie_id, callback) { }) } +Rentals.available = function(movie_id, callback){ + db.rentals.find({movie_id: movie_id}, function(error, rentals) { + if(error || !rentals) { + callback(error || new Error("Could not retrieve rentals"), undefined); + } else { + callback(null, rentals); + } + }); +} module.exports = Rentals; From db21a3e70283ec1481c7a28a5c60a2aa6e173c42 Mon Sep 17 00:00:00 2001 From: melissajimison Date: Fri, 17 Jun 2016 16:46:20 -0700 Subject: [PATCH 041/118] replace the massive setup for the fancy two lines of code --- tasks/seed.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tasks/seed.js b/tasks/seed.js index b3198528a..ad2fa6e5a 100644 --- a/tasks/seed.js +++ b/tasks/seed.js @@ -1,6 +1,6 @@ -var massive = require('massive'); -var connectionString = "postgres://localhost/videoStore"; -var db = massive.connectSync({connectionString : connectionString}); +var app = require("../app"); +var db = app.get("db"); + var data_movies = require('../db/seeds/movies.json'); var data_customers = require('../db/seeds/customers.json'); var data_history = require('../db/seeds/history.json'); From e6c5a159e19ed701bdc73dab8dba8c1472354871 Mon Sep 17 00:00:00 2001 From: melissajimison Date: Fri, 17 Jun 2016 16:47:05 -0700 Subject: [PATCH 042/118] coment out a non-used route --- routes/rentals.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routes/rentals.js b/routes/rentals.js index a36831e53..5747a379e 100644 --- a/routes/rentals.js +++ b/routes/rentals.js @@ -6,6 +6,6 @@ var RentalsController = require('../controllers/rentals_controller.js'); // Look a movie up by title to see (/rentals/Jaws) router.get('/:title', RentalsController.find_movie); /* GET customers details. */ -router.get('/:id/current', RentalsController.current); +// router.get('/:id/current', RentalsController.current); module.exports = router; From beb2bcd99d32c7c80109ad455258125fae4d5f67 Mon Sep 17 00:00:00 2001 From: melissajimison Date: Fri, 17 Jun 2016 17:09:45 -0700 Subject: [PATCH 043/118] added the length --- models/rentals.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/rentals.js b/models/rentals.js index 8e99d13e1..f50176783 100644 --- a/models/rentals.js +++ b/models/rentals.js @@ -45,7 +45,7 @@ Rentals.available = function(movie_id, callback){ if(error || !rentals) { callback(error || new Error("Could not retrieve rentals"), undefined); } else { - callback(null, rentals); + callback(null, rentals.length); } }); } From 3d3a88b194ece2ada343f2a902d5ddda1b94e05d Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Sat, 18 Jun 2016 19:02:57 -0700 Subject: [PATCH 044/118] updated seeds so the same customers didn't check out multiple copies of the same movie in rentals (it was confusing to see the same name listed multiple times when seeing current check-outs) --- tasks/seed.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tasks/seed.js b/tasks/seed.js index 3dc3c4b27..1c79642ec 100644 --- a/tasks/seed.js +++ b/tasks/seed.js @@ -25,7 +25,7 @@ for (var record of data_customers) { db.customers.saveSync(record); }; -db.rentals.saveSync({id: [10, 11, 12], status: 'rented', customer_id: 2}) -db.rentals.saveSync({id: [13, 14, 15], status: 'rented', customer_id: 4}) +db.rentals.saveSync({id: [33, 9, 15], status: 'rented', customer_id: 2}) +db.rentals.saveSync({id: [2, 10, 21], status: 'rented', customer_id: 4}) process.exit( ); From f19da05bfe0845a419014b13e39d5a8cac1d8f5d Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Sat, 18 Jun 2016 19:03:57 -0700 Subject: [PATCH 045/118] updated the sql query to use inner joins between movies, rentals, and customers tables to avoid nested callbacks --- controllers/movies_controller.js | 46 +++++++++++++++++++------------- models/movies.js | 14 +++++++++- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index 6a626c3fe..20e181759 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -45,31 +45,41 @@ var MoviesController = { current: function(req, res, next) { var movie = req.params.title; - Movies.find(movie, function(error, found_movie) { + Movies.find_customers_by_title(movie, function(error, customers) { if(error) { var err = new Error("No such movie"); err.status = 404; next(err); } else { - Rentals.get_customer_ids(found_movie.id, function(error, customer_ids) { - if(error) { - var err = new Error("No such rentals"); - err.status = 404; - next(err); - } else { - Customers.find(customer_ids, function(error, customers) { - if(error) { - var err = new Error("No such customers"); - err.status = 404; - next(err); - } else { - res.json(customers); - } - }) - } - }) + res.json(customers); } }) + + // Movies.find(movie, function(error, found_movie) { + // if(error) { + // var err = new Error("No such movie"); + // err.status = 404; + // next(err); + // } else { + // Rentals.get_customer_ids(found_movie.id, function(error, customer_ids) { + // if(error) { + // var err = new Error("No such rentals"); + // err.status = 404; + // next(err); + // } else { + // Customers.find(customer_ids, function(error, customers) { + // if(error) { + // var err = new Error("No such customers"); + // err.status = 404; + // next(err); + // } else { + // res.json(customers); + // } + // }) + // } + // }) + // } + // }) }, history: function(req, res, next) { diff --git a/models/movies.js b/models/movies.js index 176ca1185..b448c83f4 100644 --- a/models/movies.js +++ b/models/movies.js @@ -1,5 +1,6 @@ var app = require("../app"); var db = app.get("db"); +var Customers = require('../models/customers'); // Constructor function var Movies = function(movie) { @@ -39,10 +40,21 @@ Movies.find = function(title, callback) { if(error || !movie) { callback(error || new Error("Could not retrieve movie"), undefined); } else { - callback(null, new Movies(movie)); } }); } +Movies.find_customers_by_title = function(title, callback) { + db.run("select * from movies INNER JOIN rentals on movies.id=rentals.movie_id inner join customers on rentals.customer_id=customers.id where title = $1", [title], function(error, customers) { + if(error || !customers) { + callback(error || new Error("Could not retrieve customers"), undefined); + } else { + callback(null, customers.map(function(customer) { + return new Customers(customer); + })); + }; + }); +} + module.exports = Movies; From 8766c44d5679b1d8cfe9bc6b27936bb295dca868 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Sat, 18 Jun 2016 19:16:58 -0700 Subject: [PATCH 046/118] replaced nested queries using different model methods with an inner join in Movies model that connects movies/rentals/histry/customers. when a title is given, it retrieves a list of all customers that have ever rented that movie --- controllers/movies_controller.js | 62 ++++++++++++++++++-------------- models/history.js | 14 -------- models/movies.js | 12 +++++++ models/rentals.js | 14 -------- 4 files changed, 48 insertions(+), 54 deletions(-) diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index 20e181759..8a5f74928 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -86,39 +86,49 @@ var MoviesController = { var movie = req.params.title; var column = req.params.column; - Movies.find(movie, function(error, found_movie) { + Movies.find_customers_by_history(movie, function(error, customers) { if(error) { var err = new Error("No such movie"); err.status = 404; next(err); } else { - Rentals.get_rental_ids(found_movie.id, function(error, rental_ids) { - if(error) { - var err = new Error("No such rental"); - err.status = 404; - next(err); - } else { - History.get_customer_ids(rental_ids, function(error, customer_ids) { - if(error) { - var err = new Error("No such history"); - err.status = 404; - next(err); - } else { - Customers.find(customer_ids, function(error, customers) { - if(error) { - var err = new Error("No such customers"); - err.status = 404; - next(err); - } else { - res.json(customers); - } - }) - } - }) - } - }) + res.json(customers); } }) + + // Movies.find(movie, function(error, found_movie) { + // if(error) { + // var err = new Error("No such movie"); + // err.status = 404; + // next(err); + // } else { + // Rentals.get_rental_ids(found_movie.id, function(error, rental_ids) { + // if(error) { + // var err = new Error("No such rental"); + // err.status = 404; + // next(err); + // } else { + // History.get_customer_ids(rental_ids, function(error, customer_ids) { + // if(error) { + // var err = new Error("No such history"); + // err.status = 404; + // next(err); + // } else { + // Customers.find(customer_ids, function(error, customers) { + // if(error) { + // var err = new Error("No such customers"); + // err.status = 404; + // next(err); + // } else { + // res.json(customers); + // } + // }) + // } + // }) + // } + // }) + // } + // }) } }; diff --git a/models/history.js b/models/history.js index 1d7cfdb1b..da4a44356 100644 --- a/models/history.js +++ b/models/history.js @@ -10,18 +10,4 @@ var History = function(history) { this.return_date = history.return_date; }; -History.get_customer_ids = function(rental_ids, callback) { - db.history.find({rental_id: rental_ids}, function(error, histories) { - if(error || !histories) { - callback(error || new Error("Could not retrieve histories"), undefined); - } else { - callback(null, histories.map(function(history) { - var history = new History(history); - return history.customer_id; - })); - } - }) -} - - module.exports = History; diff --git a/models/movies.js b/models/movies.js index b448c83f4..f272b735c 100644 --- a/models/movies.js +++ b/models/movies.js @@ -57,4 +57,16 @@ Movies.find_customers_by_title = function(title, callback) { }); } +Movies.find_customers_by_history = function(title, callback) { + db.run("select * from movies INNER JOIN rentals on movies.id=rentals.movie_id inner join history on rentals.id=history.rental_id inner join customers on history.customer_id=customers.id where title = $1", [title], function(error, customers) { + if(error || !customers) { + callback(error || new Error("Could not retrieve customers"), undefined); + } else { + callback(null, customers.map(function(customer) { + return new Customers(customer); + })); + }; + }); +} + module.exports = Movies; diff --git a/models/rentals.js b/models/rentals.js index 7ecbbf965..8419cf32e 100644 --- a/models/rentals.js +++ b/models/rentals.js @@ -22,18 +22,4 @@ Rentals.get_customer_ids = function(movie_id, callback) { }) } -Rentals.get_rental_ids = function(movie_id, callback) { - db.rentals.find({movie_id: movie_id}, function(error, rentals) { - if(error || !rentals) { - callback(error || new Error("Could not retrieve rentals"), undefined); - } else { - callback(null, rentals.map(function(rental) { - var rental = new Rentals(rental); - return rental.id; - })); - } - }) -} - - module.exports = Rentals; From 4caca95e214a3960389e91136ffcba6ff793e208 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Sat, 18 Jun 2016 19:19:45 -0700 Subject: [PATCH 047/118] only displays name, phone number, and account credit for each customer when hitting the movies/:title/current and movies/:title/history/... paths --- models/movies.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/models/movies.js b/models/movies.js index f272b735c..8bef04342 100644 --- a/models/movies.js +++ b/models/movies.js @@ -46,7 +46,7 @@ Movies.find = function(title, callback) { } Movies.find_customers_by_title = function(title, callback) { - db.run("select * from movies INNER JOIN rentals on movies.id=rentals.movie_id inner join customers on rentals.customer_id=customers.id where title = $1", [title], function(error, customers) { + db.run("select customers.name, customers.phone, customers.account_credit from movies INNER JOIN rentals on movies.id=rentals.movie_id inner join customers on rentals.customer_id=customers.id where title = $1", [title], function(error, customers) { if(error || !customers) { callback(error || new Error("Could not retrieve customers"), undefined); } else { @@ -58,7 +58,7 @@ Movies.find_customers_by_title = function(title, callback) { } Movies.find_customers_by_history = function(title, callback) { - db.run("select * from movies INNER JOIN rentals on movies.id=rentals.movie_id inner join history on rentals.id=history.rental_id inner join customers on history.customer_id=customers.id where title = $1", [title], function(error, customers) { + db.run("select customers.name, customers.phone, customers.account_credit from movies INNER JOIN rentals on movies.id=rentals.movie_id inner join history on rentals.id=history.rental_id inner join customers on history.customer_id=customers.id where title = $1", [title], function(error, customers) { if(error || !customers) { callback(error || new Error("Could not retrieve customers"), undefined); } else { From 484a56d9a8aad842b912beb6f36e98931d211367 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Sat, 18 Jun 2016 19:20:31 -0700 Subject: [PATCH 048/118] removed a bunch of commented-out code no longer in use --- controllers/movies_controller.js | 34 -------------------------------- 1 file changed, 34 deletions(-) diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index 8a5f74928..0e1d7c982 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -95,40 +95,6 @@ var MoviesController = { res.json(customers); } }) - - // Movies.find(movie, function(error, found_movie) { - // if(error) { - // var err = new Error("No such movie"); - // err.status = 404; - // next(err); - // } else { - // Rentals.get_rental_ids(found_movie.id, function(error, rental_ids) { - // if(error) { - // var err = new Error("No such rental"); - // err.status = 404; - // next(err); - // } else { - // History.get_customer_ids(rental_ids, function(error, customer_ids) { - // if(error) { - // var err = new Error("No such history"); - // err.status = 404; - // next(err); - // } else { - // Customers.find(customer_ids, function(error, customers) { - // if(error) { - // var err = new Error("No such customers"); - // err.status = 404; - // next(err); - // } else { - // res.json(customers); - // } - // }) - // } - // }) - // } - // }) - // } - // }) } }; From 225b42c7b4afe17da709ddea3bfe04052cf1d80c Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Sat, 18 Jun 2016 19:38:49 -0700 Subject: [PATCH 049/118] add functionality for sorting customer history information either by name of customer or checkout_date --- controllers/movies_controller.js | 9 ++++++++- models/movies.js | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index 459bdaaa1..e9fc397b4 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -85,8 +85,15 @@ var MoviesController = { history: function(req, res, next) { var movie = req.params.title; var column = req.params.column; + var order_by = 'order by customers.id'; - Movies.find_customers_by_history(movie, function(error, customers) { + if (column === 'name') { + order_by = 'order by customers.name'; + } else if (column === 'checkout_date') { + order_by = 'order by history.checkout_date'; + } + + Movies.find_customers_by_history(movie, order_by, function(error, customers) { if(error) { var err = new Error("No such movie"); err.status = 404; diff --git a/models/movies.js b/models/movies.js index 8bef04342..0cbb71791 100644 --- a/models/movies.js +++ b/models/movies.js @@ -57,8 +57,8 @@ Movies.find_customers_by_title = function(title, callback) { }); } -Movies.find_customers_by_history = function(title, callback) { - db.run("select customers.name, customers.phone, customers.account_credit from movies INNER JOIN rentals on movies.id=rentals.movie_id inner join history on rentals.id=history.rental_id inner join customers on history.customer_id=customers.id where title = $1", [title], function(error, customers) { +Movies.find_customers_by_history = function(title, order_by, callback) { + db.run("select customers.name, customers.phone, customers.account_credit from movies INNER JOIN rentals on movies.id=rentals.movie_id inner join history on rentals.id=history.rental_id inner join customers on history.customer_id=customers.id where title = $1" + order_by, [title], function(error, customers) { if(error || !customers) { callback(error || new Error("Could not retrieve customers"), undefined); } else { From 5b52bd99d2e2f022587fbd25d523687f841b940b Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Mon, 20 Jun 2016 10:58:16 -0700 Subject: [PATCH 050/118] added status codes to json responses for /movies/ routes --- controllers/movies_controller.js | 36 ++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index e9fc397b4..3baef7d72 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -11,7 +11,14 @@ var MoviesController = { err.status = 500; next(err); } else { - res.json(movies); + var obj = {}; + if (movies.length === 0) { + obj["status"] = 204; + } else { + obj["status"] = 200; + } + obj["movies"] = movies; + res.json(obj); // var locals = { accounts : accounts}; // res.render("accounts/index",locals); } @@ -37,7 +44,14 @@ var MoviesController = { err.status = 404; next(err); } else { - res.json(movies); + var obj = {}; + if (movies.length === 0) { + obj["status"] = 204; + } else { + obj["status"] = 200; + } + obj["movies"] = movies; + res.json(obj); } }); }, @@ -51,7 +65,14 @@ var MoviesController = { err.status = 404; next(err); } else { - res.json(customers); + var obj = {}; + if (customers.length === 0) { + obj["status"] = 204; + } else { + obj["status"] = 200; + } + obj["customers"] = customers; + res.json(obj); } }) @@ -99,7 +120,14 @@ var MoviesController = { err.status = 404; next(err); } else { - res.json(customers); + var obj = {}; + if (customers.length === 0) { + obj["status"] = 204; + } else { + obj["status"] = 200; + } + obj["customers"] = customers; + res.json(obj); } }) } From c40ce3474fd2bcfdd183ead3db15ae75bc538651 Mon Sep 17 00:00:00 2001 From: melissajimison Date: Mon, 20 Jun 2016 10:55:26 -0700 Subject: [PATCH 051/118] See a list of customers that have currently checked out any of the movie's inventory (/rentals/Jaws/customers) --- controllers/rentals_controller.js | 67 ++++++++++++++++++++++++------- models/rentals.js | 15 +++++++ routes/rentals.js | 3 ++ 3 files changed, 70 insertions(+), 15 deletions(-) diff --git a/controllers/rentals_controller.js b/controllers/rentals_controller.js index 8b5f2aa8d..5e091fc55 100644 --- a/controllers/rentals_controller.js +++ b/controllers/rentals_controller.js @@ -1,29 +1,66 @@ var Rentals = require('../models/rentals'); var Movies = require("../models/movies"); +var Customers = require("../models/customers"); var RentalsController = { -find_movie: function(req, res, next) { - var movie = req.params.title; + find_movie: function(req, res, next) { + var movie = req.params.title; - Movies.find(movie, function(error, found_movie) { + Movies.find(movie, function(error, found_movie) { + if(error) { + var err = new Error("No such movie"); + err.status = 404; + next(err); + } else { + obj = {} + obj['Synopsis'] = found_movie.overview; + obj['Release Date'] = found_movie.release_date; + obj['Total Inventory'] = found_movie.inventory; + Rentals.available(found_movie.id, function(err, number){ + obj['Available Inventory'] = number + res.json(obj); + }); + } + }); + }, + + find_customers: function(req, res, next) { + var movie = req.params.title; + + Movies.find(movie, function(error, found_movie) { if(error) { var err = new Error("No such movie"); err.status = 404; next(err); } else { - obj = {} - obj['Synopsis'] = found_movie.overview; - obj['Release Date'] = found_movie.release_date; - obj['Total Inventory'] = found_movie.inventory; - Rentals.available(found_movie.id, function(err, number){ - obj['Available Inventory'] = number - res.json(obj); - }); - } - }); -} + Rentals.get_customer_ids_of_rented(found_movie.id, function(error, customers_id) { + if(error) { + var err = new Error("No such movie"); + err.status = 404; + next(err); + } else { + Customers.find(customers_id, function(error, found_customers) { + if(error) { + var err = new Error("No such movie"); + err.status = 404; + next(err); + } else { + obj = {} + if (found_customers.length === 0) { + obj["status"] = 204 + } else { + obj["status"] = 200 + } + obj["customers"] = found_customers + res.json(obj) + } + }); + } + }); + }; + }) + } }; -// declaring as a javascript object module.exports = RentalsController; diff --git a/models/rentals.js b/models/rentals.js index f50176783..4957847ed 100644 --- a/models/rentals.js +++ b/models/rentals.js @@ -50,4 +50,19 @@ Rentals.available = function(movie_id, callback){ }); } +Rentals.get_customer_ids_of_rented = function(movie_id, callback) { + // key value that matches one of the column names in the rentals TABLE + // value is the specific value that we want to look for in the table + db.rentals.find({movie_id : movie_id, status: "rented"}, function(error, rentals) { + if(error || !rentals) { + callback(error || new Error("Could not retrieve your rentals"), undefined); + } else { + callback(null, rentals.map(function(rental) { + var rental = new Rentals(rental); + return rental.customer_id + })); + } + }); +}; + module.exports = Rentals; diff --git a/routes/rentals.js b/routes/rentals.js index 5747a379e..94013a8e4 100644 --- a/routes/rentals.js +++ b/routes/rentals.js @@ -8,4 +8,7 @@ router.get('/:title', RentalsController.find_movie); /* GET customers details. */ // router.get('/:id/current', RentalsController.current); +router.get('/:title/customers', RentalsController.find_customers); + + module.exports = router; From 74129c6001ab80e200078349781ee5c1a398a559 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Mon, 20 Jun 2016 12:42:57 -0700 Subject: [PATCH 052/118] moved sql code for finding customers that currently have a given movie checked out to a new sql file in the db folder --- db/sql/movies/currentCustomers.sql | 6 ++++++ models/movies.js | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 db/sql/movies/currentCustomers.sql diff --git a/db/sql/movies/currentCustomers.sql b/db/sql/movies/currentCustomers.sql new file mode 100644 index 000000000..a35bd16fd --- /dev/null +++ b/db/sql/movies/currentCustomers.sql @@ -0,0 +1,6 @@ +SELECT customers.name, customers.phone, customers.account_credit FROM movies + INNER JOIN rentals + ON movies.id=rentals.movie_id + INNER JOIN customers + ON rentals.customer_id=customers.id +WHERE title = $1 \ No newline at end of file diff --git a/models/movies.js b/models/movies.js index 0cbb71791..358a65e2d 100644 --- a/models/movies.js +++ b/models/movies.js @@ -46,7 +46,7 @@ Movies.find = function(title, callback) { } Movies.find_customers_by_title = function(title, callback) { - db.run("select customers.name, customers.phone, customers.account_credit from movies INNER JOIN rentals on movies.id=rentals.movie_id inner join customers on rentals.customer_id=customers.id where title = $1", [title], function(error, customers) { + db.sql.movies.currentCustomers([title], function(error, customers) { if(error || !customers) { callback(error || new Error("Could not retrieve customers"), undefined); } else { From 5eb45ae23be0d7927b6d3b446a018936d4b463ee Mon Sep 17 00:00:00 2001 From: Risha Date: Mon, 20 Jun 2016 12:52:03 -0700 Subject: [PATCH 053/118] added the history function --- models/history.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 models/history.js diff --git a/models/history.js b/models/history.js new file mode 100644 index 000000000..9d6386e77 --- /dev/null +++ b/models/history.js @@ -0,0 +1,24 @@ +var app = require("../app"); +var db = app.get("db"); + +// Constructor function +var History = function(history) { + this.id = history.id; + this.rental_id = history.rental_id; + this.customer_id = history.customer_id; + this.checkout_date = history.checkout_date; + this.return_date = history.return_date; + +}; + +History.find_by_customer_id = function(ids, callback) { + db.customers.find({id: ids}, function(error, customers) { + if(error || !customers) { + callback(error || new Error("Could not retrieve information"), undefined); + } else { + callback(null, customers.map(function(customer) { + return new History(customer); + })); + } + }); +}; From 8dc5e537880187f6be4665549a2e177aae64d2ff Mon Sep 17 00:00:00 2001 From: melissajimison Date: Mon, 20 Jun 2016 12:51:55 -0700 Subject: [PATCH 054/118] change the name of the controller for one more semantic current_customers --- controllers/rentals_controller.js | 2 +- routes/rentals.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/controllers/rentals_controller.js b/controllers/rentals_controller.js index 5e091fc55..14808cc96 100644 --- a/controllers/rentals_controller.js +++ b/controllers/rentals_controller.js @@ -25,7 +25,7 @@ var RentalsController = { }); }, - find_customers: function(req, res, next) { + current_customers: function(req, res, next) { var movie = req.params.title; Movies.find(movie, function(error, found_movie) { diff --git a/routes/rentals.js b/routes/rentals.js index 94013a8e4..e0b3939bd 100644 --- a/routes/rentals.js +++ b/routes/rentals.js @@ -8,7 +8,7 @@ router.get('/:title', RentalsController.find_movie); /* GET customers details. */ // router.get('/:id/current', RentalsController.current); -router.get('/:title/customers', RentalsController.find_customers); +router.get('/:title/customers', RentalsController.current_customers); module.exports = router; From ace87c3fc58536d85b2c320c8658bdb879c93c16 Mon Sep 17 00:00:00 2001 From: melissajimison Date: Mon, 20 Jun 2016 13:14:29 -0700 Subject: [PATCH 055/118] refactor find_customers_by_title --- controllers/rentals_controller.js | 44 +++++++++-------------------- db/sql/rentals/currentCustomers.sql | 6 ++++ models/rentals.js | 14 +++++++++ 3 files changed, 34 insertions(+), 30 deletions(-) create mode 100644 db/sql/rentals/currentCustomers.sql diff --git a/controllers/rentals_controller.js b/controllers/rentals_controller.js index 14808cc96..2731aa241 100644 --- a/controllers/rentals_controller.js +++ b/controllers/rentals_controller.js @@ -28,37 +28,21 @@ var RentalsController = { current_customers: function(req, res, next) { var movie = req.params.title; - Movies.find(movie, function(error, found_movie) { - if(error) { - var err = new Error("No such movie"); - err.status = 404; - next(err); - } else { - Rentals.get_customer_ids_of_rented(found_movie.id, function(error, customers_id) { - if(error) { - var err = new Error("No such movie"); - err.status = 404; - next(err); + Rentals.find_customers_by_title(movie, function(error, customers) { + if(error) { + var err = new Error("No such movie"); + err.status = 404; + next(err); + } else { + var obj = {}; + if (customers.length === 0) { + obj["status"] = 204; } else { - Customers.find(customers_id, function(error, found_customers) { - if(error) { - var err = new Error("No such movie"); - err.status = 404; - next(err); - } else { - obj = {} - if (found_customers.length === 0) { - obj["status"] = 204 - } else { - obj["status"] = 200 - } - obj["customers"] = found_customers - res.json(obj) - } - }); - } - }); - }; + obj["status"] = 200; + } + obj["customers"] = customers; + res.json(obj); + } }) } }; diff --git a/db/sql/rentals/currentCustomers.sql b/db/sql/rentals/currentCustomers.sql new file mode 100644 index 000000000..0b18d4f78 --- /dev/null +++ b/db/sql/rentals/currentCustomers.sql @@ -0,0 +1,6 @@ +SELECT * FROM movies + INNER JOIN rentals + ON movies.id=rentals.movie_id + INNER JOIN customers + ON rentals.customer_id=customers.id +WHERE title = $1 diff --git a/models/rentals.js b/models/rentals.js index 4957847ed..00cdad901 100644 --- a/models/rentals.js +++ b/models/rentals.js @@ -1,5 +1,6 @@ var app = require("../app"); var db = app.get("db"); +var Customers = require('../models/customers'); // Constructor function var Rentals = function(rental) { @@ -65,4 +66,17 @@ Rentals.get_customer_ids_of_rented = function(movie_id, callback) { }); }; +//Melissa is using this. dont delete +Rentals.find_customers_by_title = function(title, callback) { + db.sql.rentals.currentCustomers([title], function(error, customers) { + if(error || !customers) { + callback(error || new Error("Could not retrieve customers"), undefined); + } else { + callback(null, customers.map(function(customer) { + return new Customers(customer); + })); + }; + }); +} + module.exports = Rentals; From fdcebcc4401f40b7e53b5056a6bd9a7362ae0e6e Mon Sep 17 00:00:00 2001 From: melissajimison Date: Mon, 20 Jun 2016 13:24:51 -0700 Subject: [PATCH 056/118] refactor the object reteurned in /rentals/Jaws. The status is included and the movie info is an object --- controllers/rentals_controller.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/controllers/rentals_controller.js b/controllers/rentals_controller.js index 2731aa241..92c09f09a 100644 --- a/controllers/rentals_controller.js +++ b/controllers/rentals_controller.js @@ -14,11 +14,13 @@ var RentalsController = { next(err); } else { obj = {} - obj['Synopsis'] = found_movie.overview; - obj['Release Date'] = found_movie.release_date; - obj['Total Inventory'] = found_movie.inventory; + obj['status'] = 200; + obj['Movie Info'] = {}; + obj['Movie Info']['Synopsis'] = found_movie.overview; + obj['Movie Info']['Release Date'] = found_movie.release_date; + obj['Movie Info']['Total Inventory'] = found_movie.inventory; Rentals.available(found_movie.id, function(err, number){ - obj['Available Inventory'] = number + obj['Movie Info']['Available Inventory'] = number res.json(obj); }); } From 1a5a585d37da32877d3cf9b533673cb83fc5b007 Mon Sep 17 00:00:00 2001 From: melissajimison Date: Mon, 20 Jun 2016 14:26:59 -0700 Subject: [PATCH 057/118] set istanbul --- .gitignore | 1 + package.json | 3 ++- spec/controllers/customers.spec.js | 12 ++++++------ spec/controllers/movies.spec.js | 2 +- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 879df49d1..8c71495a7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .DS_Store node_modules/ *.log +coverage/ diff --git a/package.json b/package.json index 8b1f9d715..18c61b42b 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "private": true, "scripts": { "start": "nodemon ./bin/www", - "test": "clear; jasmine-node --verbose spec/", + "test": "clear; ./node_modules/.bin/istanbul cover --include-all-sources ./node_modules/.bin/jasmine-node --captureExceptions --verbose spec/", "db:drop": "dropdb videoStore", "db:create": "createdb videoStore", "db:schema": "node tasks/load_schema.js", @@ -23,6 +23,7 @@ "serve-favicon": "~2.3.0" }, "devDependencies": { + "istanbul": "^0.4.4", "jasmine-node": "^1.14.5", "nodemon": "^1.9.2", "request": "^2.72.0" diff --git a/spec/controllers/customers.spec.js b/spec/controllers/customers.spec.js index c00e9d175..3cd313909 100644 --- a/spec/controllers/customers.spec.js +++ b/spec/controllers/customers.spec.js @@ -6,10 +6,10 @@ describe("Endpoint at /", function () { return base_url + "customers" + endpoint; }; - it('responds with a 200 status code', function (done) { - request.get(url('/'), function(error, response, body) { - expect(response.statusCode).toEqual(200); - done(); - }); - }); + // it('responds with a 200 status code', function (done) { + // request.get(url('/'), function(error, response, body) { + // expect(response.statusCode).toEqual(200); + // done(); + // }); + // }); }); diff --git a/spec/controllers/movies.spec.js b/spec/controllers/movies.spec.js index 67fcd26ae..e53f32307 100644 --- a/spec/controllers/movies.spec.js +++ b/spec/controllers/movies.spec.js @@ -35,7 +35,7 @@ describe("Endpoint at /sort", function () { it('responds with a 200 status code for mispelling', function (done) { request.get(url('/released-date?n=10&p=1'), function(error, response, body) { - expect(response.statusCode).toEqual(200); + expect(response.statusCode).toEqual(404); done(); }); }); From f766f18c6bd797134e4d7d6c2c944db330658f04 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Mon, 20 Jun 2016 15:25:11 -0700 Subject: [PATCH 058/118] removed history model file (not being used) --- controllers/movies_controller.js | 43 +++++------------------- db/sql/movies/currentCustomers.sql | 2 +- db/sql/movies/historyCustomersByDate.sql | 9 +++++ db/sql/movies/historyCustomersByName.sql | 9 +++++ models/history.js | 13 ------- models/movies.js | 30 ++++++++++++----- package.json | 2 +- spec/controllers/customers.spec.js | 12 +++---- 8 files changed, 55 insertions(+), 65 deletions(-) create mode 100644 db/sql/movies/historyCustomersByDate.sql create mode 100644 db/sql/movies/historyCustomersByName.sql delete mode 100644 models/history.js diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index 3baef7d72..9f73ad296 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -1,14 +1,13 @@ var Movies = require("../models/movies"); var Rentals = require('../models/rentals'); var Customers = require('../models/customers'); -var History = require('../models/history'); var MoviesController = { index: function(req, res, next) { Movies.all(function(error, movies) { // this is a callback if(error) { var err = new Error("Error retrieving movies list:\n" + error.message); - err.status = 500; + err.status = 404; next(err); } else { var obj = {}; @@ -19,9 +18,7 @@ var MoviesController = { } obj["movies"] = movies; res.json(obj); - // var locals = { accounts : accounts}; - // res.render("accounts/index",locals); - } + } }); }, @@ -75,48 +72,24 @@ var MoviesController = { res.json(obj); } }) - - // Movies.find(movie, function(error, found_movie) { - // if(error) { - // var err = new Error("No such movie"); - // err.status = 404; - // next(err); - // } else { - // Rentals.get_customer_ids(found_movie.id, function(error, customer_ids) { - // if(error) { - // var err = new Error("No such rentals"); - // err.status = 404; - // next(err); - // } else { - // Customers.find(customer_ids, function(error, customers) { - // if(error) { - // var err = new Error("No such customers"); - // err.status = 404; - // next(err); - // } else { - // res.json(customers); - // } - // }) - // } - // }) - // } - // }) }, history: function(req, res, next) { var movie = req.params.title; var column = req.params.column; - var order_by = 'order by customers.id'; + var order_by; if (column === 'name') { - order_by = 'order by customers.name'; + order_by = 'name'; } else if (column === 'checkout_date') { - order_by = 'order by history.checkout_date'; + order_by = 'checkout_date'; } + console.log(order_by); + Movies.find_customers_by_history(movie, order_by, function(error, customers) { if(error) { - var err = new Error("No such movie"); + var err = new Error("No such movie: " + error.message); err.status = 404; next(err); } else { diff --git a/db/sql/movies/currentCustomers.sql b/db/sql/movies/currentCustomers.sql index a35bd16fd..041bf00d3 100644 --- a/db/sql/movies/currentCustomers.sql +++ b/db/sql/movies/currentCustomers.sql @@ -3,4 +3,4 @@ SELECT customers.name, customers.phone, customers.account_credit FROM movies ON movies.id=rentals.movie_id INNER JOIN customers ON rentals.customer_id=customers.id -WHERE title = $1 \ No newline at end of file +WHERE title = $1; \ No newline at end of file diff --git a/db/sql/movies/historyCustomersByDate.sql b/db/sql/movies/historyCustomersByDate.sql new file mode 100644 index 000000000..9d69cefa5 --- /dev/null +++ b/db/sql/movies/historyCustomersByDate.sql @@ -0,0 +1,9 @@ +SELECT DISTINCT customers.id, customers.name, customers.phone, customers.account_credit, history.checkout_date FROM movies + INNER JOIN rentals + ON movies.id=rentals.movie_id + INNER JOIN history + ON rentals.id=history.rental_id + INNER JOIN customers + ON history.customer_id=customers.id +WHERE title = $1 +ORDER BY history.checkout_date; diff --git a/db/sql/movies/historyCustomersByName.sql b/db/sql/movies/historyCustomersByName.sql new file mode 100644 index 000000000..8d3182391 --- /dev/null +++ b/db/sql/movies/historyCustomersByName.sql @@ -0,0 +1,9 @@ +SELECT DISTINCT customers.id, customers.name, customers.phone, customers.account_credit, history.checkout_date FROM movies + INNER JOIN rentals + ON movies.id=rentals.movie_id + INNER JOIN history + ON rentals.id=history.rental_id + INNER JOIN customers + ON history.customer_id=customers.id +WHERE title = $1 +ORDER BY customers.name; diff --git a/models/history.js b/models/history.js deleted file mode 100644 index da4a44356..000000000 --- a/models/history.js +++ /dev/null @@ -1,13 +0,0 @@ -var app = require("../app"); -var db = app.get("db"); - -// Constructor function -var History = function(history) { - this.id = history.id; - this.rental_id = history.rental_id; - this.customer_id = history.customer_id; - this.checkout_date = history.checkout_date; - this.return_date = history.return_date; -}; - -module.exports = History; diff --git a/models/movies.js b/models/movies.js index 358a65e2d..686cfc1d7 100644 --- a/models/movies.js +++ b/models/movies.js @@ -58,15 +58,27 @@ Movies.find_customers_by_title = function(title, callback) { } Movies.find_customers_by_history = function(title, order_by, callback) { - db.run("select customers.name, customers.phone, customers.account_credit from movies INNER JOIN rentals on movies.id=rentals.movie_id inner join history on rentals.id=history.rental_id inner join customers on history.customer_id=customers.id where title = $1" + order_by, [title], function(error, customers) { - if(error || !customers) { - callback(error || new Error("Could not retrieve customers"), undefined); - } else { - callback(null, customers.map(function(customer) { - return new Customers(customer); - })); - }; - }); + if (order_by === 'name') { + db.sql.movies.historyCustomersByName(title, function(error, customers) { + if(error || !customers) { + callback(error || new Error("Could not retrieve customers"), undefined); + } else { + callback(null, customers.map(function(customer) { + return new Customers(customer); + })); + }; + }); + } else if (order_by === 'checkout_date') { + db.sql.movies.historyCustomersByDate(title, function(error, customers) { + if(error || !customers) { + callback(error || new Error("Could not retrieve customers"), undefined); + } else { + callback(null, customers.map(function(customer) { + return new Customers(customer); + })); + }; + }); + } } module.exports = Movies; diff --git a/package.json b/package.json index 8b1f9d715..57a4f0a8c 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "private": true, "scripts": { "start": "nodemon ./bin/www", - "test": "clear; jasmine-node --verbose spec/", + "test": "clear; jasmine-node --captureExceptions --verbose spec/", "db:drop": "dropdb videoStore", "db:create": "createdb videoStore", "db:schema": "node tasks/load_schema.js", diff --git a/spec/controllers/customers.spec.js b/spec/controllers/customers.spec.js index c00e9d175..3cd313909 100644 --- a/spec/controllers/customers.spec.js +++ b/spec/controllers/customers.spec.js @@ -6,10 +6,10 @@ describe("Endpoint at /", function () { return base_url + "customers" + endpoint; }; - it('responds with a 200 status code', function (done) { - request.get(url('/'), function(error, response, body) { - expect(response.statusCode).toEqual(200); - done(); - }); - }); + // it('responds with a 200 status code', function (done) { + // request.get(url('/'), function(error, response, body) { + // expect(response.statusCode).toEqual(200); + // done(); + // }); + // }); }); From f343231d7b099dc224c9dc8242f3ec80bf4c93b9 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Mon, 20 Jun 2016 15:26:50 -0700 Subject: [PATCH 059/118] added SQL files that have the inner join scripts to get customer information from movie titles --- db/sql/movies/currentCustomers.sql | 2 +- db/sql/movies/historyCustomersByDate.sql | 2 +- db/sql/movies/historyCustomersByName.sql | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/db/sql/movies/currentCustomers.sql b/db/sql/movies/currentCustomers.sql index 041bf00d3..7c30436f9 100644 --- a/db/sql/movies/currentCustomers.sql +++ b/db/sql/movies/currentCustomers.sql @@ -3,4 +3,4 @@ SELECT customers.name, customers.phone, customers.account_credit FROM movies ON movies.id=rentals.movie_id INNER JOIN customers ON rentals.customer_id=customers.id -WHERE title = $1; \ No newline at end of file +WHERE title = $1; diff --git a/db/sql/movies/historyCustomersByDate.sql b/db/sql/movies/historyCustomersByDate.sql index 9d69cefa5..eb3374076 100644 --- a/db/sql/movies/historyCustomersByDate.sql +++ b/db/sql/movies/historyCustomersByDate.sql @@ -1,5 +1,5 @@ SELECT DISTINCT customers.id, customers.name, customers.phone, customers.account_credit, history.checkout_date FROM movies - INNER JOIN rentals + INNER JOIN rentals ON movies.id=rentals.movie_id INNER JOIN history ON rentals.id=history.rental_id diff --git a/db/sql/movies/historyCustomersByName.sql b/db/sql/movies/historyCustomersByName.sql index 8d3182391..a662ae3b2 100644 --- a/db/sql/movies/historyCustomersByName.sql +++ b/db/sql/movies/historyCustomersByName.sql @@ -1,5 +1,5 @@ SELECT DISTINCT customers.id, customers.name, customers.phone, customers.account_credit, history.checkout_date FROM movies - INNER JOIN rentals + INNER JOIN rentals ON movies.id=rentals.movie_id INNER JOIN history ON rentals.id=history.rental_id From 7048b8f852be045c69f401582bb220cc83aef589 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Mon, 20 Jun 2016 15:32:46 -0700 Subject: [PATCH 060/118] call .sql files in db/sql/movies instead of having SQL hard-coded (forgot to commit - belongs with the last 2 commits) --- models/movies.js | 1 + 1 file changed, 1 insertion(+) diff --git a/models/movies.js b/models/movies.js index 686cfc1d7..e52bf1825 100644 --- a/models/movies.js +++ b/models/movies.js @@ -81,4 +81,5 @@ Movies.find_customers_by_history = function(title, order_by, callback) { } } + module.exports = Movies; From 4b9eac0a22d0ec6f7b7d44cec68703f2f2336e80 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Mon, 20 Jun 2016 16:45:18 -0700 Subject: [PATCH 061/118] refactored to DRY up callbacks --- models/movies.js | 71 ++++++++++++++++++------------------------------ 1 file changed, 27 insertions(+), 44 deletions(-) diff --git a/models/movies.js b/models/movies.js index e52bf1825..1474480f5 100644 --- a/models/movies.js +++ b/models/movies.js @@ -12,27 +12,11 @@ var Movies = function(movie) { }; Movies.all = function(callback) { - db.movies.find(function(error, movies) { - if(error || !movies) { - callback(error || new Error("Could not retrieve movies"), undefined); - } else { - callback(null, movies.map(function(movie) { - return new Movies(movie); - })); - } - }); + db.movies.find(movies_callback(callback)); }; Movies.sort_by_day = function(options, callback) { - db.movies.find({}, options, function(error, movies){ - if(error || !movies) { - callback(error || new Error("Could not retrieve movies"), undefined); - } else { - callback(null, movies.map(function(movie) { - return new Movies(movie); - })); - } - }); + db.movies.find({}, options, movies_callback(callback)); }; Movies.find = function(title, callback) { @@ -46,40 +30,39 @@ Movies.find = function(title, callback) { } Movies.find_customers_by_title = function(title, callback) { - db.sql.movies.currentCustomers([title], function(error, customers) { + db.sql.movies.currentCustomers([title], customer_callback(callback)); +} + +Movies.find_customers_by_history = function(title, order_by, callback) { + if (order_by === 'name') { + db.sql.movies.historyCustomersByName(title, customer_callback(callback)); + } else if (order_by === 'checkout_date') { + db.sql.movies.historyCustomersByDate(title, customer_callback(callback)); + } +} + +function customer_callback(passed_callback) { + return function(error, customers) { if(error || !customers) { - callback(error || new Error("Could not retrieve customers"), undefined); + passed_callback(error || new Error("Could not retrieve customers"), undefined); } else { - callback(null, customers.map(function(customer) { + passed_callback(null, customers.map(function(customer) { return new Customers(customer); })); }; - }); + } } -Movies.find_customers_by_history = function(title, order_by, callback) { - if (order_by === 'name') { - db.sql.movies.historyCustomersByName(title, function(error, customers) { - if(error || !customers) { - callback(error || new Error("Could not retrieve customers"), undefined); - } else { - callback(null, customers.map(function(customer) { - return new Customers(customer); - })); - }; - }); - } else if (order_by === 'checkout_date') { - db.sql.movies.historyCustomersByDate(title, function(error, customers) { - if(error || !customers) { - callback(error || new Error("Could not retrieve customers"), undefined); - } else { - callback(null, customers.map(function(customer) { - return new Customers(customer); - })); - }; - }); +function movies_callback(passed_callback) { + return function(error, movies) { + if(error || !movies) { + passed_callback(error || new Error("Could not retrieve movies"), undefined); + } else { + passed_callback(null, movies.map(function(movie) { + return new Movies(movie); + })); + } } } - module.exports = Movies; From 2f2277e66774c985fed4fb144de05a9634918ec0 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Mon, 20 Jun 2016 17:11:02 -0700 Subject: [PATCH 062/118] changed method name from sort_by_day to sort_by since it can also sort by title --- controllers/movies_controller.js | 2 +- models/movies.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index 9f73ad296..f0e85a331 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -35,7 +35,7 @@ var MoviesController = { }; // products ordered in descending fashion - Movies.sort_by_day(options, function(error, movies) { // this is a callback + Movies.sort_by(options, function(error, movies) { // this is a callback if(error) { var err = new Error("No such movie"); err.status = 404; diff --git a/models/movies.js b/models/movies.js index 1474480f5..485797079 100644 --- a/models/movies.js +++ b/models/movies.js @@ -15,7 +15,7 @@ Movies.all = function(callback) { db.movies.find(movies_callback(callback)); }; -Movies.sort_by_day = function(options, callback) { +Movies.sort_by = function(options, callback) { db.movies.find({}, options, movies_callback(callback)); }; From 20ebb108461ddcd895b10d7f50ab2c3452895b75 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Mon, 20 Jun 2016 17:11:13 -0700 Subject: [PATCH 063/118] tests for movies model --- spec/models/movies.spec.js | 81 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 spec/models/movies.spec.js diff --git a/spec/models/movies.spec.js b/spec/models/movies.spec.js new file mode 100644 index 000000000..af9f69335 --- /dev/null +++ b/spec/models/movies.spec.js @@ -0,0 +1,81 @@ +var app = require("../../app"); +var db = app.get("db"); + +var Movies = require('../../models/movies'); + +describe('Movies', function () { + var options_release = { + limit : 5, + order : 'release_date', + offset: 1 + }; + + var options_title = { + limit : 5, + order : 'title', + offset: 1 + }; + + afterEach(function () { + // delete all the accounts I created + db.end(); + }); + + describe('#all', function () { + it('should return an array of 100 Movie instances', function (done) { + Movies.all(function (error, movies) { + expect(error).toBeNull; + expect(movies).toEqual(jasmine.any(Array)); + expect(movies.length).toEqual(100); + done(); + }); + }); + }); + + describe('#sort_by', function () { + + it('should return an array of 5 Movies if limit is set to 5', function (done) { + Movies.sort_by(options_release, function (error, movies) { + expect(error).toBeNull; + expect(movies).toEqual(jasmine.any(Array)); + expect(movies.length).toEqual(5); + done(); + }); + }); + + it('should return The Phantom of the Opera first when sorted by release_date', function (done) { + Movies.sort_by(options_release, function (error, movies) { + expect(error).toBeNull; + expect(movies[0].title).toEqual("The Phantom of the Opera"); + expect(movies[0].id).toEqual(83); + done(); + }); + }); + + + it('should return 2001: A Space Odyssey first when sorted by title', function (done) { + Movies.sort_by(options_title, function (error, movies) { + expect(error).toBeNull; + expect(movies[0].title).toEqual("2001: A Space Odyssey"); + expect(movies[0].id).toEqual(40); + done(); + }) + }) + }); + + describe('#find', function () { + it('should return a Movie', function (done) { + Movies.find("Jaws", function (error, movie) { + expect(error).toBeNull; + expect(movie.title).toEqual("Jaws"); + expect(movie.release_date).toEqual("1975-06-19"); + expect(movie.inventory).toEqual(6); + expect(movie.overview).toEqual("An insatiable great white shark terrorizes the townspeople of Amity Island, The police chief, an oceanographer and a grizzled shark hunter seek to destroy the bloodthirsty beast."); + done(); + }); + }); + }); + + +}); + From 465cb2f8f9328c08503090b22cda56f5b66f0e65 Mon Sep 17 00:00:00 2001 From: melissajimison Date: Mon, 20 Jun 2016 15:00:31 -0700 Subject: [PATCH 064/118] added basic controller tests --- spec/controllers/rentals.spec.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 spec/controllers/rentals.spec.js diff --git a/spec/controllers/rentals.spec.js b/spec/controllers/rentals.spec.js new file mode 100644 index 000000000..31a7a8c9a --- /dev/null +++ b/spec/controllers/rentals.spec.js @@ -0,0 +1,31 @@ +var request = require('request'); +var base_url = "http://localhost:3000/"; + + +describe("Endpoint at /Jaws", function () { + var url = function(endpoint) { + return base_url + "rentals" + endpoint; + }; + + it('responds with a 200 status code for succesful request', function (done) { + request.get(url('/Jaws'), function(error, response, body) { + expect(response.statusCode).toEqual(200); + done(); + }); + }); + + it('responds with a 204 status code for no data', function (done) { + request.get(url('/Titanic'), function(error, response, body) { + expect(response.statusCode).toEqual(200); + done(); + }); + }); + + it('responds with a 404 status code for bad request', function (done) { + request.get(url('/Melissa'), function(error, response, body) { + expect(response.statusCode).toEqual(404); + done(); + }); + }); + +}); From 1f9ebcc284aad0eb97e0e257c68fbef3bf6942c9 Mon Sep 17 00:00:00 2001 From: melissajimison Date: Mon, 20 Jun 2016 15:06:42 -0700 Subject: [PATCH 065/118] deleted unsused get_customer_ids --- models/rentals.js | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/models/rentals.js b/models/rentals.js index 00cdad901..5a375acb8 100644 --- a/models/rentals.js +++ b/models/rentals.js @@ -28,19 +28,6 @@ Rentals.find_by_customer = function(customer_id, callback) { // when movies are returned the customer id will be deleted from the rentals table -Rentals.get_customer_ids = function(movie_id, callback) { - db.rentals.find({movie_id: movie_id}, function(error, rentals) { - if(error || !rentals) { - callback(error || new Error("Could not retrieve rentals"), undefined); - } else { - callback(null, rentals.map(function(rental) { - var rental = new Rentals(rental); - return rental.customer_id; - })); - } - }) -} - Rentals.available = function(movie_id, callback){ db.rentals.find({movie_id: movie_id}, function(error, rentals) { if(error || !rentals) { From fad591e88800a7ee680a607e387289d32f743ea0 Mon Sep 17 00:00:00 2001 From: melissajimison Date: Mon, 20 Jun 2016 17:12:14 -0700 Subject: [PATCH 066/118] fixed bug tht calculates the copies available --- models/rentals.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/rentals.js b/models/rentals.js index 5a375acb8..5f2d32efb 100644 --- a/models/rentals.js +++ b/models/rentals.js @@ -29,7 +29,7 @@ Rentals.find_by_customer = function(customer_id, callback) { // when movies are returned the customer id will be deleted from the rentals table Rentals.available = function(movie_id, callback){ - db.rentals.find({movie_id: movie_id}, function(error, rentals) { + db.rentals.find({movie_id : movie_id, status: "available"}, function(error, rentals) { if(error || !rentals) { callback(error || new Error("Could not retrieve rentals"), undefined); } else { From 150c12538d4d34e879fd019ad3cf4d8b9695a2e1 Mon Sep 17 00:00:00 2001 From: melissajimison Date: Mon, 20 Jun 2016 17:12:42 -0700 Subject: [PATCH 067/118] ettempted to write some model tests --- spec/controllers/rentals.spec.js | 2 +- spec/rentals.spec.js | 39 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 spec/rentals.spec.js diff --git a/spec/controllers/rentals.spec.js b/spec/controllers/rentals.spec.js index 31a7a8c9a..cf33dca1a 100644 --- a/spec/controllers/rentals.spec.js +++ b/spec/controllers/rentals.spec.js @@ -2,7 +2,7 @@ var request = require('request'); var base_url = "http://localhost:3000/"; -describe("Endpoint at /Jaws", function () { +describe("Endpoint at /", function () { var url = function(endpoint) { return base_url + "rentals" + endpoint; }; diff --git a/spec/rentals.spec.js b/spec/rentals.spec.js new file mode 100644 index 000000000..d8c8f2ad0 --- /dev/null +++ b/spec/rentals.spec.js @@ -0,0 +1,39 @@ +var app = require("../../app"); +var db = app.get("db"); +var Account = require('../../models/account') + +describe('Account', function () { + var rental1 + beforeEach(function (done) { + rental = { + movie_id : 42, + customer_id: 45, + status : 'rented' + } + rental1 = db.movies.saveSync(rental); + }) +}) + +afterEach(function () { + // delete all the accounts I created + db.end() +}) + +describe('#available', function () { + it('should return number of rentals available', function (done) { + rental1.available(32, function (error, aval) { + expect(error).toBeNull + expect(aval).toEqual(5) + done() + }) + }) + + // it('should not break, I guess', function (done) { + // rental1.getBalance(function (error, balance) { + // expect(error).toBeNull + // expect(balance).toEqual('$100.00') + // done() + // }) + // }) + }) +}) From a32d54030ca6aca2d235dc8f9c938cc98a769941 Mon Sep 17 00:00:00 2001 From: melissajimison Date: Mon, 20 Jun 2016 19:00:05 -0700 Subject: [PATCH 068/118] added file to the models directory, fixed the test --- spec/models/rentals.spec.js | 28 ++++++++++++++++++++++++++ spec/rentals.spec.js | 39 ------------------------------------- 2 files changed, 28 insertions(+), 39 deletions(-) create mode 100644 spec/models/rentals.spec.js delete mode 100644 spec/rentals.spec.js diff --git a/spec/models/rentals.spec.js b/spec/models/rentals.spec.js new file mode 100644 index 000000000..7960cde0f --- /dev/null +++ b/spec/models/rentals.spec.js @@ -0,0 +1,28 @@ +var app = require("../../app"); +var db = app.get("db"); +var Rentals = require('../../models/rentals') + +afterEach(function () { + // delete all the accounts I created + db.end() +}) + +describe('#available', function () { + it('should return number of rentals available', function (done) { + var movie_id = 33; + Rentals.available(movie_id, function (error, aval) { + expect(error).toBeNull + expect(aval).toEqual(9) + done() + }) + }) + + it('should return number of rentals available', function (done) { + var movie_id = 2; + Rentals.available(movie_id, function (error, aval) { + expect(error).toBeNull + expect(aval).toEqual(4) + done() + }) + }) +}) diff --git a/spec/rentals.spec.js b/spec/rentals.spec.js deleted file mode 100644 index d8c8f2ad0..000000000 --- a/spec/rentals.spec.js +++ /dev/null @@ -1,39 +0,0 @@ -var app = require("../../app"); -var db = app.get("db"); -var Account = require('../../models/account') - -describe('Account', function () { - var rental1 - beforeEach(function (done) { - rental = { - movie_id : 42, - customer_id: 45, - status : 'rented' - } - rental1 = db.movies.saveSync(rental); - }) -}) - -afterEach(function () { - // delete all the accounts I created - db.end() -}) - -describe('#available', function () { - it('should return number of rentals available', function (done) { - rental1.available(32, function (error, aval) { - expect(error).toBeNull - expect(aval).toEqual(5) - done() - }) - }) - - // it('should not break, I guess', function (done) { - // rental1.getBalance(function (error, balance) { - // expect(error).toBeNull - // expect(balance).toEqual('$100.00') - // done() - // }) - // }) - }) -}) From 1be5b991b1471019eecd1838ca7cea3087c906c7 Mon Sep 17 00:00:00 2001 From: melissajimison Date: Mon, 20 Jun 2016 20:09:42 -0700 Subject: [PATCH 069/118] added more models test and extended the controllers tests --- spec/controllers/rentals.spec.js | 34 +++++++++++++++++------ spec/models/rentals.spec.js | 46 +++++++++++++++++++++++++++++--- 2 files changed, 68 insertions(+), 12 deletions(-) diff --git a/spec/controllers/rentals.spec.js b/spec/controllers/rentals.spec.js index cf33dca1a..e8820f10a 100644 --- a/spec/controllers/rentals.spec.js +++ b/spec/controllers/rentals.spec.js @@ -9,23 +9,41 @@ describe("Endpoint at /", function () { it('responds with a 200 status code for succesful request', function (done) { request.get(url('/Jaws'), function(error, response, body) { - expect(response.statusCode).toEqual(200); + var body_parsed = JSON.parse(body) + expect(response.statusCode).toEqual(200) + expect(typeof body).toEqual("string") + expect(typeof body_parsed).toEqual("object") + expect(body_parsed["status"]).toEqual(200) + expect(body_parsed["Movie Info"]["Synopsis"]).toEqual(jasmine.any(String)) + expect(body_parsed["Movie Info"]["Release Date"]).toEqual("1975-06-19") + expect(body_parsed["Movie Info"]["Total Inventory"]).toEqual(6) + expect(body_parsed["Movie Info"]["Available Inventory"]).toEqual(4) done(); }); }); it('responds with a 204 status code for no data', function (done) { request.get(url('/Titanic'), function(error, response, body) { - expect(response.statusCode).toEqual(200); + var body_parsed = JSON.parse(body) + expect(response.statusCode).toEqual(200) + expect(typeof body).toEqual("string") + expect(typeof body_parsed).toEqual("object") + expect(body_parsed["Movie Info"]["Synopsis"]).toEqual(jasmine.any(String)) + expect(body_parsed["Movie Info"]["Release Date"]).toEqual("1997-12-19") + expect(body_parsed["Movie Info"]["Total Inventory"]).toEqual(5) + expect(body_parsed["Movie Info"]["Available Inventory"]).toEqual(5) done(); }); }); - it('responds with a 404 status code for bad request', function (done) { - request.get(url('/Melissa'), function(error, response, body) { - expect(response.statusCode).toEqual(404); - done(); - }); - }); + // it('responds with a 404 status code for bad request', function (done) { + // request.get(url('/Melissa'), function(error, response, body) { + // var body_parsed = JSON.parse(body) + // expect(response.statusCode).toEqual(200) + // expect(typeof body).toEqual("string") + // expect(typeof body_parsed).toEqual("object") + // done(); + // }); + // }); }); diff --git a/spec/models/rentals.spec.js b/spec/models/rentals.spec.js index 7960cde0f..6700dda57 100644 --- a/spec/models/rentals.spec.js +++ b/spec/models/rentals.spec.js @@ -10,18 +10,56 @@ afterEach(function () { describe('#available', function () { it('should return number of rentals available', function (done) { var movie_id = 33; - Rentals.available(movie_id, function (error, aval) { + Rentals.available(movie_id, function (error, costumers) { expect(error).toBeNull - expect(aval).toEqual(9) + expect(costumers).toEqual(9) done() }) }) it('should return number of rentals available', function (done) { var movie_id = 2; - Rentals.available(movie_id, function (error, aval) { + Rentals.available(movie_id, function (error, result) { expect(error).toBeNull - expect(aval).toEqual(4) + expect(result).toEqual(4) + done() + }) + }) +}) + +describe('#find_customers_by_title', function () { + it('should return custumers info with a givin title', function (done) { + var title = "Jaws"; + Rentals.find_customers_by_title(title, function (error, costumers) { + expect(error).toBeNull + expect(Array.isArray(costumers)).toEqual(true) + expect(typeof costumers[0]).toEqual('object') + expect(costumers[0].name).toEqual('Curran Stout') + expect(costumers[0].registered_at).toEqual('Wed, 16 Apr 2014 21:40:20 -0700') + expect(costumers[0].address).toEqual('Ap #658-1540 Erat Rd.') + expect(costumers[0].city).toEqual('San Francisco') + expect(costumers[0].state).toEqual('California') + expect(costumers[0].postal_code).toEqual('94267') + done() + }) + }) + + it('should return an empty array if no customers were found', function (done) { + var title = "Titanic"; + Rentals.find_customers_by_title(title, function (error, costumers) { + expect(error).toBeNull + expect(Array.isArray(costumers)).toEqual(true) + expect(costumers).toEqual([]) + done() + }) + }) + + it('should return an empty array if given a bad request', function (done) { + var title = "melissa"; + Rentals.find_customers_by_title(title, function (error, costumers) { + expect(error).toBeNull + expect(Array.isArray(costumers)).toEqual(true) + expect(costumers).toEqual([]) done() }) }) From 8d20e522597a4cebd79c216a3bef21009d52f82a Mon Sep 17 00:00:00 2001 From: melissajimison Date: Mon, 20 Jun 2016 21:59:36 -0700 Subject: [PATCH 070/118] added tests for enpoint /:title/customers --- spec/controllers/rentals.spec.js | 53 ++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/spec/controllers/rentals.spec.js b/spec/controllers/rentals.spec.js index e8820f10a..19dbcce73 100644 --- a/spec/controllers/rentals.spec.js +++ b/spec/controllers/rentals.spec.js @@ -22,12 +22,13 @@ describe("Endpoint at /", function () { }); }); - it('responds with a 204 status code for no data', function (done) { + it('responds with a 200 status code, and all movie info, for succesful request', function (done) { request.get(url('/Titanic'), function(error, response, body) { var body_parsed = JSON.parse(body) expect(response.statusCode).toEqual(200) expect(typeof body).toEqual("string") expect(typeof body_parsed).toEqual("object") + expect(body_parsed["status"]).toEqual(200) expect(body_parsed["Movie Info"]["Synopsis"]).toEqual(jasmine.any(String)) expect(body_parsed["Movie Info"]["Release Date"]).toEqual("1997-12-19") expect(body_parsed["Movie Info"]["Total Inventory"]).toEqual(5) @@ -38,12 +39,52 @@ describe("Endpoint at /", function () { // it('responds with a 404 status code for bad request', function (done) { // request.get(url('/Melissa'), function(error, response, body) { - // var body_parsed = JSON.parse(body) - // expect(response.statusCode).toEqual(200) - // expect(typeof body).toEqual("string") - // expect(typeof body_parsed).toEqual("object") - // done(); + // // var body_parsed = JSON.parse(body) + // // expect(response.statusCode).toEqual(200) + // // expect(typeof body).toEqual("string") + // // expect(typeof body_parsed).toEqual("object") + // // done(); // }); // }); }); + +describe("Endpoint at /", function () { + var url = function(endpoint) { + return base_url + "rentals" + endpoint; + }; + + it('responds with a 200 status code, and all customers info, for succesful request', function (done) { + request.get(url('/Jaws/customers'), function(error, response, body) { + var body_parsed = JSON.parse(body) + expect(response.statusCode).toEqual(200) + expect(typeof body).toEqual("string") + expect(typeof body_parsed).toEqual("object") + expect(body_parsed["status"]).toEqual(200) + expect(body_parsed["customers"]).toEqual(jasmine.any(Array)) + expect(body_parsed["customers"][0]["name"]).toEqual("Curran Stout") + expect(body_parsed["customers"][0]["registered_at"]).toEqual("Wed, 16 Apr 2014 21:40:20 -0700") + expect(body_parsed["customers"][0]["address"]).toEqual("Ap #658-1540 Erat Rd.") + expect(body_parsed["customers"][0]["city"]).toEqual("San Francisco") + expect(body_parsed["customers"][0]["state"]).toEqual("California") + expect(body_parsed["customers"][0]["postal_code"]).toEqual("94267") + expect(body_parsed["customers"][0]["phone"]).toEqual("(908) 949-6758") + expect(body_parsed["customers"][0]["account_credit"]).toEqual("35.66") + done(); + }); + }); + + it('responds with a 200 status code, but 204 response status for not data found', function (done) { + request.get(url('/Titanic/customers'), function(error, response, body) { + var body_parsed = JSON.parse(body) + expect(response.statusCode).toEqual(200) + expect(typeof body).toEqual("string") + expect(typeof body_parsed).toEqual("object") + expect(body_parsed["status"]).toEqual(204) + expect(body_parsed["customers"]).toEqual(jasmine.any(Array)) + expect(body_parsed["customers"]).toEqual([]) + done(); + }); + }); + +}); From 931c5a0fe03fabbea50c5da70a896a2eb5740bfd Mon Sep 17 00:00:00 2001 From: Risha Date: Tue, 21 Jun 2016 09:12:02 -0700 Subject: [PATCH 071/118] working on flow for customer history flow. --- controllers/customers_controller.js | 4 ++-- controllers/rentals_controller.js | 21 ++++++++++++--------- db/seeds/history.json | 2 +- models/customers.js | 2 ++ models/history.js | 14 ++++++-------- models/movies.js | 10 +++++----- models/rentals.js | 11 +++++++---- 7 files changed, 35 insertions(+), 29 deletions(-) diff --git a/controllers/customers_controller.js b/controllers/customers_controller.js index daaa83c97..1c980f0ff 100644 --- a/controllers/customers_controller.js +++ b/controllers/customers_controller.js @@ -1,5 +1,6 @@ var Customers = require("../models/customers"); var Rentals = require("../models/rentals"); +var History = require("../models/history"); var CustomersController = { index: function(req, res, next) { @@ -53,7 +54,7 @@ var CustomersController = { history: function(req, res, next) { //model has a find method that takes in 2 arguments (id, callback function) - Rentals.find_by_customer(req.params.id, function(error, history) { + History.getPastRentalHistory(req.params.id, function(error, history) { if(error) { var err = new Error("No history at this store"); err.status = 404; @@ -65,5 +66,4 @@ var CustomersController = { }, }; - module.exports = CustomersController; diff --git a/controllers/rentals_controller.js b/controllers/rentals_controller.js index 5e091fc55..2a69ed0ac 100644 --- a/controllers/rentals_controller.js +++ b/controllers/rentals_controller.js @@ -1,6 +1,7 @@ var Rentals = require('../models/rentals'); var Movies = require("../models/movies"); var Customers = require("../models/customers"); +var Histoy = require("../models/history"); var RentalsController = { @@ -13,12 +14,12 @@ var RentalsController = { err.status = 404; next(err); } else { - obj = {} + obj = {}; obj['Synopsis'] = found_movie.overview; obj['Release Date'] = found_movie.release_date; obj['Total Inventory'] = found_movie.inventory; Rentals.available(found_movie.id, function(err, number){ - obj['Available Inventory'] = number + obj['Available Inventory'] = number; res.json(obj); }); } @@ -46,21 +47,23 @@ var RentalsController = { err.status = 404; next(err); } else { - obj = {} + obj = {}; if (found_customers.length === 0) { - obj["status"] = 204 + obj["status"] = 204; } else { - obj["status"] = 200 + obj["status"] = 200; } - obj["customers"] = found_customers - res.json(obj) + obj["customers"] = found_customers; + res.json(obj); } }); } }); - }; - }) + } + }); } }; + + module.exports = RentalsController; diff --git a/db/seeds/history.json b/db/seeds/history.json index 1aea4f103..52562d7d8 100644 --- a/db/seeds/history.json +++ b/db/seeds/history.json @@ -35,4 +35,4 @@ "checkout_date": "2004-06-16", "return_date": "2004-06-26" } -] \ No newline at end of file +] diff --git a/models/customers.js b/models/customers.js index f011eed46..aa6f5ff4b 100644 --- a/models/customers.js +++ b/models/customers.js @@ -54,4 +54,6 @@ Customers.find = function(ids, callback) { + + module.exports = Customers; diff --git a/models/history.js b/models/history.js index 80025dbac..ca848329b 100644 --- a/models/history.js +++ b/models/history.js @@ -11,17 +11,15 @@ var History = function(history) { }; -History.find_by_customer_id = function(ids, callback) { - db.customers.find({id: ids}, function(error, customers) { - if(error || !customers) { - callback(error || new Error("Could not retrieve information"), undefined); +History.getPastRentalHistory = function(customer_id, callback) { + db.run("SELECT customer_id, checkout_date, return_date FROM history WHERE customer_id=$1 AND returned=$2 ORDER BY return_date ASC", [customer_id, true], function(error, history) { + if(error) { + callback(error, undefined); } else { - callback(null, customers.map(function(customer) { - return new History(customer); - })); + callback(null, history); } }); }; -}; + module.exports = History; diff --git a/models/movies.js b/models/movies.js index 358a65e2d..cc4180536 100644 --- a/models/movies.js +++ b/models/movies.js @@ -43,7 +43,7 @@ Movies.find = function(title, callback) { callback(null, new Movies(movie)); } }); -} +}; Movies.find_customers_by_title = function(title, callback) { db.sql.movies.currentCustomers([title], function(error, customers) { @@ -53,9 +53,9 @@ Movies.find_customers_by_title = function(title, callback) { callback(null, customers.map(function(customer) { return new Customers(customer); })); - }; + } }); -} +}; Movies.find_customers_by_history = function(title, order_by, callback) { db.run("select customers.name, customers.phone, customers.account_credit from movies INNER JOIN rentals on movies.id=rentals.movie_id inner join history on rentals.id=history.rental_id inner join customers on history.customer_id=customers.id where title = $1" + order_by, [title], function(error, customers) { @@ -65,8 +65,8 @@ Movies.find_customers_by_history = function(title, order_by, callback) { callback(null, customers.map(function(customer) { return new Customers(customer); })); - }; + } }); -} +}; module.exports = Movies; diff --git a/models/rentals.js b/models/rentals.js index 4957847ed..436f5c895 100644 --- a/models/rentals.js +++ b/models/rentals.js @@ -37,8 +37,8 @@ Rentals.get_customer_ids = function(movie_id, callback) { return rental.customer_id; })); } - }) -} + }); +}; Rentals.available = function(movie_id, callback){ db.rentals.find({movie_id: movie_id}, function(error, rentals) { @@ -48,7 +48,7 @@ Rentals.available = function(movie_id, callback){ callback(null, rentals.length); } }); -} +}; Rentals.get_customer_ids_of_rented = function(movie_id, callback) { // key value that matches one of the column names in the rentals TABLE @@ -59,10 +59,13 @@ Rentals.get_customer_ids_of_rented = function(movie_id, callback) { } else { callback(null, rentals.map(function(rental) { var rental = new Rentals(rental); - return rental.customer_id + return rental.customer_id; })); } }); }; + + + module.exports = Rentals; From 0528ccf4b77a1ce23cfe8ddcc367554faf0fceca Mon Sep 17 00:00:00 2001 From: melissajimison Date: Tue, 21 Jun 2016 13:06:13 -0700 Subject: [PATCH 072/118] added overdue column to history table, and update the seed file --- db/seeds/history.json | 74 +++++++++++++++++++++++++++++++++++-------- db/setup/schema.sql | 3 +- 2 files changed, 63 insertions(+), 14 deletions(-) diff --git a/db/seeds/history.json b/db/seeds/history.json index 1aea4f103..bbc97b18e 100644 --- a/db/seeds/history.json +++ b/db/seeds/history.json @@ -2,37 +2,85 @@ { "rental_id": 1, "customer_id": 1, -"checkout_date": "1960-06-16", -"return_date": "1960-06-26" +"checkout_date": "2016-06-16", +"return_date": "2016-06-26", +"overdue": false }, { "rental_id": 7, "customer_id": 3, -"checkout_date": "1990-06-16", -"return_date": "1990-06-26" +"checkout_date": "2016-06-17", +"return_date": "2016-06-27", +"overdue": false }, { "rental_id": 12, "customer_id": 2, -"checkout_date": "1999-06-16", -"return_date": "1999-06-26" +"checkout_date": "2016-06-18", +"return_date": "2016-06-28", +"overdue": false }, { "rental_id": 4, "customer_id": 1, -"checkout_date": "1998-06-16", -"return_date": "1998-06-26" +"checkout_date": "2016-06-19", +"return_date": "2016-06-29", +"overdue": false }, { "rental_id": 9, "customer_id": 11, -"checkout_date": "2002-06-16", -"return_date": "2002-06-26" +"checkout_date": "2016-06-10", +"return_date": "2016-06-20", +"overdue": true }, { "rental_id": 14, "customer_id": 2, -"checkout_date": "2004-06-16", -"return_date": "2004-06-26" +"checkout_date": "2016-06-06", +"return_date": "2016-06-16", +"overdue": true +}, +{ +"rental_id": 2, +"customer_id": 1, +"checkout_date": "2016-06-16", +"return_date": "2016-06-26", +"overdue": false +}, +{ +"rental_id": 3, +"customer_id": 3, +"checkout_date": "2016-07-10", +"return_date": "2016-07-20", +"overdue": true +}, +{ +"rental_id": 12, +"customer_id": 2, +"checkout_date": "2016-03-13", +"return_date": "2016-04-23", +"overdue": false +}, +{ +"rental_id": 4, +"customer_id": 1, +"checkout_date": "2016-06-16", +"return_date": "2016-06-26", +"overdue": false +}, +{ +"rental_id": 6, +"customer_id": 11, +"checkout_date": "2016-06-16", +"return_date": "2016-06-26", +"overdue": false +}, +{ +"rental_id": 5, +"customer_id": 2, +"checkout_date": "2016-06-12", +"return_date": "2016-06-22", +"overdue": false } -] \ No newline at end of file +] diff --git a/db/setup/schema.sql b/db/setup/schema.sql index 5b0c54e78..b628c0ef7 100644 --- a/db/setup/schema.sql +++ b/db/setup/schema.sql @@ -41,7 +41,8 @@ CREATE TABLE history( rental_id integer, customer_id integer, checkout_date text, - return_date text + return_date text, + overdue boolean ); CREATE INDEX history_customer_id ON history (customer_id); From 94ad964d6bed1434df2634a0878745edd479663a Mon Sep 17 00:00:00 2001 From: Risha Date: Tue, 21 Jun 2016 13:30:35 -0700 Subject: [PATCH 073/118] added a conditional that checks the request and makes sure there are perameters for the request. --- controllers/customers_controller.js | 28 ++++++++++++++++++++++++---- models/customers.js | 12 +----------- models/history.js | 2 +- models/rentals.js | 15 --------------- 4 files changed, 26 insertions(+), 31 deletions(-) diff --git a/controllers/customers_controller.js b/controllers/customers_controller.js index 1c980f0ff..f0ae7544f 100644 --- a/controllers/customers_controller.js +++ b/controllers/customers_controller.js @@ -10,14 +10,21 @@ var CustomersController = { err.status = 500; next(err); } else { - res.json(customers); - // var locals = { accounts : accounts}; - // res.render("accounts/index",locals); + var obj = {}; + if (customers.length === 0) { + obj["status"] = 204; + } else { + obj["status"] = 200; + } + obj["customers"] = customers; + res.json(obj); } }); }, +// the word phone breaks the code: sort: function(req, res, next) { + if(req.params.column === "name" || req.params.column === "registered_at" || req.params.column === "postal_code") { //Send in an ORDER clause and a LIMIT with OFFSET var options = { limit : req.query.n, @@ -33,9 +40,22 @@ var CustomersController = { err.status = 404; next(err); } else { - res.json(customers); + var obj = {}; + if (customers.length === 0) { + obj["status"] = 204; + } else { + obj["status"] = 200; + } + obj["customers"] = customers; + res.json(obj); } }); + } else { + var obj = {}; + obj["status"] = 400; + obj["message"] = "invalid request"; + res.json(obj); + } }, // this is a property whose value is a function (this is now a method) // this is the response from node diff --git a/models/customers.js b/models/customers.js index aa6f5ff4b..cc17f39e7 100644 --- a/models/customers.js +++ b/models/customers.js @@ -38,17 +38,7 @@ Customers.sort_by = function(options, callback) { }); }; -Customers.find = function(ids, callback) { - db.customers.find({id: ids}, function(error, customers) { - if(error || !customers) { - callback(error || new Error("Could not retrieve customers"), undefined); - } else { - callback(null, customers.map(function(customer) { - return new Customers(customer); - })); - } - }); -}; + diff --git a/models/history.js b/models/history.js index ca848329b..4d042bd25 100644 --- a/models/history.js +++ b/models/history.js @@ -12,7 +12,7 @@ var History = function(history) { }; History.getPastRentalHistory = function(customer_id, callback) { - db.run("SELECT customer_id, checkout_date, return_date FROM history WHERE customer_id=$1 AND returned=$2 ORDER BY return_date ASC", [customer_id, true], function(error, history) { + db.run("SELECT customer_id, checkout_date, return_date FROM history WHERE customer_id=$1 ORDER BY return_date ASC", [customer_id], function(error, history) { if(error) { callback(error, undefined); } else { diff --git a/models/rentals.js b/models/rentals.js index c8dbc12fc..6e84aef52 100644 --- a/models/rentals.js +++ b/models/rentals.js @@ -38,21 +38,6 @@ Rentals.available = function(movie_id, callback){ }); }; -Rentals.get_customer_ids_of_rented = function(movie_id, callback) { - // key value that matches one of the column names in the rentals TABLE - // value is the specific value that we want to look for in the table - db.rentals.find({movie_id : movie_id, status: "rented"}, function(error, rentals) { - if(error || !rentals) { - callback(error || new Error("Could not retrieve your rentals"), undefined); - } else { - callback(null, rentals.map(function(rental) { - var rental = new Rentals(rental); - return rental.customer_id; - })); - } - }); -}; - //Melissa is using this. dont delete Rentals.find_customers_by_title = function(title, callback) { db.sql.rentals.currentCustomers([title], function(error, customers) { From 37c99e7e610af214840ddd4bb19f574bdf21ba81 Mon Sep 17 00:00:00 2001 From: melissajimison Date: Tue, 21 Jun 2016 13:56:20 -0700 Subject: [PATCH 074/118] first atempt to do post requests --- controllers/rentals_controller.js | 23 +++++++++++++++++++++-- models/rentals.js | 12 +++++++++++- routes/rentals.js | 3 +++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/controllers/rentals_controller.js b/controllers/rentals_controller.js index 92c09f09a..332721f0c 100644 --- a/controllers/rentals_controller.js +++ b/controllers/rentals_controller.js @@ -46,7 +46,26 @@ var RentalsController = { res.json(obj); } }) - } -}; + }, + + checkout: function(req, res, next) { + var id = req.params.id; + var movie = req.params.title; + Rentals.mark_as_checkout (movie, id, function(error, customers) { + if(error) { + var err = new Error("No such movie"); + err.status = 404; + next(err); + } else { + + + + obj = {} + obj["id"] = id + obj["movie"] = movie + + res.json(obj); + }, +}; module.exports = RentalsController; diff --git a/models/rentals.js b/models/rentals.js index 5f2d32efb..9cd5debf8 100644 --- a/models/rentals.js +++ b/models/rentals.js @@ -64,6 +64,16 @@ Rentals.find_customers_by_title = function(title, callback) { })); }; }); -} +}; +Rentals.mark_as_checkout = function(movie, id, callback) { + db.run("update rentals set customer_id=$1, status=rented from rentals inner join movies on rentals.movie_id = movies.id where title=$2",[id, movie], function(error, rental){ + if(error) { + callback(error, undefined) + } else { + callback(null, rental) + console.log(rental); + }; + }); +}; module.exports = Rentals; diff --git a/routes/rentals.js b/routes/rentals.js index e0b3939bd..d579d2e14 100644 --- a/routes/rentals.js +++ b/routes/rentals.js @@ -10,5 +10,8 @@ router.get('/:title', RentalsController.find_movie); router.get('/:title/customers', RentalsController.current_customers); +router.post('/:title/check-out/:id', RentalsController.checkout); + + module.exports = router; From 612129d721ff0307b2759b4b9354e322553738cb Mon Sep 17 00:00:00 2001 From: melissajimison Date: Tue, 21 Jun 2016 16:35:10 -0700 Subject: [PATCH 075/118] more attempting post request, for now the update, apdates all the(movies copy) rentals with that movie id --- controllers/rentals_controller.js | 29 ++++++++++++++--------------- models/rentals.js | 11 +++++------ 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/controllers/rentals_controller.js b/controllers/rentals_controller.js index a98398186..ca6b8557e 100644 --- a/controllers/rentals_controller.js +++ b/controllers/rentals_controller.js @@ -50,23 +50,22 @@ var RentalsController = { }, checkout: function(req, res, next) { - var id = req.params.id; + var customer_id = req.params.id; var movie = req.params.title; - Rentals.mark_as_checkout (movie, id, function(error, customers) { - if(error) { - var err = new Error("No such movie"); - err.status = 404; - next(err); - } else { - - - obj = {} - obj["id"] = id - obj["movie"] = movie - - res.json(obj); - }, + Rentals.mark_as_checkout (movie, customer_id, function(error, rental_count) { + if(error) { + var err = new Error("No such movie"); + err.status = 404; + next(err); + } else { + obj = {} + obj["status"] = 200 + obj["message"] = rental_count + res.json(obj); + } + }) + } }; module.exports = RentalsController; diff --git a/models/rentals.js b/models/rentals.js index b76a4f202..e3189a3e4 100644 --- a/models/rentals.js +++ b/models/rentals.js @@ -51,15 +51,14 @@ Rentals.find_customers_by_title = function(title, callback) { }); }; -Rentals.mark_as_checkout = function(movie, id, callback) { - db.run("update rentals set customer_id=$1, status=rented from rentals inner join movies on rentals.movie_id = movies.id where title=$2",[id, movie], function(error, rental){ +Rentals.mark_as_checkout = function(movie, customer_id, callback) { + // movie = "Jaws" + db.run("UPDATE rentals AS r SET customer_id = $1, status = 'rented' FROM movies AS m WHERE r.movie_id = (SELECT id FROM movies WHERE title=$2) RETURNING * ", [customer_id, movie], function(error, rental_count){ if(error) { callback(error, undefined) } else { - callback(null, rental) - console.log(rental); + callback(null, rental_count) }; }); }; ->>>>>>> jimison -module.exports = Rentals; +module.exports = Rentals From dc97e6665e20680bfdf845d15bf5ebb9d9eae563 Mon Sep 17 00:00:00 2001 From: melissajimison Date: Tue, 21 Jun 2016 16:49:32 -0700 Subject: [PATCH 076/118] istanbul does not count spec folders anymore --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 18c61b42b..02935d05b 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "private": true, "scripts": { "start": "nodemon ./bin/www", - "test": "clear; ./node_modules/.bin/istanbul cover --include-all-sources ./node_modules/.bin/jasmine-node --captureExceptions --verbose spec/", + "test": "clear; ./node_modules/.bin/istanbul cover -x 'spec/**/*' -- ./node_modules/.bin/jasmine-node --captureExceptions --verbose spec/", "db:drop": "dropdb videoStore", "db:create": "createdb videoStore", "db:schema": "node tasks/load_schema.js", From 2cd5e6b4fd62c0a3c959268d63c00b2191087b99 Mon Sep 17 00:00:00 2001 From: melissajimison Date: Wed, 22 Jun 2016 13:37:03 -0700 Subject: [PATCH 077/118] addded the SQL for updating into its own file. It is succesfully updating on record --- controllers/rentals_controller.js | 4 ++-- db/sql/rentals/checkout.sql | 12 ++++++++++++ models/rentals.js | 4 ++-- 3 files changed, 16 insertions(+), 4 deletions(-) create mode 100644 db/sql/rentals/checkout.sql diff --git a/controllers/rentals_controller.js b/controllers/rentals_controller.js index ca6b8557e..7f57b62b0 100644 --- a/controllers/rentals_controller.js +++ b/controllers/rentals_controller.js @@ -54,7 +54,7 @@ var RentalsController = { var movie = req.params.title; - Rentals.mark_as_checkout (movie, customer_id, function(error, rental_count) { + Rentals.mark_as_checkout (movie, customer_id, function(error, rental_id) { if(error) { var err = new Error("No such movie"); err.status = 404; @@ -62,7 +62,7 @@ var RentalsController = { } else { obj = {} obj["status"] = 200 - obj["message"] = rental_count + obj["message"] = rental_id res.json(obj); } }) diff --git a/db/sql/rentals/checkout.sql b/db/sql/rentals/checkout.sql new file mode 100644 index 000000000..bcc214952 --- /dev/null +++ b/db/sql/rentals/checkout.sql @@ -0,0 +1,12 @@ +UPDATE rentals AS r +SET customer_id = $1, status='rented' +FROM ( + SELECT rentals.id + FROM rentals + INNER JOIN movies + ON movies.id=rentals.movie_id + WHERE title=$2 AND rentals.status = 'available' + LIMIT 1 +) AS result +WHERE r.id=result.id +RETURNING rentals.id; diff --git a/models/rentals.js b/models/rentals.js index e3189a3e4..93e220757 100644 --- a/models/rentals.js +++ b/models/rentals.js @@ -53,11 +53,11 @@ Rentals.find_customers_by_title = function(title, callback) { Rentals.mark_as_checkout = function(movie, customer_id, callback) { // movie = "Jaws" - db.run("UPDATE rentals AS r SET customer_id = $1, status = 'rented' FROM movies AS m WHERE r.movie_id = (SELECT id FROM movies WHERE title=$2) RETURNING * ", [customer_id, movie], function(error, rental_count){ + db.sql.rentals.checkout([customer_id, movie], function(error, result){ if(error) { callback(error, undefined) } else { - callback(null, rental_count) + callback(null, result) }; }); }; From dba3be4badc1ac032b1944ea0de00d0dc71f37e6 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Wed, 22 Jun 2016 14:25:34 -0700 Subject: [PATCH 078/118] checkout.sql returns a rental instance to the rental controller which is used to create a new entry in the history table. --- controllers/movies_controller.js | 2 -- controllers/rentals_controller.js | 25 +++++++++++++++++-------- db/sql/rentals/checkout.sql | 2 +- models/history.js | 17 +++++++++++++++++ 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index f0e85a331..3a4e00d9f 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -85,8 +85,6 @@ var MoviesController = { order_by = 'checkout_date'; } - console.log(order_by); - Movies.find_customers_by_history(movie, order_by, function(error, customers) { if(error) { var err = new Error("No such movie: " + error.message); diff --git a/controllers/rentals_controller.js b/controllers/rentals_controller.js index 7f57b62b0..931a4334f 100644 --- a/controllers/rentals_controller.js +++ b/controllers/rentals_controller.js @@ -1,7 +1,7 @@ var Rentals = require('../models/rentals'); var Movies = require("../models/movies"); var Customers = require("../models/customers"); -var Histoy = require("../models/history"); +var History = require("../models/history"); var RentalsController = { @@ -54,18 +54,27 @@ var RentalsController = { var movie = req.params.title; - Rentals.mark_as_checkout (movie, customer_id, function(error, rental_id) { + Rentals.mark_as_checkout (movie, customer_id, function(error, rental_array) { if(error) { - var err = new Error("No such movie"); + var err = new Error(error.message); err.status = 404; next(err); } else { - obj = {} - obj["status"] = 200 - obj["message"] = rental_id - res.json(obj); + var rental_id = rental_array[0].id + History.create_record(rental_id, customer_id, function(error, history_result) { + if(error) { + var err = new Error(error.message); + err.status = 404; + next(err); + } else { + obj = {} + obj["status"] = 200; + obj["message"] = history_result; + res.json(obj); + } + }); } - }) + }); } }; module.exports = RentalsController; diff --git a/db/sql/rentals/checkout.sql b/db/sql/rentals/checkout.sql index bcc214952..cf8eaf5fe 100644 --- a/db/sql/rentals/checkout.sql +++ b/db/sql/rentals/checkout.sql @@ -9,4 +9,4 @@ FROM ( LIMIT 1 ) AS result WHERE r.id=result.id -RETURNING rentals.id; +RETURNING *; diff --git a/models/history.js b/models/history.js index 4d042bd25..84cabc493 100644 --- a/models/history.js +++ b/models/history.js @@ -21,5 +21,22 @@ History.getPastRentalHistory = function(customer_id, callback) { }); }; +History.create_record = function(rental_id, customer_id, callback) { + db.history.save({rental_id: rental_id, customer_id: customer_id, checkout_date: new Date(), return_date: make_return_date(), overdue: false}, function(error,inserted){ + if(error) { + callback(error, undefined); + } else { + callback(null, inserted); + } + }); +} + +function make_return_date() { + var date = new Date(); + var return_days = 10; + date.setDate(date.getDate() + return_days); + return date; +} + module.exports = History; From b2a1b318693296da1d60245a4e402ac2de5533d3 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Thu, 23 Jun 2016 14:47:27 -0700 Subject: [PATCH 079/118] removed distinct from select clause so that all entries are found even if customer_id and rental_id are the same (seed file has duplicates, so there should be multiple entries like this) --- db/sql/movies/historyCustomersByDate.sql | 2 +- db/sql/movies/historyCustomersByName.sql | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/db/sql/movies/historyCustomersByDate.sql b/db/sql/movies/historyCustomersByDate.sql index eb3374076..c1c537856 100644 --- a/db/sql/movies/historyCustomersByDate.sql +++ b/db/sql/movies/historyCustomersByDate.sql @@ -1,4 +1,4 @@ -SELECT DISTINCT customers.id, customers.name, customers.phone, customers.account_credit, history.checkout_date FROM movies +SELECT customers.id, customers.name, customers.phone, customers.account_credit, history.checkout_date FROM movies INNER JOIN rentals ON movies.id=rentals.movie_id INNER JOIN history diff --git a/db/sql/movies/historyCustomersByName.sql b/db/sql/movies/historyCustomersByName.sql index a662ae3b2..e78bfa135 100644 --- a/db/sql/movies/historyCustomersByName.sql +++ b/db/sql/movies/historyCustomersByName.sql @@ -1,4 +1,4 @@ -SELECT DISTINCT customers.id, customers.name, customers.phone, customers.account_credit, history.checkout_date FROM movies +SELECT customers.id, customers.name, customers.phone, customers.account_credit, history.checkout_date FROM movies INNER JOIN rentals ON movies.id=rentals.movie_id INNER JOIN history From f7db5a19530bf687d266a2b938084da419b49498 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Thu, 23 Jun 2016 15:26:55 -0700 Subject: [PATCH 080/118] added a default order_by for customer history. if an invalid sort option is given, it will sort by id --- db/sql/movies/historyCustomersDefault.sql | 9 +++++++++ models/movies.js | 2 ++ 2 files changed, 11 insertions(+) create mode 100644 db/sql/movies/historyCustomersDefault.sql diff --git a/db/sql/movies/historyCustomersDefault.sql b/db/sql/movies/historyCustomersDefault.sql new file mode 100644 index 000000000..b0678fcf1 --- /dev/null +++ b/db/sql/movies/historyCustomersDefault.sql @@ -0,0 +1,9 @@ +SELECT customers.id, customers.name, customers.phone, customers.account_credit, history.checkout_date FROM movies + INNER JOIN rentals + ON movies.id=rentals.movie_id + INNER JOIN history + ON rentals.id=history.rental_id + INNER JOIN customers + ON history.customer_id=customers.id +WHERE title = $1 +ORDER BY customers.id; diff --git a/models/movies.js b/models/movies.js index 75a5ebd22..363ac7c72 100644 --- a/models/movies.js +++ b/models/movies.js @@ -38,6 +38,8 @@ Movies.find_customers_by_history = function(title, order_by, callback) { db.sql.movies.historyCustomersByName(title, customer_callback(callback)); } else if (order_by === 'checkout_date') { db.sql.movies.historyCustomersByDate(title, customer_callback(callback)); + } else { + db.sql.movies.historyCustomersDefault(title, customer_callback(callback)); } } From fc52d3353cfd169f37978eb67ee32bea0c9defae Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Thu, 23 Jun 2016 15:27:21 -0700 Subject: [PATCH 081/118] added tests for movies model --- spec/models/movies.spec.js | 66 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 3 deletions(-) diff --git a/spec/models/movies.spec.js b/spec/models/movies.spec.js index af9f69335..da15edb83 100644 --- a/spec/models/movies.spec.js +++ b/spec/models/movies.spec.js @@ -2,6 +2,7 @@ var app = require("../../app"); var db = app.get("db"); var Movies = require('../../models/movies'); +var Customers = require('../../models/customers'); describe('Movies', function () { var options_release = { @@ -17,7 +18,6 @@ describe('Movies', function () { }; afterEach(function () { - // delete all the accounts I created db.end(); }); @@ -59,8 +59,8 @@ describe('Movies', function () { expect(movies[0].title).toEqual("2001: A Space Odyssey"); expect(movies[0].id).toEqual(40); done(); - }) - }) + }); + }); }); describe('#find', function () { @@ -76,6 +76,66 @@ describe('Movies', function () { }); }); + describe('#find_customers_by_title', function() { + it('should return an Array of Customer instances', function(done) { + Movies.find_customers_by_title("Jaws", function(error, customers) { + expect(error).toBeNull; + expect(customers).toEqual(jasmine.any(Array)); + expect(customers[0]).toEqual(jasmine.any(Customers)); + done(); + }); + }); + + it('should return only name, phone, and account_credit info for each customer', function(done) { + Movies.find_customers_by_title("Jaws", function(error, customers) { + expect(customers[0].name).toNotBe(undefined); + expect(customers[0].phone).toNotBe(undefined); + expect(customers[0].account_credit).toNotBe(undefined); + done(); + }); + }); + }); + + describe('#find_customers_by_history', function() { + it('should return an Array of Customer instances', function(done) { + Movies.find_customers_by_history("Jaws", "name", function(error, customers) { + expect(error).toBeNull; + expect(customers).toEqual(jasmine.any(Array)); + expect(customers[0]).toEqual(jasmine.any(Customers)); + done(); + }); + }); + + it('returns 8 instances of history for the movie Psycho', function(done) { + Movies.find_customers_by_history("Psycho", "name", function(error, customers) { + expect(customers.length).toEqual(8); + done(); + }); + }); + + it('can order by name', function(done) { + Movies.find_customers_by_history("Psycho", "name", function(error, customers) { + expect(customers[0].id).toEqual(11); + expect(customers[customers.length-1].id).toEqual(1); + done(); + }); + }); + + it('can order by checkout_date', function(done) { + Movies.find_customers_by_history("Psycho", "checkout_date", function(error, customers) { + expect(customers[0].id).toEqual(2); + expect(customers[customers.length-1].id).toEqual(3); + done(); + }); + }); + it('orders by id by default if an invalid sort column was given', function(done) { + Movies.find_customers_by_history("Psycho", "blahblahblah", function(error, customers) { + expect(customers[0].id).toEqual(1); + expect(customers[customers.length-1].id).toEqual(11); + done(); + }); + }); + }); }); From b8c803a53c663e6e4aaec9e824ae0506eb63a2ad Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Thu, 23 Jun 2016 16:04:43 -0700 Subject: [PATCH 082/118] changed to status 500 for errors --- controllers/movies_controller.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index 3a4e00d9f..baae0cde9 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -7,7 +7,7 @@ var MoviesController = { Movies.all(function(error, movies) { // this is a callback if(error) { var err = new Error("Error retrieving movies list:\n" + error.message); - err.status = 404; + err.status = 500; next(err); } else { var obj = {}; @@ -38,7 +38,7 @@ var MoviesController = { Movies.sort_by(options, function(error, movies) { // this is a callback if(error) { var err = new Error("No such movie"); - err.status = 404; + err.status = 500; next(err); } else { var obj = {}; @@ -59,7 +59,7 @@ var MoviesController = { Movies.find_customers_by_title(movie, function(error, customers) { if(error) { var err = new Error("No such movie"); - err.status = 404; + err.status = 500; next(err); } else { var obj = {}; @@ -88,7 +88,7 @@ var MoviesController = { Movies.find_customers_by_history(movie, order_by, function(error, customers) { if(error) { var err = new Error("No such movie: " + error.message); - err.status = 404; + err.status = 500; next(err); } else { var obj = {}; From fc33b0c5d5f6959510be4d4b3cffc0edc0acd0fa Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Thu, 23 Jun 2016 16:04:53 -0700 Subject: [PATCH 083/118] added tests for the movies controller --- spec/controllers/movies.spec.js | 106 ++++++++++++++++++++++++-------- 1 file changed, 81 insertions(+), 25 deletions(-) diff --git a/spec/controllers/movies.spec.js b/spec/controllers/movies.spec.js index e53f32307..bda50371b 100644 --- a/spec/controllers/movies.spec.js +++ b/spec/controllers/movies.spec.js @@ -1,42 +1,98 @@ var request = require('request'); var base_url = "http://localhost:3000/"; -describe("Endpoint at /", function () { - var url = function(endpoint) { - return base_url + "movies" + endpoint; - }; +describe('MoviesController', function() { + describe("Endpoint at /", function() { + var url = function(endpoint) { + return base_url + "movies" + endpoint; + }; - it('responds with a 200 status code', function (done) { - request.get(url('/'), function(error, response, body) { - expect(response.statusCode).toEqual(200); - done(); + it('responds with a 200 status code', function(done) { + request.get(url('/'), function(error, response, body) { + expect(JSON.parse(body).status).toEqual(200); + done(); + }); }); }); -}); -describe("Endpoint at /sort", function () { - var url = function(endpoint) { - return base_url + "movies/sort" + endpoint; - }; + describe("Endpoint at /sort", function() { + var url = function(endpoint) { + return base_url + "movies/sort" + endpoint; + }; + + it('responds with a 200 status code for a valid url', function(done) { + request.get(url('/title?n=10&p=1'), function(error, response, body) { + expect(JSON.parse(body).status).toEqual(200); + done(); + }); + }); + + it('responds with a 200 status code for released_date', function(done) { + request.get(url('/release_date?n=6&p=1'), function(error, response, body) { + expect(JSON.parse(body).status).toEqual(200); + done(); + }); + }); + + it('responds with a 200 status code for title', function(done) { + request.get(url('/title?n=6&p=1'), function(error, response, body) { + expect(JSON.parse(body).status).toEqual(200); + done(); + }); + }); - it('responds with a 200 status code', function (done) { - request.get(url('/title?n=10&p=1'), function(error, response, body) { - expect(response.statusCode).toEqual(200); - done(); + it('responds with a 500 status code for mispelling', function(done) { + request.get(url('/released-date?n=10&p=1'), function(error, response, body) { + expect(response.statusCode).toEqual(500); + done(); + }); }); }); - it('responds with a 200 status code released_date', function (done) { - request.get(url('/release_date?n=6&p=1'), function(error, response, body) { - expect(response.statusCode).toEqual(200); - done(); + describe("Endpoint at movies/:title/current", function() { + var url = function(endpoint) { + return base_url + "movies" + endpoint; + }; + + it('responds with a 200 status code for a valid movie', function(done) { + request.get(url('/Jaws/current'), function(error, response, body) { + expect(JSON.parse(body).status).toEqual(200); + done(); + }); + }); + + it('responds with a 204 status code for an invalid movie', function(done) { + request.get(url('/asdfasdfasdf/current'), function(error, response, body) { + expect(JSON.parse(body).status).toEqual(204); + done(); + }); }); }); - it('responds with a 200 status code for mispelling', function (done) { - request.get(url('/released-date?n=10&p=1'), function(error, response, body) { - expect(response.statusCode).toEqual(404); - done(); + describe("Endpoint at movies/:title/history/sort/:column", function() { + var url = function(endpoint) { + return base_url + "movies" + endpoint; + }; + + it('responds with a 200 status code for a valid movie', function(done) { + request.get(url('/Jaws/history/sort/name'), function(error, response, body) { + expect(JSON.parse(body).status).toEqual(200); + done(); + }); + }); + + it('responds with a 204 status code for an invalid movie', function(done) { + request.get(url('/asdfasdfasdf/history/sort/name'), function(error, response, body) { + expect(JSON.parse(body).status).toEqual(204); + done(); + }); + }); + + it('responds with a 200 status code for an invalid sort column (sorted by id by default)', function(done) { + request.get(url('/Jaws/history/sort/blahblah'), function(error, response, body) { + expect(JSON.parse(body).status).toEqual(200); + done(); + }); }); }); }); From 804ff1ef23d80ddc86df9a5270a1ee27f78873e0 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Thu, 23 Jun 2016 16:38:51 -0700 Subject: [PATCH 084/118] DRY up movies controller. if sort column for movies/sort/:column path is invalid, sorts by id by default. updated spec to reflect default id sort change --- controllers/movies_controller.js | 102 ++++++++++--------------------- spec/controllers/movies.spec.js | 4 +- 2 files changed, 33 insertions(+), 73 deletions(-) diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index baae0cde9..28812d29a 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -4,74 +4,30 @@ var Customers = require('../models/customers'); var MoviesController = { index: function(req, res, next) { - Movies.all(function(error, movies) { // this is a callback - if(error) { - var err = new Error("Error retrieving movies list:\n" + error.message); - err.status = 500; - next(err); - } else { - var obj = {}; - if (movies.length === 0) { - obj["status"] = 204; - } else { - obj["status"] = 200; - } - obj["movies"] = movies; - res.json(obj); - } - }); + Movies.all(callback(res, "movies")); }, sort: function(req, res, next) { - // // GET /search?q=tobi+ferret - // req.query.q - // // => "tobi ferret" - - //Send in an ORDER clause and a LIMIT with OFFSET + var order_by = req.params.column; var options = { limit : req.query.n, - order : req.params.column, offset: req.query.p }; + + if (order_by === "release_date" || order_by === "release-date") { + options["order"] = "release_date"; + } else if (order_by === "title") { + options["order"] = "title"; + } else { + options["order"] = "id"; + } - // products ordered in descending fashion - Movies.sort_by(options, function(error, movies) { // this is a callback - if(error) { - var err = new Error("No such movie"); - err.status = 500; - next(err); - } else { - var obj = {}; - if (movies.length === 0) { - obj["status"] = 204; - } else { - obj["status"] = 200; - } - obj["movies"] = movies; - res.json(obj); - } - }); + Movies.sort_by(options, callback(res, "movies")); }, current: function(req, res, next) { var movie = req.params.title; - - Movies.find_customers_by_title(movie, function(error, customers) { - if(error) { - var err = new Error("No such movie"); - err.status = 500; - next(err); - } else { - var obj = {}; - if (customers.length === 0) { - obj["status"] = 204; - } else { - obj["status"] = 200; - } - obj["customers"] = customers; - res.json(obj); - } - }) + Movies.find_customers_by_title(movie, callback(res, "customers")); }, history: function(req, res, next) { @@ -85,24 +41,28 @@ var MoviesController = { order_by = 'checkout_date'; } - Movies.find_customers_by_history(movie, order_by, function(error, customers) { - if(error) { - var err = new Error("No such movie: " + error.message); - err.status = 500; - next(err); + Movies.find_customers_by_history(movie, order_by, callback(res, "customers")); + } +}; + +function callback(res, type) { + return function(error, result) { // this is a callback + if(error) { + var err = new Error("Error retrieving results:\n" + error.message); + err.status = 500; + res.render(err); + } else { + var obj = {}; + if (result.length === 0) { + obj["status"] = 204; } else { - var obj = {}; - if (customers.length === 0) { - obj["status"] = 204; - } else { - obj["status"] = 200; - } - obj["customers"] = customers; - res.json(obj); + obj["status"] = 200; } - }) + obj[type] = result; + res.json(obj); + } } -}; +} module.exports = MoviesController; diff --git a/spec/controllers/movies.spec.js b/spec/controllers/movies.spec.js index bda50371b..07052018c 100644 --- a/spec/controllers/movies.spec.js +++ b/spec/controllers/movies.spec.js @@ -41,9 +41,9 @@ describe('MoviesController', function() { }); }); - it('responds with a 500 status code for mispelling', function(done) { + it('responds with a 200 status code for mispelling (by default sorts by id if invalid sort column given)', function(done) { request.get(url('/released-date?n=10&p=1'), function(error, response, body) { - expect(response.statusCode).toEqual(500); + expect(response.statusCode).toEqual(200); done(); }); }); From 4491e303c9b3c7552a0dca17da166a50a062e14a Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Fri, 24 Jun 2016 09:29:42 -0700 Subject: [PATCH 085/118] removed a test --- spec/models/movies.spec.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/spec/models/movies.spec.js b/spec/models/movies.spec.js index da15edb83..334dcec7e 100644 --- a/spec/models/movies.spec.js +++ b/spec/models/movies.spec.js @@ -106,13 +106,6 @@ describe('Movies', function () { }); }); - it('returns 8 instances of history for the movie Psycho', function(done) { - Movies.find_customers_by_history("Psycho", "name", function(error, customers) { - expect(customers.length).toEqual(8); - done(); - }); - }); - it('can order by name', function(done) { Movies.find_customers_by_history("Psycho", "name", function(error, customers) { expect(customers[0].id).toEqual(11); From 2881e5c9fab3178f917a79109848d548a8c6e7a6 Mon Sep 17 00:00:00 2001 From: Risha Date: Fri, 24 Jun 2016 09:30:18 -0700 Subject: [PATCH 086/118] crerating json api flow. --- controllers/customers_controller.js | 28 +--- controllers/index_controller.js | 9 ++ db/sql/movies/currentCustomers.sql | 2 +- json.js | 243 ++++++++++++++++++++++++++++ models/customers.js | 1 + routes/index.js | 7 + spec/controllers/customers.spec.js | 18 +++ 7 files changed, 283 insertions(+), 25 deletions(-) create mode 100644 controllers/index_controller.js create mode 100644 json.js diff --git a/controllers/customers_controller.js b/controllers/customers_controller.js index f0ae7544f..1c980f0ff 100644 --- a/controllers/customers_controller.js +++ b/controllers/customers_controller.js @@ -10,21 +10,14 @@ var CustomersController = { err.status = 500; next(err); } else { - var obj = {}; - if (customers.length === 0) { - obj["status"] = 204; - } else { - obj["status"] = 200; - } - obj["customers"] = customers; - res.json(obj); + res.json(customers); + // var locals = { accounts : accounts}; + // res.render("accounts/index",locals); } }); }, -// the word phone breaks the code: sort: function(req, res, next) { - if(req.params.column === "name" || req.params.column === "registered_at" || req.params.column === "postal_code") { //Send in an ORDER clause and a LIMIT with OFFSET var options = { limit : req.query.n, @@ -40,22 +33,9 @@ var CustomersController = { err.status = 404; next(err); } else { - var obj = {}; - if (customers.length === 0) { - obj["status"] = 204; - } else { - obj["status"] = 200; - } - obj["customers"] = customers; - res.json(obj); + res.json(customers); } }); - } else { - var obj = {}; - obj["status"] = 400; - obj["message"] = "invalid request"; - res.json(obj); - } }, // this is a property whose value is a function (this is now a method) // this is the response from node diff --git a/controllers/index_controller.js b/controllers/index_controller.js new file mode 100644 index 000000000..aa987be0a --- /dev/null +++ b/controllers/index_controller.js @@ -0,0 +1,9 @@ +var docs = require ("../json.js"); + +var IndexController = { + getjson: function (req, res, next) { + res.status(200).json(docs); + } +}; + +module.exports = IndexController; diff --git a/db/sql/movies/currentCustomers.sql b/db/sql/movies/currentCustomers.sql index 7c30436f9..451b1fab8 100644 --- a/db/sql/movies/currentCustomers.sql +++ b/db/sql/movies/currentCustomers.sql @@ -1,4 +1,4 @@ -SELECT customers.name, customers.phone, customers.account_credit FROM movies +SELECT customers.name, customers.phone, customers.account_credit FROM movies INNER JOIN rentals ON movies.id=rentals.movie_id INNER JOIN customers diff --git a/json.js b/json.js new file mode 100644 index 000000000..78a217239 --- /dev/null +++ b/json.js @@ -0,0 +1,243 @@ +var docs = { + customers: { + list_all_customers: { + routeName: 'index', + httpRequest: 'GET /customers', + requiredParameters: 'none', + optionalParameters: 'none', + intendedUse: 'This endpoint contains a list of all customers in your database, generated at time of request.', + dataBreakdown: { + someDataFound: 'returns the found data', + noDataFound: 'returns status 500, []', + error: 'Error' + } + } + }, + + customers_sorted_by_name: { + routeName: 'index, sort', + httpRequest: 'GET /customers/sort/cloumn', + requiredParameters: 'none', + optionalParameters: 'n=? to limit the number of records returned, p=? for offset', + intendedUse: 'This endpoint returns a list of all customers in your database (generated at time of query), sorted by name. Query string n=? will limit the number of returned records, and p=? will offset your records by that number.', + dataBreakdown: { + someDataFound: 'returns the found data', + noDataFound: 'Error', + error: 'Error' + } + }, + + customers_sorted_by_postal_code: { + routeName: 'index, sort', + httpRequest: 'GET /customers/sort/column', + requiredParameters: 'none', + optionalParameters: 'n=? to limit the number of records returned, p=? for offset', + intendedUse: 'This endpoint returns a list of customers in your database (generated at time of query) sorted by zipcode. Query string n=? will limit the number of records returned, and p=? will offset your records by that number.', + dataBreakdown: { + someDataFound: 'returns the found data', + noDataFound: 'Error', + error: 'Error' + } + }, + + list_customer_checkouts: { + routeName: 'List Checkouts to Customer', + httpRequest: 'GET /customers/:id/current', + requiredParameters: 'customer id number', + optionalParameters: 'none', + intendedUse: 'Given a customer id, this endpoint returns a list (generated at time of query) of all checkouts a customer currently has.', + dataBreakdown: { + someDataFound: 'returns the found data', + noDataFound: 'Error', + error: 'Error' + } + }, + + customer_checkout_history: { + routeName: 'List Customer Checkout History', + httpRequest: 'GET /customers/:id/history', + requiredParameters: 'customer id number', + optionalParameters: 'none', + intendedUse: 'This endpoint returns a list (generated at time of query) of all checkouts a customer has previously made. (These are all films that have been returned.)', + dataBreakdown: { + someDataFound: 'returns the found data', + noDataFound: 'Error', + error: 'Error' + } + }, + + movies: { + list_all_movies: { + routeName: 'List All Movies', + httpRequest: 'GET /movies', + requiredParameters: 'none', + optionalParameters: 'none', + intendedUse: 'This endpoint will return a list of all movies in your database, generated at time of query.', + dataBreakdown: { + someDataFound: 'returns the found data', + noDataFound: 'Error', + error: 'Error' + } + }, + + movies_sorted_by_title: { + routeName: 'List Movies, Sorted by Title', + httpRequest: 'GET /movies/sort/title', + requiredParameters: 'none', + optionalParameters: 'n=? to limit the number of records returned, p=? for offset', + intendedUse: 'This endpoint will return a list of all movies in your database, generated at time of query. Query string n=? will allow you to limit how many records are returned, and query string p=? will offset your records by that number.', + dataBreakdown: { + someDataFound: 'returns the found data', + noDataFound: 'Error', + error: 'Error' + } + }, + + movies_sorted_by_release: { + routeName: 'List Movies, Sorted by Title', + httpRequest: 'GET /movies/sort/title', + requiredParameters: 'none', + optionalParameters: 'n=? to limit the number of records returned, p=? for offset', + intendedUse: 'This endpoint will return a list of all movies in your database, generated at time of query. Query string n=? will allow you to limit how many records are returned, and query string p=? will offset your records by that number.', + dataBreakdown: { + someDataFound: 'returns the found data', + noDataFound: 'Error', + error: 'Error' + } + }, + + movie_details: { + routeName: 'View Details for Title', + httpRequest: 'GET /movies/:title', + requiredParameters: 'film title', + optionalParameters: 'none', + intendedUse: 'This endpoint returns the details for a movie title. It will show title, summary description, release date, number of copies owned, and number of copies in stock. Generated at time of query.', + dataBreakdown: { + someDataFound: 'returns the found data', + noDataFound: 'Error', + error: 'Error' + } + }, + + movie_checkouts: { + routeName: 'View Checkouts for Title', + httpRequest: 'GET /movies/:title/current', + requiredParameters: 'film title', + optionalParameters: 'none', + intendedUse: 'This endpoint returns the the current checkout records for the given film. It will return customer name, phone number, and account credit for each checked out copy of the film.', + dataBreakdown: { + someDataFound: 'returns the found data', + noDataFound: 'Error', + error: 'Error' + } + }, + + movie_hist_name_sort: { + routeName: 'View Historical Checkouts for Title by Name', + httpRequest: 'GET /movies/:title/history/sort/name', + requiredParameters: 'film title', + optionalParameters: 'none', + intendedUse: 'This endpoint returns the the historical checkout records for the given film. It will return customer name, phone number, and account credit for each checked out copy of the film and is ordered by customer name.', + dataBreakdown: { + someDataFound: 'returns the found data', + noDataFound: 'Error', + error: 'Error' + } + }, + + movie_hist_checkout_sort: { + routeName: 'View Historical Checkouts for Title by Date', + httpRequest: 'GET /movies/:title/history/sort/date', + requiredParameters: 'film title', + optionalParameters: 'none', + intendedUse: 'This endpoint returns the the historical checkout records for the given film. It will return customer name, phone number, and account credit for each checked out copy of the film and is ordered by customer name.', + dataBreakdown: { + someDataFound: 'returns the found data', + noDataFound: 'Error', + error: 'Error' + } + } + }, + + rentals: { + rental_checkouts: { + routeName: 'View Current Checkouts for Title', + httpRequest: 'GET /rentals/:title/current', + requiredParameters: 'film title', + optionalParameters: 'none', + intendedUse: 'This endpoint returns the the historical checkout records for the given film. It will return customer name, phone number, and account credit for each checked out copy of the film and is ordered by customer name.', + dataBreakdown: { + someDataFound: 'returns the found data', + noDataFound: 'Error', + error: 'Error' + } + }, + + rental_hist_name_sort: { + routeName: 'View Historical Checkouts for Title by Name', + httpRequest: 'GET /rentals/:title/history/sort/name', + requiredParameters: 'film title', + optionalParameters: 'none', + intendedUse: 'This endpoint returns the the historical checkout records for the given film. It will return customer name, phone number, and account credit for each checked out copy of the film and is ordered by customer name.', + dataBreakdown: { + someDataFound: 'returns the found data', + noDataFound: 'Error', + error: 'Error' + } + }, + + rental_hist_checkout_sort: { + routeName: 'View Historical Checkouts for Title by Date', + httpRequest: 'GET /rentals/:title/history/sort/date', + requiredParameters: 'film title', + optionalParameters: 'none', + intendedUse: 'This endpoint returns the the historical checkout records for the given film. It will return customer name, phone number, and account credit for each checked out copy of the film and is ordered by customer name.', + dataBreakdown: { + someDataFound: 'returns the found data', + noDataFound: 'Error', + error: 'Error' + } + }, + + checkout_film_to_cust: { + routeName: 'View Historical Checkouts for Title by Date', + httpRequest: 'POST /rentals/:title/checkout', + requiredParameters: 'film title in route, customer ID in post body', + optionalParameters: 'none', + intendedUse: 'This endpoint will create a new checkout record tying the customer id and film title together in the rentals database along with date checked out and due date. Current default rental period is 3 days.', + dataBreakdown: { + someDataFound: 'returns the checkout record', + noDataFound: 'Error', + error: 'Error' + } + }, + + checkin_film_from_cust: { + routeName: 'View Historical Checkouts for Title by Date', + httpRequest: 'POST /rentals/:title/return', + requiredParameters: 'film title in route, customer ID in post body', + optionalParameters: 'none', + intendedUse: 'This endpoint will update the checkout record tying the customer id and film title together with the date checked in. It will also deduct any fees from customer credit. (This can send customer credit negative, which means they owe money.', + dataBreakdown: { + someDataFound: 'returns the checkout record', + noDataFound: 'Error', + error: 'Error' + } + }, + + list_of_overdue_films: { + routeName: 'List All Overdue Films', + httpRequest: 'GET /rentals/overdue', + requiredParameters: 'none', + optionalParameters: 'none', + intendedUse: 'This endpoint will return a list (generated at time of query) of all films currently checked out and overdue.', + dataBreakdown: { + someDataFound: 'returns the found data', + noDataFound: 'Error', + error: 'Error' + } + } + } +}; + +module.exports = docs; diff --git a/models/customers.js b/models/customers.js index cc17f39e7..58d3e6f73 100644 --- a/models/customers.js +++ b/models/customers.js @@ -46,4 +46,5 @@ Customers.sort_by = function(options, callback) { + module.exports = Customers; diff --git a/routes/index.js b/routes/index.js index 38f8e423e..4126eaf1d 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,5 +1,6 @@ var express = require('express'); var router = express.Router(); +var IndexController = require('../controllers/index_controller.js'); /* GET home page. */ router.get('/', function(req, res, next) { @@ -10,4 +11,10 @@ router.get('/zomg', function(req, res, next) { res.status(200).json({it_works: 'it works!'}); }); +// router.get('/api/docs.json', function (req, res, next){ +// res.status(200).json(docs); +// }); + +router.get('/api/docs.json', IndexController.getjson); + module.exports = router; diff --git a/spec/controllers/customers.spec.js b/spec/controllers/customers.spec.js index 3cd313909..9b16f02a4 100644 --- a/spec/controllers/customers.spec.js +++ b/spec/controllers/customers.spec.js @@ -6,6 +6,24 @@ describe("Endpoint at /", function () { return base_url + "customers" + endpoint; }; + it('responds with a 200 status code', function (done) { + request.get(url('/'), function(error, response, body) { + expect(response.statusCode).toEqual(200); + done(); + }); + }); +}); + +// +// describe("the returned json data", function() { +// it('has the right keys', function(done) { +// request.get(base_url, function(error, response, body) { +// var data = JSON.parse(body); +// expect(Object.keys(data)).toEqual(['']); +// done(); +// }); +// }); + // it('responds with a 200 status code', function (done) { // request.get(url('/'), function(error, response, body) { // expect(response.statusCode).toEqual(200); From c505728918c4f196830baad21e9156221b8d9632 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Fri, 24 Jun 2016 10:14:49 -0700 Subject: [PATCH 087/118] #create_record returns an instance of History --- models/history.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/models/history.js b/models/history.js index 84cabc493..240b9ab26 100644 --- a/models/history.js +++ b/models/history.js @@ -8,6 +8,7 @@ var History = function(history) { this.customer_id = history.customer_id; this.checkout_date = history.checkout_date; this.return_date = history.return_date; + this.overdue = history.overdue; }; @@ -26,7 +27,7 @@ History.create_record = function(rental_id, customer_id, callback) { if(error) { callback(error, undefined); } else { - callback(null, inserted); + callback(null, new History(inserted)); } }); } From 305e238cb13237eccbb93356599639c52b362644 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Fri, 24 Jun 2016 10:14:59 -0700 Subject: [PATCH 088/118] added tests for history model --- spec/models/history.spec.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 spec/models/history.spec.js diff --git a/spec/models/history.spec.js b/spec/models/history.spec.js new file mode 100644 index 000000000..aa1a8a7ac --- /dev/null +++ b/spec/models/history.spec.js @@ -0,0 +1,25 @@ +var app = require("../../app"); +var db = app.get("db"); + +var History = require('../../models/history'); + +describe('History', function () { + afterEach(function () { + db.end(); + }); + + describe('#create_record', function () { + it('returns a History instance that has all properties defined', function (done) { + History.create_record(1, 1, function(error, result) { + expect(error).toBeNull; + expect(result.id).toNotBe(null); + expect(result.rental_id).toNotBe(null); + expect(result.customer_id).toNotBe(null); + expect(result.checkout_date).toNotBe(null); + expect(result.return_date).toNotBe(null); + expect(result.overdue).toBe(false); + db.history.destroy({id: result.id}, function(err, res){done()}); + }); + }); + }); +}); From 2c2d322309dff735d286ab8b477c9a0638c25095 Mon Sep 17 00:00:00 2001 From: melissajimison Date: Fri, 24 Jun 2016 10:21:00 -0700 Subject: [PATCH 089/118] one test --- spec/models/rentals.spec.js | 105 +++++++++++++++++++----------------- 1 file changed, 55 insertions(+), 50 deletions(-) diff --git a/spec/models/rentals.spec.js b/spec/models/rentals.spec.js index 6700dda57..0227e42a3 100644 --- a/spec/models/rentals.spec.js +++ b/spec/models/rentals.spec.js @@ -2,65 +2,70 @@ var app = require("../../app"); var db = app.get("db"); var Rentals = require('../../models/rentals') -afterEach(function () { - // delete all the accounts I created - db.end() -}) +describe('Rentals', function () { -describe('#available', function () { - it('should return number of rentals available', function (done) { - var movie_id = 33; - Rentals.available(movie_id, function (error, costumers) { - expect(error).toBeNull - expect(costumers).toEqual(9) - done() - }) + afterEach(function () { + // delete all the accounts I created + db.end() }) - it('should return number of rentals available', function (done) { - var movie_id = 2; - Rentals.available(movie_id, function (error, result) { - expect(error).toBeNull - expect(result).toEqual(4) - done() + describe('#available', function () { + it('should return number of rentals available', function (done) { + var movie_id = 33; + Rentals.available(movie_id, function (error, costumers) { + expect(error).toBeNull + expect(costumers).toEqual(9) + done() + }) }) - }) -}) -describe('#find_customers_by_title', function () { - it('should return custumers info with a givin title', function (done) { - var title = "Jaws"; - Rentals.find_customers_by_title(title, function (error, costumers) { - expect(error).toBeNull - expect(Array.isArray(costumers)).toEqual(true) - expect(typeof costumers[0]).toEqual('object') - expect(costumers[0].name).toEqual('Curran Stout') - expect(costumers[0].registered_at).toEqual('Wed, 16 Apr 2014 21:40:20 -0700') - expect(costumers[0].address).toEqual('Ap #658-1540 Erat Rd.') - expect(costumers[0].city).toEqual('San Francisco') - expect(costumers[0].state).toEqual('California') - expect(costumers[0].postal_code).toEqual('94267') - done() + it('should return number of rentals available', function (done) { + var movie_id = 2; + Rentals.available(movie_id, function (error, result) { + expect(error).toBeNull + expect(result).toEqual(4) + done() + }) }) }) - it('should return an empty array if no customers were found', function (done) { - var title = "Titanic"; - Rentals.find_customers_by_title(title, function (error, costumers) { - expect(error).toBeNull - expect(Array.isArray(costumers)).toEqual(true) - expect(costumers).toEqual([]) - done() + describe('#find_customers_by_title', function () { + it('should return custumers info with a given title', function (done) { + var title = "Jaws"; + Rentals.find_customers_by_title(title, function (error, costumers) { + expect(error).toBeNull + expect(Array.isArray(costumers)).toEqual(true) + expect(typeof costumers[0]).toEqual('object') + expect(costumers[0].name).toEqual('Curran Stout') + expect(costumers[0].registered_at).toEqual('Wed, 16 Apr 2014 21:40:20 -0700') + expect(costumers[0].address).toEqual('Ap #658-1540 Erat Rd.') + expect(costumers[0].city).toEqual('San Francisco') + expect(costumers[0].state).toEqual('California') + expect(costumers[0].postal_code).toEqual('94267') + expect(costumers[0].account_credit).toEqual('35.66') + + done() + }) + }) + + it('should return an empty array if no customers were found', function (done) { + var title = "Titanic"; + Rentals.find_customers_by_title(title, function (error, costumers) { + expect(error).toBeNull + expect(Array.isArray(costumers)).toEqual(true) + expect(costumers).toEqual([]) + done() + }) }) - }) - it('should return an empty array if given a bad request', function (done) { - var title = "melissa"; - Rentals.find_customers_by_title(title, function (error, costumers) { - expect(error).toBeNull - expect(Array.isArray(costumers)).toEqual(true) - expect(costumers).toEqual([]) - done() + it('should return an empty array if given a bad request', function (done) { + var title = "melissa"; + Rentals.find_customers_by_title(title, function (error, costumers) { + expect(error).toBeNull + expect(Array.isArray(costumers)).toEqual(true) + expect(costumers).toEqual([]) + done() + }) }) }) -}) +}); From 71822d324b9c0a8a59fe0eca49374e4fc541f014 Mon Sep 17 00:00:00 2001 From: melissajimison Date: Fri, 24 Jun 2016 10:22:20 -0700 Subject: [PATCH 090/118] trying to update the customer account --- models/rentals.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/models/rentals.js b/models/rentals.js index 93e220757..64bcb2404 100644 --- a/models/rentals.js +++ b/models/rentals.js @@ -52,13 +52,24 @@ Rentals.find_customers_by_title = function(title, callback) { }; Rentals.mark_as_checkout = function(movie, customer_id, callback) { - // movie = "Jaws" db.sql.rentals.checkout([customer_id, movie], function(error, result){ if(error) { callback(error, undefined) } else { - callback(null, result) + Rentals.update_custumer_credit(result, callback) }; }); }; + + +Rentals.update_custumer_credit = function (result, callback) { + var bonus = 0.50 + var customer_id =result.customer_id + db.run("update customers set account_credit =$1 where id=$2", [bonus, customer_id], function (error, customer_updated) { + if (error) { callback(error, undefined) } + callback(null, result) + }) +} + + module.exports = Rentals From 0e3961f8418212500d53dec1f956f641ce463555 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Thu, 23 Jun 2016 14:47:27 -0700 Subject: [PATCH 091/118] removed distinct from select clause so that all entries are found even if customer_id and rental_id are the same (seed file has duplicates, so there should be multiple entries like this) --- db/sql/movies/historyCustomersByDate.sql | 2 +- db/sql/movies/historyCustomersByName.sql | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/db/sql/movies/historyCustomersByDate.sql b/db/sql/movies/historyCustomersByDate.sql index eb3374076..c1c537856 100644 --- a/db/sql/movies/historyCustomersByDate.sql +++ b/db/sql/movies/historyCustomersByDate.sql @@ -1,4 +1,4 @@ -SELECT DISTINCT customers.id, customers.name, customers.phone, customers.account_credit, history.checkout_date FROM movies +SELECT customers.id, customers.name, customers.phone, customers.account_credit, history.checkout_date FROM movies INNER JOIN rentals ON movies.id=rentals.movie_id INNER JOIN history diff --git a/db/sql/movies/historyCustomersByName.sql b/db/sql/movies/historyCustomersByName.sql index a662ae3b2..e78bfa135 100644 --- a/db/sql/movies/historyCustomersByName.sql +++ b/db/sql/movies/historyCustomersByName.sql @@ -1,4 +1,4 @@ -SELECT DISTINCT customers.id, customers.name, customers.phone, customers.account_credit, history.checkout_date FROM movies +SELECT customers.id, customers.name, customers.phone, customers.account_credit, history.checkout_date FROM movies INNER JOIN rentals ON movies.id=rentals.movie_id INNER JOIN history From cc9435d05d5025f137be31a21520e56e1aa47c36 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Thu, 23 Jun 2016 15:26:55 -0700 Subject: [PATCH 092/118] added a default order_by for customer history. if an invalid sort option is given, it will sort by id --- db/sql/movies/historyCustomersDefault.sql | 9 +++++++++ models/movies.js | 2 ++ 2 files changed, 11 insertions(+) create mode 100644 db/sql/movies/historyCustomersDefault.sql diff --git a/db/sql/movies/historyCustomersDefault.sql b/db/sql/movies/historyCustomersDefault.sql new file mode 100644 index 000000000..b0678fcf1 --- /dev/null +++ b/db/sql/movies/historyCustomersDefault.sql @@ -0,0 +1,9 @@ +SELECT customers.id, customers.name, customers.phone, customers.account_credit, history.checkout_date FROM movies + INNER JOIN rentals + ON movies.id=rentals.movie_id + INNER JOIN history + ON rentals.id=history.rental_id + INNER JOIN customers + ON history.customer_id=customers.id +WHERE title = $1 +ORDER BY customers.id; diff --git a/models/movies.js b/models/movies.js index 75a5ebd22..363ac7c72 100644 --- a/models/movies.js +++ b/models/movies.js @@ -38,6 +38,8 @@ Movies.find_customers_by_history = function(title, order_by, callback) { db.sql.movies.historyCustomersByName(title, customer_callback(callback)); } else if (order_by === 'checkout_date') { db.sql.movies.historyCustomersByDate(title, customer_callback(callback)); + } else { + db.sql.movies.historyCustomersDefault(title, customer_callback(callback)); } } From 2282f59557186f0bee8ffda61a558e54b4762b40 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Thu, 23 Jun 2016 15:27:21 -0700 Subject: [PATCH 093/118] added tests for movies model --- spec/models/movies.spec.js | 66 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 3 deletions(-) diff --git a/spec/models/movies.spec.js b/spec/models/movies.spec.js index af9f69335..da15edb83 100644 --- a/spec/models/movies.spec.js +++ b/spec/models/movies.spec.js @@ -2,6 +2,7 @@ var app = require("../../app"); var db = app.get("db"); var Movies = require('../../models/movies'); +var Customers = require('../../models/customers'); describe('Movies', function () { var options_release = { @@ -17,7 +18,6 @@ describe('Movies', function () { }; afterEach(function () { - // delete all the accounts I created db.end(); }); @@ -59,8 +59,8 @@ describe('Movies', function () { expect(movies[0].title).toEqual("2001: A Space Odyssey"); expect(movies[0].id).toEqual(40); done(); - }) - }) + }); + }); }); describe('#find', function () { @@ -76,6 +76,66 @@ describe('Movies', function () { }); }); + describe('#find_customers_by_title', function() { + it('should return an Array of Customer instances', function(done) { + Movies.find_customers_by_title("Jaws", function(error, customers) { + expect(error).toBeNull; + expect(customers).toEqual(jasmine.any(Array)); + expect(customers[0]).toEqual(jasmine.any(Customers)); + done(); + }); + }); + + it('should return only name, phone, and account_credit info for each customer', function(done) { + Movies.find_customers_by_title("Jaws", function(error, customers) { + expect(customers[0].name).toNotBe(undefined); + expect(customers[0].phone).toNotBe(undefined); + expect(customers[0].account_credit).toNotBe(undefined); + done(); + }); + }); + }); + + describe('#find_customers_by_history', function() { + it('should return an Array of Customer instances', function(done) { + Movies.find_customers_by_history("Jaws", "name", function(error, customers) { + expect(error).toBeNull; + expect(customers).toEqual(jasmine.any(Array)); + expect(customers[0]).toEqual(jasmine.any(Customers)); + done(); + }); + }); + + it('returns 8 instances of history for the movie Psycho', function(done) { + Movies.find_customers_by_history("Psycho", "name", function(error, customers) { + expect(customers.length).toEqual(8); + done(); + }); + }); + + it('can order by name', function(done) { + Movies.find_customers_by_history("Psycho", "name", function(error, customers) { + expect(customers[0].id).toEqual(11); + expect(customers[customers.length-1].id).toEqual(1); + done(); + }); + }); + + it('can order by checkout_date', function(done) { + Movies.find_customers_by_history("Psycho", "checkout_date", function(error, customers) { + expect(customers[0].id).toEqual(2); + expect(customers[customers.length-1].id).toEqual(3); + done(); + }); + }); + it('orders by id by default if an invalid sort column was given', function(done) { + Movies.find_customers_by_history("Psycho", "blahblahblah", function(error, customers) { + expect(customers[0].id).toEqual(1); + expect(customers[customers.length-1].id).toEqual(11); + done(); + }); + }); + }); }); From 53b3f3d42a37cdd03fe7f9765ba055785f80b5e9 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Thu, 23 Jun 2016 16:04:43 -0700 Subject: [PATCH 094/118] changed to status 500 for errors --- controllers/movies_controller.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index 3a4e00d9f..baae0cde9 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -7,7 +7,7 @@ var MoviesController = { Movies.all(function(error, movies) { // this is a callback if(error) { var err = new Error("Error retrieving movies list:\n" + error.message); - err.status = 404; + err.status = 500; next(err); } else { var obj = {}; @@ -38,7 +38,7 @@ var MoviesController = { Movies.sort_by(options, function(error, movies) { // this is a callback if(error) { var err = new Error("No such movie"); - err.status = 404; + err.status = 500; next(err); } else { var obj = {}; @@ -59,7 +59,7 @@ var MoviesController = { Movies.find_customers_by_title(movie, function(error, customers) { if(error) { var err = new Error("No such movie"); - err.status = 404; + err.status = 500; next(err); } else { var obj = {}; @@ -88,7 +88,7 @@ var MoviesController = { Movies.find_customers_by_history(movie, order_by, function(error, customers) { if(error) { var err = new Error("No such movie: " + error.message); - err.status = 404; + err.status = 500; next(err); } else { var obj = {}; From 64aedb6db01767438b9868adb80b4777180685c9 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Thu, 23 Jun 2016 16:04:53 -0700 Subject: [PATCH 095/118] added tests for the movies controller --- spec/controllers/movies.spec.js | 106 ++++++++++++++++++++++++-------- 1 file changed, 81 insertions(+), 25 deletions(-) diff --git a/spec/controllers/movies.spec.js b/spec/controllers/movies.spec.js index e53f32307..bda50371b 100644 --- a/spec/controllers/movies.spec.js +++ b/spec/controllers/movies.spec.js @@ -1,42 +1,98 @@ var request = require('request'); var base_url = "http://localhost:3000/"; -describe("Endpoint at /", function () { - var url = function(endpoint) { - return base_url + "movies" + endpoint; - }; +describe('MoviesController', function() { + describe("Endpoint at /", function() { + var url = function(endpoint) { + return base_url + "movies" + endpoint; + }; - it('responds with a 200 status code', function (done) { - request.get(url('/'), function(error, response, body) { - expect(response.statusCode).toEqual(200); - done(); + it('responds with a 200 status code', function(done) { + request.get(url('/'), function(error, response, body) { + expect(JSON.parse(body).status).toEqual(200); + done(); + }); }); }); -}); -describe("Endpoint at /sort", function () { - var url = function(endpoint) { - return base_url + "movies/sort" + endpoint; - }; + describe("Endpoint at /sort", function() { + var url = function(endpoint) { + return base_url + "movies/sort" + endpoint; + }; + + it('responds with a 200 status code for a valid url', function(done) { + request.get(url('/title?n=10&p=1'), function(error, response, body) { + expect(JSON.parse(body).status).toEqual(200); + done(); + }); + }); + + it('responds with a 200 status code for released_date', function(done) { + request.get(url('/release_date?n=6&p=1'), function(error, response, body) { + expect(JSON.parse(body).status).toEqual(200); + done(); + }); + }); + + it('responds with a 200 status code for title', function(done) { + request.get(url('/title?n=6&p=1'), function(error, response, body) { + expect(JSON.parse(body).status).toEqual(200); + done(); + }); + }); - it('responds with a 200 status code', function (done) { - request.get(url('/title?n=10&p=1'), function(error, response, body) { - expect(response.statusCode).toEqual(200); - done(); + it('responds with a 500 status code for mispelling', function(done) { + request.get(url('/released-date?n=10&p=1'), function(error, response, body) { + expect(response.statusCode).toEqual(500); + done(); + }); }); }); - it('responds with a 200 status code released_date', function (done) { - request.get(url('/release_date?n=6&p=1'), function(error, response, body) { - expect(response.statusCode).toEqual(200); - done(); + describe("Endpoint at movies/:title/current", function() { + var url = function(endpoint) { + return base_url + "movies" + endpoint; + }; + + it('responds with a 200 status code for a valid movie', function(done) { + request.get(url('/Jaws/current'), function(error, response, body) { + expect(JSON.parse(body).status).toEqual(200); + done(); + }); + }); + + it('responds with a 204 status code for an invalid movie', function(done) { + request.get(url('/asdfasdfasdf/current'), function(error, response, body) { + expect(JSON.parse(body).status).toEqual(204); + done(); + }); }); }); - it('responds with a 200 status code for mispelling', function (done) { - request.get(url('/released-date?n=10&p=1'), function(error, response, body) { - expect(response.statusCode).toEqual(404); - done(); + describe("Endpoint at movies/:title/history/sort/:column", function() { + var url = function(endpoint) { + return base_url + "movies" + endpoint; + }; + + it('responds with a 200 status code for a valid movie', function(done) { + request.get(url('/Jaws/history/sort/name'), function(error, response, body) { + expect(JSON.parse(body).status).toEqual(200); + done(); + }); + }); + + it('responds with a 204 status code for an invalid movie', function(done) { + request.get(url('/asdfasdfasdf/history/sort/name'), function(error, response, body) { + expect(JSON.parse(body).status).toEqual(204); + done(); + }); + }); + + it('responds with a 200 status code for an invalid sort column (sorted by id by default)', function(done) { + request.get(url('/Jaws/history/sort/blahblah'), function(error, response, body) { + expect(JSON.parse(body).status).toEqual(200); + done(); + }); }); }); }); From 37a54e58e639917fc4cfaa3905ef50a435bd8c89 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Thu, 23 Jun 2016 16:38:51 -0700 Subject: [PATCH 096/118] DRY up movies controller. if sort column for movies/sort/:column path is invalid, sorts by id by default. updated spec to reflect default id sort change --- controllers/movies_controller.js | 102 ++++++++++--------------------- spec/controllers/movies.spec.js | 4 +- 2 files changed, 33 insertions(+), 73 deletions(-) diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index baae0cde9..28812d29a 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -4,74 +4,30 @@ var Customers = require('../models/customers'); var MoviesController = { index: function(req, res, next) { - Movies.all(function(error, movies) { // this is a callback - if(error) { - var err = new Error("Error retrieving movies list:\n" + error.message); - err.status = 500; - next(err); - } else { - var obj = {}; - if (movies.length === 0) { - obj["status"] = 204; - } else { - obj["status"] = 200; - } - obj["movies"] = movies; - res.json(obj); - } - }); + Movies.all(callback(res, "movies")); }, sort: function(req, res, next) { - // // GET /search?q=tobi+ferret - // req.query.q - // // => "tobi ferret" - - //Send in an ORDER clause and a LIMIT with OFFSET + var order_by = req.params.column; var options = { limit : req.query.n, - order : req.params.column, offset: req.query.p }; + + if (order_by === "release_date" || order_by === "release-date") { + options["order"] = "release_date"; + } else if (order_by === "title") { + options["order"] = "title"; + } else { + options["order"] = "id"; + } - // products ordered in descending fashion - Movies.sort_by(options, function(error, movies) { // this is a callback - if(error) { - var err = new Error("No such movie"); - err.status = 500; - next(err); - } else { - var obj = {}; - if (movies.length === 0) { - obj["status"] = 204; - } else { - obj["status"] = 200; - } - obj["movies"] = movies; - res.json(obj); - } - }); + Movies.sort_by(options, callback(res, "movies")); }, current: function(req, res, next) { var movie = req.params.title; - - Movies.find_customers_by_title(movie, function(error, customers) { - if(error) { - var err = new Error("No such movie"); - err.status = 500; - next(err); - } else { - var obj = {}; - if (customers.length === 0) { - obj["status"] = 204; - } else { - obj["status"] = 200; - } - obj["customers"] = customers; - res.json(obj); - } - }) + Movies.find_customers_by_title(movie, callback(res, "customers")); }, history: function(req, res, next) { @@ -85,24 +41,28 @@ var MoviesController = { order_by = 'checkout_date'; } - Movies.find_customers_by_history(movie, order_by, function(error, customers) { - if(error) { - var err = new Error("No such movie: " + error.message); - err.status = 500; - next(err); + Movies.find_customers_by_history(movie, order_by, callback(res, "customers")); + } +}; + +function callback(res, type) { + return function(error, result) { // this is a callback + if(error) { + var err = new Error("Error retrieving results:\n" + error.message); + err.status = 500; + res.render(err); + } else { + var obj = {}; + if (result.length === 0) { + obj["status"] = 204; } else { - var obj = {}; - if (customers.length === 0) { - obj["status"] = 204; - } else { - obj["status"] = 200; - } - obj["customers"] = customers; - res.json(obj); + obj["status"] = 200; } - }) + obj[type] = result; + res.json(obj); + } } -}; +} module.exports = MoviesController; diff --git a/spec/controllers/movies.spec.js b/spec/controllers/movies.spec.js index bda50371b..07052018c 100644 --- a/spec/controllers/movies.spec.js +++ b/spec/controllers/movies.spec.js @@ -41,9 +41,9 @@ describe('MoviesController', function() { }); }); - it('responds with a 500 status code for mispelling', function(done) { + it('responds with a 200 status code for mispelling (by default sorts by id if invalid sort column given)', function(done) { request.get(url('/released-date?n=10&p=1'), function(error, response, body) { - expect(response.statusCode).toEqual(500); + expect(response.statusCode).toEqual(200); done(); }); }); From fa037318c6bedfc45bf4a1d87bed5378c458ecc6 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Fri, 24 Jun 2016 09:29:42 -0700 Subject: [PATCH 097/118] removed a test --- spec/models/movies.spec.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/spec/models/movies.spec.js b/spec/models/movies.spec.js index da15edb83..334dcec7e 100644 --- a/spec/models/movies.spec.js +++ b/spec/models/movies.spec.js @@ -106,13 +106,6 @@ describe('Movies', function () { }); }); - it('returns 8 instances of history for the movie Psycho', function(done) { - Movies.find_customers_by_history("Psycho", "name", function(error, customers) { - expect(customers.length).toEqual(8); - done(); - }); - }); - it('can order by name', function(done) { Movies.find_customers_by_history("Psycho", "name", function(error, customers) { expect(customers[0].id).toEqual(11); From 188f34781314d059b46d41c322f78d5e2c66cda3 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Fri, 24 Jun 2016 10:14:49 -0700 Subject: [PATCH 098/118] #create_record returns an instance of History --- models/history.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/models/history.js b/models/history.js index 84cabc493..240b9ab26 100644 --- a/models/history.js +++ b/models/history.js @@ -8,6 +8,7 @@ var History = function(history) { this.customer_id = history.customer_id; this.checkout_date = history.checkout_date; this.return_date = history.return_date; + this.overdue = history.overdue; }; @@ -26,7 +27,7 @@ History.create_record = function(rental_id, customer_id, callback) { if(error) { callback(error, undefined); } else { - callback(null, inserted); + callback(null, new History(inserted)); } }); } From f485379ef43a63a8c1f8fc32eeb8b7a38473edf6 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Fri, 24 Jun 2016 10:14:59 -0700 Subject: [PATCH 099/118] added tests for history model --- spec/models/history.spec.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 spec/models/history.spec.js diff --git a/spec/models/history.spec.js b/spec/models/history.spec.js new file mode 100644 index 000000000..aa1a8a7ac --- /dev/null +++ b/spec/models/history.spec.js @@ -0,0 +1,25 @@ +var app = require("../../app"); +var db = app.get("db"); + +var History = require('../../models/history'); + +describe('History', function () { + afterEach(function () { + db.end(); + }); + + describe('#create_record', function () { + it('returns a History instance that has all properties defined', function (done) { + History.create_record(1, 1, function(error, result) { + expect(error).toBeNull; + expect(result.id).toNotBe(null); + expect(result.rental_id).toNotBe(null); + expect(result.customer_id).toNotBe(null); + expect(result.checkout_date).toNotBe(null); + expect(result.return_date).toNotBe(null); + expect(result.overdue).toBe(false); + db.history.destroy({id: result.id}, function(err, res){done()}); + }); + }); + }); +}); From 6c3a17a1b0a35023beb62055db188f9de89ae20d Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Fri, 24 Jun 2016 11:27:43 -0700 Subject: [PATCH 100/118] added status codes customers controller methods and updated history method so it returns a status as well as history array --- controllers/customers_controller.js | 74 ++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 23 deletions(-) diff --git a/controllers/customers_controller.js b/controllers/customers_controller.js index 1c980f0ff..5cbd68da5 100644 --- a/controllers/customers_controller.js +++ b/controllers/customers_controller.js @@ -10,32 +10,53 @@ var CustomersController = { err.status = 500; next(err); } else { - res.json(customers); - // var locals = { accounts : accounts}; - // res.render("accounts/index",locals); + var obj = {}; + if (customers.length === 0) { + obj["status"] = 204; + } else { + obj["status"] = 200; } + } + obj["customers"] = customers; + res.json(obj); }); }, + // the word phone breaks the code: sort: function(req, res, next) { - //Send in an ORDER clause and a LIMIT with OFFSET - var options = { - limit : req.query.n, - order : req.params.column, - offset: req.query.p - }; - // products ordered in descending fashion - // takes 2 parameters - // options and the callback fucntion - Customers.sort_by(options, function(error, customers) { // this will return and array of customers - if(error) { - var err = new Error("No such customer"); - err.status = 404; - next(err); - } else { - res.json(customers); - } - }); + if(req.params.column === "name" || req.params.column === "registered_at" || req.params.column === "postal_code") { + + //Send in an ORDER clause and a LIMIT with OFFSET + var options = { + limit : req.query.n, + order : req.params.column, + offset: req.query.p + }; + // products ordered in descending fashion + // takes 2 parameters + // options and the callback fucntion + Customers.sort_by(options, function(error, customers) { // this will return and array of customers + if(error) { + var err = new Error("No such customer"); + err.status = 404; + next(err); + } else { + var obj = {}; + if (customers.length === 0) { + obj["status"] = 204; + } else { + obj["status"] = 200; + } + obj["customers"] = customers; + res.json(obj); + } + }); + } else { + var obj = {}; + obj["status"] = 400; + obj["message"] = "invalid request"; + res.json(obj); + } }, // this is a property whose value is a function (this is now a method) // this is the response from node @@ -60,10 +81,17 @@ var CustomersController = { err.status = 404; next(err); } else { - res.json(history); // write out the history as json + var obj = {}; + if (history.length === 0) { + obj["status"] = 204; + } else { + obj["status"] = 200; + } + obj["history"] = history; + res.json(obj); // write out the history as json } }); - }, + } }; module.exports = CustomersController; From ea1a68237c029dbb95b8d75339212bb33296c647 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Fri, 24 Jun 2016 11:29:20 -0700 Subject: [PATCH 101/118] added a space --- json.js | 1 + 1 file changed, 1 insertion(+) diff --git a/json.js b/json.js index 78a217239..928bef3ae 100644 --- a/json.js +++ b/json.js @@ -240,4 +240,5 @@ var docs = { } }; + module.exports = docs; From 276aec5d3c80fd4fd286779691f4045cd75b3ae0 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Fri, 24 Jun 2016 13:49:40 -0700 Subject: [PATCH 102/118] added overdue functionality. get customer name, movie title, checkout date, and return date at /rentals/overdue --- db/sql/rentals/overdue.sql | 13 +++++++++++++ models/rentals.js | 10 ++++++++++ routes/rentals.js | 6 ++++-- 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 db/sql/rentals/overdue.sql diff --git a/db/sql/rentals/overdue.sql b/db/sql/rentals/overdue.sql new file mode 100644 index 000000000..82fe943ed --- /dev/null +++ b/db/sql/rentals/overdue.sql @@ -0,0 +1,13 @@ +-- customer name +-- movie title +-- history check-out date +-- history return date + +SELECT customers.name, movies.title, history.checkout_date, history.return_date FROM history + INNER JOIN rentals + ON history.rental_id = rentals.id + INNER JOIN movies + ON rentals.movie_id = movies.id + INNER JOIN customers + ON history.customer_id = customers.id +WHERE history.overdue = true; \ No newline at end of file diff --git a/models/rentals.js b/models/rentals.js index 64bcb2404..8d53ece05 100644 --- a/models/rentals.js +++ b/models/rentals.js @@ -68,6 +68,16 @@ Rentals.update_custumer_credit = function (result, callback) { db.run("update customers set account_credit =$1 where id=$2", [bonus, customer_id], function (error, customer_updated) { if (error) { callback(error, undefined) } callback(null, result) + }); +} + +Rentals.get_overdue = function(callback) { + db.sql.rentals.overdue(function(error, customers) { + if(error) { + callback(error, undefined); + } else { + callback(null, customers); + } }) } diff --git a/routes/rentals.js b/routes/rentals.js index d579d2e14..bf0a73213 100644 --- a/routes/rentals.js +++ b/routes/rentals.js @@ -2,9 +2,13 @@ var express = require('express'); var router = express.Router(); var RentalsController = require('../controllers/rentals_controller.js'); +// GET overdue rentals +router.get('/overdue', RentalsController.overdue); + /* GET rentals listing by title. */ // Look a movie up by title to see (/rentals/Jaws) router.get('/:title', RentalsController.find_movie); + /* GET customers details. */ // router.get('/:id/current', RentalsController.current); @@ -12,6 +16,4 @@ router.get('/:title/customers', RentalsController.current_customers); router.post('/:title/check-out/:id', RentalsController.checkout); - - module.exports = router; From 97cfe5646e7f9fe698d673ccb84541f2c88d9b06 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Fri, 24 Jun 2016 13:50:57 -0700 Subject: [PATCH 103/118] forgot to add with last commit. overdue functionality for rentals --- controllers/rentals_controller.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/controllers/rentals_controller.js b/controllers/rentals_controller.js index 931a4334f..c585d9eaa 100644 --- a/controllers/rentals_controller.js +++ b/controllers/rentals_controller.js @@ -75,6 +75,20 @@ var RentalsController = { }); } }); + }, + + // include customer name, movie title, check-out date, and return date + overdue: function(req, res, next) { + Rentals.get_overdue(function(error, customers) { + if(error) { + var err = new Error(error.message); + err.status = 500; + next(err); + } else { + res.json(customers); + } + }) } }; + module.exports = RentalsController; From 2fb800d771b9b9d2bd9c827a2808b4319764a453 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Fri, 24 Jun 2016 14:17:21 -0700 Subject: [PATCH 104/118] added status code to json response for overdue path --- controllers/rentals_controller.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/controllers/rentals_controller.js b/controllers/rentals_controller.js index c585d9eaa..817225638 100644 --- a/controllers/rentals_controller.js +++ b/controllers/rentals_controller.js @@ -85,7 +85,14 @@ var RentalsController = { err.status = 500; next(err); } else { - res.json(customers); + obj = {}; + if (customers.length === 0) { + obj["status"] = 204; + } else { + obj["status"] = 200; + } + obj["customers"] = customers + res.json(obj); } }) } From 8addb9c192d0b22be9afe19c710db7dc7b3aeceb Mon Sep 17 00:00:00 2001 From: melissajimison Date: Fri, 24 Jun 2016 14:22:53 -0700 Subject: [PATCH 105/118] mark_as_checkout and updatecustomer working crispy --- controllers/rentals_controller.js | 10 +++++++--- db/sql/rentals/updatecustomer.sql | 4 ++++ models/rentals.js | 22 ++++++++++++---------- 3 files changed, 23 insertions(+), 13 deletions(-) create mode 100644 db/sql/rentals/updatecustomer.sql diff --git a/controllers/rentals_controller.js b/controllers/rentals_controller.js index 931a4334f..71d41afee 100644 --- a/controllers/rentals_controller.js +++ b/controllers/rentals_controller.js @@ -54,7 +54,7 @@ var RentalsController = { var movie = req.params.title; - Rentals.mark_as_checkout (movie, customer_id, function(error, rental_array) { + Rentals.mark_as_checkout (movie, customer_id, function(error, rental_array, customer_updated) { if(error) { var err = new Error(error.message); err.status = 404; @@ -68,8 +68,12 @@ var RentalsController = { next(err); } else { obj = {} - obj["status"] = 200; - obj["message"] = history_result; + obj["Status"] = 200; + obj["Message"] = "Checkout succesfully processed" + obj["Return day"] = history_result["return_date"] + obj["Customer's Name"] = customer_updated[0]["name"] + obj["Customer's Credit"] = customer_updated[0]["account_credit"] + res.json(obj); } }); diff --git a/db/sql/rentals/updatecustomer.sql b/db/sql/rentals/updatecustomer.sql new file mode 100644 index 000000000..3d30f81c0 --- /dev/null +++ b/db/sql/rentals/updatecustomer.sql @@ -0,0 +1,4 @@ +UPDATE customers +SET account_credit= account_credit + $1 +WHERE id=$2 +RETURNING*; diff --git a/models/rentals.js b/models/rentals.js index 64bcb2404..f7df69a5f 100644 --- a/models/rentals.js +++ b/models/rentals.js @@ -56,20 +56,22 @@ Rentals.mark_as_checkout = function(movie, customer_id, callback) { if(error) { callback(error, undefined) } else { - Rentals.update_custumer_credit(result, callback) + Rentals.update_custumer_credit(result, customer_id, callback) }; }); }; - -Rentals.update_custumer_credit = function (result, callback) { +Rentals.update_custumer_credit = function (result, customer_id, callback) { + console.log(result); var bonus = 0.50 - var customer_id =result.customer_id - db.run("update customers set account_credit =$1 where id=$2", [bonus, customer_id], function (error, customer_updated) { - if (error) { callback(error, undefined) } - callback(null, result) - }) -} - + var customer_id = Number(customer_id) + db.sql.rentals.updatecustomer([bonus, customer_id], function (error, customer_updated) { + if (error) { + callback(error, undefined) + } else { + callback(null, result, customer_updated) + }; + }); +}; module.exports = Rentals From d912c35af6a28049fe8c2ea0a6abda7c8d6fb265 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Fri, 24 Jun 2016 15:09:39 -0700 Subject: [PATCH 106/118] updated history seed file to include movies currently rented in rentals table --- db/seeds/history.json | 82 ++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 47 deletions(-) diff --git a/db/seeds/history.json b/db/seeds/history.json index bbc97b18e..c33d3d1d0 100644 --- a/db/seeds/history.json +++ b/db/seeds/history.json @@ -1,86 +1,74 @@ [ { -"rental_id": 1, -"customer_id": 1, -"checkout_date": "2016-06-16", -"return_date": "2016-06-26", -"overdue": false +"rental_id": 33, +"customer_id": 2, +"checkout_date": "4/16/2016", +"return_date": "4/26/2016" }, { -"rental_id": 7, -"customer_id": 3, -"checkout_date": "2016-06-17", -"return_date": "2016-06-27", -"overdue": false +"rental_id": 9, +"customer_id": 2, +"checkout_date": "6/17/2016", +"return_date": "6/27/2016" }, { -"rental_id": 12, +"rental_id": 15, "customer_id": 2, -"checkout_date": "2016-06-18", -"return_date": "2016-06-28", -"overdue": false +"checkout_date": "6/18/2016", +"return_date": "6/28/2016" }, { -"rental_id": 4, -"customer_id": 1, -"checkout_date": "2016-06-19", -"return_date": "2016-06-29", -"overdue": false +"rental_id": 2, +"customer_id": 4, +"checkout_date": "2/19/2016", +"return_date": "2/29/2016" }, { -"rental_id": 9, -"customer_id": 11, -"checkout_date": "2016-06-10", -"return_date": "2016-06-20", -"overdue": true +"rental_id": 10, +"customer_id": 4, +"checkout_date": "6/10/2016", +"return_date": "6/20/2016" }, { -"rental_id": 14, -"customer_id": 2, -"checkout_date": "2016-06-06", -"return_date": "2016-06-16", -"overdue": true +"rental_id": 21, +"customer_id": 4, +"checkout_date": "6/6/2016", +"return_date": "6/16/2016" }, { "rental_id": 2, "customer_id": 1, -"checkout_date": "2016-06-16", -"return_date": "2016-06-26", -"overdue": false +"checkout_date": "6/16/2016", +"return_date": "6/26/2016" }, { "rental_id": 3, "customer_id": 3, -"checkout_date": "2016-07-10", -"return_date": "2016-07-20", -"overdue": true +"checkout_date": "7/10/2016", +"return_date": "7/20/2016" }, { "rental_id": 12, "customer_id": 2, -"checkout_date": "2016-03-13", -"return_date": "2016-04-23", -"overdue": false +"checkout_date": "3/13/2016", +"return_date": "4/23/2016" }, { "rental_id": 4, "customer_id": 1, -"checkout_date": "2016-06-16", -"return_date": "2016-06-26", -"overdue": false +"checkout_date": "6/16/2016", +"return_date": "6/26/2016" }, { "rental_id": 6, "customer_id": 11, -"checkout_date": "2016-06-16", -"return_date": "2016-06-26", -"overdue": false +"checkout_date": "6/16/2016", +"return_date": "6/26/2016" }, { "rental_id": 5, "customer_id": 2, -"checkout_date": "2016-06-12", -"return_date": "2016-06-22", -"overdue": false +"checkout_date": "6/12/2016", +"return_date": "6/22/2016" } ] From 4b7f7c0fdbf1a0ffd2fbc1b9f00d55d76a029018 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Fri, 24 Jun 2016 15:11:21 -0700 Subject: [PATCH 107/118] overdue method returns results of rentals that have a status 'rented' and whose return date is before today's date --- db/sql/{rentals => history}/overdue.sql | 5 ++++- models/rentals.js | 3 ++- 2 files changed, 6 insertions(+), 2 deletions(-) rename db/sql/{rentals => history}/overdue.sql (69%) diff --git a/db/sql/rentals/overdue.sql b/db/sql/history/overdue.sql similarity index 69% rename from db/sql/rentals/overdue.sql rename to db/sql/history/overdue.sql index 82fe943ed..9b166c5d4 100644 --- a/db/sql/rentals/overdue.sql +++ b/db/sql/history/overdue.sql @@ -10,4 +10,7 @@ SELECT customers.name, movies.title, history.checkout_date, history.return_date ON rentals.movie_id = movies.id INNER JOIN customers ON history.customer_id = customers.id -WHERE history.overdue = true; \ No newline at end of file +WHERE return_date < $1 AND rentals.status = 'rented'; + + +-- if the status in the rentals is 'rented' and the return date in the history table is before today \ No newline at end of file diff --git a/models/rentals.js b/models/rentals.js index 8d53ece05..de240e9b9 100644 --- a/models/rentals.js +++ b/models/rentals.js @@ -72,7 +72,8 @@ Rentals.update_custumer_credit = function (result, callback) { } Rentals.get_overdue = function(callback) { - db.sql.rentals.overdue(function(error, customers) { + var today = new Date().toLocaleDateString(); + db.sql.history.overdue([today], function(error, customers) { if(error) { callback(error, undefined); } else { From 780df8777ad8e414cbe92526c11866df46f59d18 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Fri, 24 Jun 2016 15:12:06 -0700 Subject: [PATCH 108/118] dates that are entered when inserting new history records in the table have a string date in the format MM/DD/YYYY using .toLocaleDateString method --- models/history.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/models/history.js b/models/history.js index 240b9ab26..58a189803 100644 --- a/models/history.js +++ b/models/history.js @@ -23,7 +23,7 @@ History.getPastRentalHistory = function(customer_id, callback) { }; History.create_record = function(rental_id, customer_id, callback) { - db.history.save({rental_id: rental_id, customer_id: customer_id, checkout_date: new Date(), return_date: make_return_date(), overdue: false}, function(error,inserted){ + db.history.save({rental_id: rental_id, customer_id: customer_id, checkout_date: new Date().toLocaleDateString(), return_date: make_return_date(), overdue: false}, function(error,inserted){ if(error) { callback(error, undefined); } else { @@ -36,7 +36,7 @@ function make_return_date() { var date = new Date(); var return_days = 10; date.setDate(date.getDate() + return_days); - return date; + return date.toLocaleDateString(); } From 89cf0e50c85b69877dbb7c3bbe0d10f261ea0802 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Fri, 24 Jun 2016 15:17:35 -0700 Subject: [PATCH 109/118] removed overdue column from the history table (not needed) --- db/setup/schema.sql | 3 +-- models/history.js | 4 +--- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/db/setup/schema.sql b/db/setup/schema.sql index b628c0ef7..5b0c54e78 100644 --- a/db/setup/schema.sql +++ b/db/setup/schema.sql @@ -41,8 +41,7 @@ CREATE TABLE history( rental_id integer, customer_id integer, checkout_date text, - return_date text, - overdue boolean + return_date text ); CREATE INDEX history_customer_id ON history (customer_id); diff --git a/models/history.js b/models/history.js index 58a189803..54d496a12 100644 --- a/models/history.js +++ b/models/history.js @@ -8,8 +8,6 @@ var History = function(history) { this.customer_id = history.customer_id; this.checkout_date = history.checkout_date; this.return_date = history.return_date; - this.overdue = history.overdue; - }; History.getPastRentalHistory = function(customer_id, callback) { @@ -23,7 +21,7 @@ History.getPastRentalHistory = function(customer_id, callback) { }; History.create_record = function(rental_id, customer_id, callback) { - db.history.save({rental_id: rental_id, customer_id: customer_id, checkout_date: new Date().toLocaleDateString(), return_date: make_return_date(), overdue: false}, function(error,inserted){ + db.history.save({rental_id: rental_id, customer_id: customer_id, checkout_date: new Date().toLocaleDateString(), return_date: make_return_date()}, function(error,inserted){ if(error) { callback(error, undefined); } else { From 5b0212e7935c16d397bc5d36ddd97408d8e2f131 Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Fri, 24 Jun 2016 15:17:56 -0700 Subject: [PATCH 110/118] updated tests to reflect changes in seed files --- spec/models/movies.spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/models/movies.spec.js b/spec/models/movies.spec.js index 334dcec7e..894974fbf 100644 --- a/spec/models/movies.spec.js +++ b/spec/models/movies.spec.js @@ -108,7 +108,7 @@ describe('Movies', function () { it('can order by name', function(done) { Movies.find_customers_by_history("Psycho", "name", function(error, customers) { - expect(customers[0].id).toEqual(11); + expect(customers[0].id).toEqual(4); expect(customers[customers.length-1].id).toEqual(1); done(); }); @@ -116,7 +116,7 @@ describe('Movies', function () { it('can order by checkout_date', function(done) { Movies.find_customers_by_history("Psycho", "checkout_date", function(error, customers) { - expect(customers[0].id).toEqual(2); + expect(customers[0].id).toEqual(4); expect(customers[customers.length-1].id).toEqual(3); done(); }); From 4d81bb4a3f778fa3026032718f3e92276180e7de Mon Sep 17 00:00:00 2001 From: melissajimison Date: Fri, 24 Jun 2016 15:36:02 -0700 Subject: [PATCH 111/118] /rentals/:title/return/:id --- controllers/rentals_controller.js | 22 ++++++++++++++++++++-- db/sql/rentals/retunMovie.sql | 12 ++++++++++++ models/rentals.js | 13 +++++++++++++ routes/rentals.js | 4 +--- 4 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 db/sql/rentals/retunMovie.sql diff --git a/controllers/rentals_controller.js b/controllers/rentals_controller.js index 71d41afee..b6433106d 100644 --- a/controllers/rentals_controller.js +++ b/controllers/rentals_controller.js @@ -69,7 +69,7 @@ var RentalsController = { } else { obj = {} obj["Status"] = 200; - obj["Message"] = "Checkout succesfully processed" + obj["Message"] = "Checkout has been processed succesfully" obj["Return day"] = history_result["return_date"] obj["Customer's Name"] = customer_updated[0]["name"] obj["Customer's Credit"] = customer_updated[0]["account_credit"] @@ -79,6 +79,24 @@ var RentalsController = { }); } }); - } + }, + + return_a_rental: function(req, res, next) { + var customer_id = req.params.id; + var movie = req.params.title; + Rentals.return_rental(customer_id, movie, function(error, updated_rental) { + if(error) { + var err = new Error(error.message); + err.status = 404; + next(err); + } else { + obj = {} + obj["Status"] = 200; + obj["Message"] = "Return has been processed succesfully" + obj["Rental info"] = updated_rental[0] + res.json(obj); + } + }) + }, }; module.exports = RentalsController; diff --git a/db/sql/rentals/retunMovie.sql b/db/sql/rentals/retunMovie.sql new file mode 100644 index 000000000..fb01b9b13 --- /dev/null +++ b/db/sql/rentals/retunMovie.sql @@ -0,0 +1,12 @@ +UPDATE rentals AS r +SET customer_id = NULL, status='available' +FROM ( + SELECT rentals.id + FROM rentals + INNER JOIN movies + ON movies.id=rentals.movie_id + WHERE title=$2 AND rentals.customer_id = $1 + LIMIT 1 +) AS result +WHERE r.id=result.id +RETURNING *; diff --git a/models/rentals.js b/models/rentals.js index f7df69a5f..0bd92eae6 100644 --- a/models/rentals.js +++ b/models/rentals.js @@ -74,4 +74,17 @@ Rentals.update_custumer_credit = function (result, customer_id, callback) { }); }; +Rentals.return_rental = function(customer_id, movie, callback){ + //reach into the movies table, look by title, get movie_id, go to rentals, see that movie_id and customer_id matches + //change the status to available and delete the costumer-id or set it to cero + + db.sql.rentals.retunMovie([customer_id, movie], function(error, updated_rental) { + if(error || !updated_rental) { + callback(error || new Error("Could not retrieve updated_rental"), undefined); + } else { + callback(null, updated_rental); + } + }); +}; + module.exports = Rentals diff --git a/routes/rentals.js b/routes/rentals.js index d579d2e14..28390f88b 100644 --- a/routes/rentals.js +++ b/routes/rentals.js @@ -6,12 +6,10 @@ var RentalsController = require('../controllers/rentals_controller.js'); // Look a movie up by title to see (/rentals/Jaws) router.get('/:title', RentalsController.find_movie); /* GET customers details. */ -// router.get('/:id/current', RentalsController.current); - router.get('/:title/customers', RentalsController.current_customers); router.post('/:title/check-out/:id', RentalsController.checkout); - +router.post('/:title/return/:id', RentalsController.return_a_rental); module.exports = router; From 8c7d07231421ab06b1e1865983cf3222db4e50bc Mon Sep 17 00:00:00 2001 From: Lisa Rolczynski Date: Fri, 24 Jun 2016 15:39:18 -0700 Subject: [PATCH 112/118] added history tests --- spec/models/history.spec.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/spec/models/history.spec.js b/spec/models/history.spec.js index aa1a8a7ac..0135f7897 100644 --- a/spec/models/history.spec.js +++ b/spec/models/history.spec.js @@ -17,9 +17,22 @@ describe('History', function () { expect(result.customer_id).toNotBe(null); expect(result.checkout_date).toNotBe(null); expect(result.return_date).toNotBe(null); - expect(result.overdue).toBe(false); db.history.destroy({id: result.id}, function(err, res){done()}); }); }); }); + + describe('#getPastRentalHistory', function() { + it('returns an array of customer id, checkout date, and return date', function(done) { + History.getPastRentalHistory(1, function(error, result) { + expect(error).toBeNull; + expect(result).toEqual(jasmine.any(Array)); + expect(result[0].customer_id).toBe(1); + expect(result[0].checkout_date).toNotBe(null); + expect(result[0].return_date).toNotBe(null); + expect(Object.keys(result[0])).toEqual(['customer_id', 'checkout_date', 'return_date']); + done(); + }) + }) + }) }); From 62e30f56b9ed36262f501ce0136b9261903bb3ea Mon Sep 17 00:00:00 2001 From: Risha Date: Fri, 24 Jun 2016 15:44:25 -0700 Subject: [PATCH 113/118] created an html view and changed to json file vs js file --- app.js | 4 +- controllers/index_controller.js | 18 ++- doc.json | 241 ++++++++++++++++++++++++++++ json.js | 244 ----------------------------- package.json | 1 + routes/index.js | 7 +- spec/controllers/customers.spec.js | 30 ++-- spec/models/customers.spec.js | 22 +++ views/docs.ejs | 13 ++ views/{error.jade => error.ejs} | 0 views/{index.jade => index.ejs} | 0 views/{layout.jade => layout.ejs} | 0 12 files changed, 308 insertions(+), 272 deletions(-) create mode 100644 doc.json delete mode 100644 json.js create mode 100644 spec/models/customers.spec.js create mode 100644 views/docs.ejs rename views/{error.jade => error.ejs} (100%) rename views/{index.jade => index.ejs} (100%) rename views/{layout.jade => layout.ejs} (100%) diff --git a/app.js b/app.js index e6fa4f31a..8fd6d9460 100644 --- a/app.js +++ b/app.js @@ -12,12 +12,12 @@ var app = module.exports = express(); var connectionString = "postgres://localhost/videoStore"; // the node module that is used to access the database var db = massive.connectSync({connectionString: connectionString}); -// this gives me a way to grab the database from the code, like in the model +// this gives me a way to grab the database from the code, like in the model app.set('db', db); // view engine setup app.set('views', path.join(__dirname, 'views')); -app.set('view engine', 'jade'); +app.set('view engine', 'ejs'); // uncomment after placing your favicon in /public //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); diff --git a/controllers/index_controller.js b/controllers/index_controller.js index aa987be0a..ff1627c70 100644 --- a/controllers/index_controller.js +++ b/controllers/index_controller.js @@ -1,9 +1,21 @@ -var docs = require ("../json.js"); +var doc = require ("../doc.json"); + var IndexController = { - getjson: function (req, res, next) { - res.status(200).json(docs); + locals: { + documentation: doc + }, + + getJSON: function (req, res, next) { + res.json(200, doc); + }, + + getHTML: function(req, res, next) { + // console.log(string_docs); + res.render('docs', IndexController.locals); } }; + + module.exports = IndexController; diff --git a/doc.json b/doc.json new file mode 100644 index 000000000..6b9e7b03e --- /dev/null +++ b/doc.json @@ -0,0 +1,241 @@ +[ { + "customers": { + "list_all_customers": { + "routeName": "index", + "httpRequest": "GET /customers", + "requiredParameters": "none", + "optionalParameters": "none", + "intendedUse": "This endpoint contains a list of all customers in your database, generated at time of request.", + "dataBreakdown": { + "someDataFound": "returns the found data", + "noDataFound": "returns status 500, []", + "error": "Error" + } + } + }, + + "customers_sorted_by_name": { + "routeName": "index, sort", + "httpRequest": "GET /customers/sort/cloumn", + "requiredParameters": "none", + "optionalParameters": "n=? to limit the number of records returned, p=? for offset", + "intendedUse": "This endpoint returns a list of all customers in your database (generated at time of query), sorted by name. Query string n=? will limit the number of returned records, and p=? will offset your records by that number.", + "dataBreakdown": { + "someDataFound": "returns the found data", + "noDataFound": "Error", + "error": "Error" + } + }, + + "customers_sorted_by_postal_code": { + "routeName": "index, sort", + "httpRequest": "GET /customers/sort/column", + "requiredParameters": "none", + "optionalParameters": "n=? to limit the number of records returned, p=? for offset", + "intendedUse": "This endpoint returns a list of customers in your database (generated at time of query) sorted by zipcode. Query string n=? will limit the number of records returned, and p=? will offset your records by that number.", + "dataBreakdown": { + "someDataFound": "returns the found data", + "noDataFound": "Error", + "error": "Error" + } + }, + + "list_customer_checkouts": { + "routeName": "List Checkouts to Customer", + "httpRequest": "GET /customers/:id/current", + "requiredParameters": "customer id number", + "optionalParameters": "none", + "intendedUse": "Given a customer id, this endpoint returns a list (generated at time of query) of all checkouts a customer currently has.", + "dataBreakdown": { + "someDataFound": "returns the found data", + "noDataFound": "Error", + "error": "Error" + } + }, + + "customer_checkout_history": { + "routeName": "List Customer Checkout History", + "httpRequest": "GET /customers/:id/history", + "requiredParameters": "customer id number", + "optionalParameters": "none", + "intendedUse": "This endpoint returns a list (generated at time of query) of all checkouts a customer has previously made. (These are all films that have been returned.)", + "dataBreakdown": { + "someDataFound": "returns the found data", + "noDataFound": "Error", + "error": "Error" + } + }, + + "movies": { + "list_all_movies": { + "routeName": "List All Movies", + "httpRequest": "GET /movies", + "requiredParameters": "none", + "optionalParameters": "none", + "intendedUse": "This endpoint will return a list of all movies in your database, generated at time of query.", + "dataBreakdown": { + "someDataFound": "returns the found data", + "noDataFound": "Error", + "error": "Error" + } + }, + + "movies_sorted_by_title": { + "routeName": "List Movies, Sorted by Title", + "httpRequest": "GET /movies/sort/title", + "requiredParameters": "none", + "optionalParameters": "n=? to limit the number of records returned, p=? for offset", + "intendedUse": "This endpoint will return a list of all movies in your database, generated at time of query. Query string n=? will allow you to limit how many records are returned, and query string p=? will offset your records by that number.", + "dataBreakdown": { + "someDataFound": "returns the found data", + "noDataFound": "Error", + "error": "Error" + } + }, + + "movies_sorted_by_release": { + "routeName": "List Movies, Sorted by Title", + "httpRequest": "GET /movies/sort/title", + "requiredParameters": "none", + "optionalParameters": "n=? to limit the number of records returned, p=? for offset", + "intendedUse": "This endpoint will return a list of all movies in your database, generated at time of query. Query string n=? will allow you to limit how many records are returned, and query string p=? will offset your records by that number.", + "dataBreakdown": { + "someDataFound": "returns the found data", + "noDataFound": "Error", + "error": "Error" + } + }, + + "movie_details": { + "routeName": "View Details for Title", + "httpRequest": "GET /movies/:title", + "requiredParameters": "film title", + "optionalParameters": "none", + "intendedUse": "This endpoint returns the details for a movie title. It will show title, summary description, release date, number of copies owned, and number of copies in stock. Generated at time of query.", + "dataBreakdown": { + "someDataFound": "returns the found data", + "noDataFound": "Error", + "error": "Error" + } + }, + + "movie_checkouts": { + "routeName": "View Checkouts for Title", + "httpRequest": "GET /movies/:title/current", + "requiredParameters": "film title", + "optionalParameters": "none", + "intendedUse": "This endpoint returns the the current checkout records for the given film. It will return customer name, phone number, and account credit for each checked out copy of the film.", + "dataBreakdown": { + "someDataFound": "returns the found data", + "noDataFound": "Error", + "error": "Error" + } + }, + + "movie_hist_name_sort": { + "routeName": "View Historical Checkouts for Title by Name", + "httpRequest": "GET /movies/:title/history/sort/name", + "requiredParameters": "film title", + "optionalParameters": "none", + "intendedUse": "This endpoint returns the the historical checkout records for the given film. It will return customer name, phone number, and account credit for each checked out copy of the film and is ordered by customer name.", + "dataBreakdown": { + "someDataFound": "returns the found data", + "noDataFound": "Error", + "error": "Error" + } + }, + + "movie_hist_checkout_sort": { + "routeName": "View Historical Checkouts for Title by Date", + "httpRequest": "GET /movies/:title/history/sort/date", + "requiredParameters": "film title", + "optionalParameters": "none", + "intendedUse": "This endpoint returns the the historical checkout records for the given film. It will return customer name, phone number, and account credit for each checked out copy of the film and is ordered by customer name.", + "dataBreakdown": { + "someDataFound": "returns the found data", + "noDataFound": "Error", + "error": "Error" + } + } + }, + + "rentals": { + "rental_checkouts": { + "routeName": "View Current Checkouts for Title", + "httpRequest": "GET /rentals/:title/current", + "requiredParameters": "film title", + "optionalParameters": "none", + "intendedUse": "This endpoint returns the the historical checkout records for the given film. It will return customer name, phone number, and account credit for each checked out copy of the film and is ordered by customer name.", + "dataBreakdown": { + "someDataFound": "returns the found data", + "noDataFound": "Error", + "error": "Error" + } + }, + + "rental_hist_name_sort": { + "routeName": "View Historical Checkouts for Title by Name", + "httpRequest": "GET /rentals/:title/history/sort/name", + "requiredParameters": "film title", + "optionalParameters": "none", + "intendedUse": "This endpoint returns the the historical checkout records for the given film. It will return customer name, phone number, and account credit for each checked out copy of the film and is ordered by customer name.", + "dataBreakdown": { + "someDataFound": "returns the found data", + "noDataFound": "Error", + "error": "Error" + } + }, + + "rental_hist_checkout_sort": { + "routeName": "View Historical Checkouts for Title by Date", + "httpRequest": "GET /rentals/:title/history/sort/date", + "requiredParameters": "film title", + "optionalParameters": "none", + "intendedUse": "This endpoint returns the the historical checkout records for the given film. It will return customer name, phone number, and account credit for each checked out copy of the film and is ordered by customer name.", + "dataBreakdown": { + "someDataFound": "returns the found data", + "noDataFound": "Error", + "error": "Error" + } + }, + + "checkout_film_to_cust": { + "routeName": "View Historical Checkouts for Title by Date", + "httpRequest": "POST /rentals/:title/checkout", + "requiredParameters": "film title in route, customer ID in post body", + "optionalParameters": "none", + "intendedUse": "This endpoint will create a new checkout record tying the customer id and film title together in the rentals database along with date checked out and due date. Current default rental period is 3 days.", + "dataBreakdown": { + "someDataFound": "returns the checkout record", + "noDataFound": "Error", + "error": "Error" + } + }, + + "checkin_film_from_cust": { + "routeName": "View Historical Checkouts for Title by Date", + "httpRequest": "POST /rentals/:title/return", + "requiredParameters": "film title in route, customer ID in post body", + "optionalParameters": "none", + "intendedUse": "This endpoint will update the checkout record tying the customer id and film title together with the date checked in. It will also deduct any fees from customer credit. (This can send customer credit negative, which means they owe money.", + "dataBreakdown": { + "someDataFound": "returns the checkout record", + "noDataFound": "Error", + "error": "Error" + } + }, + + "list_of_overdue_films": { + "routeName": "List All Overdue Films", + "httpRequest": "GET /rentals/overdue", + "requiredParameters": "none", + "optionalParameters": "none", + "intendedUse": "This endpoint will return a list (generated at time of query) of all films currently checked out and overdue.", + "dataBreakdown": { + "someDataFound": "returns the found data", + "noDataFound": "Error", + "error": "Error" + } + } + } +}] diff --git a/json.js b/json.js deleted file mode 100644 index 928bef3ae..000000000 --- a/json.js +++ /dev/null @@ -1,244 +0,0 @@ -var docs = { - customers: { - list_all_customers: { - routeName: 'index', - httpRequest: 'GET /customers', - requiredParameters: 'none', - optionalParameters: 'none', - intendedUse: 'This endpoint contains a list of all customers in your database, generated at time of request.', - dataBreakdown: { - someDataFound: 'returns the found data', - noDataFound: 'returns status 500, []', - error: 'Error' - } - } - }, - - customers_sorted_by_name: { - routeName: 'index, sort', - httpRequest: 'GET /customers/sort/cloumn', - requiredParameters: 'none', - optionalParameters: 'n=? to limit the number of records returned, p=? for offset', - intendedUse: 'This endpoint returns a list of all customers in your database (generated at time of query), sorted by name. Query string n=? will limit the number of returned records, and p=? will offset your records by that number.', - dataBreakdown: { - someDataFound: 'returns the found data', - noDataFound: 'Error', - error: 'Error' - } - }, - - customers_sorted_by_postal_code: { - routeName: 'index, sort', - httpRequest: 'GET /customers/sort/column', - requiredParameters: 'none', - optionalParameters: 'n=? to limit the number of records returned, p=? for offset', - intendedUse: 'This endpoint returns a list of customers in your database (generated at time of query) sorted by zipcode. Query string n=? will limit the number of records returned, and p=? will offset your records by that number.', - dataBreakdown: { - someDataFound: 'returns the found data', - noDataFound: 'Error', - error: 'Error' - } - }, - - list_customer_checkouts: { - routeName: 'List Checkouts to Customer', - httpRequest: 'GET /customers/:id/current', - requiredParameters: 'customer id number', - optionalParameters: 'none', - intendedUse: 'Given a customer id, this endpoint returns a list (generated at time of query) of all checkouts a customer currently has.', - dataBreakdown: { - someDataFound: 'returns the found data', - noDataFound: 'Error', - error: 'Error' - } - }, - - customer_checkout_history: { - routeName: 'List Customer Checkout History', - httpRequest: 'GET /customers/:id/history', - requiredParameters: 'customer id number', - optionalParameters: 'none', - intendedUse: 'This endpoint returns a list (generated at time of query) of all checkouts a customer has previously made. (These are all films that have been returned.)', - dataBreakdown: { - someDataFound: 'returns the found data', - noDataFound: 'Error', - error: 'Error' - } - }, - - movies: { - list_all_movies: { - routeName: 'List All Movies', - httpRequest: 'GET /movies', - requiredParameters: 'none', - optionalParameters: 'none', - intendedUse: 'This endpoint will return a list of all movies in your database, generated at time of query.', - dataBreakdown: { - someDataFound: 'returns the found data', - noDataFound: 'Error', - error: 'Error' - } - }, - - movies_sorted_by_title: { - routeName: 'List Movies, Sorted by Title', - httpRequest: 'GET /movies/sort/title', - requiredParameters: 'none', - optionalParameters: 'n=? to limit the number of records returned, p=? for offset', - intendedUse: 'This endpoint will return a list of all movies in your database, generated at time of query. Query string n=? will allow you to limit how many records are returned, and query string p=? will offset your records by that number.', - dataBreakdown: { - someDataFound: 'returns the found data', - noDataFound: 'Error', - error: 'Error' - } - }, - - movies_sorted_by_release: { - routeName: 'List Movies, Sorted by Title', - httpRequest: 'GET /movies/sort/title', - requiredParameters: 'none', - optionalParameters: 'n=? to limit the number of records returned, p=? for offset', - intendedUse: 'This endpoint will return a list of all movies in your database, generated at time of query. Query string n=? will allow you to limit how many records are returned, and query string p=? will offset your records by that number.', - dataBreakdown: { - someDataFound: 'returns the found data', - noDataFound: 'Error', - error: 'Error' - } - }, - - movie_details: { - routeName: 'View Details for Title', - httpRequest: 'GET /movies/:title', - requiredParameters: 'film title', - optionalParameters: 'none', - intendedUse: 'This endpoint returns the details for a movie title. It will show title, summary description, release date, number of copies owned, and number of copies in stock. Generated at time of query.', - dataBreakdown: { - someDataFound: 'returns the found data', - noDataFound: 'Error', - error: 'Error' - } - }, - - movie_checkouts: { - routeName: 'View Checkouts for Title', - httpRequest: 'GET /movies/:title/current', - requiredParameters: 'film title', - optionalParameters: 'none', - intendedUse: 'This endpoint returns the the current checkout records for the given film. It will return customer name, phone number, and account credit for each checked out copy of the film.', - dataBreakdown: { - someDataFound: 'returns the found data', - noDataFound: 'Error', - error: 'Error' - } - }, - - movie_hist_name_sort: { - routeName: 'View Historical Checkouts for Title by Name', - httpRequest: 'GET /movies/:title/history/sort/name', - requiredParameters: 'film title', - optionalParameters: 'none', - intendedUse: 'This endpoint returns the the historical checkout records for the given film. It will return customer name, phone number, and account credit for each checked out copy of the film and is ordered by customer name.', - dataBreakdown: { - someDataFound: 'returns the found data', - noDataFound: 'Error', - error: 'Error' - } - }, - - movie_hist_checkout_sort: { - routeName: 'View Historical Checkouts for Title by Date', - httpRequest: 'GET /movies/:title/history/sort/date', - requiredParameters: 'film title', - optionalParameters: 'none', - intendedUse: 'This endpoint returns the the historical checkout records for the given film. It will return customer name, phone number, and account credit for each checked out copy of the film and is ordered by customer name.', - dataBreakdown: { - someDataFound: 'returns the found data', - noDataFound: 'Error', - error: 'Error' - } - } - }, - - rentals: { - rental_checkouts: { - routeName: 'View Current Checkouts for Title', - httpRequest: 'GET /rentals/:title/current', - requiredParameters: 'film title', - optionalParameters: 'none', - intendedUse: 'This endpoint returns the the historical checkout records for the given film. It will return customer name, phone number, and account credit for each checked out copy of the film and is ordered by customer name.', - dataBreakdown: { - someDataFound: 'returns the found data', - noDataFound: 'Error', - error: 'Error' - } - }, - - rental_hist_name_sort: { - routeName: 'View Historical Checkouts for Title by Name', - httpRequest: 'GET /rentals/:title/history/sort/name', - requiredParameters: 'film title', - optionalParameters: 'none', - intendedUse: 'This endpoint returns the the historical checkout records for the given film. It will return customer name, phone number, and account credit for each checked out copy of the film and is ordered by customer name.', - dataBreakdown: { - someDataFound: 'returns the found data', - noDataFound: 'Error', - error: 'Error' - } - }, - - rental_hist_checkout_sort: { - routeName: 'View Historical Checkouts for Title by Date', - httpRequest: 'GET /rentals/:title/history/sort/date', - requiredParameters: 'film title', - optionalParameters: 'none', - intendedUse: 'This endpoint returns the the historical checkout records for the given film. It will return customer name, phone number, and account credit for each checked out copy of the film and is ordered by customer name.', - dataBreakdown: { - someDataFound: 'returns the found data', - noDataFound: 'Error', - error: 'Error' - } - }, - - checkout_film_to_cust: { - routeName: 'View Historical Checkouts for Title by Date', - httpRequest: 'POST /rentals/:title/checkout', - requiredParameters: 'film title in route, customer ID in post body', - optionalParameters: 'none', - intendedUse: 'This endpoint will create a new checkout record tying the customer id and film title together in the rentals database along with date checked out and due date. Current default rental period is 3 days.', - dataBreakdown: { - someDataFound: 'returns the checkout record', - noDataFound: 'Error', - error: 'Error' - } - }, - - checkin_film_from_cust: { - routeName: 'View Historical Checkouts for Title by Date', - httpRequest: 'POST /rentals/:title/return', - requiredParameters: 'film title in route, customer ID in post body', - optionalParameters: 'none', - intendedUse: 'This endpoint will update the checkout record tying the customer id and film title together with the date checked in. It will also deduct any fees from customer credit. (This can send customer credit negative, which means they owe money.', - dataBreakdown: { - someDataFound: 'returns the checkout record', - noDataFound: 'Error', - error: 'Error' - } - }, - - list_of_overdue_films: { - routeName: 'List All Overdue Films', - httpRequest: 'GET /rentals/overdue', - requiredParameters: 'none', - optionalParameters: 'none', - intendedUse: 'This endpoint will return a list (generated at time of query) of all films currently checked out and overdue.', - dataBreakdown: { - someDataFound: 'returns the found data', - noDataFound: 'Error', - error: 'Error' - } - } - } -}; - - -module.exports = docs; diff --git a/package.json b/package.json index 02935d05b..e67c6bb4e 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "body-parser": "~1.13.2", "cookie-parser": "~1.3.5", "debug": "~2.2.0", + "ejs": "^2.4.2", "express": "~4.13.1", "jade": "~1.11.0", "massive": "^2.3.0", diff --git a/routes/index.js b/routes/index.js index 4126eaf1d..45de1a885 100644 --- a/routes/index.js +++ b/routes/index.js @@ -11,10 +11,9 @@ router.get('/zomg', function(req, res, next) { res.status(200).json({it_works: 'it works!'}); }); -// router.get('/api/docs.json', function (req, res, next){ -// res.status(200).json(docs); -// }); +router.get('/api/docs.json', IndexController.getJSON); + +router.get('/api/docs', IndexController.getHTML); -router.get('/api/docs.json', IndexController.getjson); module.exports = router; diff --git a/spec/controllers/customers.spec.js b/spec/controllers/customers.spec.js index 9b16f02a4..6f663071a 100644 --- a/spec/controllers/customers.spec.js +++ b/spec/controllers/customers.spec.js @@ -1,5 +1,6 @@ var request = require('request'); -var base_url = "http://localhost:3000"; +var base_url = "http://localhost:3000/"; + describe("Endpoint at /", function () { var url = function(endpoint) { @@ -8,26 +9,17 @@ describe("Endpoint at /", function () { it('responds with a 200 status code', function (done) { request.get(url('/'), function(error, response, body) { - expect(response.statusCode).toEqual(200); + expect(JSON.parse(body).status).toEqual(200); done(); }); }); -}); - -// -// describe("the returned json data", function() { -// it('has the right keys', function(done) { -// request.get(base_url, function(error, response, body) { -// var data = JSON.parse(body); -// expect(Object.keys(data)).toEqual(['']); -// done(); -// }); -// }); - // it('responds with a 200 status code', function (done) { - // request.get(url('/'), function(error, response, body) { - // expect(response.statusCode).toEqual(200); - // done(); - // }); - // }); + it('the returned json data has the right keys', function(done) { + request.get('/', function(error, response, body) { + // var data = JSON.parse(body); + expect(body).toEqual("melissa"); + expect(Object.keys(data)).toEqual(['']); + done(); + }); + }); }); diff --git a/spec/models/customers.spec.js b/spec/models/customers.spec.js new file mode 100644 index 000000000..84922e688 --- /dev/null +++ b/spec/models/customers.spec.js @@ -0,0 +1,22 @@ +var app = require("../../app"); +var db = app.get("db"); + +var Customers = require('../../models/customers'); +var Movies = require('../../models/customers'); + +describe('Customers', function () { + afterEach(function () { + db.end(); + }); + + describe('#all', function () { + it('should return an array of customer instances', function (done) { + Customers.all(function (error, customers) { + expect(error).toBeNull + expect(customers).toEqual(jasmine.any(Array)); + expect(customers.length).toEqual(200); + done(); + }); + }); + }); +}); diff --git a/views/docs.ejs b/views/docs.ejs new file mode 100644 index 000000000..15d4a22f8 --- /dev/null +++ b/views/docs.ejs @@ -0,0 +1,13 @@ + + + + VideoStoreAPI Documentation + + + + +
+

VideoStoreAPI

+

API Documentation

+ + diff --git a/views/error.jade b/views/error.ejs similarity index 100% rename from views/error.jade rename to views/error.ejs diff --git a/views/index.jade b/views/index.ejs similarity index 100% rename from views/index.jade rename to views/index.ejs diff --git a/views/layout.jade b/views/layout.ejs similarity index 100% rename from views/layout.jade rename to views/layout.ejs From d24fdbf93e050f52421f172d6046853a9fd6f03b Mon Sep 17 00:00:00 2001 From: melissajimison Date: Fri, 24 Jun 2016 16:19:53 -0700 Subject: [PATCH 114/118] delete a console.log --- models/rentals.js | 1 - 1 file changed, 1 deletion(-) diff --git a/models/rentals.js b/models/rentals.js index 0cbc73c0e..c8c3cfbeb 100644 --- a/models/rentals.js +++ b/models/rentals.js @@ -62,7 +62,6 @@ Rentals.mark_as_checkout = function(movie, customer_id, callback) { }; Rentals.update_custumer_credit = function (result, customer_id, callback) { - console.log(result); var bonus = 0.50 var customer_id = Number(customer_id) db.sql.rentals.updatecustomer([bonus, customer_id], function (error, customer_updated) { From 8c0198450d7e1bcf1ab568e0d814417e1c772caa Mon Sep 17 00:00:00 2001 From: Risha Date: Fri, 24 Jun 2016 16:40:04 -0700 Subject: [PATCH 115/118] added some more tests --- spec/controllers/customers.spec.js | 32 +++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/spec/controllers/customers.spec.js b/spec/controllers/customers.spec.js index 6f663071a..6dbf44c57 100644 --- a/spec/controllers/customers.spec.js +++ b/spec/controllers/customers.spec.js @@ -13,13 +13,31 @@ describe("Endpoint at /", function () { done(); }); }); +}); - it('the returned json data has the right keys', function(done) { - request.get('/', function(error, response, body) { - // var data = JSON.parse(body); - expect(body).toEqual("melissa"); - expect(Object.keys(data)).toEqual(['']); - done(); + describe("Endpoint at /sort", function() { + var url = function(endpoint) { + return base_url + "customers/sort" + endpoint; + }; + + it('responds with a 200 status code for a valid url', function(done) { + request.get(url('/name?n=10&p=2'), function(error, response, body) { + expect(JSON.parse(body).status).toEqual(200); + done(); + }); + }); + + it('responds with a 200 status code for a valid url', function(done) { + request.get(url('/registered_at?n=10&p=2'), function(error, response, body) { + expect(JSON.parse(body).status).toEqual(200); + done(); + }); + }); + + it('responds with a 200 status code for a valid url', function(done) { + request.get(url('/postal_code?n=10&p=2'), function(error, response, body) { + expect(JSON.parse(body).status).toEqual(200); + done(); + }); }); - }); }); From 932ecf2392d74cde3292913889d2234354495b67 Mon Sep 17 00:00:00 2001 From: Risha Date: Fri, 24 Jun 2016 16:52:56 -0700 Subject: [PATCH 116/118] added more tests in the model --- spec/models/customers.spec.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/spec/models/customers.spec.js b/spec/models/customers.spec.js index 84922e688..e7307d7fb 100644 --- a/spec/models/customers.spec.js +++ b/spec/models/customers.spec.js @@ -19,4 +19,22 @@ describe('Customers', function () { }); }); }); + + describe('#sort_by', function () { + var options_release = { + limit : 5, + order : 'name', + offset: 1 + }; + + + it('should return an array of 5 Customers if limit is set to 5', function (done) { + Movies.sort_by(options_release, function (error, movies) { + expect(error).toBeNull; + expect(movies).toEqual(jasmine.any(Array)); + expect(movies.length).toEqual(5); + done(); + }); + }); + }); }); From bf35bbf47eef836ce588953f11d72e48187cb75d Mon Sep 17 00:00:00 2001 From: melissajimison Date: Fri, 24 Jun 2016 18:20:58 -0700 Subject: [PATCH 117/118] fixed some styling issues, attempt to make test fot posr request, it makes too many changes in the data base that alterates the other test, it also behave weird the second time is ran --- spec/controllers/rentals.spec.js | 190 ++++++++++++++++++++----------- 1 file changed, 123 insertions(+), 67 deletions(-) diff --git a/spec/controllers/rentals.spec.js b/spec/controllers/rentals.spec.js index 19dbcce73..0a32f628d 100644 --- a/spec/controllers/rentals.spec.js +++ b/spec/controllers/rentals.spec.js @@ -1,90 +1,146 @@ var request = require('request'); var base_url = "http://localhost:3000/"; - -describe("Endpoint at /", function () { +describe('RentalsController', function() { var url = function(endpoint) { return base_url + "rentals" + endpoint; }; - it('responds with a 200 status code for succesful request', function (done) { - request.get(url('/Jaws'), function(error, response, body) { - var body_parsed = JSON.parse(body) - expect(response.statusCode).toEqual(200) - expect(typeof body).toEqual("string") - expect(typeof body_parsed).toEqual("object") - expect(body_parsed["status"]).toEqual(200) - expect(body_parsed["Movie Info"]["Synopsis"]).toEqual(jasmine.any(String)) - expect(body_parsed["Movie Info"]["Release Date"]).toEqual("1975-06-19") - expect(body_parsed["Movie Info"]["Total Inventory"]).toEqual(6) - expect(body_parsed["Movie Info"]["Available Inventory"]).toEqual(4) - done(); + describe("Endpoint at /Jaws", function () { + it('responds with a 200 status code for succesful request', function (done) { + request.get(url('/Jaws'), function(error, response, body) { + var body_parsed = JSON.parse(body) + expect(response.statusCode).toEqual(200) + expect(typeof body).toEqual("string") + expect(typeof body_parsed).toEqual("object") + expect(body_parsed["status"]).toEqual(200) + done(); + }); }); - }); - it('responds with a 200 status code, and all movie info, for succesful request', function (done) { - request.get(url('/Titanic'), function(error, response, body) { - var body_parsed = JSON.parse(body) - expect(response.statusCode).toEqual(200) - expect(typeof body).toEqual("string") - expect(typeof body_parsed).toEqual("object") - expect(body_parsed["status"]).toEqual(200) - expect(body_parsed["Movie Info"]["Synopsis"]).toEqual(jasmine.any(String)) - expect(body_parsed["Movie Info"]["Release Date"]).toEqual("1997-12-19") - expect(body_parsed["Movie Info"]["Total Inventory"]).toEqual(5) - expect(body_parsed["Movie Info"]["Available Inventory"]).toEqual(5) - done(); + it('response with an object with movie and rental information', function (done) { + request.get(url('/Jaws'), function(error, response, body) { + var body_parsed = JSON.parse(body) + expect(body_parsed["Movie Info"]["Synopsis"]).toEqual(jasmine.any(String)) + expect(body_parsed["Movie Info"]["Release Date"]).toEqual("1975-06-19") + expect(body_parsed["Movie Info"]["Total Inventory"]).toEqual(6) + expect(body_parsed["Movie Info"]["Available Inventory"]).toEqual(4) + done(); + }); }); + }); - // it('responds with a 404 status code for bad request', function (done) { - // request.get(url('/Melissa'), function(error, response, body) { - // // var body_parsed = JSON.parse(body) - // // expect(response.statusCode).toEqual(200) - // // expect(typeof body).toEqual("string") - // // expect(typeof body_parsed).toEqual("object") - // // done(); - // }); - // }); + describe("Endpoint at /Titanic", function () { + it('responds with a 200 status code for succesful request', function (done) { + request.get(url('/Titanic'), function(error, response, body) { + var body_parsed = JSON.parse(body) + expect(response.statusCode).toEqual(200) + expect(typeof body).toEqual("string") + expect(typeof body_parsed).toEqual("object") + expect(body_parsed["status"]).toEqual(200) + done(); + }); + }); -}); + it('response with an object with movie and rental information', function (done) { + request.get(url('/Titanic'), function(error, response, body) { + var body_parsed = JSON.parse(body) + expect(body_parsed["Movie Info"]["Synopsis"]).toEqual(jasmine.any(String)) + expect(body_parsed["Movie Info"]["Release Date"]).toEqual("1997-12-19") + expect(body_parsed["Movie Info"]["Total Inventory"]).toEqual(5) + expect(body_parsed["Movie Info"]["Available Inventory"]).toEqual(5) + done(); + }); + }); + // it('responds with a 404 status code for bad request', function (done) { + // request.get(url('/Melissa'), function(error, response, body) { + // // var body_parsed = JSON.parse(body) + // // expect(response.statusCode).toEqual(200) + // // expect(typeof body).toEqual("string") + // // expect(typeof body_parsed).toEqual("object") + // // done(); + // }); + // }); + // + }); -describe("Endpoint at /", function () { - var url = function(endpoint) { - return base_url + "rentals" + endpoint; - }; + describe("Endpoint at /Jaws/customers", function () { - it('responds with a 200 status code, and all customers info, for succesful request', function (done) { - request.get(url('/Jaws/customers'), function(error, response, body) { - var body_parsed = JSON.parse(body) - expect(response.statusCode).toEqual(200) - expect(typeof body).toEqual("string") - expect(typeof body_parsed).toEqual("object") - expect(body_parsed["status"]).toEqual(200) - expect(body_parsed["customers"]).toEqual(jasmine.any(Array)) - expect(body_parsed["customers"][0]["name"]).toEqual("Curran Stout") - expect(body_parsed["customers"][0]["registered_at"]).toEqual("Wed, 16 Apr 2014 21:40:20 -0700") - expect(body_parsed["customers"][0]["address"]).toEqual("Ap #658-1540 Erat Rd.") - expect(body_parsed["customers"][0]["city"]).toEqual("San Francisco") - expect(body_parsed["customers"][0]["state"]).toEqual("California") - expect(body_parsed["customers"][0]["postal_code"]).toEqual("94267") - expect(body_parsed["customers"][0]["phone"]).toEqual("(908) 949-6758") - expect(body_parsed["customers"][0]["account_credit"]).toEqual("35.66") + it('responds with a 200 status code for succesful request', function (done) { + request.get(url('/Jaws/customers'), function(error, response, body) { + var body_parsed = JSON.parse(body) + expect(response.statusCode).toEqual(200) + expect(typeof body).toEqual("string") + expect(typeof body_parsed).toEqual("object") + expect(body_parsed["status"]).toEqual(200) done(); + }); + }); + + it('responds with all customers info, for succesful request', function (done) { + request.get(url('/Jaws/customers'), function(error, response, body) { + var body_parsed = JSON.parse(body) + expect(body_parsed["customers"]).toEqual(jasmine.any(Array)) + expect(body_parsed["customers"][0]["name"]).toEqual("Curran Stout") + expect(body_parsed["customers"][0]["registered_at"]).toEqual("Wed, 16 Apr 2014 21:40:20 -0700") + expect(body_parsed["customers"][0]["address"]).toEqual("Ap #658-1540 Erat Rd.") + expect(body_parsed["customers"][0]["city"]).toEqual("San Francisco") + expect(body_parsed["customers"][0]["state"]).toEqual("California") + expect(body_parsed["customers"][0]["postal_code"]).toEqual("94267") + expect(body_parsed["customers"][0]["phone"]).toEqual("(908) 949-6758") + expect(body_parsed["customers"][0]["account_credit"]).toEqual("35.66") + done(); + }); }); }); - it('responds with a 200 status code, but 204 response status for not data found', function (done) { - request.get(url('/Titanic/customers'), function(error, response, body) { - var body_parsed = JSON.parse(body) - expect(response.statusCode).toEqual(200) - expect(typeof body).toEqual("string") - expect(typeof body_parsed).toEqual("object") - expect(body_parsed["status"]).toEqual(204) - expect(body_parsed["customers"]).toEqual(jasmine.any(Array)) - expect(body_parsed["customers"]).toEqual([]) - done(); + describe("Endpoint at /Titanic/customers", function () { + + it('responds with a 204 status for not data found', function (done) { + request.get(url('/Titanic/customers'), function(error, response, body) { + var body_parsed = JSON.parse(body) + expect(response.statusCode).toEqual(200) + expect(typeof body).toEqual("string") + expect(typeof body_parsed).toEqual("object") + expect(body_parsed["status"]).toEqual(204) + done(); + }); + }); + + it('responds with a empty array, for not data found', function (done) { + request.get(url('/Titanic/customers'), function(error, response, body) { + var body_parsed = JSON.parse(body) + expect(body_parsed["customers"]).toEqual(jasmine.any(Array)) + expect(body_parsed["customers"]).toEqual([]) + done(); + }); }); }); + // describe("Endpoint at /Notorious/check-out", function () { + // it('responds with a 200 status code, for succesful request', function (done) { + // request.post(url('/Notorious/check-out/100'), function(error, response, body) { + // var body_parsed = JSON.parse(body) + // expect(response.statusCode).toEqual(200) + // expect(typeof body).toEqual("string") + // expect(typeof body_parsed).toEqual("object") + // expect(body_parsed).toEqual(200) + // done(); + // }); + // }); + + // it('responds with a Message for succesful request', function (done) { + // request.post(url('/Alien/check-out/100'), function(error, response, body) { + // var body_parsed = JSON.parse(body) + // expect(typeof body_parsed).toEqual("object") + // expect(body_parsed["Message"]).toEqual("Checkout has been processed succesfully") + // expect(typeof body_parsed["Message"]).toEqual("string") + // expect(body_parsed["Customer's Name"]).toEqual("Barbara Jacobson") + // expect(body_parsed["Return day"]).toEqual("string") + // expect(body_parsed["Customer's Credit"]).toEqual("13.91") + // done(); + // }); + // }); + // }); }); From fad219e8a72b76d56ae3f461f6d54fd7eaadc192 Mon Sep 17 00:00:00 2001 From: melissajimison Date: Fri, 24 Jun 2016 19:16:15 -0700 Subject: [PATCH 118/118] added more tests to the rentals model, edited some of the controllers --- spec/controllers/rentals.spec.js | 22 +++++------ spec/models/rentals.spec.js | 67 ++++++++++++++++++++++++++++---- 2 files changed, 71 insertions(+), 18 deletions(-) diff --git a/spec/controllers/rentals.spec.js b/spec/controllers/rentals.spec.js index 0a32f628d..0d9303f03 100644 --- a/spec/controllers/rentals.spec.js +++ b/spec/controllers/rentals.spec.js @@ -82,14 +82,14 @@ describe('RentalsController', function() { request.get(url('/Jaws/customers'), function(error, response, body) { var body_parsed = JSON.parse(body) expect(body_parsed["customers"]).toEqual(jasmine.any(Array)) - expect(body_parsed["customers"][0]["name"]).toEqual("Curran Stout") - expect(body_parsed["customers"][0]["registered_at"]).toEqual("Wed, 16 Apr 2014 21:40:20 -0700") - expect(body_parsed["customers"][0]["address"]).toEqual("Ap #658-1540 Erat Rd.") - expect(body_parsed["customers"][0]["city"]).toEqual("San Francisco") - expect(body_parsed["customers"][0]["state"]).toEqual("California") - expect(body_parsed["customers"][0]["postal_code"]).toEqual("94267") - expect(body_parsed["customers"][0]["phone"]).toEqual("(908) 949-6758") - expect(body_parsed["customers"][0]["account_credit"]).toEqual("35.66") + expect(typeof body_parsed["customers"][0]["name"]).toEqual("string") + expect(typeof body_parsed["customers"][0]["registered_at"]).toEqual("string") + expect(typeof body_parsed["customers"][0]["address"]).toEqual("string") + expect(typeof body_parsed["customers"][0]["city"]).toEqual("string") + expect(typeof body_parsed["customers"][0]["state"]).toEqual("string") + expect(typeof body_parsed["customers"][0]["postal_code"]).toEqual("string") + expect(typeof body_parsed["customers"][0]["phone"]).toEqual("string") + expect(typeof body_parsed["customers"][0]["account_credit"]).toEqual("string") done(); }); }); @@ -125,7 +125,7 @@ describe('RentalsController', function() { // expect(response.statusCode).toEqual(200) // expect(typeof body).toEqual("string") // expect(typeof body_parsed).toEqual("object") - // expect(body_parsed).toEqual(200) + // expect(body_parsed["Status"]).toEqual(200) // done(); // }); // }); @@ -137,8 +137,8 @@ describe('RentalsController', function() { // expect(body_parsed["Message"]).toEqual("Checkout has been processed succesfully") // expect(typeof body_parsed["Message"]).toEqual("string") // expect(body_parsed["Customer's Name"]).toEqual("Barbara Jacobson") - // expect(body_parsed["Return day"]).toEqual("string") - // expect(body_parsed["Customer's Credit"]).toEqual("13.91") + // expect(typeof body_parsed["Return day"]).toEqual("string") + // expect(body_parsed["Customer's Credit"]).toEqual("13.41") // done(); // }); // }); diff --git a/spec/models/rentals.spec.js b/spec/models/rentals.spec.js index 0227e42a3..f5b0c4bec 100644 --- a/spec/models/rentals.spec.js +++ b/spec/models/rentals.spec.js @@ -27,6 +27,15 @@ describe('Rentals', function () { done() }) }) + + it('should return number of rentals available', function (done) { + var movie_id = 25; + Rentals.available(movie_id, function (error, result) { + expect(error).toBeNull + expect(result).toEqual(5) + done() + }) + }) }) describe('#find_customers_by_title', function () { @@ -36,13 +45,13 @@ describe('Rentals', function () { expect(error).toBeNull expect(Array.isArray(costumers)).toEqual(true) expect(typeof costumers[0]).toEqual('object') - expect(costumers[0].name).toEqual('Curran Stout') - expect(costumers[0].registered_at).toEqual('Wed, 16 Apr 2014 21:40:20 -0700') - expect(costumers[0].address).toEqual('Ap #658-1540 Erat Rd.') - expect(costumers[0].city).toEqual('San Francisco') - expect(costumers[0].state).toEqual('California') - expect(costumers[0].postal_code).toEqual('94267') - expect(costumers[0].account_credit).toEqual('35.66') + expect(typeof costumers[0].name).toEqual('string') + expect(typeof costumers[0].registered_at).toEqual('string') + expect(typeof costumers[0].address).toEqual('string') + expect(typeof costumers[0].city).toEqual('string') + expect(typeof costumers[0].state).toEqual('string') + expect(typeof costumers[0].postal_code).toEqual('string') + expect(typeof costumers[0].account_credit).toEqual('string') done() }) @@ -68,4 +77,48 @@ describe('Rentals', function () { }) }) }) + + describe('#mark_as_checkout', function () { + it('should return number of rentals mark_as_checkout', function (done) { + var movie = 'Raging Bull'; + var costumer_id = 102; + + Rentals.mark_as_checkout(movie, costumer_id, function (error, info) { + expect(error).toBeNull + expect(Array.isArray(info)).toEqual(true) + // expect(typeof info[0].id).toEqual('number') + // expect(info[0][movie_id]).toEqual(51) + // expect(info[0][customer_id).toEqual(102) + // expect(info[0][status).toEqual('rented') + done() + }) + }) + + it('should return number of rentals mark_as_checkout', function (done) { + var movie = "The Great Escape"; + var costumer_id = 42; + Rentals.mark_as_checkout(movie, costumer_id, function (error, info) { + expect(error).toBeNull + expect(Array.isArray(info)).toEqual(true) + done() + }) + }) + }) + + describe('#get_overdue', function () { + it('should return all the of rentals get_overdue', function (done) { + Rentals.get_overdue(function (error, info) { + expect(error).toBeNull + expect(Array.isArray(info)).toEqual(true) + expect(typeof info[0]).toEqual("object") + expect(typeof info[0].name).toEqual("string") + expect(typeof info[0].title).toEqual("string") + expect(typeof info[0].checkout_date).toEqual("string") + expect(typeof info[0].return_date).toEqual("string") + done() + }) + }) + + }) + });