From 9d587a737340ea133af51b8692c41772dae61bae Mon Sep 17 00:00:00 2001 From: Mindy Carson Date: Tue, 14 Jun 2016 14:53:27 -0700 Subject: [PATCH 01/28] created a route, controller and view for index --- .gitignore | 32 ++++++++++++++++++++++++++++++-- app.js | 4 +++- bin/www | 2 +- controllers/index.js | 7 +++++++ package.json | 14 ++++---------- routes/index.js | 5 ++++- routes/users.js | 9 +++++++++ views/error.ejs | 3 +++ views/index.ejs | 11 +++++++++++ 9 files changed, 72 insertions(+), 15 deletions(-) create mode 100644 controllers/index.js create mode 100644 routes/users.js create mode 100644 views/error.ejs create mode 100644 views/index.ejs diff --git a/.gitignore b/.gitignore index 646ac519e..fa76cfae7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,30 @@ -.DS_Store -node_modules/ +# Logs +logs +*.log + +# Runtime data +pids +*.pid +*.seed + +# Directory for instrumented libs generated by jscoverage/JSCover +lib-cov + +# Coverage directory used by tools like istanbul +coverage + +# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) +.grunt + +# node-waf configuration +.lock-wscript + +# Compiled binary addons (http://nodejs.org/api/addons.html) +build/Release + +# Dependency directory +# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git- +node_modules + +# Debug log from npm +npm-debug.log diff --git a/app.js b/app.js index f0579b1dc..03f85957c 100644 --- a/app.js +++ b/app.js @@ -6,12 +6,13 @@ var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var routes = require('./routes/index'); +var users = require('./routes/users'); var app = express(); // 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'))); @@ -22,6 +23,7 @@ app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); app.use('/', routes); +app.use('/users', users); // catch 404 and forward to error handler app.use(function(req, res, next) { diff --git a/bin/www b/bin/www index 23e9de753..5e1dbd444 100755 --- a/bin/www +++ b/bin/www @@ -5,7 +5,7 @@ */ var app = require('../app'); -var debug = require('debug')('video-store-api:server'); +var debug = require('debug')('VideoStoreAPI:server'); var http = require('http'); /** diff --git a/controllers/index.js b/controllers/index.js new file mode 100644 index 000000000..659bc15b9 --- /dev/null +++ b/controllers/index.js @@ -0,0 +1,7 @@ +var HomePage = { + zomg: function(req,response) { + response.render('index', {cow: 'ZOMG!'}) + } +}; + +module.exports = HomePage; diff --git a/package.json b/package.json index d39b26403..1fc41f407 100644 --- a/package.json +++ b/package.json @@ -1,24 +1,18 @@ { - "name": "video-store-api", + "name": "VideoStoreAPI", "version": "0.0.0", "private": true, "scripts": { - "start": "nodemon ./bin/www", - "test": "clear; jasmine-node --verbose spec/" + "start": "nodemon ./bin/www" }, "dependencies": { "body-parser": "~1.13.2", "cookie-parser": "~1.3.5", "debug": "~2.2.0", + "ejs": "~2.3.3", "express": "~4.13.1", - "jade": "~1.11.0", "morgan": "~1.6.1", - "sequelize": "^3.23.3", - "serve-favicon": "~2.3.0" - }, - "devDependencies": { - "jasmine-node": "^1.14.5", "nodemon": "^1.9.2", - "request": "^2.72.0" + "serve-favicon": "~2.3.0" } } diff --git a/routes/index.js b/routes/index.js index 06cfc1137..e86168a17 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,9 +1,12 @@ var express = require('express'); var router = express.Router(); +var Controller = require('../controllers/index') /* GET home page. */ router.get('/', function(req, res, next) { - res.status(200).json({whatevs: 'whatevs!!!'}) + res.render('index', { title: 'Express' }); }); +router.get('/zomg', Controller.zomg) + module.exports = router; diff --git a/routes/users.js b/routes/users.js new file mode 100644 index 000000000..623e4302b --- /dev/null +++ b/routes/users.js @@ -0,0 +1,9 @@ +var express = require('express'); +var router = express.Router(); + +/* GET users listing. */ +router.get('/', function(req, res, next) { + res.send('respond with a resource'); +}); + +module.exports = router; diff --git a/views/error.ejs b/views/error.ejs new file mode 100644 index 000000000..7cf94edf1 --- /dev/null +++ b/views/error.ejs @@ -0,0 +1,3 @@ +

<%= message %>

+

<%= error.status %>

+
<%= error.stack %>
diff --git a/views/index.ejs b/views/index.ejs new file mode 100644 index 000000000..ccd602f8d --- /dev/null +++ b/views/index.ejs @@ -0,0 +1,11 @@ + + + + <%= cow %> + + + +

<%= cow %>

+

Welcome to <%= cow %>

+ + From 41068be5d3c53096179b90be1012560e31204dc4 Mon Sep 17 00:00:00 2001 From: Mindy Carson Date: Tue, 14 Jun 2016 15:15:46 -0700 Subject: [PATCH 02/28] Created a route that responds to /zomg that serves a json-encoded method --- controllers/index.js | 7 ++++++- routes/index.js | 9 ++++++--- views/index.ejs | 6 +++--- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/controllers/index.js b/controllers/index.js index 659bc15b9..1e9f632e2 100644 --- a/controllers/index.js +++ b/controllers/index.js @@ -1,6 +1,11 @@ var HomePage = { zomg: function(req,response) { - response.render('index', {cow: 'ZOMG!'}) + var hi= {"hi": "ZOMG"} + response.json({cow: 'ZOMG!'}) + }, + + nothing: function(req, response) { + response.render('index', {title: 'Express'}); } }; diff --git a/routes/index.js b/routes/index.js index e86168a17..3caa0217a 100644 --- a/routes/index.js +++ b/routes/index.js @@ -3,9 +3,12 @@ var router = express.Router(); var Controller = require('../controllers/index') /* GET home page. */ -router.get('/', function(req, res, next) { - res.render('index', { title: 'Express' }); -}); +// router.get('/', function(req, res, next) { +// res.render('index', { title: 'Express' }); +// }); + +router.get('/', Controller.nothing) + router.get('/zomg', Controller.zomg) diff --git a/views/index.ejs b/views/index.ejs index ccd602f8d..7b7a1d6de 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -1,11 +1,11 @@ - <%= cow %> + <%= title %> -

<%= cow %>

-

Welcome to <%= cow %>

+

<%= title %>

+

Welcome to <%= title %>

From dc781efd530921130a85ec8e07c84317ef37b90d Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Tue, 14 Jun 2016 16:19:57 -0700 Subject: [PATCH 03/28] added a tasks folder with a load_schema.. that currently doesn't load schema. but we added in our 3 tables for movies, customers and rentals in the schema.sql setup. called the seed files in package.json mebbe. still dk how to seed. --- app.js | 15 ++++++++++++++- db/setup/schema.sql | 32 ++++++++++++++++++++++++++++++++ package.json | 8 +++++++- routes/index.js | 1 - tasks/load_schema.js | 13 +++++++++++++ 5 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 db/setup/schema.sql create mode 100644 tasks/load_schema.js diff --git a/app.js b/app.js index 03f85957c..846efd941 100644 --- a/app.js +++ b/app.js @@ -4,11 +4,24 @@ var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); +var app = express(); var routes = require('./routes/index'); var users = require('./routes/users'); -var app = express(); +var massive = require("massive") +var connectionString = "postgres://localhost/radio_star" + + +// connect to Massive and get the db instance. You can safely use the +// convenience sync method here because its on app load +// you can also use loadSync - it's an alias +var massiveInstance = massive.connectSync({connectionString : connectionString}) + +// Set a reference to the massive instance on Express' app: +app.set('db', massiveInstance) + + // view engine setup app.set('views', path.join(__dirname, 'views')); diff --git a/db/setup/schema.sql b/db/setup/schema.sql new file mode 100644 index 000000000..e838aed28 --- /dev/null +++ b/db/setup/schema.sql @@ -0,0 +1,32 @@ +DROP TABLE IF EXISTS words; +CREATE TABLE movies( + id serial PRIMARY KEY, + title text, + overview text, + release_date text, + inventory integer +); + +CREATE TABLE customers( + id serial PRIMARY KEY, + name text, + registered_at text, + address text, + city text, + state text, + postal_code text, + phone integer, + account_credit integer +); + + +CREATE TABLE rentals( + id serial PRIMARY KEY, + movie_id integer, + customer_id integer, + checked boolean, + rental_date text, + due_date text, +); + +-- CREATE INDEX words_word ON words (word); diff --git a/package.json b/package.json index 1fc41f407..5c526871a 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,12 @@ "version": "0.0.0", "private": true, "scripts": { - "start": "nodemon ./bin/www" + "start": "nodemon ./bin/www", + "db:drop": "dropdb radio_star", + "db:create": "createdb radio_star", + "db:schema": "node tasks/load_schema.js", + "db:seed": "node db/seeds/movies.json; node db/seeds/customers.json", + "db:reset": "npm run db:drop; npm run db:create; npm run db:seed; npm run db:schema" }, "dependencies": { "body-parser": "~1.13.2", @@ -11,6 +16,7 @@ "debug": "~2.2.0", "ejs": "~2.3.3", "express": "~4.13.1", + "massive": "^2.3.0", "morgan": "~1.6.1", "nodemon": "^1.9.2", "serve-favicon": "~2.3.0" diff --git a/routes/index.js b/routes/index.js index 3caa0217a..2cef08cff 100644 --- a/routes/index.js +++ b/routes/index.js @@ -8,7 +8,6 @@ var Controller = require('../controllers/index') // }); router.get('/', Controller.nothing) - router.get('/zomg', Controller.zomg) diff --git a/tasks/load_schema.js b/tasks/load_schema.js new file mode 100644 index 000000000..b6fbfcc52 --- /dev/null +++ b/tasks/load_schema.js @@ -0,0 +1,13 @@ +var massive = require('massive') +var connectionString = "postgres://localhost/radio_star" + +var db = massive.connectSync({connectionString : connectionString}) + +db.setup.schema([], function(err, res) { + if (err) { + return console.log('migration error', err) + } + + console.log("yay schema!") + process.exit() +}) From c072b525994de447f7f65f9a76707bf86d834bef Mon Sep 17 00:00:00 2001 From: Mindy Carson Date: Tue, 14 Jun 2016 16:44:54 -0700 Subject: [PATCH 04/28] added pseudo routes --- routes/index.js | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/routes/index.js b/routes/index.js index 2cef08cff..7279ef334 100644 --- a/routes/index.js +++ b/routes/index.js @@ -11,4 +11,79 @@ router.get('/', Controller.nothing) router.get('/zomg', Controller.zomg) +// Retrive a list of all customers +router.get('/customers/', Controller.) + + +// Retrive a subset of customers +// Given a sort column, return n customer records, offset by p records (this will be used to create "pages" of customers) +// Sort columns are: name, registered_at, postal_code +router.get('/customers/sort/name?n=10&p=2', Controller.) + + +// Given a customer's id... +// List the movies they currently have checked out +router.get('/customers/:id/current', Controller.) + + +// Given a customer's id... +// List the movies a customer has checked out in the past (ordered by check out date, includes return date) +router.get('/customers/:id/history', Controller.) + + + + + +// Retrieve a list of all movies +router.get('/movies') + + +// Retrieve a subset of movie +// 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 + router.get('/movies/sort/release-date?n=5&p=1') + + + // Given a movie's title... + // Get a list of customers that have currently checked out a copy of the film + // include each customer's name, phone number, and account credit + router.get('/movies/:movie_title/current') + + +// Given a movie's title... +// Get a list of customers that have checked out a copy in the past +// include each customer's name, phone number, and account credit +// ordered by customer name or ordered by check out date +router.get('/movies/:movie_title/history/sort/name') + + + + + + // Look a movie up by title to see: it's synopsis, release date, available inventory (not currently checked-out to a customer), and inventory total +router.get('/rentals/:movie_title') + + +// See a list of customers that have currently checked out any of the movie's inventory +router.get('/rentals/:movie_title/customers') + + + +// Given a customer's id and a movie's title ... +// "check out" one of the movie's inventory to the customer +// Establish a return date, Charge the customer's account (cost up to you) +router.get ('/rentals/:movie_title/check-out') + + +// Given a customer's id and a movie's title ... +// "check in" one of customer's rentals +// return the movie to its inventory +router.get('/rentals/:movie_title/return') + + +// See a list of customers with overdue movies +// include customer name, movie title, check-out date, and return date +router.get('/rentals/overdue') + + module.exports = router; From 8995ae32526ef30c805ddcdc8a1d4fe6fe617274 Mon Sep 17 00:00:00 2001 From: Mindy Carson Date: Wed, 15 Jun 2016 09:41:53 -0700 Subject: [PATCH 05/28] did some changes to the schema files --- db/setup/schema.sql | 19 +++++++++++++++---- package.json | 14 ++++++++++---- tasks/load_schema.js | 3 ++- tasks/seed_data.js | 16 ++++++++++++++++ 4 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 tasks/seed_data.js diff --git a/db/setup/schema.sql b/db/setup/schema.sql index e838aed28..1e2c564f3 100644 --- a/db/setup/schema.sql +++ b/db/setup/schema.sql @@ -7,6 +7,10 @@ CREATE TABLE movies( inventory integer ); +CREATE INDEX movies_title ON movies (title); +CREATE INDEX movies_date ON movies (release_date); + +DROP TABLE IF EXISTS customers; CREATE TABLE customers( id serial PRIMARY KEY, name text, @@ -15,18 +19,25 @@ CREATE TABLE customers( city text, state text, postal_code text, - phone integer, - account_credit integer + phone text, + account_credit decimal ); +CREATE INDEX customers_name ON customers (name); +CREATE INDEX customers_date ON customers (registered_at); +CREATE INDEX customers_postal ON customers (postal_code); +DROP TABLE IF EXISTS rentals; CREATE TABLE rentals( id serial PRIMARY KEY, - movie_id integer, - customer_id integer, + movie_id integer REFERENCES movies (id), + customer_id integer REFERENCES customers (id), checked boolean, rental_date text, due_date text, ); +CREATE INDEX rentals_customers ON rentals (customer_id); +CREATE INDEX rentals_states ON rentals (status); + -- CREATE INDEX words_word ON words (word); diff --git a/package.json b/package.json index 5c526871a..10615d4c7 100644 --- a/package.json +++ b/package.json @@ -4,21 +4,27 @@ "private": true, "scripts": { "start": "nodemon ./bin/www", + "test": "clear; jasmine-node --verbose spec/", "db:drop": "dropdb radio_star", "db:create": "createdb radio_star", "db:schema": "node tasks/load_schema.js", - "db:seed": "node db/seeds/movies.json; node db/seeds/customers.json", - "db:reset": "npm run db:drop; npm run db:create; npm run db:seed; npm run db:schema" + "db:seed": "node tasks/seed_data.js", + "db:reset": "npm run db:drop; npm run db:create; npm run db:schema" }, "dependencies": { "body-parser": "~1.13.2", "cookie-parser": "~1.3.5", "debug": "~2.2.0", - "ejs": "~2.3.3", "express": "~4.13.1", + "jade": "~1.11.0", "massive": "^2.3.0", "morgan": "~1.6.1", "nodemon": "^1.9.2", "serve-favicon": "~2.3.0" - } + }, + "devDependencies": { + "jasmine-node": "^1.14.5", + "nodemon": "^1.9.2", + "request": "^2.72.0" + } } diff --git a/tasks/load_schema.js b/tasks/load_schema.js index b6fbfcc52..8f0f90085 100644 --- a/tasks/load_schema.js +++ b/tasks/load_schema.js @@ -5,7 +5,8 @@ var db = massive.connectSync({connectionString : connectionString}) db.setup.schema([], function(err, res) { if (err) { - return console.log('migration error', err) + // throw(new Error(err.message)) + return console.log('migration error', err.message) } console.log("yay schema!") diff --git a/tasks/seed_data.js b/tasks/seed_data.js new file mode 100644 index 000000000..efee83c73 --- /dev/null +++ b/tasks/seed_data.js @@ -0,0 +1,16 @@ +var massive = require('massive') + +var connectionString = "postgres://localhost/radio_star" +var db = massive.connectSync({connectionString : connectionString}) + +var movies_data = require("../db/seeds/movies") +// var movies_array = JSON.parse(movies_data) +console.log(movies_data) + +for(var movie of movies_data){ + db.movies.save({title: movie.title, overview: movie.overview, release_date: movie.release_date, inventory: movie.inventory}, function(err,res){ + if(err) { + throw new Error(err.message) + } + }) +} From ee3e937d1664830d7ecebe65bd029a85c93c0db1 Mon Sep 17 00:00:00 2001 From: Mindy Carson Date: Wed, 15 Jun 2016 09:53:39 -0700 Subject: [PATCH 06/28] changes words on schema file --- 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 1e2c564f3..25fcb2e72 100644 --- a/db/setup/schema.sql +++ b/db/setup/schema.sql @@ -1,4 +1,4 @@ -DROP TABLE IF EXISTS words; +DROP TABLE IF EXISTS movies; CREATE TABLE movies( id serial PRIMARY KEY, title text, From 46e74c3f56ebe59e4e89dfe9ad092a6ed1d8c5d3 Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Thu, 16 Jun 2016 13:49:22 -0700 Subject: [PATCH 07/28] following jeremy in class for seeding data --- db/setup/schema.sql | 8 +++----- routes/index.js | 16 ++++++++-------- tasks/seed_data.js | 42 ++++++++++++++++++++++++++++++++++-------- 3 files changed, 45 insertions(+), 21 deletions(-) diff --git a/db/setup/schema.sql b/db/setup/schema.sql index 25fcb2e72..dadb73089 100644 --- a/db/setup/schema.sql +++ b/db/setup/schema.sql @@ -32,12 +32,10 @@ CREATE TABLE rentals( id serial PRIMARY KEY, movie_id integer REFERENCES movies (id), customer_id integer REFERENCES customers (id), - checked boolean, + checked text, rental_date text, - due_date text, + due_date text ); CREATE INDEX rentals_customers ON rentals (customer_id); -CREATE INDEX rentals_states ON rentals (status); - --- CREATE INDEX words_word ON words (word); +CREATE INDEX rentals_states ON rentals (checked); diff --git a/routes/index.js b/routes/index.js index 7279ef334..73541ddf7 100644 --- a/routes/index.js +++ b/routes/index.js @@ -12,49 +12,49 @@ router.get('/', Controller.nothing) router.get('/zomg', Controller.zomg) // Retrive a list of all customers -router.get('/customers/', Controller.) +// router.get('/customers/', Controller.) // Retrive a subset of customers // Given a sort column, return n customer records, offset by p records (this will be used to create "pages" of customers) // Sort columns are: name, registered_at, postal_code -router.get('/customers/sort/name?n=10&p=2', Controller.) +// router.get('/customers/sort/name?n=10&p=2', Controller.) // Given a customer's id... // List the movies they currently have checked out -router.get('/customers/:id/current', Controller.) +// router.get('/customers/:id/current', Controller.) // Given a customer's id... // List the movies a customer has checked out in the past (ordered by check out date, includes return date) -router.get('/customers/:id/history', Controller.) +// router.get('/customers/:id/history', Controller.) // Retrieve a list of all movies -router.get('/movies') +// router.get('/movies') // Retrieve a subset of movie // 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 - router.get('/movies/sort/release-date?n=5&p=1') + // router.get('/movies/sort/release-date?n=5&p=1') // Given a movie's title... // Get a list of customers that have currently checked out a copy of the film // include each customer's name, phone number, and account credit - router.get('/movies/:movie_title/current') + // router.get('/movies/:movie_title/current') // Given a movie's title... // Get a list of customers that have checked out a copy in the past // include each customer's name, phone number, and account credit // ordered by customer name or ordered by check out date -router.get('/movies/:movie_title/history/sort/name') +// router.get('/movies/:movie_title/history/sort/name') diff --git a/tasks/seed_data.js b/tasks/seed_data.js index efee83c73..4153f0413 100644 --- a/tasks/seed_data.js +++ b/tasks/seed_data.js @@ -3,14 +3,40 @@ var massive = require('massive') var connectionString = "postgres://localhost/radio_star" var db = massive.connectSync({connectionString : connectionString}) -var movies_data = require("../db/seeds/movies") +var movies_data = require("../db/seeds/movies.json") +var customer_data = require("../db/seeds/customers.json") // var movies_array = JSON.parse(movies_data) -console.log(movies_data) +// console.log(movies_data) for(var movie of movies_data){ - db.movies.save({title: movie.title, overview: movie.overview, release_date: movie.release_date, inventory: movie.inventory}, function(err,res){ - if(err) { - throw new Error(err.message) - } - }) -} + db.movies.saveSync(movie); + } + +for(var customer of customer_data){ + db.customers.saveSync(customer); + } + +console.log("seeding done") +process.exit() + +////////////////////////////////////////////////////////////// +// function checkFinish() { +// db.movies.count(function(err, res) { +// console.log("already in db: ", res) +// if (res >= records) {process.exit()} +// }) +// } +// +// for (var movie of movies_data) { +// db.movies.save(movie, function(err, res) { +// console.log('saved: ', JSON.stringify(res)) +// checkFinish() +// }) +// } + // {title: movie.title, overview: movie.overview, release_date: movie.release_date, inventory: movie.inventory} + // function(err,res){ + // if(err) { + // throw new Error(err.message) +// // } +// }) +// } From f30975bbe2b4d4235fed5c6a9c9c7ca4b3db11b3 Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Thu, 16 Jun 2016 16:45:28 -0700 Subject: [PATCH 08/28] fixed the routes and added a function and callback for Movies.all --- app.js | 18 +++--- controllers/movies.js | 16 ++++++ models/customer.js | 0 models/movie.js | 125 +++++++++++++++++++++++++++++++++++++++++ routes/index.js | 126 ++++++++++++++++++++++-------------------- 5 files changed, 216 insertions(+), 69 deletions(-) create mode 100644 controllers/movies.js create mode 100644 models/customer.js create mode 100644 models/movie.js diff --git a/app.js b/app.js index 846efd941..cfd65febe 100644 --- a/app.js +++ b/app.js @@ -6,20 +6,21 @@ var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var app = express(); -var routes = require('./routes/index'); -var users = require('./routes/users'); var massive = require("massive") -var connectionString = "postgres://localhost/radio_star" +var connectionString = "postgres://localhost/radio_star"; +// + app.get('env'); // connect to Massive and get the db instance. You can safely use the // convenience sync method here because its on app load // you can also use loadSync - it's an alias -var massiveInstance = massive.connectSync({connectionString : connectionString}) +var db = massive.connectSync({connectionString : connectionString}); +// console.log(db) +// var db = massive.connectSync({db : "radio_star"}) // Set a reference to the massive instance on Express' app: -app.set('db', massiveInstance) +app.set("db", db); @@ -27,6 +28,10 @@ app.set('db', massiveInstance) app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'ejs'); +module.exports = app; + +var routes = require('./routes/index'); +var users = require('./routes/users'); // uncomment after placing your favicon in /public //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); app.use(logger('dev')); @@ -68,6 +73,3 @@ app.use(function(err, req, res, next) { error: {} }); }); - - -module.exports = app; diff --git a/controllers/movies.js b/controllers/movies.js new file mode 100644 index 000000000..df4c6faf7 --- /dev/null +++ b/controllers/movies.js @@ -0,0 +1,16 @@ +var Movie = require("../models/movie"); + +var MovieController = { + index: function(req, res, next) { + Movie.all(function(error, movies) { + if(error) { + var err = new Error("Error retrieving movies list:\n" + error.message); + err.status = 500; + next(err); + } else { + res.json(movies) + } + }); + } +} +module.exports = MovieController; diff --git a/models/customer.js b/models/customer.js new file mode 100644 index 000000000..e69de29bb diff --git a/models/movie.js b/models/movie.js new file mode 100644 index 000000000..fdd6ff874 --- /dev/null +++ b/models/movie.js @@ -0,0 +1,125 @@ +// Retrieve a list of all movies (/movies) + +// Retrieve a subset of movies (/movies/sort/release-date?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 movie's title... +// Get a list of customers that have currently checked out a copy of the film (/movies/Jaws/current) +// include each customer's name, phone number, and account credit +// Get a list of customers that have checked out a copy in the past (/movies/Jaws/history/sort/name) +// include each customer's name, phone number, and account credit +// ordered by customer name or +// ordered by check out date + + +var app = require("../app"); +// var massive = require("massive"); +// var db = massive.connectSync({db: "radio_star"}); +var db = app.get("db"); +// console.log(app); +// Constructor function +var Movie = function(id) { + this.id = id; +}; + +// Instance functions + + +// class +Movie.all = function(callback) { + db.query("select * from movies", function(error, movies) { + if(error || !movies) { + callback(error || new Error("Could not retrieve movies"), undefined); + } else { + var allMovies = movies.map(function(movie) { + return new Movie(movie); + }); + console.log(allMovies) + callback(null, allMovies) + }; + }); +}; +// +// return this; +// }; +// +// var balanceResultCallback = function(account, callback) { +// return function(error, result) { +// if(error) { +// callback(error, undefined); +// } else { +// account.getBalance(function(error, balance) { +// callback(error, balance); +// }); +// } +// }; +// }; +// +// Movies.prototype.deposit = function(amount, callback) { +// db.movies_deposit(this.id, amount, balanceResultCallback(this, callback)); +// return this; +// }; +// +// Movies.prototype.withdraw = function(amount, callback) { +// db.movies_withdraw(this.id, amount, balanceResultCallback(this, callback)); +// return this; +// }; +// +// Movies.prototype.transfer = function(to, amount, callback) { +// db.movies_transfer(this.id, to.id, amount, balanceResultCallback(this, callback)); +// return this; +// }; +// +// // Class Functions +// Movies.create = function(initialBalance, callback) { +// db.movies.save({ +// balance: initialBalance +// }, function(error, account) { +// if(error || !account) { +// callback(error || new Error("Could not create account"), undefined); +// } else { +// callback(null, new Movies(account.id)); +// } +// }); +// }; +// +// Movies.createSync = function(initialBalance) { +// var account = db.movies.saveSync({ +// balance: initialBalance +// }); +// +// return new Movies(account.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(account) { +// return new Movies(account.id); +// })); +// } +// }); +// }; +// +// Movies.find = function(id, callback) { +// db.movies.findOne({id: id}, function(error, account) { +// if(error || !account) { +// callback(error || new Error("Movies not found"), undefined); +// } else { +// callback(null, new Movies(account.id)); +// } +// }); +// }; +// +// // only attach this function if we're in test mode +// if (app.get('env') === 'test') { +// Movies.close_connection = function() { +// console.log("closing connection") +// db.end() +// } +// } +module.exports = Movie; diff --git a/routes/index.js b/routes/index.js index 73541ddf7..3a1440966 100644 --- a/routes/index.js +++ b/routes/index.js @@ -2,15 +2,21 @@ var express = require('express'); var router = express.Router(); var Controller = require('../controllers/index') +var MovieController = require('../controllers/movies') + + /* GET home page. */ -// router.get('/', function(req, res, next) { -// res.render('index', { title: 'Express' }); -// }); +router.get('/', function(req, res, next) { + res.render('index', { title: 'Express' }); +}); router.get('/', Controller.nothing) router.get('/zomg', Controller.zomg) +// Retrieve a list of all movies +router.get('/movies', MovieController.index); + // Retrive a list of all customers // router.get('/customers/', Controller.) @@ -24,66 +30,64 @@ router.get('/zomg', Controller.zomg) // Given a customer's id... // List the movies they currently have checked out // router.get('/customers/:id/current', Controller.) - - -// Given a customer's id... -// List the movies a customer has checked out in the past (ordered by check out date, includes return date) +// +// +// // Given a customer's id... +// // List the movies a customer has checked out in the past (ordered by check out date, includes return date) // router.get('/customers/:id/history', Controller.) - - - - - -// Retrieve a list of all movies -// router.get('/movies') - - -// Retrieve a subset of movie -// 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 - // router.get('/movies/sort/release-date?n=5&p=1') - - - // Given a movie's title... - // Get a list of customers that have currently checked out a copy of the film - // include each customer's name, phone number, and account credit - // router.get('/movies/:movie_title/current') - - -// Given a movie's title... -// Get a list of customers that have checked out a copy in the past -// include each customer's name, phone number, and account credit -// ordered by customer name or ordered by check out date +// +// +// +// +// +// +// +// // Retrieve a subset of movie +// // 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 +// router.get('/movies/sort/release-date?n=5&p=1') +// +// +// // Given a movie's title... +// // Get a list of customers that have currently checked out a copy of the film +// // include each customer's name, phone number, and account credit +// router.get('/movies/:movie_title/current') +// +// +// // Given a movie's title... +// // Get a list of customers that have checked out a copy in the past +// // include each customer's name, phone number, and account credit +// // ordered by customer name or ordered by check out date // router.get('/movies/:movie_title/history/sort/name') - - - - - - // Look a movie up by title to see: it's synopsis, release date, available inventory (not currently checked-out to a customer), and inventory total -router.get('/rentals/:movie_title') - - -// See a list of customers that have currently checked out any of the movie's inventory -router.get('/rentals/:movie_title/customers') - - - -// Given a customer's id and a movie's title ... -// "check out" one of the movie's inventory to the customer -// Establish a return date, Charge the customer's account (cost up to you) -router.get ('/rentals/:movie_title/check-out') - - -// Given a customer's id and a movie's title ... -// "check in" one of customer's rentals -// return the movie to its inventory -router.get('/rentals/:movie_title/return') - - -// See a list of customers with overdue movies -// include customer name, movie title, check-out date, and return date -router.get('/rentals/overdue') +// +// +// +// +// +// // Look a movie up by title to see: it's synopsis, release date, available inventory (not currently checked-out to a customer), and inventory total +// router.get('/rentals/:movie_title') +// +// +// // See a list of customers that have currently checked out any of the movie's inventory +// router.get('/rentals/:movie_title/customers') +// +// +// +// // Given a customer's id and a movie's title ... +// // "check out" one of the movie's inventory to the customer +// // Establish a return date, Charge the customer's account (cost up to you) +// router.get ('/rentals/:movie_title/check-out') +// +// +// // Given a customer's id and a movie's title ... +// // "check in" one of customer's rentals +// // return the movie to its inventory +// router.get('/rentals/:movie_title/return') +// +// +// // See a list of customers with overdue movies +// // include customer name, movie title, check-out date, and return date +// router.get('/rentals/overdue') module.exports = router; From ed7220720386674d109c5301140a731a0c717e73 Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Fri, 17 Jun 2016 12:58:05 -0700 Subject: [PATCH 09/28] added customers and movies controller and models --- controllers/customers.js | 76 ++++++++++++++++++ controllers/movies.js | 68 +++++++++++++++++ models/customer.js | 161 +++++++++++++++++++++++++++++++++++++++ models/movie.js | 97 +++++++++++++++++------ routes/index.js | 71 +++++++++-------- 5 files changed, 415 insertions(+), 58 deletions(-) create mode 100644 controllers/customers.js diff --git a/controllers/customers.js b/controllers/customers.js new file mode 100644 index 000000000..fe9b01cb0 --- /dev/null +++ b/controllers/customers.js @@ -0,0 +1,76 @@ +var Cust = require("../models/customer"); + +var CustController = { + index: function(req, res, next) { + Cust.all(function(error, custs) { + if(error) { + var err = new Error("Error retrieving customer list:\n" + error.message); + err.status = 500; + next(err); + } else { + res.json(custs) + } + }); + }, + + subset: function(req, res, next) { + console.log(req.query) + console.log("req params query: ", req.params.query) + + Cust.sort(req.params.query, req.query.n , req.query.p, function(error, custs) { + // var n = req.params.n + // var p = req.params.p + if(error) { + var err = new Error("Error retrieving customer list:\n" + error.message); + err.status = 500; + next(err); + } else { + res.json(custs) + } + }); + }, + + current: function(req, res, next) { + Cust.find(req.params.id, function(error, custs) { + if(error) { + var err = new Error("Error retrieving customer list:\n" + error.message); + err.status = 500; + next(err); + } else { + res.json(custs) + } + }); + }, + + history: function(req, res, next) { + Cust.history(req.params.id, function(error, custs) { + if(error) { + var err = new Error("Error retrieving customer list:\n" + error.message); + err.status = 500; + next(err); + } else { + res.json(custs) + } + }); + } +} + +// Retrieve a subset of custs (/custs/sort/release-date?n=5&p=1) +// Given a sort column, return n movie records, offset by p records (this will be used to create "pages" of custs) +// Sort columns are +// title +// release_date + +// sort: function(req, res, next) { +// Cust.sort(function(error, custs) { +// if(error) { +// var err = new Error("Error retrieving movie info:\n" + error.message); +// err.status = 500; +// next(err); +// } else { +// res.json(custs) +// } +// }) +// } +// } +module.exports = CustController; diff --git a/controllers/movies.js b/controllers/movies.js index df4c6faf7..45dc65969 100644 --- a/controllers/movies.js +++ b/controllers/movies.js @@ -11,6 +11,74 @@ var MovieController = { res.json(movies) } }); + }, + + subset: function(req, res, next) { + Movie.sort(req.params.query, req.query.n , req.query.p, function(error, movies) { + if(error) { + var err = new Error("Error retrieving customer list:\n" + error.message); + err.status = 500; + next(err); + } else { + res.json(movies) + } + }); + }, + + current: function(req, res, next) { + Movie.find(req.params.id, function(error, movies) { + if(error) { + var err = new Error("Error retrieving customer list:\n" + error.message); + err.status = 500; + next(err); + } else { + res.json(movies) + } + }); + }, + + history: function(req, res, next) { + Movie.history(req.params.id, function(error, movies) { + if(error) { + var err = new Error("Error retrieving customer list:\n" + error.message); + err.status = 500; + next(err); + } else { + res.json(movies) + } + }); } + + // subset: function(req, res, next) { + // Movie.subs(function(error, movies) { + // var n = req.params.n + // var p = req.params.p + // if(error) { + // var err = new Error("Error retrieving movies list:\n" + error.message); + // err.status = 500; + // next(err); + // } else { + // res.json(movies) + // } + // }); + + +// Retrieve a subset of movies (/movies/sort/release-date?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 + + // sort: function(req, res, next) { + // Movie.sort(function(error, movies) { + // if(error) { + // var err = new Error("Error retrieving movie info:\n" + error.message); + // err.status = 500; + // next(err); + // } else { + // res.json(movies) + // } + // }) + // } } module.exports = MovieController; diff --git a/models/customer.js b/models/customer.js index e69de29bb..d7f809da2 100644 --- a/models/customer.js +++ b/models/customer.js @@ -0,0 +1,161 @@ +var app = require("../app"); +var db = app.get("db"); +var Cust = function(cust) { + this.id = cust; + // this.id = cust.id; + // this.name = cust.name; + // this.address = cust.address; +}; + +// Instance functions + + +// class +Cust.all = function(callback) { + db.query("select * from customers", function(error, custs) { + if(error || !custs) { + callback(error || new Error("Could not retrieve custs"), undefined); + } else { + var allCusts = custs.map(function(cust) { + return new Cust(cust); + }); + // console.log(allCusts) + callback(null, allCusts) + }; + }); +}; + +Cust.sort = function(query, n, p, callback) { + db.customers.find({}, { + order: query, + limit: n, + offset: p + }, function(error, custs) { + if(error || !custs) { + callback(error || new Error("Could not retrieve customer"), undefined); + + // } else if ((query != "name") && (query != "registered_at") && (query != "postal_code")) { + // callback(error || new Error("Undefined sort term"), undefined); + } else { + var allCusts = custs.map(function(cust) { + return new Cust(cust); + }); + callback(null, allCusts) + }; + }); +}; + + +Cust.find = function(ids, callback) { + db.rentals.find({customer_id: ids, checked: "true"}, function(error, custs) { + if(error || !custs) { + callback(error || new Error("Could not retrieve custs"), undefined); + } else { + var allCusts = custs.map(function(rental) { + // var x = new Cust(cust); + return rental.due_date; + }); + callback(null, allCusts) + }; + }); +}; + + +Cust.history = function(ids, callback) { + db.rentals.find({ + id: ids, checked: "false"}, + // order: rental_date, + + function(error, custs) { + if(error || !custs) { + callback(error || new Error("Could not retrieve custs"), undefined); + } else { + var allCusts = custs.map(function(cust) { + return new Cust(cust); + }); + callback(null, allCusts) + }; + }); +}; +// +// return this; +// }; +// +// var balanceResultCallback = function(account, callback) { +// return function(error, result) { +// if(error) { +// callback(error, undefined); +// } else { +// account.getBalance(function(error, balance) { +// callback(error, balance); +// }); +// } +// }; +// }; +// +// Custs.prototype.deposit = function(amount, callback) { +// db.custs_deposit(this.id, amount, balanceResultCallback(this, callback)); +// return this; +// }; +// +// Custs.prototype.withdraw = function(amount, callback) { +// db.custs_withdraw(this.id, amount, balanceResultCallback(this, callback)); +// return this; +// }; +// +// Custs.prototype.transfer = function(to, amount, callback) { +// db.custs_transfer(this.id, to.id, amount, balanceResultCallback(this, callback)); +// return this; +// }; +// +// // Class Functions +// Custs.create = function(initialBalance, callback) { +// db.custs.save({ +// balance: initialBalance +// }, function(error, account) { +// if(error || !account) { +// callback(error || new Error("Could not create account"), undefined); +// } else { +// callback(null, new Custs(account.id)); +// } +// }); +// }; +// +// Custs.createSync = function(initialBalance) { +// var account = db.custs.saveSync({ +// balance: initialBalance +// }); +// +// return new Custs(account.id); +// }; +// +// Custs.all = function(callback) { +// db.custs.find(function(error, custs) { +// if(error || !custs) { +// callback(error || new Error("Could not retrieve custs"), undefined); +// } else { +// callback(null, custs.map(function(account) { +// return new Custs(account.id); +// })); +// } +// }); +// }; +// +// Custs.find = function(id, callback) { +// db.custs.findOne({id: id}, function(error, account) { +// if(error || !account) { +// callback(error || new Error("Custs not found"), undefined); +// } else { +// callback(null, new Custs(account.id)); +// } +// }); +// }; +// +// // only attach this function if we're in test mode +// if (app.get('env') === 'test') { +// Custs.close_connection = function() { +// console.log("closing connection") +// db.end() +// } +// } +module.exports = Cust; diff --git a/models/movie.js b/models/movie.js index fdd6ff874..e983cf7de 100644 --- a/models/movie.js +++ b/models/movie.js @@ -1,27 +1,10 @@ -// Retrieve a list of all movies (/movies) - -// Retrieve a subset of movies (/movies/sort/release-date?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 movie's title... -// Get a list of customers that have currently checked out a copy of the film (/movies/Jaws/current) -// include each customer's name, phone number, and account credit -// Get a list of customers that have checked out a copy in the past (/movies/Jaws/history/sort/name) -// include each customer's name, phone number, and account credit -// ordered by customer name or -// ordered by check out date - - var app = require("../app"); -// var massive = require("massive"); -// var db = massive.connectSync({db: "radio_star"}); var db = app.get("db"); -// console.log(app); -// Constructor function -var Movie = function(id) { - this.id = id; +var Movie = function(movie) { + this.id = movie.id; + this.title = movie.title; + this.release_date = movie.release_date; + this.synopsis = movie.overview; }; // Instance functions @@ -29,6 +12,20 @@ var Movie = function(id) { // class Movie.all = function(callback) { + db.query("select * from movies", function(error, movies) { + if(error || !movies) { + callback(error || new Error("Could not retrieve movies"), undefined); + } else { + var allMovies = movies.map(function(movie) { + return new Movie(movie); + }); + // console.log(allMovies) + callback(null, allMovies) + }; + }); +}; + +Movie.subset = function(callback) { db.query("select * from movies", function(error, movies) { if(error || !movies) { callback(error || new Error("Could not retrieve movies"), undefined); @@ -41,6 +38,62 @@ Movie.all = function(callback) { }; }); }; + + +Movie.sort = function(query, n, p, callback) { + db.movies.find({}, { + order: query, + limit: n, + offset: p + }, function(error, movies) { + if(error || !movies) { + callback(error || new Error("Could not retrieve movies"), undefined); + + // } else if ((query != "name") && (query != "registered_at") && (query != "postal_code")) { + // callback(error || new Error("Undefined sort term"), undefined); + } else { + var allMovies = movies.map(function(movie) { + return new Movie(movie); + }); + callback(null, allMovies) + }; + }); +}; + + +Movie.find = function(ids, callback) { + db.rentals.find({movie_id: ids, checked: "true"}, function(error, movies) { + if(error || !movies) { + callback(error || new Error("Could not retrieve movies"), undefined); + } else { + var allMovies = movies.map(function(rental) { + // var x = new Movie(movie); + return rental.due_date; + }); + // console.log(allMovies) + callback(null, allMovies) + }; + }); +}; + + +Movie.history = function(ids, callback) { + // console.log(n) + db.rentals.find({ + id: ids, checked: "false"}, + // order: rental_date, + + function(error, movies) { + if(error || !movies) { + callback(error || new Error("Could not retrieve movies"), undefined); + } else { + var allMovies = movies.map(function(movie) { + return new Movie(movie); + }); + callback(null, allMovies) + }; + }); +}; // // return this; // }; diff --git a/routes/index.js b/routes/index.js index 3a1440966..d006b6635 100644 --- a/routes/index.js +++ b/routes/index.js @@ -2,7 +2,8 @@ var express = require('express'); var router = express.Router(); var Controller = require('../controllers/index') -var MovieController = require('../controllers/movies') +var MovieController = require('../controllers/movies'); +var CustController = require('../controllers/customers'); /* GET home page. */ @@ -14,52 +15,50 @@ router.get('/', Controller.nothing) router.get('/zomg', Controller.zomg) -// Retrieve a list of all movies -router.get('/movies', MovieController.index); // Retrive a list of all customers -// router.get('/customers/', Controller.) +router.get('/customers/', CustController.index); // Retrive a subset of customers // Given a sort column, return n customer records, offset by p records (this will be used to create "pages" of customers) // Sort columns are: name, registered_at, postal_code -// router.get('/customers/sort/name?n=10&p=2', Controller.) - +// router.get('/customers/sort/:query', CustController.subset); +router.get('/customers/sort/:query?n=10&p=2', CustController.subset); // Given a customer's id... // List the movies they currently have checked out -// router.get('/customers/:id/current', Controller.) -// -// -// // Given a customer's id... -// // List the movies a customer has checked out in the past (ordered by check out date, includes return date) -// router.get('/customers/:id/history', Controller.) -// -// -// -// -// -// -// -// // Retrieve a subset of movie -// // 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 -// router.get('/movies/sort/release-date?n=5&p=1') -// -// -// // Given a movie's title... -// // Get a list of customers that have currently checked out a copy of the film -// // include each customer's name, phone number, and account credit -// router.get('/movies/:movie_title/current') -// -// -// // Given a movie's title... -// // Get a list of customers that have checked out a copy in the past -// // include each customer's name, phone number, and account credit -// // ordered by customer name or ordered by check out date -// router.get('/movies/:movie_title/history/sort/name') +router.get('/customers/:id/current', CustController.current) + + +// Given a customer's id... +// List the movies a customer has checked out in the past (ordered by check out date, includes return date) +router.get('/customers/:id/history', CustController.history) + + +// Retrieve a list of all movies +router.get('/movies', MovieController.index); + +// Retrieve a subset of movie +// 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 + router.get('/movies/sort/:query', MovieController.subset) + // router.get('/movies/sort/:query?n=1&p=5', MovieController.subset) + // query of title or release_date + + + // Given a movie's title... + // Get a list of customers that have currently checked out a copy of the film + // include each customer's name, phone number, and account credit + router.get('/movies/:movie_title/current', MovieController.current) + // +// Given a movie's title... +// Get a list of customers that have checked out a copy in the past +// include each customer's name, phone number, and account credit +// ordered by customer name or ordered by check out date +// router.get('/movies/:movie_title/history/sort/:name') + // // // From 514d424a6efd4d62679c574110b6cd26572bd4c5 Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Fri, 17 Jun 2016 16:30:24 -0700 Subject: [PATCH 10/28] added sequel statements to the customer and movie models instead of loops within loops. only thing to finish is order bys and history. --- controllers/customers.js | 11 +++--- controllers/movies.js | 13 +++++-- db/seeds/rentals.json | 23 +++++++++++ models/customer.js | 84 ++++++++++++++++++++++++++++------------ models/movie.js | 61 ++++++++--------------------- routes/index.js | 6 +-- tasks/seed_data.js | 5 +++ 7 files changed, 124 insertions(+), 79 deletions(-) create mode 100644 db/seeds/rentals.json diff --git a/controllers/customers.js b/controllers/customers.js index fe9b01cb0..b13099f85 100644 --- a/controllers/customers.js +++ b/controllers/customers.js @@ -30,26 +30,27 @@ var CustController = { }); }, +// /customers/:id/current current: function(req, res, next) { - Cust.find(req.params.id, function(error, custs) { + Cust.find([req.params.id, 'true'], function(error, rentals) { if(error) { - var err = new Error("Error retrieving customer list:\n" + error.message); + var err = new Error("Error retrieving customer's current movie list:\n" + error.message); err.status = 500; next(err); } else { - res.json(custs) + res.json(rentals) } }); }, history: function(req, res, next) { - Cust.history(req.params.id, function(error, custs) { + Cust.history([req.params.id, 'false'], function(error, rentals) { if(error) { var err = new Error("Error retrieving customer list:\n" + error.message); err.status = 500; next(err); } else { - res.json(custs) + res.json(rentals) } }); } diff --git a/controllers/movies.js b/controllers/movies.js index 45dc65969..a1d144a05 100644 --- a/controllers/movies.js +++ b/controllers/movies.js @@ -13,7 +13,7 @@ var MovieController = { }); }, - subset: function(req, res, next) { + find: function(req, res, next) { Movie.sort(req.params.query, req.query.n , req.query.p, function(error, movies) { if(error) { var err = new Error("Error retrieving customer list:\n" + error.message); @@ -25,8 +25,11 @@ var MovieController = { }); }, +// /movies/:movie/current current: function(req, res, next) { - Movie.find(req.params.id, function(error, movies) { + var movie = req.params.movie + var movie = movie.toLowerCase().replace(/^./, movie[0].toUpperCase()); + Movie.find(['true', movie], function(error, movies) { if(error) { var err = new Error("Error retrieving customer list:\n" + error.message); err.status = 500; @@ -38,7 +41,11 @@ var MovieController = { }, history: function(req, res, next) { - Movie.history(req.params.id, function(error, movies) { + var movie = req.params.movie + var movie = movie.toLowerCase().replace(/^./, movie[0].toUpperCase()); + + console.log(req.params.query) + Movie.history(['false', movie], req.params.query, function(error, movies) { if(error) { var err = new Error("Error retrieving customer list:\n" + error.message); err.status = 500; diff --git a/db/seeds/rentals.json b/db/seeds/rentals.json new file mode 100644 index 000000000..6a28c37c0 --- /dev/null +++ b/db/seeds/rentals.json @@ -0,0 +1,23 @@ +[ +{ +"movie_id": "1", +"customer_id": "1", +"checked": "false", +"rental_date": "Wed, 29 Apr 2015 07:54:14 -0700", +"due_date": "Wed, 06 May 2015 07:54:14 -0700" +}, +{ +"movie_id": "2", +"customer_id": "1", +"checked": "true", +"rental_date": "Fri, Jun 17 2016 13:33:34 -0700", +"due_date": "Fri, Jun 24 2016 13:33:34 -0700" +}, +{ +"movie_id": "3", +"customer_id": "1", +"checked": "true", +"rental_date": "Fri, Jun 17 2016 13:33:34 -0700", +"due_date": "Fri, Jun 24 2016 13:33:34 -0700" +} +] diff --git a/models/customer.js b/models/customer.js index d7f809da2..0b299d7da 100644 --- a/models/customer.js +++ b/models/customer.js @@ -7,6 +7,13 @@ var Cust = function(cust) { // this.address = cust.address; }; +var Movie = function(movie) { + this.title = movie.title; + // this.id = cust.id; + // this.name = cust.name; + // this.address = cust.address; +}; + // Instance functions @@ -45,38 +52,67 @@ Cust.sort = function(query, n, p, callback) { }); }; - -Cust.find = function(ids, callback) { - db.rentals.find({customer_id: ids, checked: "true"}, function(error, custs) { - if(error || !custs) { - callback(error || new Error("Could not retrieve custs"), undefined); +Cust.find = function(input, callback) { + // find all rentals that this customer has checked out + // console/.log(input) + db.run("SELECT * FROM movies INNER JOIN rentals ON rentals.movie_id=movies.id WHERE rentals.customer_id=$1 and rentals.checked=$2;", input, function(error, rentals) { + console.log(rentals) + if(error || !rentals) { + callback(error || new Error("Could not retrieve customers rentals"), undefined); } else { - var allCusts = custs.map(function(rental) { - // var x = new Cust(cust); - return rental.due_date; - }); - callback(null, allCusts) + callback(null, rentals.map(function(rental) { + return rental; + })); }; }); }; +// Cust.find = function(ids, callback) { +// // find all rentals that this customer has checked out +// db.rentals.find({customer_id: ids, checked: "true"}, function(error, rentals) { +// // console.log(rentals) +// if(error || !rentals) { +// callback(error || new Error("Could not retrieve rentals"), undefined); +// } else { +// // callback(null, rentals.map(function(rental) { +// var arrayOfMovies = []; +// +// var allMovies = rentals.map(function(rental) { +// console.log(rental) +// db.query("select * from movies where id=$1", [rental.movie_id], function(error, movie) { +// // console.log(rental.movie_id) +// // new Movie(rental); +// return arrayOfMovies.push(new Movie(movie[0])) +// console.log(arrayOfMovies) +// // console.log(movie[0].title) +// // return arrayOfMovies; +// }); +// // console.log(arrayOfMovies) +// +// // return new Movie(movie[0]); +// }); +// console.log(allMovies) +// console.log(arrayOfMovies) +// // return arrayOfMovies; +// callback(null, arrayOfMovies) +// // console.log(arrayOfMovies) +// // callback(null, movie) +// }; +// }); +// }; -Cust.history = function(ids, callback) { - db.rentals.find({ - id: ids, checked: "false"}, - // order: rental_date, - - function(error, custs) { - if(error || !custs) { - callback(error || new Error("Could not retrieve custs"), undefined); +Cust.history = function(input, callback) { + db.run("SELECT * FROM movies INNER JOIN rentals ON rentals.movie_id=movies.id WHERE rentals.customer_id=$1 and rentals.checked=$2 ORDER BY rentals.rental_date;", input, function(error, rentals) { + console.log(rentals) + if(error || !rentals) { + callback(error || new Error("Could not retrieve customers rentals"), undefined); } else { - var allCusts = custs.map(function(cust) { - return new Cust(cust); - }); - callback(null, allCusts) - }; + callback(null, rentals.map(function(rental) { + return (rental); + })); + } }); -}; +} // // return this; // }; diff --git a/models/movie.js b/models/movie.js index e983cf7de..6e74e9816 100644 --- a/models/movie.js +++ b/models/movie.js @@ -25,21 +25,6 @@ Movie.all = function(callback) { }); }; -Movie.subset = function(callback) { - db.query("select * from movies", function(error, movies) { - if(error || !movies) { - callback(error || new Error("Could not retrieve movies"), undefined); - } else { - var allMovies = movies.map(function(movie) { - return new Movie(movie); - }); - console.log(allMovies) - callback(null, allMovies) - }; - }); -}; - - Movie.sort = function(query, n, p, callback) { db.movies.find({}, { order: query, @@ -48,9 +33,6 @@ Movie.sort = function(query, n, p, callback) { }, function(error, movies) { if(error || !movies) { callback(error || new Error("Could not retrieve movies"), undefined); - - // } else if ((query != "name") && (query != "registered_at") && (query != "postal_code")) { - // callback(error || new Error("Undefined sort term"), undefined); } else { var allMovies = movies.map(function(movie) { return new Movie(movie); @@ -61,39 +43,30 @@ Movie.sort = function(query, n, p, callback) { }; -Movie.find = function(ids, callback) { - db.rentals.find({movie_id: ids, checked: "true"}, function(error, movies) { - if(error || !movies) { - callback(error || new Error("Could not retrieve movies"), undefined); +Movie.find = function(input, callback) { + db.run("SELECT customers.* FROM movies INNER JOIN rentals ON rentals.movie_id=movies.id INNER JOIN customers ON customers.id=rentals.customer_id WHERE rentals.checked=$1 AND movies.title=$2", input, function(error,customers) { + if(error || !customers) { + callback(error || new Error("Could not retrieve customers customers"), undefined); } else { - var allMovies = movies.map(function(rental) { - // var x = new Movie(movie); - return rental.due_date; - }); - // console.log(allMovies) - callback(null, allMovies) + callback(null, customers.map(function(customer) { + return (customer); + })); }; }); }; - -Movie.history = function(ids, callback) { - // console.log(n) - db.rentals.find({ - id: ids, checked: "false"}, - // order: rental_date, - - function(error, movies) { - if(error || !movies) { - callback(error || new Error("Could not retrieve movies"), undefined); +Movie.history = function(input, query, callback) { + console.log(input) + db.run("SELECT customers.* FROM movies INNER JOIN rentals ON rentals.movie_id=movies.id INNER JOIN customers ON customers.id=rentals.customer_id WHERE rentals.checked = $1 AND movies.title=$2 ORDER BY " + query + ";", input, function(error,customers) { + if(error || !customers) { + callback(error || new Error("Could not retrieve customers customers"), undefined); } else { - var allMovies = movies.map(function(movie) { - return new Movie(movie); - }); - callback(null, allMovies) - }; + callback(null, customers.map(function(customer) { + return (customer); + })); + } }); -}; +} // // return this; // }; diff --git a/routes/index.js b/routes/index.js index d006b6635..04e4c696a 100644 --- a/routes/index.js +++ b/routes/index.js @@ -42,7 +42,7 @@ router.get('/movies', MovieController.index); // Retrieve a subset of movie // 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 - router.get('/movies/sort/:query', MovieController.subset) + router.get('/movies/sort/:query', MovieController.find) // router.get('/movies/sort/:query?n=1&p=5', MovieController.subset) // query of title or release_date @@ -50,14 +50,14 @@ router.get('/movies', MovieController.index); // Given a movie's title... // Get a list of customers that have currently checked out a copy of the film // include each customer's name, phone number, and account credit - router.get('/movies/:movie_title/current', MovieController.current) + router.get('/movies/:movie/current', MovieController.current) // // Given a movie's title... // Get a list of customers that have checked out a copy in the past // include each customer's name, phone number, and account credit // ordered by customer name or ordered by check out date -// router.get('/movies/:movie_title/history/sort/:name') +router.get('/movies/:movie/history/sort/:query', MovieController.history) // // diff --git a/tasks/seed_data.js b/tasks/seed_data.js index 4153f0413..ecb39c478 100644 --- a/tasks/seed_data.js +++ b/tasks/seed_data.js @@ -5,6 +5,7 @@ var db = massive.connectSync({connectionString : connectionString}) var movies_data = require("../db/seeds/movies.json") var customer_data = require("../db/seeds/customers.json") +var rental_data = require("../db/seeds/rentals.json") // var movies_array = JSON.parse(movies_data) // console.log(movies_data) @@ -16,6 +17,10 @@ for(var customer of customer_data){ db.customers.saveSync(customer); } +for(var rental of rental_data){ + db.rentals.saveSync(rental); + } + console.log("seeding done") process.exit() From 4afa5e146bd2e7748d3d84e32cedee266e641d5f Mon Sep 17 00:00:00 2001 From: Mindy Carson Date: Mon, 20 Jun 2016 11:06:16 -0700 Subject: [PATCH 11/28] tried fixing the queries for all rentals and specific rentals info --- app.js | 3 +++ controllers/rentals.js | 35 +++++++++++++++++++++++++++++++++++ db/seeds/rentals.json | 23 +++++++++++++++++++++++ models/rental.js | 32 ++++++++++++++++++++++++++++++++ package.json | 2 +- routes/index.js | 28 +--------------------------- routes/rentals.js | 36 ++++++++++++++++++++++++++++++++++++ tasks/seed_data.js | 5 +++++ 8 files changed, 136 insertions(+), 28 deletions(-) create mode 100644 controllers/rentals.js create mode 100644 db/seeds/rentals.json create mode 100644 models/rental.js create mode 100644 routes/rentals.js diff --git a/app.js b/app.js index cfd65febe..a42935f2d 100644 --- a/app.js +++ b/app.js @@ -32,6 +32,8 @@ module.exports = app; var routes = require('./routes/index'); var users = require('./routes/users'); +var rentals = require('./routes/rentals'); + // uncomment after placing your favicon in /public //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); app.use(logger('dev')); @@ -42,6 +44,7 @@ app.use(express.static(path.join(__dirname, 'public'))); app.use('/', routes); app.use('/users', users); +app.use('/rentals', rentals) // catch 404 and forward to error handler app.use(function(req, res, next) { diff --git a/controllers/rentals.js b/controllers/rentals.js new file mode 100644 index 000000000..45be41ee4 --- /dev/null +++ b/controllers/rentals.js @@ -0,0 +1,35 @@ +var Rental = require('../models/rental'); + +var RentalsController = { + locals: { + title: 'Rentals' + } + + }, + + getRentals: function(req, res) { + Rental.all (function (error, movies) { + if (error) { + var err = new Error( "Sorry, We're having problems retrieving rental list:\n" + error.message); + err.status = 500 + next(err); + } else { + res.json(movies) + } + }) + }, + + getRentalsShow: function(req, res, next) { + Rental.find (req.params.title, function(error, rental) { + if (error) { + var err = new Error("No rentals found"); + err.status = 404; + next(err); + } else { + res.json(rental) + } + }) + } +} + +module.exports = RentalsController diff --git a/db/seeds/rentals.json b/db/seeds/rentals.json new file mode 100644 index 000000000..16ab9cd89 --- /dev/null +++ b/db/seeds/rentals.json @@ -0,0 +1,23 @@ +[ +{ +"customer_id": 1, +"movie_id": 2, +"rental_date": "2016-06-14", +"checked": true, +"due_date": "2016-06-24" +}, +{ +"customer_id": 2, +"movie_id": 4, +"rental_date": "2015-02-14", +"checked": true, +"due_date": "2015-02-24" +}, +{ +"customer_id": 2, +"movie_id": 7, +"rental_date": "2013-06-15", +"checked": false, +"due_date": "2013-06-30" +} +] diff --git a/models/rental.js b/models/rental.js new file mode 100644 index 000000000..1278d75eb --- /dev/null +++ b/models/rental.js @@ -0,0 +1,32 @@ +var app = require('../app') +var db = app.get("db") +var Movie = require('./movie') + +var Rental = function () {} + + +Rental.all = function (callback) { + db.movies.find (function (error, movies) { + if (error || !movies) { + callback(new Error("Could not retrieve movies"), undefined) + } else { + callback(null, movies.map (function (movie) { + return new Movie(movie) + })) + } + }) +} + +Rental.find = function (title, callback) { + db.movies.search({columns:["title"], term: title}, function (error, movies) { + if (error || !movies) { + callback(new Error("Could not retrieve movies"), undefined) + } else { + callback(null, movies.map (function (movie) { + return new Movie(movie) + })) + } + }) +} + +module.exports = Rental diff --git a/package.json b/package.json index 10615d4c7..06c23ea78 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "db:create": "createdb radio_star", "db:schema": "node tasks/load_schema.js", "db:seed": "node tasks/seed_data.js", - "db:reset": "npm run db:drop; npm run db:create; npm run db:schema" + "db:reset": "npm run db:drop; npm run db:create; npm run db:schema; npm run db:seed" }, "dependencies": { "body-parser": "~1.13.2", diff --git a/routes/index.js b/routes/index.js index d006b6635..93b5cb48a 100644 --- a/routes/index.js +++ b/routes/index.js @@ -24,7 +24,7 @@ router.get('/customers/', CustController.index); // Given a sort column, return n customer records, offset by p records (this will be used to create "pages" of customers) // Sort columns are: name, registered_at, postal_code // router.get('/customers/sort/:query', CustController.subset); -router.get('/customers/sort/:query?n=10&p=2', CustController.subset); +router.get('/customers/sort/:query', CustController.subset); // Given a customer's id... // List the movies they currently have checked out @@ -63,30 +63,4 @@ router.get('/movies', MovieController.index); // // // -// // Look a movie up by title to see: it's synopsis, release date, available inventory (not currently checked-out to a customer), and inventory total -// router.get('/rentals/:movie_title') -// -// -// // See a list of customers that have currently checked out any of the movie's inventory -// router.get('/rentals/:movie_title/customers') -// -// -// -// // Given a customer's id and a movie's title ... -// // "check out" one of the movie's inventory to the customer -// // Establish a return date, Charge the customer's account (cost up to you) -// router.get ('/rentals/:movie_title/check-out') -// -// -// // Given a customer's id and a movie's title ... -// // "check in" one of customer's rentals -// // return the movie to its inventory -// router.get('/rentals/:movie_title/return') -// -// -// // See a list of customers with overdue movies -// // include customer name, movie title, check-out date, and return date -// router.get('/rentals/overdue') - - module.exports = router; diff --git a/routes/rentals.js b/routes/rentals.js new file mode 100644 index 000000000..581aa1c69 --- /dev/null +++ b/routes/rentals.js @@ -0,0 +1,36 @@ +var express = require('express'); +var router = express.Router(); +var RentalController = require('../controllers/rentals'); + +router.get('/', RentalController.index); + + +// Look a movie up by title to see: it's synopsis, release date, available inventory (not currently checked-out to a customer), and inventory total + router.get('/:movie_title', RentalController.find_title) +// +// +// // See a list of customers that have currently checked out any of the movie's inventory + router.get(':movie_title/customers') +// +// +// +// // Given a customer's id and a movie's title ... +// // "check out" one of the movie's inventory to the customer +// // Establish a return date, Charge the customer's account (cost up to you) + router.get (':movie_title/check-out') +// +// +// // Given a customer's id and a movie's title ... +// // "check in" one of customer's rentals +// // return the movie to its inventory + router.get(':movie_title/return') +// +// +// // See a list of customers with overdue movies +// // include customer name, movie title, check-out date, and return date + router.get('/overdue') + + + + +module.exports = router; diff --git a/tasks/seed_data.js b/tasks/seed_data.js index 4153f0413..08e0dae94 100644 --- a/tasks/seed_data.js +++ b/tasks/seed_data.js @@ -5,6 +5,7 @@ var db = massive.connectSync({connectionString : connectionString}) var movies_data = require("../db/seeds/movies.json") var customer_data = require("../db/seeds/customers.json") +var rental_data = require("../db/seeds/rentals.json") // var movies_array = JSON.parse(movies_data) // console.log(movies_data) @@ -16,6 +17,10 @@ for(var customer of customer_data){ db.customers.saveSync(customer); } +for(var rental of rental_data){ + db.rentals.saveSync(rental); +} + console.log("seeding done") process.exit() From 93dc7d9e58c6df98dc2d20075641306f9ff75daf Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Mon, 20 Jun 2016 13:20:44 -0700 Subject: [PATCH 12/28] successfully created a testing and development environment. db:reset will reset both testing and development database. now to make sure models/controllers are in the development environment when necessary. --- app.js | 7 ++- bin/www | 2 +- controllers/customers.js | 20 +-------- models/customer.js | 94 +++++----------------------------------- models/movie.js | 10 +++++ package.json | 20 ++++++--- setup.js | 27 ++++++++++++ tasks/load_schema.js | 46 +++++++++++++++----- tasks/seed_data.js | 77 ++++++++++++++++++++++++++------ 9 files changed, 165 insertions(+), 138 deletions(-) create mode 100644 setup.js diff --git a/app.js b/app.js index cfd65febe..0aa5372f0 100644 --- a/app.js +++ b/app.js @@ -6,10 +6,11 @@ var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var app = express(); +app.use(bodyParser.json()); +app.use(bodyParser.urlencoded({ extended: false })); var massive = require("massive") -var connectionString = "postgres://localhost/radio_star"; -// + app.get('env'); +var connectionString = "postgres://localhost/radio_star_" + app.get('env'); // connect to Massive and get the db instance. You can safely use the @@ -35,8 +36,6 @@ var users = require('./routes/users'); // uncomment after placing your favicon in /public //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); app.use(logger('dev')); -app.use(bodyParser.json()); -app.use(bodyParser.urlencoded({ extended: false })); app.use(cookieParser()); app.use(express.static(path.join(__dirname, 'public'))); diff --git a/bin/www b/bin/www index 5e1dbd444..544a8a07d 100755 --- a/bin/www +++ b/bin/www @@ -5,7 +5,7 @@ */ var app = require('../app'); -var debug = require('debug')('VideoStoreAPI:server'); +var debug = require('debug')('js-bank-accounts:server'); var http = require('http'); /** diff --git a/controllers/customers.js b/controllers/customers.js index b13099f85..55bfe0aa9 100644 --- a/controllers/customers.js +++ b/controllers/customers.js @@ -44,7 +44,7 @@ var CustController = { }, history: function(req, res, next) { - Cust.history([req.params.id, 'false'], function(error, rentals) { + Cust.history([req.params.id, 'false'], "rentals.rental_date", function(error, rentals) { if(error) { var err = new Error("Error retrieving customer list:\n" + error.message); err.status = 500; @@ -56,22 +56,4 @@ var CustController = { } } -// Retrieve a subset of custs (/custs/sort/release-date?n=5&p=1) -// Given a sort column, return n movie records, offset by p records (this will be used to create "pages" of custs) -// Sort columns are -// title -// release_date - -// sort: function(req, res, next) { -// Cust.sort(function(error, custs) { -// if(error) { -// var err = new Error("Error retrieving movie info:\n" + error.message); -// err.status = 500; -// next(err); -// } else { -// res.json(custs) -// } -// }) -// } -// } module.exports = CustController; diff --git a/models/customer.js b/models/customer.js index 0b299d7da..43da0a0d4 100644 --- a/models/customer.js +++ b/models/customer.js @@ -1,5 +1,6 @@ var app = require("../app"); var db = app.get("db"); + var Cust = function(cust) { this.id = cust; // this.id = cust.id; @@ -101,8 +102,8 @@ Cust.find = function(input, callback) { // }; -Cust.history = function(input, callback) { - db.run("SELECT * FROM movies INNER JOIN rentals ON rentals.movie_id=movies.id WHERE rentals.customer_id=$1 and rentals.checked=$2 ORDER BY rentals.rental_date;", input, function(error, rentals) { +Cust.history = function(input, query, callback) { + db.run("SELECT * FROM movies INNER JOIN rentals ON rentals.movie_id=movies.id WHERE rentals.customer_id=$1 and rentals.checked=$2 ORDER BY " + query + ";" , input, function(error, rentals) { console.log(rentals) if(error || !rentals) { callback(error || new Error("Could not retrieve customers rentals"), undefined); @@ -113,85 +114,12 @@ Cust.history = function(input, callback) { } }); } -// -// return this; -// }; -// -// var balanceResultCallback = function(account, callback) { -// return function(error, result) { -// if(error) { -// callback(error, undefined); -// } else { -// account.getBalance(function(error, balance) { -// callback(error, balance); -// }); -// } -// }; -// }; -// -// Custs.prototype.deposit = function(amount, callback) { -// db.custs_deposit(this.id, amount, balanceResultCallback(this, callback)); -// return this; -// }; -// -// Custs.prototype.withdraw = function(amount, callback) { -// db.custs_withdraw(this.id, amount, balanceResultCallback(this, callback)); -// return this; -// }; -// -// Custs.prototype.transfer = function(to, amount, callback) { -// db.custs_transfer(this.id, to.id, amount, balanceResultCallback(this, callback)); -// return this; -// }; -// -// // Class Functions -// Custs.create = function(initialBalance, callback) { -// db.custs.save({ -// balance: initialBalance -// }, function(error, account) { -// if(error || !account) { -// callback(error || new Error("Could not create account"), undefined); -// } else { -// callback(null, new Custs(account.id)); -// } -// }); -// }; -// -// Custs.createSync = function(initialBalance) { -// var account = db.custs.saveSync({ -// balance: initialBalance -// }); -// -// return new Custs(account.id); -// }; -// -// Custs.all = function(callback) { -// db.custs.find(function(error, custs) { -// if(error || !custs) { -// callback(error || new Error("Could not retrieve custs"), undefined); -// } else { -// callback(null, custs.map(function(account) { -// return new Custs(account.id); -// })); -// } -// }); -// }; -// -// Custs.find = function(id, callback) { -// db.custs.findOne({id: id}, function(error, account) { -// if(error || !account) { -// callback(error || new Error("Custs not found"), undefined); -// } else { -// callback(null, new Custs(account.id)); -// } -// }); -// }; -// -// // only attach this function if we're in test mode -// if (app.get('env') === 'test') { -// Custs.close_connection = function() { -// console.log("closing connection") -// db.end() -// } -// } + +// only attach this function if we're in test mode +if (app.get('env') === 'test') { + Cust.close_connection = function() { + console.log("closing connection") + db.end() + } +} module.exports = Cust; diff --git a/models/movie.js b/models/movie.js index 6e74e9816..f2d7a4d7d 100644 --- a/models/movie.js +++ b/models/movie.js @@ -1,5 +1,7 @@ var app = require("../app"); var db = app.get("db"); + +console.log(db) var Movie = function(movie) { this.id = movie.id; this.title = movie.title; @@ -148,4 +150,12 @@ Movie.history = function(input, query, callback) { // db.end() // } // } + +// only attach this function if we're in test mode +if (app.get('env') === 'test') { + Movie.close_connection = function() { + console.log("closing connection") + db.end() + } +} module.exports = Movie; diff --git a/package.json b/package.json index 10615d4c7..167ea30bf 100644 --- a/package.json +++ b/package.json @@ -3,28 +3,34 @@ "version": "0.0.0", "private": true, "scripts": { - "start": "nodemon ./bin/www", - "test": "clear; jasmine-node --verbose spec/", - "db:drop": "dropdb radio_star", - "db:create": "createdb radio_star", + "setup": "node ./setup.js", + "start": "./node_modules/.bin/nodemon ./bin/www", + "start-test": "NODE_ENV=test ./node_modules/.bin/nodemon ./bin/www", + "test": "clear; ./node_modules/.bin/istanbul cover --include-all-sources ./node_modules/.bin/jasmine-node --verbose spec/", + "db:drop": "dropdb radio_star_development", + "db:create": "createdb radio_star_development", + "db:dropt": "dropdb radio_star_test", + "db:createt": "createdb radio_star_test", "db:schema": "node tasks/load_schema.js", "db:seed": "node tasks/seed_data.js", - "db:reset": "npm run db:drop; npm run db:create; npm run db:schema" + "db:reset": "npm run db:drop; npm run db:dropt; npm run db:create; npm run db:createt; npm run db:schema; npm run db:schema; npm run db:seed", + "db:all": "npm run setup; npm run db:reset_development; npm run db:reset_test " }, "dependencies": { "body-parser": "~1.13.2", "cookie-parser": "~1.3.5", "debug": "~2.2.0", "express": "~4.13.1", - "jade": "~1.11.0", "massive": "^2.3.0", "morgan": "~1.6.1", "nodemon": "^1.9.2", "serve-favicon": "~2.3.0" }, "devDependencies": { + "istanbul": "^0.4.4", "jasmine-node": "^1.14.5", + "massive": "^2.3.0", "nodemon": "^1.9.2", "request": "^2.72.0" - } + } } diff --git a/setup.js b/setup.js new file mode 100644 index 000000000..7678651bd --- /dev/null +++ b/setup.js @@ -0,0 +1,27 @@ +var spawn = require('child_process').spawnSync; +var Massive = require('massive'); + +['development', 'test'].forEach(function(env) { + var dbName = `radio_star_${env}`; + + // Create the DB if it doesn't already exist + var createDB = spawn('createdb', [dbName]); + if(createDB.error) { + console.log("Error creating database " + dbName + ":"); + console.log(createDB); + return; + } else { + console.log("Successfully created database " + dbName + "."); + } + + // Setup the DB schema + // var db = Massive.connectSync({ db: dbName }); + // db.create_schema(function(error, result) { + // if(error) { + // console.log("Error creating DB schema for " + dbName + ":"); + // console.log(error); + // } else { + // console.log("Successfully created DB schema for " + dbName + "."); + // } + // }); +}); diff --git a/tasks/load_schema.js b/tasks/load_schema.js index 8f0f90085..c3a39755b 100644 --- a/tasks/load_schema.js +++ b/tasks/load_schema.js @@ -1,14 +1,38 @@ -var massive = require('massive') -var connectionString = "postgres://localhost/radio_star" +// var massive = require('massive') +// var connectionString = "postgres://localhost/radio_star" -var db = massive.connectSync({connectionString : connectionString}) +// var db = massive.connectSync({connectionString : connectionString}) -db.setup.schema([], function(err, res) { - if (err) { - // throw(new Error(err.message)) - return console.log('migration error', err.message) - } +// var app = require("../app"); +// var db = app.get("db"); - console.log("yay schema!") - process.exit() -}) +var spawn = require('child_process').spawnSync; +var Massive = require('massive'); + +['development', 'test'].forEach(function(env) { + console.log(env) + // var env = 'development' + var dbName = `radio_star_${env}`; + var db = Massive.connectSync({ db: dbName }); + db.setup.schema([], function(err, res) { + if (err) { + // throw(new Error(err.message)) + return console.log('migration error', err.message); + } + console.log("yay schema!"); + process.exit(); + }); +}); + +// var env = 'test' +// var dbName = `radio_star_${env}`; +// var db = Massive.connectSync({ db: dbName }); +// db.setup.schema([], function(err, res) { +// if (err) { +// // throw(new Error(err.message)) +// return console.log('migration error', err.message); +// } +// console.log("yay schema!"); +// process.exit(); +// }); +// // }); diff --git a/tasks/seed_data.js b/tasks/seed_data.js index ecb39c478..9c616a1c4 100644 --- a/tasks/seed_data.js +++ b/tasks/seed_data.js @@ -1,7 +1,9 @@ var massive = require('massive') -var connectionString = "postgres://localhost/radio_star" -var db = massive.connectSync({connectionString : connectionString}) +// var connectionString = "postgres://localhost/radio_star" +// var db = massive.connectSync({connectionString : connectionString}) +var spawn = require('child_process').spawnSync; +var Massive = require('massive'); var movies_data = require("../db/seeds/movies.json") var customer_data = require("../db/seeds/customers.json") @@ -9,21 +11,70 @@ var rental_data = require("../db/seeds/rentals.json") // var movies_array = JSON.parse(movies_data) // console.log(movies_data) -for(var movie of movies_data){ - db.movies.saveSync(movie); - } +// ['development', 'test'].forEach(function(env) { + function seed_pls() { + // var env = 'development' + var dbName = `radio_star_test`; + var dbNames = `radio_star_development`; + var db = Massive.connectSync({ db: dbName }); + var dbs = Massive.connectSync({ db: dbNames }); + for(var movie of movies_data){ + db.movies.saveSync(movie); + dbs.movies.saveSync(movie); + } -for(var customer of customer_data){ - db.customers.saveSync(customer); - } + for(var customer of customer_data){ + db.customers.saveSync(customer); + dbs.customers.saveSync(customer); + } -for(var rental of rental_data){ - db.rentals.saveSync(rental); - } + for(var rental of rental_data){ + db.rentals.saveSync(rental); + dbs.rentals.saveSync(rental); + } -console.log("seeding done") -process.exit() + // var env = 'test' + // var dbName = `radio_star_${env}`; + // var db = Massive.connectSync({ db: dbName }); + // for(var movie of movies_data){ + // db.movies.saveSync(movie); + // } + // + // for(var customer of customer_data){ + // db.customers.saveSync(customer); + // } + // + // for(var rental of rental_data){ + // db.rentals.saveSync(rental); + // } + // console.log("seeding done") + // process.exit() + console.log("seeding done"); + process.exit(); + }; +// // db.seed([], function(err,res){ +// function seedz() { +// var env = 'test' +// var dbName = `radio_star_${env}`; +// var db = Massive.connectSync({ db: dbName }); +// for(var movie of movies_data){ +// db.movies.saveSync(movie); +// } +// +// for(var customer of customer_data){ +// db.customers.saveSync(customer); +// } +// +// for(var rental of rental_data){ +// db.rentals.saveSync(rental); +// } +// console.log("seeding done") +// process.exit() +// }; + +seed_pls(); +// seedz(); ////////////////////////////////////////////////////////////// // function checkFinish() { // db.movies.count(function(err, res) { From 671c21398513f4c3e155e6609374c20bc6561d9a Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Mon, 20 Jun 2016 13:23:57 -0700 Subject: [PATCH 13/28] previously also created a setup.js and added istanbul for testing. just deleted a bunch of comments to clean up files --- package.json | 3 +- setup.js | 10 ------- tasks/load_schema.js | 21 -------------- tasks/seed_data.js | 66 +------------------------------------------- 4 files changed, 2 insertions(+), 98 deletions(-) diff --git a/package.json b/package.json index 167ea30bf..6a5467a3d 100644 --- a/package.json +++ b/package.json @@ -13,8 +13,7 @@ "db:createt": "createdb radio_star_test", "db:schema": "node tasks/load_schema.js", "db:seed": "node tasks/seed_data.js", - "db:reset": "npm run db:drop; npm run db:dropt; npm run db:create; npm run db:createt; npm run db:schema; npm run db:schema; npm run db:seed", - "db:all": "npm run setup; npm run db:reset_development; npm run db:reset_test " + "db:reset": "npm run db:drop; npm run db:dropt; npm run db:create; npm run db:createt; npm run db:schema; npm run db:schema; npm run db:seed" }, "dependencies": { "body-parser": "~1.13.2", diff --git a/setup.js b/setup.js index 7678651bd..4d0ed823a 100644 --- a/setup.js +++ b/setup.js @@ -14,14 +14,4 @@ var Massive = require('massive'); console.log("Successfully created database " + dbName + "."); } - // Setup the DB schema - // var db = Massive.connectSync({ db: dbName }); - // db.create_schema(function(error, result) { - // if(error) { - // console.log("Error creating DB schema for " + dbName + ":"); - // console.log(error); - // } else { - // console.log("Successfully created DB schema for " + dbName + "."); - // } - // }); }); diff --git a/tasks/load_schema.js b/tasks/load_schema.js index c3a39755b..417cf6914 100644 --- a/tasks/load_schema.js +++ b/tasks/load_schema.js @@ -1,11 +1,3 @@ -// var massive = require('massive') -// var connectionString = "postgres://localhost/radio_star" - -// var db = massive.connectSync({connectionString : connectionString}) - -// var app = require("../app"); -// var db = app.get("db"); - var spawn = require('child_process').spawnSync; var Massive = require('massive'); @@ -23,16 +15,3 @@ var Massive = require('massive'); process.exit(); }); }); - -// var env = 'test' -// var dbName = `radio_star_${env}`; -// var db = Massive.connectSync({ db: dbName }); -// db.setup.schema([], function(err, res) { -// if (err) { -// // throw(new Error(err.message)) -// return console.log('migration error', err.message); -// } -// console.log("yay schema!"); -// process.exit(); -// }); -// // }); diff --git a/tasks/seed_data.js b/tasks/seed_data.js index 9c616a1c4..436bad49c 100644 --- a/tasks/seed_data.js +++ b/tasks/seed_data.js @@ -1,19 +1,13 @@ -var massive = require('massive') - -// var connectionString = "postgres://localhost/radio_star" -// var db = massive.connectSync({connectionString : connectionString}) +// var massive = require('massive') var spawn = require('child_process').spawnSync; var Massive = require('massive'); var movies_data = require("../db/seeds/movies.json") var customer_data = require("../db/seeds/customers.json") var rental_data = require("../db/seeds/rentals.json") -// var movies_array = JSON.parse(movies_data) -// console.log(movies_data) // ['development', 'test'].forEach(function(env) { function seed_pls() { - // var env = 'development' var dbName = `radio_star_test`; var dbNames = `radio_star_development`; var db = Massive.connectSync({ db: dbName }); @@ -33,66 +27,8 @@ var rental_data = require("../db/seeds/rentals.json") dbs.rentals.saveSync(rental); } - // var env = 'test' - // var dbName = `radio_star_${env}`; - // var db = Massive.connectSync({ db: dbName }); - // for(var movie of movies_data){ - // db.movies.saveSync(movie); - // } - // - // for(var customer of customer_data){ - // db.customers.saveSync(customer); - // } - // - // for(var rental of rental_data){ - // db.rentals.saveSync(rental); - // } - // console.log("seeding done") - // process.exit() console.log("seeding done"); process.exit(); }; -// // db.seed([], function(err,res){ -// function seedz() { -// var env = 'test' -// var dbName = `radio_star_${env}`; -// var db = Massive.connectSync({ db: dbName }); -// for(var movie of movies_data){ -// db.movies.saveSync(movie); -// } -// -// for(var customer of customer_data){ -// db.customers.saveSync(customer); -// } -// -// for(var rental of rental_data){ -// db.rentals.saveSync(rental); -// } -// console.log("seeding done") -// process.exit() -// }; - seed_pls(); -// seedz(); -////////////////////////////////////////////////////////////// -// function checkFinish() { -// db.movies.count(function(err, res) { -// console.log("already in db: ", res) -// if (res >= records) {process.exit()} -// }) -// } -// -// for (var movie of movies_data) { -// db.movies.save(movie, function(err, res) { -// console.log('saved: ', JSON.stringify(res)) -// checkFinish() -// }) -// } - // {title: movie.title, overview: movie.overview, release_date: movie.release_date, inventory: movie.inventory} - // function(err,res){ - // if(err) { - // throw new Error(err.message) -// // } -// }) -// } From 4856dbf76b3db7d7050192ff95f63a9a2e663c1c Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Mon, 20 Jun 2016 14:22:49 -0700 Subject: [PATCH 14/28] reseeded to add a searchable title column and an inventory total. fixed a syntax error in rentals.js. going to work on rentals for fun --- controllers/rentals.js | 2 +- db/seeds/rentals.json | 3 ++- db/setup/schema.sql | 4 +++- models/customer.js | 4 ++-- models/movie.js | 5 ++--- routes/rentals.js | 13 +++++++------ tasks/load_schema.js | 2 +- tasks/seed_data.js | 40 +++++++++++++++++++++++++++++++++++++--- 8 files changed, 55 insertions(+), 18 deletions(-) diff --git a/controllers/rentals.js b/controllers/rentals.js index 45be41ee4..8a7f4563c 100644 --- a/controllers/rentals.js +++ b/controllers/rentals.js @@ -3,7 +3,7 @@ var Rental = require('../models/rental'); var RentalsController = { locals: { title: 'Rentals' - } + // } }, diff --git a/db/seeds/rentals.json b/db/seeds/rentals.json index d415a84dc..9fcbf5fb8 100644 --- a/db/seeds/rentals.json +++ b/db/seeds/rentals.json @@ -20,7 +20,8 @@ "rental_date": "2013-06-15", "checked": false, "due_date": "2013-06-30" - +}, +{ "movie_id": "1", "customer_id": "1", "checked": "false", diff --git a/db/setup/schema.sql b/db/setup/schema.sql index dadb73089..9060c9cc9 100644 --- a/db/setup/schema.sql +++ b/db/setup/schema.sql @@ -2,9 +2,11 @@ DROP TABLE IF EXISTS movies; CREATE TABLE movies( id serial PRIMARY KEY, title text, + search_title text, overview text, release_date text, - inventory integer + inventory integer, + inventory_total integer ); CREATE INDEX movies_title ON movies (title); diff --git a/models/customer.js b/models/customer.js index 43da0a0d4..be7f21590 100644 --- a/models/customer.js +++ b/models/customer.js @@ -57,7 +57,7 @@ Cust.find = function(input, callback) { // find all rentals that this customer has checked out // console/.log(input) db.run("SELECT * FROM movies INNER JOIN rentals ON rentals.movie_id=movies.id WHERE rentals.customer_id=$1 and rentals.checked=$2;", input, function(error, rentals) { - console.log(rentals) + // console.log(rentals) if(error || !rentals) { callback(error || new Error("Could not retrieve customers rentals"), undefined); } else { @@ -104,7 +104,7 @@ Cust.find = function(input, callback) { Cust.history = function(input, query, callback) { db.run("SELECT * FROM movies INNER JOIN rentals ON rentals.movie_id=movies.id WHERE rentals.customer_id=$1 and rentals.checked=$2 ORDER BY " + query + ";" , input, function(error, rentals) { - console.log(rentals) + // console.log(rentals) if(error || !rentals) { callback(error || new Error("Could not retrieve customers rentals"), undefined); } else { diff --git a/models/movie.js b/models/movie.js index f2d7a4d7d..0fa3e1c2f 100644 --- a/models/movie.js +++ b/models/movie.js @@ -1,7 +1,7 @@ var app = require("../app"); var db = app.get("db"); -console.log(db) +// console.log(db) var Movie = function(movie) { this.id = movie.id; this.title = movie.title; @@ -11,7 +11,6 @@ var Movie = function(movie) { // Instance functions - // class Movie.all = function(callback) { db.query("select * from movies", function(error, movies) { @@ -58,7 +57,7 @@ Movie.find = function(input, callback) { }; Movie.history = function(input, query, callback) { - console.log(input) + // console.log(input) db.run("SELECT customers.* FROM movies INNER JOIN rentals ON rentals.movie_id=movies.id INNER JOIN customers ON customers.id=rentals.customer_id WHERE rentals.checked = $1 AND movies.title=$2 ORDER BY " + query + ";", input, function(error,customers) { if(error || !customers) { callback(error || new Error("Could not retrieve customers customers"), undefined); diff --git a/routes/rentals.js b/routes/rentals.js index 581aa1c69..1134c93c1 100644 --- a/routes/rentals.js +++ b/routes/rentals.js @@ -1,34 +1,35 @@ var express = require('express'); var router = express.Router(); + var RentalController = require('../controllers/rentals'); -router.get('/', RentalController.index); +router.get('/', RentalController.getRentals); // Look a movie up by title to see: it's synopsis, release date, available inventory (not currently checked-out to a customer), and inventory total - router.get('/:movie_title', RentalController.find_title) + // router.get('/:movie_title', RentalController.find_title) // // // // See a list of customers that have currently checked out any of the movie's inventory - router.get(':movie_title/customers') + // router.get(':movie_title/customers') // // // // // Given a customer's id and a movie's title ... // // "check out" one of the movie's inventory to the customer // // Establish a return date, Charge the customer's account (cost up to you) - router.get (':movie_title/check-out') + // router.get (':movie_title/check-out') // // // // Given a customer's id and a movie's title ... // // "check in" one of customer's rentals // // return the movie to its inventory - router.get(':movie_title/return') + // router.get(':movie_title/return') // // // // See a list of customers with overdue movies // // include customer name, movie title, check-out date, and return date - router.get('/overdue') + // router.get('/overdue') diff --git a/tasks/load_schema.js b/tasks/load_schema.js index 417cf6914..3bdb5a9f1 100644 --- a/tasks/load_schema.js +++ b/tasks/load_schema.js @@ -2,7 +2,7 @@ var spawn = require('child_process').spawnSync; var Massive = require('massive'); ['development', 'test'].forEach(function(env) { - console.log(env) + // console.log(env) // var env = 'development' var dbName = `radio_star_${env}`; var db = Massive.connectSync({ db: dbName }); diff --git a/tasks/seed_data.js b/tasks/seed_data.js index 741d61c2c..ba8f82bb0 100644 --- a/tasks/seed_data.js +++ b/tasks/seed_data.js @@ -12,10 +12,44 @@ var rental_data = require("../db/seeds/rentals.json") var dbNames = `radio_star_development`; var db = Massive.connectSync({ db: dbName }); var dbs = Massive.connectSync({ db: dbNames }); + for(var movie of movies_data){ - db.movies.saveSync(movie); - dbs.movies.saveSync(movie); - } + db.movies.save( + {title: movie.title, + search_title: movie.title.toLowerCase().replace(/ /g, "").replace(/\./g, ""), + overview: movie.overview, + release_date: movie.release_date, + inventory: movie.inventory, + inventory_total: movie.inventory}, function(err,res){ + if(err) { + throw new Error(err.message) + } + }) + } + + for(var movie of movies_data){ + dbs.movies.save( + {title: movie.title, + search_title: movie.title.toLowerCase().replace(/ /g, "").replace(/\./g, ""), + overview: movie.overview, + release_date: movie.release_date, + inventory: movie.inventory, + inventory_total: movie.inventory}, function(err,res){ + if(err) { + throw new Error(err.message) + } + }) + } + + // for(var movie of movies_data){ + // db.movies.saveSync(title: , + // search_title text, + // overview text, + // release_date text, + // inventory integer, + // inventory_total integer); + // dbs.movies.saveSync(movie); + // } for(var customer of customer_data){ db.customers.saveSync(customer); From 9c7f09a3a99193deac8f2f44f5b55631b5cc981c Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Tue, 21 Jun 2016 10:47:29 -0700 Subject: [PATCH 15/28] finished rentals checkout --- app.js | 4 +- controllers/customers.js | 5 +- controllers/rentals.js | 73 +++++++++---- db/setup/schema.sql | 3 +- models/customer.js | 12 +-- models/rental.js | 227 +++++++++++++++++++++++++++++++++++---- routes/index.js | 28 ++++- routes/rentals.js | 14 +-- 8 files changed, 303 insertions(+), 63 deletions(-) diff --git a/app.js b/app.js index e9d24c868..3ae5c78dd 100644 --- a/app.js +++ b/app.js @@ -31,7 +31,7 @@ module.exports = app; var routes = require('./routes/index'); var users = require('./routes/users'); -var rentals = require('./routes/rentals'); +// var rentals = require('./routes/rentals'); // uncomment after placing your favicon in /public //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); @@ -41,7 +41,7 @@ app.use(express.static(path.join(__dirname, 'public'))); app.use('/', routes); app.use('/users', users); -app.use('/rentals', rentals) +// app.use('/rentals', rentals) // catch 404 and forward to error handler app.use(function(req, res, next) { diff --git a/controllers/customers.js b/controllers/customers.js index 55bfe0aa9..89447de88 100644 --- a/controllers/customers.js +++ b/controllers/customers.js @@ -14,9 +14,8 @@ var CustController = { }, subset: function(req, res, next) { - console.log(req.query) - console.log("req params query: ", req.params.query) - + // console.log(req.query) + // console.log("req params query: ", req.params.query) Cust.sort(req.params.query, req.query.n , req.query.p, function(error, custs) { // var n = req.params.n // var p = req.params.p diff --git a/controllers/rentals.js b/controllers/rentals.js index 8a7f4563c..044d3defc 100644 --- a/controllers/rentals.js +++ b/controllers/rentals.js @@ -1,35 +1,70 @@ var Rental = require('../models/rental'); var RentalsController = { - locals: { - title: 'Rentals' - // } +// /rentals/:movie +// returns overview, release_date available inventory and total inventory + find: function(req, res, next) { + Rental.search([req.params.movie.toLowerCase().replace(/ /g, "").replace(/\./g, "")], function(error, movie) { + if(error) { + var err = new Error("Error retrieving movie:\n" + error.message); + err.status = 500; + next(err); + } else { + res.json(movie) + } + }); }, - getRentals: function(req, res) { - Rental.all (function (error, movies) { - if (error) { - var err = new Error( "Sorry, We're having problems retrieving rental list:\n" + error.message); - err.status = 500 + findCustomers: function(req, res, next) { + Rental.searchCust(['true', req.params.movie.toLowerCase().replace(/ /g, "").replace(/\./g, "")], function(error, customers) { + if(error) { + var err = new Error("Error retrieving customers:\n" + error.message); + err.status = 500; next(err); } else { - res.json(movies) + res.json(customers) } - }) + }); }, - getRentalsShow: function(req, res, next) { - Rental.find (req.params.title, function(error, rental) { - if (error) { - var err = new Error("No rentals found"); - err.status = 404; + checkOut: function(req, res, next) { + // Rental.checkout([req.body.customer], [req.params.movie.toLowerCase().replace(/ /g, "").replace(/\./g, "")], function(error, customers) { + Rental.checkout([2], [req.params.movie.toLowerCase().replace(/ /g, "").replace(/\./g, "")], function(error, customers) { + if(error) { + var err = new Error("Error retrieving customers:\n" + error.message); + err.status = 500; next(err); } else { - res.json(rental) + res.json(customers) } - }) + }); + }, + + return: function(req, res, next) { + // Rental.return([req.body.customer], [req.params.movie.toLowerCase().replace(/ /g, "").replace(/\./g, "")], function(error, customers) { + Rental.return([2], [req.params.movie.toLowerCase().replace(/ /g, "").replace(/\./g, "")], function(error, customers) { + if(error) { + var err = new Error("Error retrieving customers:\n" + error.message); + err.status = 500; + next(err); + } else { + res.json(customers) + } + }); } -} -module.exports = RentalsController + // getRentalsShow: function(req, res, next) { + // Rental.find (req.params.title, function(error, rental) { + // if (error) { + // var err = new Error("No rentals found"); + // err.status = 404; + // next(err); + // } else { + // res.json(rental)l + // } + // }); + // } +}; + +module.exports = RentalsController; diff --git a/db/setup/schema.sql b/db/setup/schema.sql index 9060c9cc9..813839358 100644 --- a/db/setup/schema.sql +++ b/db/setup/schema.sql @@ -36,7 +36,8 @@ CREATE TABLE rentals( customer_id integer REFERENCES customers (id), checked text, rental_date text, - due_date text + due_date text, + return_date text ); CREATE INDEX rentals_customers ON rentals (customer_id); diff --git a/models/customer.js b/models/customer.js index be7f21590..85e820645 100644 --- a/models/customer.js +++ b/models/customer.js @@ -8,12 +8,12 @@ var Cust = function(cust) { // this.address = cust.address; }; -var Movie = function(movie) { - this.title = movie.title; - // this.id = cust.id; - // this.name = cust.name; - // this.address = cust.address; -}; +// var Movie = function(movie) { +// this.title = movie.title; +// // this.id = cust.id; +// // this.name = cust.name; +// // this.address = cust.address; +// }; // Instance functions diff --git a/models/rental.js b/models/rental.js index 1278d75eb..21d98e0a4 100644 --- a/models/rental.js +++ b/models/rental.js @@ -1,32 +1,219 @@ var app = require('../app') var db = app.get("db") -var Movie = require('./movie') +// var Movie = require('./movie') -var Rental = function () {} +var Rental = function (rental) { + this.rental = rental; +}; - -Rental.all = function (callback) { - db.movies.find (function (error, movies) { - if (error || !movies) { - callback(new Error("Could not retrieve movies"), undefined) +// /rentals/:movie +// returns overview, release_date available inventory and total inventory +Rental.search = function (input, callback) { + db.run("SELECT overview, release_date, inventory, inventory_total FROM movies WHERE search_title=$1", input, function (error, movie) { + if (error || !movie) { + callback(new Error("Could not retrieve movie"), undefined) } else { - callback(null, movies.map (function (movie) { - return new Movie(movie) - })) + callback(null, movie.map (function (movies) { + return (movies) + })); } - }) + }); } -Rental.find = function (title, callback) { - db.movies.search({columns:["title"], term: title}, function (error, movies) { - if (error || !movies) { - callback(new Error("Could not retrieve movies"), undefined) +// /rentals/:movie/customers +// returns list of customers (assume its full info) +Rental.searchCust = function (input, callback) { + console.log(input) + db.run("SELECT customers.* FROM movies INNER JOIN rentals ON rentals.movie_id=movies.id INNER JOIN customers ON customers.id=rentals.customer_id WHERE rentals.checked=$1 AND search_title=$2", input, function (error, customer) { + if (error || !customer) { + callback(new Error("Could not retrieve customer"), undefined) } else { - callback(null, movies.map (function (movie) { - return new Movie(movie) - })) + callback(null, customer.map (function (customers) { + return (customers) + })); } - }) + }); } -module.exports = Rental + + +Rental.checkout = function (customer_id, movie, callback) { + // check to see if movie exists + db.run("SELECT movies.id from movies WHERE search_title=$1", movie, function (error, movieid) { + if (error || !movieid || movieid.length===0) { + callback(new Error("Could not retrieve movie based on movie title."), undefined) + } else { + + // check to see if there is inventory + db.run("SELECT * from movies WHERE search_title=$1 AND inventory > 0", movie, function (error, movieinfo) { + if (error || !movieinfo || movieinfo.length===0) { + callback(new Error("Movie is not available to rent. Zero inventory."), undefined) + } else { + + // check to see if customer exists + db.run("SELECT customers.id from customers WHERE id=$1", customer_id, function (error, customerid) { + if (error || !customerid || customerid.length===0) { + callback(new Error("Could not retrieve customer from database. Customer id should be sent in JSON body."), undefined) + } else { + + // update RENTALSSSS. this should always work, thanks to the movie and customer checks above. + var insertArray = []; + // duedate is 7 days after Date.now + var duedate = (new Date(Date.now(new Date())+604800000)).toString(); + insertArray.push(movieid[0]["id"], customer_id[0], 'true', (new Date()).toString(), duedate) + db.run("INSERT INTO rentals (movie_id, customer_id, checked, rental_date, due_date) VALUES ($1, $2, $3, $4, $5);", insertArray, function (error, rentals) { + if (error || !rentals) { + callback(new Error("Could not update rental info"), undefined) + } else { + + // update customer credit -1.50 AFTER making the rental + db.run("UPDATE customers SET account_credit=account_credit-1.5 WHERE id=$1 AND account_credit > 1.45;", customer_id, function (error, updates) { + if (error || !updates) { + callback(new Error("Could not update customer credit, might not have enough money"), undefined) + } else { + + // update movie inventory + db.run("UPDATE movies SET inventory=inventory-1 WHERE search_title=$1;", movie, function (error, updatesmovie) { + if (error || !updatesmovie) { + callback(new Error("Could not update movie inventory"), undefined) + } else { + + // NO ERRORSSSSS! return the due date for the rental + callback(null, {return_date: duedate}); + } + }) + }}); + }}); + }}); + }}); + }}); +}; + +Rental.return = function (customer_id, movie_title, callback) { + // check to see if movie exists + db.movies.where("search_title=$1", movie_title, function (error, movie) { + // console.log(movie) + if (error || !movie) { + callback(new Error("Could not retrieve movie based on given movie title"), undefined) + } else { + // if no errors, check to see if customer exists + db.customers.where("id=$1", customer_id, function (error, customer) { + if (error || !customer) { + callback(new Error("Could not retrieve customer from database"), undefined) + } else { + // does this person even have a rental?? + var arrCheck = [] + arrCheck.push(customer_id[0], movie[0].id, 'true') + console.log(arrCheck) + db.run("SELECT * FROM rentals WHERE customer_id=$1 AND movie_id=$2 AND checked=$3", arrCheck, function (error, rental) { + console.log(rental) + if (error || !rental || rental.length===0) { + callback(new Error("This customer does not have this movie checked out"), undefined) + } else { + + // if no errors, customer, movie and rentals exist. + // update rentals back to checked in + var insertArray = []; + var returndate = (new Date()).toString(); + insertArray.push('false', returndate, customer_id[0], movie[0].id) + console.log(insertArray) + db.run("UPDATE rentals SET checked=$1, return_date=$2 WHERE customer_id=$3 AND movie_id=$4;", insertArray, function (error, rentalupdate) { + if (error || !rentalupdate) { + callback(new Error("Could not update rental info"), undefined) + } else { + // update movie inventory + db.run("UPDATE movies SET inventory=inventory+1 WHERE search_title=$1;", movie_title, function (error, updatesmovie) { + if (error || !updatesmovie) { + callback(new Error("Could not update movie inventory"), undefined) + } else { + callback(undefined, rentalupdate) + }}) + }}) + }}) + }}) + }}) + } + // }) + // }}) + // } + // var insertArray = []; + // var returndate = (new Date(Date.now(new Date())+604800000)).toString(); + // insertArray.push(movieid[0]["id"], customer_id[0], 'true', (new Date()).toString(), returndate) + + // console.log("movie id: ", movieid) +// +// +// // console.log(updates) +// // if no errors, update movie inventory +// db.run("UPDATE movies SET inventory=inventory-1 WHERE search_title=$1;", movie, function (error, updatesmovie) { +// if (error || !updatesmovie) { +// callback(new Error("Could not update movie inventory"), undefined) +// } else { +// +// // if no errors, update RENTALSSSS +// var insertArray = []; +// var returndate = (new Date(Date.now(new Date())+604800000)).toString(); +// insertArray.push(movieid[0]["id"], customer_id[0], 'true', (new Date()).toString(), returndate) +// +// db.run("INSERT INTO rentals (movie_id, customer_id, checked, rental_date, due_date) VALUES ($1, $2, $3, $4, $5);", insertArray, function (error, rentals) { +// if (error || !rentals) { +// callback(new Error("Could not update rental info"), undefined) +// } else { +// callback(null, {return_date: returndate}); +// } +// }) +// }}); +// }}); +// }}); +// }}); +// }; + + + + + + + + // callback(null, inventories) + + // return (inventories) + // }}) + // }); + + // }}); + + // db.run("INSERT INTO rentals (movie_id, customer_id, checked, rental_date, due_date) VALUES (5, 1, 'true', '2016-07-19', '2016-07-31');", customer_id, function (error, inventory) { + // if (error || !inventory) { + // callback(new Error("Could not retrieve inventory"), undefined) + // } else { + // callback(null, inventory.map (function (inventories) { + // return (inventories) + // })); + // } + // }); + // + // db.run("UPDATE customers SET account_credit=account_credit-1.5 WHERE id=$1;", customer_id, function (error, inventory) { + // if (error || !inventory) { + // callback(new Error("Could not retrieve inventory"), undefined) + // } else { + // callback(null, inventory.map (function (inventories) { + // return (inventories) + // })); + // } + // }); + +// } +// +// Rental.find = function (title, callback) { +// db.movies.search({columns:["title"], term: title}, function (error, movies) { +// if (error || !movies) { +// callback(new Error("Could not retrieve movies"), undefined) +// } else { +// callback(null, movies.map (function (movie) { +// return new Movie(movie) +// })) +// } +// }) +// } + +module.exports = Rental; diff --git a/routes/index.js b/routes/index.js index 1a574ec0f..0f38e71a7 100644 --- a/routes/index.js +++ b/routes/index.js @@ -4,7 +4,7 @@ var Controller = require('../controllers/index') var MovieController = require('../controllers/movies'); var CustController = require('../controllers/customers'); - +var RentalController = require('../controllers/rentals'); /* GET home page. */ router.get('/', function(req, res, next) { @@ -59,8 +59,26 @@ router.get('/movies', MovieController.index); // ordered by customer name or ordered by check out date router.get('/movies/:movie/history/sort/:query', MovieController.history) -// -// -// -// +// Look a movie up by title to see (/rentals/Jaws) +// it's synopsis +// release date +// available inventory (not currently checked-out to a customer) +// and inventory total +router.get('/rentals/:movie', RentalController.find); + +// See a list of customers that have currently checked out any of the movie's inventory (/rentals/Jaws/customers) +router.get('/rentals/:movie/customers', RentalController.findCustomers); + +// Given a customer's id and a movie's title ... +// "check out" one of the movie's inventory to the customer (/rentals/Jaws/check-out) +// Establish a return date +// Charge the customer's account (cost up to you) +router.get('/rentals/:movie/check-out', RentalController.checkOut); + + +// "check in" one of customer's rentals (/rentals/Jaws/return) +// return the movie to its inventory +router.get('/rentals/:movie/return', RentalController.return); + + module.exports = router; diff --git a/routes/rentals.js b/routes/rentals.js index 1134c93c1..eca048eb9 100644 --- a/routes/rentals.js +++ b/routes/rentals.js @@ -1,9 +1,9 @@ -var express = require('express'); -var router = express.Router(); - -var RentalController = require('../controllers/rentals'); - -router.get('/', RentalController.getRentals); +// var express = require('express'); +// var router = express.Router(); +// +// var RentalController = require('../controllers/rentals'); +// +// router.get('/', RentalController.getRentals); // Look a movie up by title to see: it's synopsis, release date, available inventory (not currently checked-out to a customer), and inventory total @@ -34,4 +34,4 @@ router.get('/', RentalController.getRentals); -module.exports = router; +// module.exports = router; From 12791c96ac418daaab85b839ab536880ed3bcccf Mon Sep 17 00:00:00 2001 From: Mindy Carson Date: Tue, 21 Jun 2016 10:58:04 -0700 Subject: [PATCH 16/28] got functions for seraching movie titles and searching for customers who currently have queried movies checked out --- controllers/rentals.js | 26 ++++++++++++++-------- db/seeds/rentals.json | 21 ------------------ db/setup/schema.sql | 5 +++-- models/movie.js | 2 +- models/rental.js | 50 +++++++++++++++++++++++++++++++++++------- routes/rentals.js | 8 +++---- 6 files changed, 67 insertions(+), 45 deletions(-) diff --git a/controllers/rentals.js b/controllers/rentals.js index 45be41ee4..ac390f235 100644 --- a/controllers/rentals.js +++ b/controllers/rentals.js @@ -1,13 +1,9 @@ var Rental = require('../models/rental'); var RentalsController = { - locals: { - title: 'Rentals' - } - }, - getRentals: function(req, res) { + index: function(req, res) { Rental.all (function (error, movies) { if (error) { var err = new Error( "Sorry, We're having problems retrieving rental list:\n" + error.message); @@ -19,14 +15,26 @@ var RentalsController = { }) }, - getRentalsShow: function(req, res, next) { - Rental.find (req.params.title, function(error, rental) { + findTitle: function(req, res, next) { + Rental.find (req.params.movie_title, function(error, movies) { + if (error) { + var err = new Error("No movies found"); + err.status = 404; + next(err); + } else { + res.json(movies) + } + }) + }, + + customersNames: function(req, res, next) { + Rental.customers (req.params.movie_title, function(error,customerIds) { if (error) { - var err = new Error("No rentals found"); + var err = new Error("No customers found"); err.status = 404; next(err); } else { - res.json(rental) + res.json(customerIds) } }) } diff --git a/db/seeds/rentals.json b/db/seeds/rentals.json index d415a84dc..6a28c37c0 100644 --- a/db/seeds/rentals.json +++ b/db/seeds/rentals.json @@ -1,26 +1,5 @@ [ { - -"customer_id": 1, -"movie_id": 2, -"rental_date": "2016-06-14", -"checked": true, -"due_date": "2016-06-24" -}, -{ -"customer_id": 2, -"movie_id": 4, -"rental_date": "2015-02-14", -"checked": true, -"due_date": "2015-02-24" -}, -{ -"customer_id": 2, -"movie_id": 7, -"rental_date": "2013-06-15", -"checked": false, -"due_date": "2013-06-30" - "movie_id": "1", "customer_id": "1", "checked": "false", diff --git a/db/setup/schema.sql b/db/setup/schema.sql index dadb73089..c9586d6bc 100644 --- a/db/setup/schema.sql +++ b/db/setup/schema.sql @@ -1,4 +1,7 @@ +DROP TABLE IF EXISTS rentals; DROP TABLE IF EXISTS movies; +DROP TABLE IF EXISTS customers; + CREATE TABLE movies( id serial PRIMARY KEY, title text, @@ -10,7 +13,6 @@ CREATE TABLE movies( CREATE INDEX movies_title ON movies (title); CREATE INDEX movies_date ON movies (release_date); -DROP TABLE IF EXISTS customers; CREATE TABLE customers( id serial PRIMARY KEY, name text, @@ -27,7 +29,6 @@ CREATE INDEX customers_name ON customers (name); CREATE INDEX customers_date ON customers (registered_at); CREATE INDEX customers_postal ON customers (postal_code); -DROP TABLE IF EXISTS rentals; CREATE TABLE rentals( id serial PRIMARY KEY, movie_id integer REFERENCES movies (id), diff --git a/models/movie.js b/models/movie.js index f2d7a4d7d..e7920f19b 100644 --- a/models/movie.js +++ b/models/movie.js @@ -1,7 +1,7 @@ var app = require("../app"); var db = app.get("db"); -console.log(db) +// console.log(db) var Movie = function(movie) { this.id = movie.id; this.title = movie.title; diff --git a/models/rental.js b/models/rental.js index 1278d75eb..952e33b4e 100644 --- a/models/rental.js +++ b/models/rental.js @@ -2,7 +2,9 @@ var app = require('../app') var db = app.get("db") var Movie = require('./movie') -var Rental = function () {} +var Rental = function(id) { + this.id = id; +} Rental.all = function (callback) { @@ -10,21 +12,53 @@ Rental.all = function (callback) { if (error || !movies) { callback(new Error("Could not retrieve movies"), undefined) } else { - callback(null, movies.map (function (movie) { - return new Movie(movie) - })) + callback(null, movies) //??????? } }) } Rental.find = function (title, callback) { - db.movies.search({columns:["title"], term: title}, function (error, movies) { + db.movies.find({title: title}, function (error, movies) { + if (error || !movies) { + callback(new Error("Could not retrieve movies"), undefined) + } else { + //look up all rentals for that movie + db.rentals.find({movie_id: movies[0].id}, function (error, rentals) { + if (error || !rentals) { + callback(new Error("Could not retrieve rentals"), undefined) + } else { + var out = 0 + + for (movie of rentals) { + if (movie.checked === "true") { + out += 1 + } + } + movies[0]["available_inventory"] = (movies[0].inventory - out) + + callback(null, movies[0]) + } + }) + } + }) +} + +Rental.customers = function (title, callback) { + db.movies.findOne({title: title}, function (error, movies) { if (error || !movies) { callback(new Error("Could not retrieve movies"), undefined) } else { - callback(null, movies.map (function (movie) { - return new Movie(movie) - })) + db.rentals.find({movie_id: movies.id}, function (error, rentalRecords) { + if(error || !rentalRecords) { + callback(new Error("Could not retrieve movies"), undefined) + } else { + var customerIds = [] + for (var record of rentalRecords) { + customerIds.push(record.customer_id) + } + callback(null, customerIds) + } + }) } }) } diff --git a/routes/rentals.js b/routes/rentals.js index 581aa1c69..11ec5f7e8 100644 --- a/routes/rentals.js +++ b/routes/rentals.js @@ -6,24 +6,24 @@ router.get('/', RentalController.index); // Look a movie up by title to see: it's synopsis, release date, available inventory (not currently checked-out to a customer), and inventory total - router.get('/:movie_title', RentalController.find_title) + router.get('/:movie_title', RentalController.findTitle) // // // // See a list of customers that have currently checked out any of the movie's inventory - router.get(':movie_title/customers') + router.get('/:movie_title/customers', RentalController.customersNames) // // // // // Given a customer's id and a movie's title ... // // "check out" one of the movie's inventory to the customer // // Establish a return date, Charge the customer's account (cost up to you) - router.get (':movie_title/check-out') + router.get ('/:movie_title/check-out') // // // // Given a customer's id and a movie's title ... // // "check in" one of customer's rentals // // return the movie to its inventory - router.get(':movie_title/return') + router.get('/:movie_title/return') // // // // See a list of customers with overdue movies From a4dabad2266932bd1e8f839a601992519c8edf77 Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Tue, 21 Jun 2016 12:56:13 -0700 Subject: [PATCH 17/28] added return functionality. it works --- models/rental.js | 80 ++++++++++++++++++++++++++++-------------------- 1 file changed, 47 insertions(+), 33 deletions(-) diff --git a/models/rental.js b/models/rental.js index 21d98e0a4..41ab2496c 100644 --- a/models/rental.js +++ b/models/rental.js @@ -56,32 +56,39 @@ Rental.checkout = function (customer_id, movie, callback) { callback(new Error("Could not retrieve customer from database. Customer id should be sent in JSON body."), undefined) } else { - // update RENTALSSSS. this should always work, thanks to the movie and customer checks above. - var insertArray = []; - // duedate is 7 days after Date.now - var duedate = (new Date(Date.now(new Date())+604800000)).toString(); - insertArray.push(movieid[0]["id"], customer_id[0], 'true', (new Date()).toString(), duedate) - db.run("INSERT INTO rentals (movie_id, customer_id, checked, rental_date, due_date) VALUES ($1, $2, $3, $4, $5);", insertArray, function (error, rentals) { - if (error || !rentals) { - callback(new Error("Could not update rental info"), undefined) - } else { - - // update customer credit -1.50 AFTER making the rental - db.run("UPDATE customers SET account_credit=account_credit-1.5 WHERE id=$1 AND account_credit > 1.45;", customer_id, function (error, updates) { - if (error || !updates) { - callback(new Error("Could not update customer credit, might not have enough money"), undefined) + // check to see if customer got $$$$$ + db.run("SELECT * from customers WHERE id=$1 AND account_credit > 1.49", customer_id, function (error, cust) { + if (error || !cust || cust.length===0) { + callback(new Error("Customer does not have enough money for a rental."), undefined) + } else { + + // update RENTALSSSS. this should always work, thanks to the movie and customer checks above. + var insertArray = []; + // duedate is 7 days after Date.now + var duedate = (new Date(Date.now(new Date())+604800000)).toString(); + insertArray.push(movieid[0]["id"], customer_id[0], 'true', (new Date()).toString(), duedate) + db.run("INSERT INTO rentals (movie_id, customer_id, checked, rental_date, due_date) VALUES ($1, $2, $3, $4, $5);", insertArray, function (error, rentals) { + if (error || !rentals) { + callback(new Error("Could not update rental info"), undefined) } else { - // update movie inventory - db.run("UPDATE movies SET inventory=inventory-1 WHERE search_title=$1;", movie, function (error, updatesmovie) { - if (error || !updatesmovie) { - callback(new Error("Could not update movie inventory"), undefined) + // update customer credit -1.50 AFTER making the rental + db.run("UPDATE customers SET account_credit=account_credit-1.5 WHERE id=$1;", customer_id, function (error, updates) { + if (error || !updates) { + callback(new Error("Could not update customer credit, might not have enough money"), undefined) } else { - // NO ERRORSSSSS! return the due date for the rental - callback(null, {return_date: duedate}); - } - }) + // update movie inventory + db.run("UPDATE movies SET inventory=inventory-1 WHERE search_title=$1;", movie, function (error, updatesmovie) { + if (error || !updatesmovie) { + callback(new Error("Could not update movie inventory"), undefined) + } else { + + // NO ERRORSSSSS! return the due date for the rental + callback(null, {return_date: duedate}); + } + }) + }}); }}); }}); }}); @@ -96,43 +103,50 @@ Rental.return = function (customer_id, movie_title, callback) { if (error || !movie) { callback(new Error("Could not retrieve movie based on given movie title"), undefined) } else { - // if no errors, check to see if customer exists + + // check to see if customer exists db.customers.where("id=$1", customer_id, function (error, customer) { if (error || !customer) { callback(new Error("Could not retrieve customer from database"), undefined) } else { + // does this person even have a rental?? + // if multiple rentals.. assume customer wants to return one with closest due date -> LIMIT 1 will limit to the first found id. + // UPDATE rentals SET checked='false', return_date='Tue Jun 21 2016 11:24:33 GMT-0700 (PDT)' FROM rentals WHERE customer_id=2 AND movie_id=2 AND checked='true' ORDER BY id LIMIT 1; + + var arrCheck = [] arrCheck.push(customer_id[0], movie[0].id, 'true') - console.log(arrCheck) - db.run("SELECT * FROM rentals WHERE customer_id=$1 AND movie_id=$2 AND checked=$3", arrCheck, function (error, rental) { - console.log(rental) + db.run("SELECT * FROM rentals WHERE customer_id=$1 AND movie_id=$2 AND checked=$3 LIMIT 1", arrCheck, function (error, rental) { if (error || !rental || rental.length===0) { - callback(new Error("This customer does not have this movie checked out"), undefined) + callback(new Error("This customer currently does not have this movie checked out"), undefined) } else { - // if no errors, customer, movie and rentals exist. - // update rentals back to checked in + // Update rentals checked in and return date + // This should always work, thanks to the movie, customer and rental checks above. var insertArray = []; var returndate = (new Date()).toString(); - insertArray.push('false', returndate, customer_id[0], movie[0].id) - console.log(insertArray) - db.run("UPDATE rentals SET checked=$1, return_date=$2 WHERE customer_id=$3 AND movie_id=$4;", insertArray, function (error, rentalupdate) { + insertArray.push('false', returndate, rental[0]["id"]) + // insertArray.push('false', returndate, customer_id[0], movie[0].id) + db.run("UPDATE rentals SET checked=$1, return_date=$2 WHERE id=$3;", insertArray, function (error, rentalupdate) { if (error || !rentalupdate) { callback(new Error("Could not update rental info"), undefined) } else { + // update movie inventory + console.log(movie_title) db.run("UPDATE movies SET inventory=inventory+1 WHERE search_title=$1;", movie_title, function (error, updatesmovie) { if (error || !updatesmovie) { callback(new Error("Could not update movie inventory"), undefined) } else { - callback(undefined, rentalupdate) + callback(undefined, {return: "Movie has been successfully returned"}) }}) }}) }}) }}) }}) } + // }) // }}) // } From 53fb652e450a2664c15d0a9526653f299058783e Mon Sep 17 00:00:00 2001 From: Mindy Carson Date: Tue, 21 Jun 2016 15:57:47 -0700 Subject: [PATCH 18/28] updated the find customers name function to show name instead of id --- controllers/rentals.js | 16 ++++++++-------- models/rental.js | 24 ++++++++++++++++++++---- routes/rentals.js | 7 ------- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/controllers/rentals.js b/controllers/rentals.js index 1b5775a8c..94ca0abf9 100644 --- a/controllers/rentals.js +++ b/controllers/rentals.js @@ -1,13 +1,13 @@ var Rental = require('../models/rental'); -var RentalsController = { - locals: { - title: 'Rentals' - } +var RentalController = { + // locals: { + // title: 'Rentals' + // }, - index: function(req, res) { + getRentals: function(req, res) { Rental.all (function (error, movies) { if (error) { var err = new Error( "Sorry, We're having problems retrieving rental list:\n" + error.message); @@ -32,16 +32,16 @@ var RentalsController = { }, customersNames: function(req, res, next) { - Rental.customers (req.params.movie_title, function(error,customerIds) { + Rental.customers (req.params.movie_title, function(error, customerNames) { if (error) { var err = new Error("No customers found"); err.status = 404; next(err); } else { - res.json(customerIds) + res.json(customerNames) } }) } } -module.exports = RentalsController +module.exports = RentalController diff --git a/models/rental.js b/models/rental.js index 952e33b4e..43d6c941e 100644 --- a/models/rental.js +++ b/models/rental.js @@ -2,6 +2,7 @@ var app = require('../app') var db = app.get("db") var Movie = require('./movie') + var Rental = function(id) { this.id = id; } @@ -52,11 +53,26 @@ Rental.customers = function (title, callback) { if(error || !rentalRecords) { callback(new Error("Could not retrieve movies"), undefined) } else { - var customerIds = [] - for (var record of rentalRecords) { - customerIds.push(record.customer_id) + console.log(rentalRecords) + //loop for each of the customer_ids then pass that whole array into the db call + var custIds = [] + for (var cust of rentalRecords) { + custIds.push(cust.customer_id) } - callback(null, customerIds) + console.log(custIds) + db.customers.find({id: custIds}, function (error, customersNames) { + console.log(customersNames) + if(error || !customersNames) { + callback(new Error("Could not retrieve customers"), undefined) + } else { + var customerNames = [] + for (var record of customersNames) { + console.log(record.name) + customerNames.push(record.name) + } + callback(null, customerNames) + } + }) } }) } diff --git a/routes/rentals.js b/routes/rentals.js index b865e2d61..0f5419b27 100644 --- a/routes/rentals.js +++ b/routes/rentals.js @@ -14,14 +14,7 @@ router.get('/', RentalController.getRentals); // // See a list of customers that have currently checked out any of the movie's inventory router.get('/:movie_title/customers', RentalController.customersNames) - // router.get('/:movie_title', RentalController.find_title) -// -// -// // See a list of customers that have currently checked out any of the movie's inventory - // router.get(':movie_title/customers') -// -// // // // Given a customer's id and a movie's title ... // // "check out" one of the movie's inventory to the customer From b7da68f8d1562db9cf681877173f494bb646386c Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Wed, 22 Jun 2016 12:59:23 -0700 Subject: [PATCH 19/28] finished return --- controllers/rentals.js | 29 ++++---- models/rental.js | 148 +++++++++-------------------------------- routes/index.js | 4 ++ 3 files changed, 51 insertions(+), 130 deletions(-) diff --git a/controllers/rentals.js b/controllers/rentals.js index 044d3defc..1b84b7cb0 100644 --- a/controllers/rentals.js +++ b/controllers/rentals.js @@ -32,7 +32,7 @@ var RentalsController = { // Rental.checkout([req.body.customer], [req.params.movie.toLowerCase().replace(/ /g, "").replace(/\./g, "")], function(error, customers) { Rental.checkout([2], [req.params.movie.toLowerCase().replace(/ /g, "").replace(/\./g, "")], function(error, customers) { if(error) { - var err = new Error("Error retrieving customers:\n" + error.message); + var err = new Error("Error:\n" + error.message); err.status = 500; next(err); } else { @@ -45,7 +45,7 @@ var RentalsController = { // Rental.return([req.body.customer], [req.params.movie.toLowerCase().replace(/ /g, "").replace(/\./g, "")], function(error, customers) { Rental.return([2], [req.params.movie.toLowerCase().replace(/ /g, "").replace(/\./g, "")], function(error, customers) { if(error) { - var err = new Error("Error retrieving customers:\n" + error.message); + var err = new Error("Error:\n" + error.message); err.status = 500; next(err); } else { @@ -53,18 +53,21 @@ var RentalsController = { } }); } + // See a list of customers with overdue movies (/rentals/overdue) + // include customer name, movie title, check-out date, and return date - // getRentalsShow: function(req, res, next) { - // Rental.find (req.params.title, function(error, rental) { - // if (error) { - // var err = new Error("No rentals found"); - // err.status = 404; - // next(err); - // } else { - // res.json(rental)l - // } - // }); - // } +// return: function(req, res, next) { +// // Rental.return([req.body.customer], [req.params.movie.toLowerCase().replace(/ /g, "").replace(/\./g, "")], function(error, customers) { +// Rental.return([2], [req.params.movie.toLowerCase().replace(/ /g, "").replace(/\./g, "")], function(error, customers) { +// if(error) { +// var err = new Error("Error:\n" + error.message); +// err.status = 500; +// next(err); +// } else { +// res.json(customers) +// } +// }); +// } }; module.exports = RentalsController; diff --git a/models/rental.js b/models/rental.js index 41ab2496c..14137d084 100644 --- a/models/rental.js +++ b/models/rental.js @@ -104,130 +104,44 @@ Rental.return = function (customer_id, movie_title, callback) { callback(new Error("Could not retrieve movie based on given movie title"), undefined) } else { - // check to see if customer exists - db.customers.where("id=$1", customer_id, function (error, customer) { - if (error || !customer) { - callback(new Error("Could not retrieve customer from database"), undefined) - } else { + // check to see if customer exists + db.customers.where("id=$1", customer_id, function (error, customer) { + if (error || !customer) { + callback(new Error("Could not retrieve customer from database"), undefined) + } else { - // does this person even have a rental?? - // if multiple rentals.. assume customer wants to return one with closest due date -> LIMIT 1 will limit to the first found id. - // UPDATE rentals SET checked='false', return_date='Tue Jun 21 2016 11:24:33 GMT-0700 (PDT)' FROM rentals WHERE customer_id=2 AND movie_id=2 AND checked='true' ORDER BY id LIMIT 1; + // does this person even have a rental?? + // if multiple rentals.. assume customer wants to return one with closest due date -> LIMIT 1 will limit to the first found id. + var arrCheck = [] + arrCheck.push(customer_id[0], movie[0].id, 'true') + db.run("SELECT * FROM rentals WHERE customer_id=$1 AND movie_id=$2 AND checked=$3 LIMIT 1", arrCheck, function (error, rental) { + if (error || !rental || rental.length===0) { + callback(new Error("Customer currently does not have this movie checked out"), undefined) + } else { + // Update rentals checked in and return date + // This should always work, thanks to the movie, customer and rental checks above. + var insertArray = []; + var returndate = (new Date()).toString(); + insertArray.push('false', returndate, rental[0]["id"]) + db.run("UPDATE rentals SET checked=$1, return_date=$2 WHERE id=$3;", insertArray, function (error, rentalupdate) { + if (error || !rentalupdate) { + callback(new Error("Could not update rental info"), undefined) + } else { - var arrCheck = [] - arrCheck.push(customer_id[0], movie[0].id, 'true') - db.run("SELECT * FROM rentals WHERE customer_id=$1 AND movie_id=$2 AND checked=$3 LIMIT 1", arrCheck, function (error, rental) { - if (error || !rental || rental.length===0) { - callback(new Error("This customer currently does not have this movie checked out"), undefined) + // update movie inventory + console.log(movie_title) + db.run("UPDATE movies SET inventory=inventory+1 WHERE search_title=$1;", movie_title, function (error, updatesmovie) { + if (error || !updatesmovie) { + callback(new Error("Could not update movie inventory"), undefined) } else { - - // Update rentals checked in and return date - // This should always work, thanks to the movie, customer and rental checks above. - var insertArray = []; - var returndate = (new Date()).toString(); - insertArray.push('false', returndate, rental[0]["id"]) - // insertArray.push('false', returndate, customer_id[0], movie[0].id) - db.run("UPDATE rentals SET checked=$1, return_date=$2 WHERE id=$3;", insertArray, function (error, rentalupdate) { - if (error || !rentalupdate) { - callback(new Error("Could not update rental info"), undefined) - } else { - - // update movie inventory - console.log(movie_title) - db.run("UPDATE movies SET inventory=inventory+1 WHERE search_title=$1;", movie_title, function (error, updatesmovie) { - if (error || !updatesmovie) { - callback(new Error("Could not update movie inventory"), undefined) - } else { - callback(undefined, {return: "Movie has been successfully returned"}) - }}) - }}) + callback(undefined, {return: "Movie has been successfully returned"}) }}) }}) }}) - } + }}) + }}) +} - // }) - // }}) - // } - // var insertArray = []; - // var returndate = (new Date(Date.now(new Date())+604800000)).toString(); - // insertArray.push(movieid[0]["id"], customer_id[0], 'true', (new Date()).toString(), returndate) - - // console.log("movie id: ", movieid) -// -// -// // console.log(updates) -// // if no errors, update movie inventory -// db.run("UPDATE movies SET inventory=inventory-1 WHERE search_title=$1;", movie, function (error, updatesmovie) { -// if (error || !updatesmovie) { -// callback(new Error("Could not update movie inventory"), undefined) -// } else { -// -// // if no errors, update RENTALSSSS -// var insertArray = []; -// var returndate = (new Date(Date.now(new Date())+604800000)).toString(); -// insertArray.push(movieid[0]["id"], customer_id[0], 'true', (new Date()).toString(), returndate) -// -// db.run("INSERT INTO rentals (movie_id, customer_id, checked, rental_date, due_date) VALUES ($1, $2, $3, $4, $5);", insertArray, function (error, rentals) { -// if (error || !rentals) { -// callback(new Error("Could not update rental info"), undefined) -// } else { -// callback(null, {return_date: returndate}); -// } -// }) -// }}); -// }}); -// }}); -// }}); -// }; - - - - - - - - // callback(null, inventories) - - // return (inventories) - // }}) - // }); - - // }}); - - // db.run("INSERT INTO rentals (movie_id, customer_id, checked, rental_date, due_date) VALUES (5, 1, 'true', '2016-07-19', '2016-07-31');", customer_id, function (error, inventory) { - // if (error || !inventory) { - // callback(new Error("Could not retrieve inventory"), undefined) - // } else { - // callback(null, inventory.map (function (inventories) { - // return (inventories) - // })); - // } - // }); - // - // db.run("UPDATE customers SET account_credit=account_credit-1.5 WHERE id=$1;", customer_id, function (error, inventory) { - // if (error || !inventory) { - // callback(new Error("Could not retrieve inventory"), undefined) - // } else { - // callback(null, inventory.map (function (inventories) { - // return (inventories) - // })); - // } - // }); - -// } -// -// Rental.find = function (title, callback) { -// db.movies.search({columns:["title"], term: title}, function (error, movies) { -// if (error || !movies) { -// callback(new Error("Could not retrieve movies"), undefined) -// } else { -// callback(null, movies.map (function (movie) { -// return new Movie(movie) -// })) -// } -// }) -// } module.exports = Rental; diff --git a/routes/index.js b/routes/index.js index 0f38e71a7..63437834a 100644 --- a/routes/index.js +++ b/routes/index.js @@ -80,5 +80,9 @@ router.get('/rentals/:movie/check-out', RentalController.checkOut); // return the movie to its inventory router.get('/rentals/:movie/return', RentalController.return); +// See a list of customers with overdue movies (/rentals/overdue) +// include customer name, movie title, check-out date, and return date +// router.get('/rentals/overdue', RentalController.overdue); + module.exports = router; From a9df2f6e49647f21647447127e9e9b6150a9db48 Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Wed, 22 Jun 2016 13:13:18 -0700 Subject: [PATCH 20/28] sending over rental model changes --- models/rental.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/rental.js b/models/rental.js index c6efd331d..5ba791014 100644 --- a/models/rental.js +++ b/models/rental.js @@ -19,7 +19,7 @@ Rental.search = function (input, callback) { } }); } - +// fsdoiahfisohfios // /rentals/:movie/customers // returns list of customers (assume its full info) From c0ec8296f06038a59ae12ca65b0f60de8625b545 Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Wed, 22 Jun 2016 14:27:12 -0700 Subject: [PATCH 21/28] tried to finish overdue. turns out it was a problem with the order of our routes for rentals. we need to figure out how to get movie and customer info from the rentals row info in the model. --- controllers/rentals.js | 13 +++++++ models/rental.js | 82 +++++++++++------------------------------- routes/index.js | 8 +++-- 3 files changed, 39 insertions(+), 64 deletions(-) diff --git a/controllers/rentals.js b/controllers/rentals.js index a74dcd1ed..3e0895712 100644 --- a/controllers/rentals.js +++ b/controllers/rentals.js @@ -75,6 +75,19 @@ var RentalsController = { res.json(customers) } }); + }, + + overdue: function(req, res, next) { + // Rental.return([req.body.customer], [req.params.movie.toLowerCase().replace(/ /g, "").replace(/\./g, "")], function(error, customers) { + Rental.overdue(['true', (new Date()).toString()], function(error, overdueInfo) { + if(error) { + var err = new Error("Error:\n" + error.message); + err.status = 500; + next(err); + } else { + res.json(overdueInfo) + } + }); } // See a list of customers with overdue movies (/rentals/overdue) // include customer name, movie title, check-out date, and return date diff --git a/models/rental.js b/models/rental.js index 5ba791014..64d02c074 100644 --- a/models/rental.js +++ b/models/rental.js @@ -19,7 +19,6 @@ Rental.search = function (input, callback) { } }); } -// fsdoiahfisohfios // /rentals/:movie/customers // returns list of customers (assume its full info) @@ -36,65 +35,6 @@ Rental.searchCust = function (input, callback) { }) } -// Rental.find = function (title, callback) { -// db.movies.find({title: title}, function (error, movies) { -// if (error || !movies) { -// callback(new Error("Could not retrieve movies"), undefined) -// } else { -// //look up all rentals for that movie -// db.rentals.find({movie_id: movies[0].id}, function (error, rentals) { -// if (error || !rentals) { -// callback(new Error("Could not retrieve rentals"), undefined) -// } else { -// var out = 0 -// -// for (movie of rentals) { -// if (movie.checked === "true") { -// out += 1 -// } -// } -// movies[0]["available_inventory"] = (movies[0].inventory - out) -// -// callback(null, movies[0]) -// } -// }) - -// Rental.customers = function (title, callback) { -// db.movies.findOne({title: title}, function (error, movies) { -// if (error || !movies) { -// callback(new Error("Could not retrieve movies"), undefined) -// } else { -// db.rentals.find({movie_id: movies.id}, function (error, rentalRecords) { -// if(error || !rentalRecords) { -// callback(new Error("Could not retrieve movies"), undefined) -// } else { -// console.log(rentalRecords) -// //loop for each of the customer_ids then pass that whole array into the db call -// var custIds = [] -// for (var cust of rentalRecords) { -// custIds.push(cust.customer_id) -// } -// console.log(custIds) -// db.customers.find({id: custIds}, function (error, customersNames) { -// console.log(customersNames) -// if(error || !customersNames) { -// callback(new Error("Could not retrieve customers"), undefined) -// } else { -// var customerNames = [] -// for (var record of customersNames) { -// console.log(record.name) -// customerNames.push(record.name) -// } -// callback(null, customerNames) -// } -// }) -// } -// }) -// } -// }); -// } - - Rental.checkout = function (customer_id, movie, callback) { // check to see if movie exists @@ -200,7 +140,27 @@ Rental.return = function (customer_id, movie_title, callback) { }}) }}) }}) -} +}; + +Rental.overdue = function (input, callback) { + db.run("SELECT * FROM rentals WHERE checked=$1 AND due_date < $2", input, function (error, overdue) { + console.log(input) + if (error || !overdue || overdue.length ===0) { + callback(new Error("Could not overdue rentals"), undefined) + }else { + // db.run("UPDATE movies SET inventory=inventory+1 WHERE search_title=$1;", movie_title, function (error, updatesmovie) { + // if (error || !updatesmovie) { + // callback(new Error("Could not update movie inventory"), undefined) + // } else { + callback(undefined, overdue.map(function(info) { + return info; + console.log(info); + }));) + + } + }) +}; + module.exports = Rental; diff --git a/routes/index.js b/routes/index.js index 63437834a..f71c2b44a 100644 --- a/routes/index.js +++ b/routes/index.js @@ -59,6 +59,10 @@ router.get('/movies', MovieController.index); // ordered by customer name or ordered by check out date router.get('/movies/:movie/history/sort/:query', MovieController.history) +// See a list of customers with overdue movies (/rentals/overdue) +// include customer name, movie title, check-out date, and return date +router.get('/rentals/overdue', RentalController.overdue); + // Look a movie up by title to see (/rentals/Jaws) // it's synopsis // release date @@ -80,9 +84,7 @@ router.get('/rentals/:movie/check-out', RentalController.checkOut); // return the movie to its inventory router.get('/rentals/:movie/return', RentalController.return); -// See a list of customers with overdue movies (/rentals/overdue) -// include customer name, movie title, check-out date, and return date -// router.get('/rentals/overdue', RentalController.overdue); + module.exports = router; From 0fdb38a05c4e28c9aa05996b93eebf8ea6d85260 Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Thu, 23 Jun 2016 11:28:50 -0700 Subject: [PATCH 22/28] can find overdue rentals. had to split it up to be able to do time logic with javascript. --- controllers/rentals.js | 15 +------------- models/rental.js | 47 +++++++++++++++++++++++++++++++----------- package.json | 2 +- 3 files changed, 37 insertions(+), 27 deletions(-) diff --git a/controllers/rentals.js b/controllers/rentals.js index 3e0895712..bc31c1b37 100644 --- a/controllers/rentals.js +++ b/controllers/rentals.js @@ -78,8 +78,7 @@ var RentalsController = { }, overdue: function(req, res, next) { - // Rental.return([req.body.customer], [req.params.movie.toLowerCase().replace(/ /g, "").replace(/\./g, "")], function(error, customers) { - Rental.overdue(['true', (new Date()).toString()], function(error, overdueInfo) { + Rental.overdue(Date.now(), function(error, overdueInfo) { if(error) { var err = new Error("Error:\n" + error.message); err.status = 500; @@ -92,18 +91,6 @@ var RentalsController = { // See a list of customers with overdue movies (/rentals/overdue) // include customer name, movie title, check-out date, and return date -// return: function(req, res, next) { -// // Rental.return([req.body.customer], [req.params.movie.toLowerCase().replace(/ /g, "").replace(/\./g, "")], function(error, customers) { -// Rental.return([2], [req.params.movie.toLowerCase().replace(/ /g, "").replace(/\./g, "")], function(error, customers) { -// if(error) { -// var err = new Error("Error:\n" + error.message); -// err.status = 500; -// next(err); -// } else { -// res.json(customers) -// } -// }); -// } }; diff --git a/models/rental.js b/models/rental.js index 64d02c074..f9f162d7d 100644 --- a/models/rental.js +++ b/models/rental.js @@ -142,22 +142,45 @@ Rental.return = function (customer_id, movie_title, callback) { }}) }; -Rental.overdue = function (input, callback) { - db.run("SELECT * FROM rentals WHERE checked=$1 AND due_date < $2", input, function (error, overdue) { - console.log(input) +Rental.overdue = function (date, callback) { + // find all rentals that are checked out + db.run("SELECT * FROM rentals WHERE checked='true'", function (error, overdue) { + console.log(date) + console.log(overdue) if (error || !overdue || overdue.length ===0) { callback(new Error("Could not overdue rentals"), undefined) }else { - // db.run("UPDATE movies SET inventory=inventory+1 WHERE search_title=$1;", movie_title, function (error, updatesmovie) { - // if (error || !updatesmovie) { - // callback(new Error("Could not update movie inventory"), undefined) - // } else { - callback(undefined, overdue.map(function(info) { - return info; - console.log(info); - }));) - + var arrayOfRentalIds = [] + overdue.map(function(info) { + if (date > (new Date(info.due_date).getTime()) ) { + // db.run + arrayOfRentalIds.push(info.id) + // console.log("MEOW ", info.id) + } + }) + callback(undefined,arrayOfRentalIds) } + + + // db.run("SELECT * FROM rentals WHERE checked='true'", function (error, overdue) { + // + // + // callback(undefined, overdue.map(function(info) { + // return info; + // db.customers.find("id=$1", [info.customer_id], function (error, customer) { + // console.log(customer) + // if (error || !customer || overdue.length ===0) { + // // callback(new Error("Could not find customer"), undefined) + // }else { + // console.log("MEOW") + // // callback(undefined, customer) + // }} + // // console.log(info.movie_id); + // // return info; + // ) + // })) + + // } }) }; diff --git a/package.json b/package.json index 6a5467a3d..f1c3c188a 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "setup": "node ./setup.js", "start": "./node_modules/.bin/nodemon ./bin/www", "start-test": "NODE_ENV=test ./node_modules/.bin/nodemon ./bin/www", - "test": "clear; ./node_modules/.bin/istanbul cover --include-all-sources ./node_modules/.bin/jasmine-node --verbose spec/", + "test": "clear; ./node_modules/.bin/istanbul cover -x 'spec/**/*' -- ./node_modules/.bin/jasmine-node --captureExceptions --verbose spec/", "db:drop": "dropdb radio_star_development", "db:create": "createdb radio_star_development", "db:dropt": "dropdb radio_star_test", From c61f37c9616e4a8e5e059ed3ab66d4c250801ecc Mon Sep 17 00:00:00 2001 From: Mindy Carson Date: Thu, 23 Jun 2016 23:06:54 -0700 Subject: [PATCH 23/28] added html view of api documentation --- views/documentation.html | 195 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 195 insertions(+) create mode 100644 views/documentation.html diff --git a/views/documentation.html b/views/documentation.html new file mode 100644 index 000000000..8cf692432 --- /dev/null +++ b/views/documentation.html @@ -0,0 +1,195 @@ + + + + + + Radio Star Video Store API Documentation + + + + + + + +

Customers

+ +
    + +
  • Retrive a list of all customers: (GET '/customers/')
  • +
      Data returns: +
    • Found:
    • +
    • Not found:
    • +
    • Error:
    • +
    + + +
  • Retrive a subset of customers: (GET '/customers/sort/:query')
  • +
      +
    • Given a sort column, return n customer records, offset by p records (this will be used to create “pages” of customers)
    • +
    • Sort params are
    • +
        +
      • Name example: ('customers/sort/name?n=10&p=2')
      • +
      • Registered_at example: ('customers/sort/registered-at?n=10&p=2')
      • +
      • Postal_code example: ('customers/sort'/postal-code?n=10&p=2')
      • +
      +
    +
      Data returns: +
    • Found:
    • +
    • Not found:
    • +
    • Error:
    • +
    + + +
  • Given a customer’s id
  • +
      +
    • List the movies they currently have checked out (GET '/customers/:id/current')
    • +
        Data returns: +
      • Found:
      • +
      • Not found:
      • +
      • Error:
      • +
      + +
    • List the movies a customer has checked out in the past (GET '/customers/:id/history')
    • +
        +
      • ordered by check out date
      • +
      • includes return date
      • +
      +
        Data returns: +
      • Found:
      • +
      • Not found:
      • +
      • Error:
      • +
      +
    + +
+ + + +

Movies

+ +
    + +
  • Retrieve a list of all movies (GET '/movies')
  • +
      Data returns: +
    • Found:
    • +
    • Not found:
    • +
    • Error:
    • +
    + +
  • Retrieve a subset of movies (GET '/movies/sort/:query')
  • +
      +
    • Given a sort column, return n movie records, offset by p records (this will be used to create “pages” of movies)
    • +
    • Sort params are
    • +
        +
      • Title example: ('movies/sort/title?n=5&p=1')
      • +
      • Release_date example: ('movies/sort/release-date?n=5&p=1')
      • +
      +
        Data returns: +
      • Found:
      • +
      • Not found:
      • +
      • Error:
      • +
      +
    + +
  • Given a movie’s title…
  • +
      +
    • Get a list of customers that have currently checked out a copy of the film (GET '/movies/:movie/current')
    • +
        +
      • includes each customer’s name, phone number, and account credit
      • + +
      +
        Data returns: +
      • Found:
      • +
      • Not found:
      • +
      • Error:
      • +
      + +
    • Get a list of customers that have checked out a copy in the past (GET '/movies/:movie/history/sort/:query')
    • +
        +
      • includes each customer’s name, phone number, and account credit
      • +
      • ordered by customer name: ('/movies/:movie_id/history/sort/name')
      • +
      • ordered by check-out: ('/movies/:movie_id/history/sort/date')
      • +
      +
        Data returns: +
      • Found:
      • +
      • Not found:
      • +
      • Error:
      • +
      +
    + +
+ + +

Rental

+ +
    + +
  • Look a movie up by title (GET '/rentals/:title)

    +
      Returned info: +
    • movie’s synopsis
    • +
    • release date
    • +
    • available inventory (not currently checked-out to a customer)
    • +
    • and inventory total
    • +
    +
      Data returns: +
    • Found:
    • +
    • Not found:
    • +
    • Error:
    • +
    +
  • + +
  • See a list of customers that have currently checked out any of the movie’s inventory (GET '/rentals/:movie/customers')
  • + +
  • Given a customer’s id and a movie’s title …
  • +
      + + +
    • “check out” one of the movie’s inventory to the customer (GET '/rentals/:movie/check-out')
    • +
        +
      • Establish a return date
      • +
      • Charge the customer’s account (cost up to you)
      • +
      +
        Data returns: +
      • Found:
      • +
      • Not found:
      • +
      • Error:
      • +
      + + + + +
    • “check in” one of customer’s rentals (GET '/rentals/:movie/return')
    • +
        +
      • return the movie to its inventory
      • +
      +
        Data returns: +
      • Found:
      • +
      • Not found:
      • +
      • Error:
      • +
      + +
    + + +
  • See a list of customers with overdue movies (GET '/rentals/overdue')
  • +
      +
    • include customer name, movie title, check-out date, and return date
    • +
    +
      Data returns: +
    • Found:
    • +
    • Not found:
    • +
    • Error:
    • +
    + + + + From 3fc3ce8fc33034da77595a107899e42c89e554c3 Mon Sep 17 00:00:00 2001 From: Mindy Carson Date: Fri, 24 Jun 2016 09:19:14 -0700 Subject: [PATCH 24/28] added instructions for customer_id sent through JSON body --- views/documentation.html | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/views/documentation.html b/views/documentation.html index 8cf692432..f88d78d24 100644 --- a/views/documentation.html +++ b/views/documentation.html @@ -9,6 +9,7 @@ no data found error occurred --> + @@ -151,10 +152,11 @@

    Rental

  • Given a customer’s id and a movie’s title …
    • - +
    • “check out” one of the movie’s inventory to the customer (GET '/rentals/:movie/check-out')
      • +
      • Customer info given through JSON body
      • Establish a return date
      • Charge the customer’s account (cost up to you)
      @@ -165,10 +167,11 @@

      Rental

    - +
  • “check in” one of customer’s rentals (GET '/rentals/:movie/return')
    • +
    • Customer info given through JSON body
    • return the movie to its inventory
      Data returns: @@ -191,5 +194,13 @@

      Rental

    + + +

    Informational endpoints

    +
      +
    • (/api/docs) (Serves a HTML view of documentation)
    • +
    • (/api/docs.json) (Serves JSON documentation)
    • +
    + From 4944f856bf6ca6d6c1d686f43342a5bad08ceb23 Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Fri, 24 Jun 2016 11:09:58 -0700 Subject: [PATCH 25/28] finished overdue functionality for rentals --- .DS_Store | Bin 0 -> 6148 bytes controllers/rentals.js | 4 +-- models/rental.js | 65 +++++++++++++++++++++-------------------- package.json | 2 +- 4 files changed, 36 insertions(+), 35 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..0a76604366bf91f2e00c1a17a314fcc176cfa122 GIT binary patch literal 6148 zcmeHKI|>3Z5S{S@f{mqRuHX%V=n1@lg(89|C~CWv=kjR2`83O7r-kwcCNG)HOUNsB zc0@$y*Ue01A|fNWp*(Epn(do+td|i5!g0pswmKgVr{k`Z`z~PIq16>#E}G4U=FOTNiu&z1zj(T64dh4#s6bnRek>PeATY>xe! V*aSKqai;_MGhn*VsKB=scmcl46z2c{ literal 0 HcmV?d00001 diff --git a/controllers/rentals.js b/controllers/rentals.js index bc31c1b37..0efd894ae 100644 --- a/controllers/rentals.js +++ b/controllers/rentals.js @@ -77,6 +77,8 @@ var RentalsController = { }); }, + // See a list of customers with overdue movies (/rentals/overdue) + // include customer name, movie title, check-out date, and return date overdue: function(req, res, next) { Rental.overdue(Date.now(), function(error, overdueInfo) { if(error) { @@ -88,8 +90,6 @@ var RentalsController = { } }); } - // See a list of customers with overdue movies (/rentals/overdue) - // include customer name, movie title, check-out date, and return date }; diff --git a/models/rental.js b/models/rental.js index f9f162d7d..2f3b2c6c8 100644 --- a/models/rental.js +++ b/models/rental.js @@ -142,48 +142,49 @@ Rental.return = function (customer_id, movie_title, callback) { }}) }; + Rental.overdue = function (date, callback) { // find all rentals that are checked out + var totaloutput = [] db.run("SELECT * FROM rentals WHERE checked='true'", function (error, overdue) { - console.log(date) - console.log(overdue) if (error || !overdue || overdue.length ===0) { - callback(new Error("Could not overdue rentals"), undefined) + callback(new Error("Could not find overdue rentals"), undefined) }else { - var arrayOfRentalIds = [] + var arrayOfOverdue = [] overdue.map(function(info) { if (date > (new Date(info.due_date).getTime()) ) { - // db.run - arrayOfRentalIds.push(info.id) - // console.log("MEOW ", info.id) - } - }) - callback(undefined,arrayOfRentalIds) + arrayOfOverdue.push(info) + }}); + for (var rental of arrayOfOverdue) { + console.log(rental) + db.movies.findOne({id: rental.movie_id}, function(rental, error, movie) { + console.log("movie?: ", movie) + if (error || !movie) { + callback(new Error("Could not find movie"), undefined) + } else { + db.customers.findOne({id: rental.customer_id}, function(error, customer) { + console.log("customer?: ", customer) + if (error || !customer) { + callback(new Error("Could not find customer"), undefined) + } else { + var output = { + customer: customer.name, + movie: movie.title, + checkout_date: rental.rental_date, + due_date: rental.due_date + } + totaloutput.push(output) + if (arrayOfOverdue.length === totaloutput.length) { + callback(null, totaloutput); + } + } + }); + } + }.bind(db.movies, rental) + )} } - - - // db.run("SELECT * FROM rentals WHERE checked='true'", function (error, overdue) { - // - // - // callback(undefined, overdue.map(function(info) { - // return info; - // db.customers.find("id=$1", [info.customer_id], function (error, customer) { - // console.log(customer) - // if (error || !customer || overdue.length ===0) { - // // callback(new Error("Could not find customer"), undefined) - // }else { - // console.log("MEOW") - // // callback(undefined, customer) - // }} - // // console.log(info.movie_id); - // // return info; - // ) - // })) - - // } }) }; - module.exports = Rental; diff --git a/package.json b/package.json index f1c3c188a..6acf93282 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "setup": "node ./setup.js", "start": "./node_modules/.bin/nodemon ./bin/www", "start-test": "NODE_ENV=test ./node_modules/.bin/nodemon ./bin/www", - "test": "clear; ./node_modules/.bin/istanbul cover -x 'spec/**/*' -- ./node_modules/.bin/jasmine-node --captureExceptions --verbose spec/", + "test": "tclear; ./node_modules/.bin/istanbul cover -x 'spec/**/*' -- ./node_modules/.bin/jasmine-node --captureExceptions --verbose spec/", "db:drop": "dropdb radio_star_development", "db:create": "createdb radio_star_development", "db:dropt": "dropdb radio_star_test", From 300cb04a29f8bcd6eda93c28c7c7aa125b700fec Mon Sep 17 00:00:00 2001 From: Mindy Carson Date: Fri, 24 Jun 2016 15:14:06 -0700 Subject: [PATCH 26/28] finished json api documentation --- controllers/index.js | 39 +++++-- controllers/movies.js | 2 +- controllers/rentals.js | 6 +- docs.json | 238 +++++++++++++++++++++++++++++++++++++++ routes/index.js | 11 +- routes/rentals.js | 10 +- views/documentation.html | 206 --------------------------------- views/error.ejs | 9 +- views/error.jade | 6 - views/error2.ejs | 3 + views/html-doc.ejs | 214 +++++++++++++++++++++++++++++++++++ views/index.ejs | 16 +-- views/index.jade | 5 - views/index2.ejs | 11 ++ 14 files changed, 524 insertions(+), 252 deletions(-) create mode 100644 docs.json delete mode 100644 views/documentation.html delete mode 100644 views/error.jade create mode 100644 views/error2.ejs create mode 100644 views/html-doc.ejs delete mode 100644 views/index.jade create mode 100644 views/index2.ejs diff --git a/controllers/index.js b/controllers/index.js index 1e9f632e2..d694cbad9 100644 --- a/controllers/index.js +++ b/controllers/index.js @@ -1,12 +1,37 @@ -var HomePage = { - zomg: function(req,response) { - var hi= {"hi": "ZOMG"} - response.json({cow: 'ZOMG!'}) +var docs = require('../docs.json'); + + +var Controller = { + locals: { + documentation: docs + }, + + index: function(req, res, next) { + res.json('It works!!'); + }, + + docsHTML: function(req, res, next) { + res.render('docs', Controller.locals); }, - nothing: function(req, response) { - response.render('index', {title: 'Express'}); + docsJSON: function(req, res, next) { + res.json(200, docs); } }; -module.exports = HomePage; +module.exports = Controller; + + + +// var HomePage = { +// zomg: function(req,response) { +// var hi= {"hi": "ZOMG"} +// response.json({cow: 'ZOMG!'}) +// }, +// +// nothing: function(req, response) { +// response.render('index', {title: 'Express'}); +// } +// }; +// +// module.exports = HomePage; diff --git a/controllers/movies.js b/controllers/movies.js index a1d144a05..84a71d63c 100644 --- a/controllers/movies.js +++ b/controllers/movies.js @@ -16,7 +16,7 @@ var MovieController = { find: function(req, res, next) { Movie.sort(req.params.query, req.query.n , req.query.p, function(error, movies) { if(error) { - var err = new Error("Error retrieving customer list:\n" + error.message); + var err = new Error("Error retrieving movies list:\n" + error.message); err.status = 500; next(err); } else { diff --git a/controllers/rentals.js b/controllers/rentals.js index bc31c1b37..7a4c79f8b 100644 --- a/controllers/rentals.js +++ b/controllers/rentals.js @@ -33,7 +33,7 @@ var RentalsController = { // Rental.checkout([req.body.customer], [req.params.movie.toLowerCase().replace(/ /g, "").replace(/\./g, "")], function(error, customers) { Rental.checkout([2], [req.params.movie.toLowerCase().replace(/ /g, "").replace(/\./g, "")], function(error, customers) { if(error) { - var err = new Error("Error:\n" + error.message); + var err = new Error("Error, could not check out movie at this time:\n" + error.message); err.status = 500; next(err); } else { @@ -68,7 +68,7 @@ var RentalsController = { // Rental.return([req.body.customer], [req.params.movie.toLowerCase().replace(/ /g, "").replace(/\./g, "")], function(error, customers) { Rental.return([2], [req.params.movie.toLowerCase().replace(/ /g, "").replace(/\./g, "")], function(error, customers) { if(error) { - var err = new Error("Error:\n" + error.message); + var err = new Error("Error, could not return movie at this time:\n" + error.message); err.status = 500; next(err); } else { @@ -80,7 +80,7 @@ var RentalsController = { overdue: function(req, res, next) { Rental.overdue(Date.now(), function(error, overdueInfo) { if(error) { - var err = new Error("Error:\n" + error.message); + var err = new Error("Error could not retrieve customers:\n" + error.message); err.status = 500; next(err); } else { diff --git a/docs.json b/docs.json new file mode 100644 index 000000000..47a069add --- /dev/null +++ b/docs.json @@ -0,0 +1,238 @@ +{ + "base_url": "http://localhost:3000", + "data_format": "JSON", + + "endpoints": [ + { + "verb": "GET", + "uri": "/customers", + "description": "Returns a list of all customers that exist in the database.", + "required_parameters": [], + "optional_parameters": [], + "return_data": { + "found": "Search results are JSON documents containing an array of customer result hashes", + "no_data": "Error retrieving customer list, status code 500", + "error": "Error retrieving customer list, status code 500" + } + }, + + { + "verb": "GET", + "uri": "/customers/sort/:query", + "description": "Returns a list of customers in the database, sorted by a given parameter. By using an optional query param, the amount displayed can be limited.", + "required_parameters (select one)": [ + { + "parameter": "name", + "description": "customers returned will be sorted in alphabetical order by name" + }, + { + "parameter": "registered_at", + "description": "customers returned will be sorted in ascending order by when they registered" + }, + { + "parameter": "postal_code", + "description": "customers returned will be sorted in ascending order by postal code" + } + ], + "optional_parameters": [ + { + "parameter": "n", + "description": "a query parameter that determines the number of customer records to return" + }, + { + "parameter": "p", + "description": "a query parameter that determines the offset of customer records, to create pages of customers" + } + ], + "return_data": { + "found": "Search results are JSON documents containing an array of customer result hashes", + "no_data": "Error retrieving customer list, status code 500", + "error": "Error retrieving customer list, status code 500" + } + }, + + { + "verb": "GET", + "uri": "/customers/:id/current", + "description": "Returns a list of movies that a customer currently has checked out.", + "required_parameters": [], + "optional_parameters": [], + "return_data": { + "found": "Search results are JSON documents containing an array of movie result hashes", + "no_data": "Error retrieving customer's current movie list, status code 500", + "error": "Error retrieving customer's current movie list, status code 500" + } + }, + + { + "verb": "GET", + "uri": "/customers/:id/history", + "description": "Returns a list of movies that a customer has checked out in the past, ordered by checkout date.", + "required_parameters": [], + "optional_parameters": [], + "return_data": { + "found": "Search results are JSON documents containing an array of movie result hashes", + "no_data": "Error retrieving rental history list, status code 500", + "error": "Error retrieving rental history list, status code 500" + } + }, + + + { + "verb": "GET", + "uri": "/movies", + "description": "Returns a list of all movies that exist in the database.", + "required_parameters": [], + "optional_parameters": [], + "return_data": { + "found": "Search results are JSON documents containing an array of movie result hashes", + "no_data": "Error retrieving movies list, status code 500", + "error": "Error retrieving movies list, status code 500" + } + }, + + { + "verb": "GET", + "uri": "/movies/sort/:query", + "description": "Returns a list of movies in the database, sorted by a given parameter. By using an optional query param, the amount displayed can be limited.", + "required_parameters (select one)": [ + { + "parameter": "title", + "description": "movies returned will be sorted in alphabetical order by movie title" + }, + { + "parameter": "release_date", + "description": "movies returned will be sorted in ascending order by release_date" + } + ], + "optional_parameters": [ + { + "parameter": "n", + "description": "a query parameter that determines the number of movie records to return" + }, + { + "parameter": "p", + "description": "a query parameter that determines the offset of movie records, to create pages of movies" + } + ], + "return_data": { + "found": "Search results are JSON documents containing an array of movie result hashes", + "no_data": "Error retrieving movies list, status code 500", + "error": "Error retrieving movies list, status code 500" + } + }, + + { + "verb": "GET", + "uri": "/movies/:movie/current", + "description": "Returns a list of customers that have currently checked out a copy of the movie.", + "required_parameters": [], + "optional_parameters": [], + "return_data": { + "found": "Search results are JSON documents containing an array of customer result hashes", + "no_data": "Error retrieving customer list, status code 500", + "error": "Error retrieving customer list, status code 500" + } + }, + + { + "verb": "GET", + "uri": "/movies/:movie/history/sort/:query", + "description": "Returns a list of customers that have checked out a film in the past", + "required_parameters (select one)": [ + { + "parameter": "name", + "description": "customers returned will be sorted in alphabetical order by customer name" + }, + { + "parameter": "checkout date", + "description": "customers returned will be sorted in ascending order by the checkout date of their rental" + } + ], + "optional_parameters": [], + "return_data": { + "found": "Search results are JSON documents containing an array of customer result hashes", + "no_data": "Error retrieving customer list, status code 500", + "error": "Error retrieving customer list, status code 500" + } + }, + + { + "verb": "GET", + "uri": "/rentals/:title", + "description": "Returns movie info including synopsis, release date, available inventory (not currently checked-out to a customer), and inventory total", + "required_parameters": [], + "optional_parameters": [], + "return_data": { + "found": "Search results are JSON documents containing an array of movie info result hash", + "no_data": "Error retrieving movie info, status code 500", + "error": "Error retrieving movie info, status code 500" + } + }, + + { + "verb": "GET", + "uri": "/rentals/:movie/customers", + "description": "See a list of customers that have currently checked out any of the movie’s inventory", + "required_parameters": [], + "optional_parameters": [], + "return_data": { + "found": "Search results are JSON documents containing an array of customer result hash", + "no_data": "Error retrieving customers, status code 500", + "error": "Error retrieving customers, status code 500" + } + }, + + { + "verb": "GET", + "uri": "/rentals/:movie/check-out", + "description": "Check out one of the movie’s inventory to the customer, establishes a return date, and charges customer account $1.50", + "required_parameters": [], + "optional_parameters": [], + "return_data": { + "found": "JSON document that has rental due date", + "no_data": "Error, could not check out movie at this time, status code 500", + "error": "Error, could not check out movie at this time, status code 500" + } + }, + + { + "verb": "GET", + "uri": "/rentals/:movie/return", + "description": "Check in one of customer’s rentals", + "required_parameters": [], + "optional_parameters": [], + "return_data": { + "found": "JSON document confirms movie returned successfully", + "no_data": "Error, could not return movie at this time, status code 500", + "error": "Error, could not return movie at this time, status code 500" + } + }, + + { + "verb": "GET", + "uri": "/rentals/overdue", + "description": "See a list of customers that have currently checked out any of the movie’s inventory", + "required_parameters": [], + "optional_parameters": [], + "return_data": { + "found": "Search results are JSON documents containing an array of customer result hashes", + "no_data": "Error could not retrieve customers, status code 500", + "error": "Error could not retrieve customers, status code 500" + } + }, + + { + "verb": "GET", + "uri": "/api/docs", + "description": "Serves a HTML view of documentation" + }, + + { + "verb": "GET", + "uri": "/api/docs.json", + "description": "Serves JSON documentation" + } + + ] +} diff --git a/routes/index.js b/routes/index.js index f71c2b44a..77f72da1e 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,7 +1,7 @@ var express = require('express'); var router = express.Router(); -var Controller = require('../controllers/index') +var Controller = require('../controllers/index'); var MovieController = require('../controllers/movies'); var CustController = require('../controllers/customers'); var RentalController = require('../controllers/rentals'); @@ -11,9 +11,9 @@ router.get('/', function(req, res, next) { res.render('index', { title: 'Express' }); }); -router.get('/', Controller.nothing) +// router.get('/', Controller.nothing) -router.get('/zomg', Controller.zomg) +// router.get('/zomg', Controller.zomg) // Retrive a list of all customers @@ -84,7 +84,12 @@ router.get('/rentals/:movie/check-out', RentalController.checkOut); // return the movie to its inventory router.get('/rentals/:movie/return', RentalController.return); +//HTML API Documentation +router.get('/api/docs', Controller.docsHTML); +//JSON API Documentation +router.get('/api/docs.json', Controller.docsJSON); + module.exports = router; diff --git a/routes/rentals.js b/routes/rentals.js index 092ef853e..75c77f866 100644 --- a/routes/rentals.js +++ b/routes/rentals.js @@ -8,20 +8,18 @@ // Look a movie up by title to see: it's synopsis, release date, available inventory (not currently checked-out to a customer), and inventory total - router.get('/:movie_title', RentalController.findTitle) + // router.get('/:movie_title', RentalController.findTitle) // // // // See a list of customers that have currently checked out any of the movie's inventory - router.get('/:movie_title/customers', RentalController.customersNames) - + // router.get('/:movie_title/customers', RentalController.customersNames) + // // // // Given a customer's id and a movie's title ... // // "check out" one of the movie's inventory to the customer // // Establish a return date, Charge the customer's account (cost up to you) - router.get ('/:movie_title/check-out') - // router.get (':movie_title/check-out') // @@ -30,8 +28,6 @@ // // "check in" one of customer's rentals // // return the movie to its inventory - router.get('/:movie_title/return') - // router.get(':movie_title/return') // // diff --git a/views/documentation.html b/views/documentation.html deleted file mode 100644 index f88d78d24..000000000 --- a/views/documentation.html +++ /dev/null @@ -1,206 +0,0 @@ - - - - - - - Radio Star Video Store API Documentation - - - - - - - -

    Customers

    - -
      - -
    • Retrive a list of all customers: (GET '/customers/')
    • -
        Data returns: -
      • Found:
      • -
      • Not found:
      • -
      • Error:
      • -
      - - -
    • Retrive a subset of customers: (GET '/customers/sort/:query')
    • -
        -
      • Given a sort column, return n customer records, offset by p records (this will be used to create “pages” of customers)
      • -
      • Sort params are
      • -
          -
        • Name example: ('customers/sort/name?n=10&p=2')
        • -
        • Registered_at example: ('customers/sort/registered-at?n=10&p=2')
        • -
        • Postal_code example: ('customers/sort'/postal-code?n=10&p=2')
        • -
        -
      -
        Data returns: -
      • Found:
      • -
      • Not found:
      • -
      • Error:
      • -
      - - -
    • Given a customer’s id
    • -
        -
      • List the movies they currently have checked out (GET '/customers/:id/current')
      • -
          Data returns: -
        • Found:
        • -
        • Not found:
        • -
        • Error:
        • -
        - -
      • List the movies a customer has checked out in the past (GET '/customers/:id/history')
      • -
          -
        • ordered by check out date
        • -
        • includes return date
        • -
        -
          Data returns: -
        • Found:
        • -
        • Not found:
        • -
        • Error:
        • -
        -
      - -
    - - - -

    Movies

    - -
      - -
    • Retrieve a list of all movies (GET '/movies')
    • -
        Data returns: -
      • Found:
      • -
      • Not found:
      • -
      • Error:
      • -
      - -
    • Retrieve a subset of movies (GET '/movies/sort/:query')
    • -
        -
      • Given a sort column, return n movie records, offset by p records (this will be used to create “pages” of movies)
      • -
      • Sort params are
      • -
          -
        • Title example: ('movies/sort/title?n=5&p=1')
        • -
        • Release_date example: ('movies/sort/release-date?n=5&p=1')
        • -
        -
          Data returns: -
        • Found:
        • -
        • Not found:
        • -
        • Error:
        • -
        -
      - -
    • Given a movie’s title…
    • -
        -
      • Get a list of customers that have currently checked out a copy of the film (GET '/movies/:movie/current')
      • -
          -
        • includes each customer’s name, phone number, and account credit
        • - -
        -
          Data returns: -
        • Found:
        • -
        • Not found:
        • -
        • Error:
        • -
        - -
      • Get a list of customers that have checked out a copy in the past (GET '/movies/:movie/history/sort/:query')
      • -
          -
        • includes each customer’s name, phone number, and account credit
        • -
        • ordered by customer name: ('/movies/:movie_id/history/sort/name')
        • -
        • ordered by check-out: ('/movies/:movie_id/history/sort/date')
        • -
        -
          Data returns: -
        • Found:
        • -
        • Not found:
        • -
        • Error:
        • -
        -
      - -
    - - -

    Rental

    - -
      - -
    • Look a movie up by title (GET '/rentals/:title)

      -
        Returned info: -
      • movie’s synopsis
      • -
      • release date
      • -
      • available inventory (not currently checked-out to a customer)
      • -
      • and inventory total
      • -
      -
        Data returns: -
      • Found:
      • -
      • Not found:
      • -
      • Error:
      • -
      -
    • - -
    • See a list of customers that have currently checked out any of the movie’s inventory (GET '/rentals/:movie/customers')
    • - -
    • Given a customer’s id and a movie’s title …
    • -
        - - -
      • “check out” one of the movie’s inventory to the customer (GET '/rentals/:movie/check-out')
      • -
          -
        • Customer info given through JSON body
        • -
        • Establish a return date
        • -
        • Charge the customer’s account (cost up to you)
        • -
        -
          Data returns: -
        • Found:
        • -
        • Not found:
        • -
        • Error:
        • -
        - - - - -
      • “check in” one of customer’s rentals (GET '/rentals/:movie/return')
      • -
          -
        • Customer info given through JSON body
        • -
        • return the movie to its inventory
        • -
        -
          Data returns: -
        • Found:
        • -
        • Not found:
        • -
        • Error:
        • -
        - -
      - - -
    • See a list of customers with overdue movies (GET '/rentals/overdue')
    • -
        -
      • include customer name, movie title, check-out date, and return date
      • -
      -
        Data returns: -
      • Found:
      • -
      • Not found:
      • -
      • Error:
      • -
      - - - - -

      Informational endpoints

      -
        -
      • (/api/docs) (Serves a HTML view of documentation)
      • -
      • (/api/docs.json) (Serves JSON documentation)
      • -
      - - - diff --git a/views/error.ejs b/views/error.ejs index 7cf94edf1..51ec12c6a 100644 --- a/views/error.ejs +++ b/views/error.ejs @@ -1,3 +1,6 @@ -

      <%= message %>

      -

      <%= error.status %>

      -
      <%= error.stack %>
      +extends layout + +block content + h1= message + h2= error.status + pre #{error.stack} diff --git a/views/error.jade b/views/error.jade deleted file mode 100644 index 51ec12c6a..000000000 --- a/views/error.jade +++ /dev/null @@ -1,6 +0,0 @@ -extends layout - -block content - h1= message - h2= error.status - pre #{error.stack} diff --git a/views/error2.ejs b/views/error2.ejs new file mode 100644 index 000000000..7cf94edf1 --- /dev/null +++ b/views/error2.ejs @@ -0,0 +1,3 @@ +

      <%= message %>

      +

      <%= error.status %>

      +
      <%= error.stack %>
      diff --git a/views/html-doc.ejs b/views/html-doc.ejs new file mode 100644 index 000000000..6ee26a7b7 --- /dev/null +++ b/views/html-doc.ejs @@ -0,0 +1,214 @@ + + + + + + + Radio Star Video Store API Documentation + + + + + + + +

      Customers

      + +
        + +
      • Retrives a list of all customers: (GET '/customers/')
      • +
          Return data: +
        • Found: Search results are JSON documents containing an array of customer result hashes
        • +
        • Not found: Error retrieving customer list, status code 500.
        • +
        • Error: Error retrieving customer list, status code 500.
        • +
        + + +
      • Retrives a subset of customers: (GET '/customers/sort/:query')
      • +
          +
        • Given a sort column, returns n customer records, offset by p records (this will be used to create “pages” of customers)
        • +
        • Sort params are
        • +
            +
          • Name example: ('customers/sort/name?n=10&p=2')
          • +
          • Registered_at example: ('customers/sort/registered-at?n=10&p=2')
          • +
          • Postal_code example: ('customers/sort'/postal-code?n=10&p=2')
          • +
          +
        +
          Return data: +
        • Found: Search results are JSON documents containing an array of customer result hashes
        • +
        • Not found: Error retrieving customer list, status code 500.
        • +
        • Error: Error retrieving customer list, status code 500.
        • +
        + + +
      • Given a customer’s id
      • +
          +
        • Lists the movies they currently have checked out (GET '/customers/:id/current')
        • +
            Return data: +
          • Found: Search results are JSON documents containing an array of movie result hashes
          • +
          • Not found: Error retrieving customer's current movie list, status code 500.
          • +
          • Error: Error retrieving customer's current movie list, status code 500.
          • +
          + +
        • Lists the movies a customer has checked out in the past (GET '/customers/:id/history')
        • +
            +
          • ordered by check out date
          • +
          • includes return date
          • +
          +
            Return data: +
          • Found: Search results are JSON documents containing an array of movie result hashes
          • +
          • Not found: Error retrieving rental history list, status code 500.
          • +
          • Error: Error retrieving rental history list, status code 500.
          • +
          +
        + +
      + + + +

      Movies

      + +
        + +
      • Retrieves a list of all movies (GET '/movies')
      • +
          Return data: +
        • Found: Search results are JSON documents containing an array of movie result hashes
        • +
        • Not found: Error retrieving movies list, status code 500.
        • +
        • Error: Error retrieving movies list, status code 500.
        • +
        + +
      • Retrieves a subset of movies (GET '/movies/sort/:query')
      • +
          +
        • Given a sort column, return n movie records, offset by p records (this will be used to create “pages” of movies)
        • +
        • Sort params are
        • +
            +
          • Title example: ('movies/sort/title?n=5&p=1')
          • +
          • Release_date example: ('movies/sort/release-date?n=5&p=1')
          • +
          +
            Return data: +
          • Found: Search results are JSON documents containing an array of movie result hashes
          • +
          • Not found: Error retrieving movies list, status code 500.
          • +
          • Error: Error retrieving movies list, status code 500.
          • +
          +
        + +
      • Given a movie’s title…
      • +
          +
        • Retrieves a list of customers that have currently checked out a copy of the movie (GET '/movies/:movie/current')
        • +
            +
          • includes each customer’s name, phone number, and account credit
          • + +
          +
            Return data: +
          • Found: Search results are JSON documents containing an array of customer result hashes
          • +
          • Not found: Error retrieving customer list, status code 500.
          • +
          • Error: Error retrieving customer list, status code 500.
          • +
          + +
        • Retrieves a list of customers that have checked out a copy in the past (GET '/movies/:movie/history/sort/:query')
        • +
            +
          • includes each customer’s name, phone number, and account credit
          • +
          • ordered by customer name: ('/movies/:movie_id/history/sort/name')
          • +
          • ordered by check-out: ('/movies/:movie_id/history/sort/date')
          • +
          +
            Return data: +
          • Found: Search results are JSON documents containing an array of customer result hashes
          • +
          • Not found: Error retrieving customer list, status code 500.
          • +
          • Error: Error retrieving customer list, status code 500.
          • +
          +
        + +
      + + +

      Rental

      + +
        + +
      • Look a movie up by title (GET '/rentals/:title)

        + Returned info: +
          +
        • movie’s synopsis
        • +
        • release date
        • +
        • available inventory (not currently checked-out to a customer)
        • +
        • and inventory total
        • +
        + +
          Return data: +
        • Found: Search results are JSON documents containing an array of movie info result hash
        • +
        • Not found: Error retrieving movie info, status code 500.
        • +
        • Error: Error retrieving movie info, status code 500.
        • +
        +
      • + +
      • See a list of customers that have currently checked out any of the movie’s inventory (GET '/rentals/:movie/customers')
      • +
          Return data: +
        • Found: Search results are JSON documents containing an array of customer result hashes
        • +
        • Not found: Error retrieving customers, status code 500.
        • +
        • Error: Error retrieving customers, status code 500.
        • +
        + + +
      • Given a customer’s id and a movie’s title…
      • +
          + + +
        • “check out” one of the movie’s inventory to the customer (GET '/rentals/:movie/check-out')
        • +
            +
          • Customer info given through JSON body
          • +
          • Establish a return date
          • +
          • Charge the customer’s account $1.50
          • +
          +
            Return data: +
          • Found: JSON document that has rental due date
          • +
          • Not found: Error, could not check out movie at this time, status code 500.
          • +
          • Error: Error, could not check out movie at this time, status code 500.
          • +
          + + + + +
        • “check in” one of customer’s rentals (GET '/rentals/:movie/return')
        • +
            +
          • Customer info given through JSON body
          • +
          • return the movie to its inventory
          • +
          +
            Return data: +
          • Found: JSON document confirms movie returned successfully
          • +
          • Not found: Error, could not return movie at this time, status code 500.
          • +
          • Error: Error, could not return movie at this time, status code 500.
          • +
          + +
        + + +
      • See a list of customers with overdue movies (GET '/rentals/overdue')
      • +
          +
        • include customer name, movie title, check-out date, and return date
        • +
        +
          Return data: +
        • Found: Search results are JSON documents containing an array of customer result hashes
        • +
        • Not found: Error could not retrieve customers, status code 500.
        • +
        • Error: Error could not retrieve customers, status code 500.
        • +
        + + + + +

        Informational endpoints

        +
          +
        • (/api/docs) (Serves a HTML view of documentation)
        • +
        • (/api/docs.json) (Serves JSON documentation)
        • +
        + + + diff --git a/views/index.ejs b/views/index.ejs index 7b7a1d6de..3d63b9a04 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -1,11 +1,5 @@ - - - - <%= title %> - - - -

        <%= title %>

        -

        Welcome to <%= title %>

        - - +extends layout + +block content + h1= title + p Welcome to #{title} diff --git a/views/index.jade b/views/index.jade deleted file mode 100644 index 3d63b9a04..000000000 --- a/views/index.jade +++ /dev/null @@ -1,5 +0,0 @@ -extends layout - -block content - h1= title - p Welcome to #{title} diff --git a/views/index2.ejs b/views/index2.ejs new file mode 100644 index 000000000..7b7a1d6de --- /dev/null +++ b/views/index2.ejs @@ -0,0 +1,11 @@ + + + + <%= title %> + + + +

        <%= title %>

        +

        Welcome to <%= title %>

        + + From 8e796ef1aeb5b924c45e3029e461e61db29c580c Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Fri, 24 Jun 2016 16:02:15 -0700 Subject: [PATCH 27/28] added controller tests for customers, movie and rentals --- controllers/rentals.js | 35 ++------ models/movie.js | 95 ++------------------ models/rental.js | 3 +- package.json | 2 +- routes/index.js | 6 +- spec/controllers/customers.spec.js | 119 ++++++++++++++++++++++++ spec/controllers/index.spec.js | 58 ++++++------ spec/controllers/movies.spec.js | 118 +++++++++++++++++++++++- spec/controllers/rentals.spec.js | 140 +++++++++++++++++++++++++++++ 9 files changed, 423 insertions(+), 153 deletions(-) create mode 100644 spec/controllers/customers.spec.js create mode 100644 spec/controllers/rentals.spec.js diff --git a/controllers/rentals.js b/controllers/rentals.js index 0efd894ae..21ab80bdd 100644 --- a/controllers/rentals.js +++ b/controllers/rentals.js @@ -1,9 +1,8 @@ var Rental = require('../models/rental'); var RentalsController = { - -// /rentals/:movie -// returns overview, release_date available inventory and total inventory + // /rentals/:movie + // returns overview, release_date available inventory and total inventory find: function(req, res, next) { Rental.search([req.params.movie.toLowerCase().replace(/ /g, "").replace(/\./g, "")], function(error, movie) { if(error) { @@ -16,11 +15,10 @@ var RentalsController = { }); }, - findCustomers: function(req, res, next) { Rental.searchCust(['true', req.params.movie.toLowerCase().replace(/ /g, "").replace(/\./g, "")], function(error, customers) { if(error) { - var err = new Error("Error retrieving customers:\n" + error.message); + var err = new Error("Error:" + error.message); err.status = 500; next(err); } else { @@ -31,7 +29,8 @@ var RentalsController = { checkOut: function(req, res, next) { // Rental.checkout([req.body.customer], [req.params.movie.toLowerCase().replace(/ /g, "").replace(/\./g, "")], function(error, customers) { - Rental.checkout([2], [req.params.movie.toLowerCase().replace(/ /g, "").replace(/\./g, "")], function(error, customers) { + console.log(req.params.id) + Rental.checkout([req.params.id], [req.params.movie.toLowerCase().replace(/ /g, "").replace(/\./g, "")], function(error, customers) { if(error) { var err = new Error("Error:\n" + error.message); err.status = 500; @@ -42,31 +41,9 @@ var RentalsController = { }); }, -// findTitle: function(req, res, next) { -// Rental.find (req.params.movie_title, function(error, movies) { -// if (error) { -// var err = new Error("No movies found"); -// err.status = 404; -// next(err); -// } else { -// res.json(movies) -// } -// }) -// }, -// -// customersNames: function(req, res, next) { -// Rental.customers (req.params.movie_title, function(error, customerNames) { -// if (error) { -// var err = new Error("No customers found"); -// err.status = 404; -// next(err); -// } else { -// res.json(customerNames) - - return: function(req, res, next) { // Rental.return([req.body.customer], [req.params.movie.toLowerCase().replace(/ /g, "").replace(/\./g, "")], function(error, customers) { - Rental.return([2], [req.params.movie.toLowerCase().replace(/ /g, "").replace(/\./g, "")], function(error, customers) { + Rental.return([req.params.id], [req.params.movie.toLowerCase().replace(/ /g, "").replace(/\./g, "")], function(error, customers) { if(error) { var err = new Error("Error:\n" + error.message); err.status = 500; diff --git a/models/movie.js b/models/movie.js index 0fa3e1c2f..afdaaba41 100644 --- a/models/movie.js +++ b/models/movie.js @@ -17,11 +17,9 @@ Movie.all = function(callback) { if(error || !movies) { callback(error || new Error("Could not retrieve movies"), undefined); } else { - var allMovies = movies.map(function(movie) { + callback(null, movies.map(function(movie){ return new Movie(movie); - }); - // console.log(allMovies) - callback(null, allMovies) + })) }; }); }; @@ -45,7 +43,7 @@ Movie.sort = function(query, n, p, callback) { Movie.find = function(input, callback) { - db.run("SELECT customers.* FROM movies INNER JOIN rentals ON rentals.movie_id=movies.id INNER JOIN customers ON customers.id=rentals.customer_id WHERE rentals.checked=$1 AND movies.title=$2", input, function(error,customers) { + db.run("SELECT customers.* FROM movies INNER JOIN rentals ON rentals.movie_id=movies.id INNER JOIN customers ON customers.id=rentals.customer_id WHERE rentals.checked=$1 AND movies.search_title=$2", input, function(error,customers) { if(error || !customers) { callback(error || new Error("Could not retrieve customers customers"), undefined); } else { @@ -59,8 +57,8 @@ Movie.find = function(input, callback) { Movie.history = function(input, query, callback) { // console.log(input) db.run("SELECT customers.* FROM movies INNER JOIN rentals ON rentals.movie_id=movies.id INNER JOIN customers ON customers.id=rentals.customer_id WHERE rentals.checked = $1 AND movies.title=$2 ORDER BY " + query + ";", input, function(error,customers) { - if(error || !customers) { - callback(error || new Error("Could not retrieve customers customers"), undefined); + if(error || !customers || customers.length ===0) { + callback(error || new Error("Could not retrieve a history of customers who have rented this movie."), undefined); } else { callback(null, customers.map(function(customer) { return (customer); @@ -68,87 +66,8 @@ Movie.history = function(input, query, callback) { } }); } -// -// return this; -// }; -// -// var balanceResultCallback = function(account, callback) { -// return function(error, result) { -// if(error) { -// callback(error, undefined); -// } else { -// account.getBalance(function(error, balance) { -// callback(error, balance); -// }); -// } -// }; -// }; -// -// Movies.prototype.deposit = function(amount, callback) { -// db.movies_deposit(this.id, amount, balanceResultCallback(this, callback)); -// return this; -// }; -// -// Movies.prototype.withdraw = function(amount, callback) { -// db.movies_withdraw(this.id, amount, balanceResultCallback(this, callback)); -// return this; -// }; -// -// Movies.prototype.transfer = function(to, amount, callback) { -// db.movies_transfer(this.id, to.id, amount, balanceResultCallback(this, callback)); -// return this; -// }; -// -// // Class Functions -// Movies.create = function(initialBalance, callback) { -// db.movies.save({ -// balance: initialBalance -// }, function(error, account) { -// if(error || !account) { -// callback(error || new Error("Could not create account"), undefined); -// } else { -// callback(null, new Movies(account.id)); -// } -// }); -// }; -// -// Movies.createSync = function(initialBalance) { -// var account = db.movies.saveSync({ -// balance: initialBalance -// }); -// -// return new Movies(account.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(account) { -// return new Movies(account.id); -// })); -// } -// }); -// }; -// -// Movies.find = function(id, callback) { -// db.movies.findOne({id: id}, function(error, account) { -// if(error || !account) { -// callback(error || new Error("Movies not found"), undefined); -// } else { -// callback(null, new Movies(account.id)); -// } -// }); -// }; -// -// // only attach this function if we're in test mode -// if (app.get('env') === 'test') { -// Movies.close_connection = function() { -// console.log("closing connection") -// db.end() -// } -// } + + // only attach this function if we're in test mode if (app.get('env') === 'test') { diff --git a/models/rental.js b/models/rental.js index 2f3b2c6c8..9a6e344bb 100644 --- a/models/rental.js +++ b/models/rental.js @@ -25,7 +25,8 @@ Rental.search = function (input, callback) { Rental.searchCust = function (input, callback) { console.log(input) db.run("SELECT customers.* FROM movies INNER JOIN rentals ON rentals.movie_id=movies.id INNER JOIN customers ON customers.id=rentals.customer_id WHERE rentals.checked=$1 AND search_title=$2", input, function (error, customer) { - if (error || !customer) { + if (error || !customer || customer.length === 0) { + console.log(customer) callback(new Error("Could not retrieve customer"), undefined) } else { callback(null, customer.map (function (customers) { diff --git a/package.json b/package.json index 6acf93282..f1c3c188a 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "setup": "node ./setup.js", "start": "./node_modules/.bin/nodemon ./bin/www", "start-test": "NODE_ENV=test ./node_modules/.bin/nodemon ./bin/www", - "test": "tclear; ./node_modules/.bin/istanbul cover -x 'spec/**/*' -- ./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 radio_star_development", "db:create": "createdb radio_star_development", "db:dropt": "dropdb radio_star_test", diff --git a/routes/index.js b/routes/index.js index f71c2b44a..68a3f4457 100644 --- a/routes/index.js +++ b/routes/index.js @@ -71,18 +71,18 @@ router.get('/rentals/overdue', RentalController.overdue); router.get('/rentals/:movie', RentalController.find); // See a list of customers that have currently checked out any of the movie's inventory (/rentals/Jaws/customers) -router.get('/rentals/:movie/customers', RentalController.findCustomers); +router.get('/rentals/:movie/customers/', RentalController.findCustomers); // Given a customer's id and a movie's title ... // "check out" one of the movie's inventory to the customer (/rentals/Jaws/check-out) // Establish a return date // Charge the customer's account (cost up to you) -router.get('/rentals/:movie/check-out', RentalController.checkOut); +router.get('/rentals/:movie/check-out/:id', RentalController.checkOut); // "check in" one of customer's rentals (/rentals/Jaws/return) // return the movie to its inventory -router.get('/rentals/:movie/return', RentalController.return); +router.get('/rentals/:movie/return/:id', RentalController.return); diff --git a/spec/controllers/customers.spec.js b/spec/controllers/customers.spec.js new file mode 100644 index 000000000..1321de0e8 --- /dev/null +++ b/spec/controllers/customers.spec.js @@ -0,0 +1,119 @@ +var request = require("request") +var baseUrl = "http://localhost:3000" + +describe("CustomerController", function() { + var url = function(endpoint) { + return baseUrl + "/customers" + endpoint + } + + describe(".index", function(done) { + it("returns a response", function(done) { + request.get(url("/"), function(error, response, body) { + done() + }) + }) + + it("returns JSON", function(done) { + request.get(url("/"), function(error, response, body) { + expect(response.headers['content-type']).toContain('application/json') + done() + }) + }) + + it("should be an array of objects", function(done) { + request.get(url("/"), function(error, response, body) { + var data = JSON.parse(body) + expect(typeof data).toEqual('object') + for (var record of data) { + expect(Object.keys(record)).toEqual([ 'id' ]) + } + done() + }) + }) + }) + + describe('.subset', function(done) { + it('returns a successful response', function(done) { + request.get(url("/sort/name"), function(error, response, body) { + expect(response.statusCode).toBe(200) + done() + }) + }) + + it("returns JSON", function(done) { + request.get(url("/sort/name"), function(error, response, body) { + expect(response.headers['content-type']).toContain('application/json') + done() + }) + }) + + it("should be an array of objects", function(done) { + request.get(url("/sort/name"), function(error, response, body) { + var data = JSON.parse(body) + expect(typeof data).toEqual('object') + + for (var record of data) { + expect(Object.keys(record)).toEqual([ 'id' ]) + } + done() + }) + }) + }) + + describe('.current', function(done) { + it('returns a successful response', function(done) { + request.get(url("/5/current"), function(error, response, body) { + expect(response.statusCode).toBe(200) + done() + }) + }) + + it("returns JSON", function(done) { + request.get(url("/5/current"), function(error, response, body) { + expect(response.headers['content-type']).toContain('application/json') + done() + }) + }) + + it("should be an array of objects", function(done) { + request.get(url("/5/current"), function(error, response, body) { + var data = JSON.parse(body) + expect(typeof data).toEqual('object') + + for (var record of data) { + expect(Object.keys(record)).toEqual([ 'id', 'title', 'release_date', 'synopsis' ]) + } + done() + }) + }) + }) + + describe('.history', function(done) { + it('returns a successful response', function(done) { + request.get(url("/5/history"), function(error, response, body) { + expect(response.statusCode).toBe(200) + done() + }) + }) + + it("returns JSON", function(done) { + request.get(url("/5/history/"), function(error, response, body) { + expect(response.headers['content-type']).toContain('application/json') + done() + }) + }) + + it("should be an array of objects", function(done) { + request.get(url("/5/history/"), function(error, response, body) { + var data = JSON.parse(body) + expect(typeof data).toEqual('object') + + for (var record of data) { + expect(Object.keys(record)).toEqual([ 'id', 'title', 'search_title', 'overview', 'release_date', 'inventory', 'inventory_total', 'movie_id', 'customer_id', 'checked', 'rental_date', 'due_date', 'return_date' ]) + } + done() + }) + }) + }) + +}) diff --git a/spec/controllers/index.spec.js b/spec/controllers/index.spec.js index e4151d267..a9198697c 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/" - -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() - }) - }) - - 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() - }) - }) - - 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 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() +// }) +// }) +// +// 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() +// }) +// }) +// +// 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() +// }) +// }) +// }) +// }) diff --git a/spec/controllers/movies.spec.js b/spec/controllers/movies.spec.js index ddcaf2f68..5671a50cb 100644 --- a/spec/controllers/movies.spec.js +++ b/spec/controllers/movies.spec.js @@ -1,5 +1,119 @@ -var request = require('request'); +var request = require("request") +var baseUrl = "http://localhost:3000" -describe("Endpoints under /movies", function() { +describe("MovieController", function() { + var url = function(endpoint) { + return baseUrl + "/movies" + endpoint + } + + describe(".all", function(done) { + it("returns a response", function(done) { + request.get(url("/"), function(error, response, body) { + done() + }) + }) + + it("returns JSON", function(done) { + request.get(url("/"), function(error, response, body) { + expect(response.headers['content-type']).toContain('application/json') + done() + }) + }) + + it("should be an array of objects", function(done) { + request.get(url("/"), function(error, response, body) { + var data = JSON.parse(body) + expect(typeof data).toEqual('object') + for (var record of data) { + expect(Object.keys(record)).toEqual([ 'id', 'title', 'release_date', 'synopsis' ]) + } + done() + }) + }) + }) + + describe('.sort', function(done) { + it('returns a successful response', function(done) { + request.get(url("/sort/title?n=1&p=2"), function(error, response, body) { + expect(response.statusCode).toBe(200) + done() + }) + }) + + it("returns JSON", function(done) { + request.get(url("/sort/title?n=1&p=2"), function(error, response, body) { + expect(response.headers['content-type']).toContain('application/json') + done() + }) + }) + + it("should be an array of objects", function(done) { + request.get(url("/sort/title?n=1&p=2"), function(error, response, body) { + var data = JSON.parse(body) + expect(typeof data).toEqual('object') + + for (var record of data) { + expect(Object.keys(record)).toEqual([ 'id', 'title', 'release_date', 'synopsis' ]) + } + done() + }) + }) + }) + + describe('.find', function(done) { + it('returns a successful response', function(done) { + request.get(url("/psycho/current"), function(error, response, body) { + expect(response.statusCode).toBe(200) + done() + }) + }) + + it("returns JSON", function(done) { + request.get(url("/psycho/current"), function(error, response, body) { + expect(response.headers['content-type']).toContain('application/json') + done() + }) + }) + + it("should be an array of objects", function(done) { + request.get(url("/psycho/current"), function(error, response, body) { + var data = JSON.parse(body) + expect(typeof data).toEqual('object') + + for (var record of data) { + expect(Object.keys(record)).toEqual([ 'id', 'title', 'release_date', 'synopsis' ]) + } + done() + }) + }) + }) + + describe('.history', function(done) { + it('returns a successful response', function(done) { + request.get(url("/psycho/history/sort/name"), function(error, response, body) { + expect(response.statusCode).toBe(200) + done() + }) + }) + + it("returns JSON", function(done) { + request.get(url("/Psycho/history/sort/name"), function(error, response, body) { + expect(response.headers['content-type']).toContain('application/json') + done() + }) + }) + + it("should be an array of objects", function(done) { + request.get(url("/Psycho/history/sort/name"), function(error, response, body) { + var data = JSON.parse(body) + expect(typeof data).toEqual('object') + + for (var record of data) { + expect(Object.keys(record)).toEqual(['id', 'name', 'registered_at', 'address', 'city', 'state', 'postal_code', 'phone', 'account_credit']) + } + done() + }) + }) + }) }) diff --git a/spec/controllers/rentals.spec.js b/spec/controllers/rentals.spec.js new file mode 100644 index 000000000..fa5ac482c --- /dev/null +++ b/spec/controllers/rentals.spec.js @@ -0,0 +1,140 @@ +var request = require("request") +var baseUrl = "http://localhost:3000" + +describe("RentalsController", function() { + var url = function(endpoint) { + return baseUrl + "/rentals" + endpoint + } + + describe(".find", function(done) { + it("returns a response", function(done) { + request.get(url("/psycho"), function(error, response, body) { + done() + }) + }) + + it("returns JSON", function(done) { + request.get(url("/psycho"), function(error, response, body) { + expect(response.headers['content-type']).toContain('application/json') + done() + }) + }) + + it("should be an array of objects", function(done) { + request.get(url("/psycho"), function(error, response, body) { + var data = JSON.parse(body) + expect(typeof data).toEqual('object') + for (var record of data) { + expect(Object.keys(record)).toEqual([ 'overview', 'release_date', 'inventory', 'inventory_total' ]) + } + done() + }) + }) + }) + + describe('.findCustomers', function(done) { + it('returns a successful response', function(done) { + request.get(url("/psycho/customers"), function(error, response, body) { + expect(response.statusCode).toBe(200) + done() + }) + }) + + it("returns JSON", function(done) { + request.get(url("/psycho/customers"), function(error, response, body) { + expect(response.headers['content-type']).toContain('application/json') + done() + }) + }) + + it("should be an array of objects", function(done) { + request.get(url("/psycho/customers"), function(error, response, body) { + var data = JSON.parse(body) + expect(typeof data).toEqual('object') + + for (var record of data) { + expect(Object.keys(record)).toEqual([ 'id', 'name', 'registered_at', 'address', 'city', 'state', 'postal_code', 'phone', 'account_credit' ]) + } + done() + }) + }) + }) + +// /rentals/:movie/check-out/:id + describe('.checkOut', function(done) { + it('returns a successful response', function(done) { + request.get(url("/alien/check-out/22"), function(error, response, body) { + expect(response.statusCode).toBe(200) + expect(response.headers['content-type']).toContain('application/json') + + // var data = JSON.parse(body) + // expect(typeof data).toEqual('object') + // + // for (var record of data) { + // expect(Object.keys(record)).toEqual([ 'return_date' ]) + // } + done() + }) + }) + }) + + describe('.return', function(done) { + it('returns a successful response', function(done) { + request.get(url("/alien/return/22"), function(error, response, body) { + expect(response.statusCode).toBe(200) + expect(response.headers['content-type']).toContain('application/json') + // var data = JSON.parse(body) + // expect(typeof data).toEqual('object') + // + // for (var record of data) { + // expect(Object.keys(record)).toEqual(['id', 'name', 'registered_at', 'address', 'city', 'state', 'postal_code', 'phone', 'account_credit']) + // } + done() + }) + }) + }) + + + // it("should be an array of objects", function(done) { + // request.get(url("/Psycho/history/sort/name"), function(error, response, body) { + // var data = JSON.parse(body) + // expect(typeof data).toEqual('object') + // + // for (var record of data) { + // expect(Object.keys(record)).toEqual(['id', 'name', 'registered_at', 'address', 'city', 'state', 'postal_code', 'phone', 'account_credit']) + // } + // done() + // }) + // }) + // }), + // + describe('.overdue', function(done) { + it('returns a successful response', function(done) { + request.get(url("/overdue"), function(error, response, body) { + expect(response.statusCode).toBe(200) + expect(response.headers['content-type']).toContain('application/json') + done() + }) + }) + }) + // + // it("returns JSON", function(done) { + // request.get(url("/Psycho/history/sort/name"), function(error, response, body) { + // done() + // }) + // }) + // + // it("should be an array of objects", function(done) { + // request.get(url("/Psycho/history/sort/name"), function(error, response, body) { + // var data = JSON.parse(body) + // expect(typeof data).toEqual('object') + // + // for (var record of data) { + // expect(Object.keys(record)).toEqual(['id', 'name', 'registered_at', 'address', 'city', 'state', 'postal_code', 'phone', 'account_credit']) + // } + // done() + // }) + // }) + // }) + +}) From 4ccb7826ee1f3bd8fcb1840178c82d1bea32f60f Mon Sep 17 00:00:00 2001 From: Cristal Tay Date: Fri, 24 Jun 2016 17:22:25 -0700 Subject: [PATCH 28/28] finished model testings for customers --- models/customer.js | 71 ++------ spec/controllers/customers.spec.js | 8 +- spec/models/customers.spec.js | 68 ++++++++ spec/models/rentals.spec.js | 263 +++++++++++++++++++++++++++++ 4 files changed, 346 insertions(+), 64 deletions(-) create mode 100644 spec/models/customers.spec.js create mode 100644 spec/models/rentals.spec.js diff --git a/models/customer.js b/models/customer.js index 85e820645..51f3414e6 100644 --- a/models/customer.js +++ b/models/customer.js @@ -3,26 +3,13 @@ var db = app.get("db"); var Cust = function(cust) { this.id = cust; - // this.id = cust.id; - // this.name = cust.name; - // this.address = cust.address; }; -// var Movie = function(movie) { -// this.title = movie.title; -// // this.id = cust.id; -// // this.name = cust.name; -// // this.address = cust.address; -// }; - -// Instance functions - - // class Cust.all = function(callback) { db.query("select * from customers", function(error, custs) { - if(error || !custs) { - callback(error || new Error("Could not retrieve custs"), undefined); + if(error || !custs || custs === 0) { + callback(error || new Error("Could not retrieve customers"), undefined); } else { var allCusts = custs.map(function(cust) { return new Cust(cust); @@ -39,11 +26,9 @@ Cust.sort = function(query, n, p, callback) { limit: n, offset: p }, function(error, custs) { + console.log(custs) if(error || !custs) { callback(error || new Error("Could not retrieve customer"), undefined); - - // } else if ((query != "name") && (query != "registered_at") && (query != "postal_code")) { - // callback(error || new Error("Undefined sort term"), undefined); } else { var allCusts = custs.map(function(cust) { return new Cust(cust); @@ -58,7 +43,7 @@ Cust.find = function(input, callback) { // console/.log(input) db.run("SELECT * FROM movies INNER JOIN rentals ON rentals.movie_id=movies.id WHERE rentals.customer_id=$1 and rentals.checked=$2;", input, function(error, rentals) { // console.log(rentals) - if(error || !rentals) { + if(error || !rentals || rentals.length ===0) { callback(error || new Error("Could not retrieve customers rentals"), undefined); } else { callback(null, rentals.map(function(rental) { @@ -67,45 +52,11 @@ Cust.find = function(input, callback) { }; }); }; -// Cust.find = function(ids, callback) { -// // find all rentals that this customer has checked out -// db.rentals.find({customer_id: ids, checked: "true"}, function(error, rentals) { -// // console.log(rentals) -// if(error || !rentals) { -// callback(error || new Error("Could not retrieve rentals"), undefined); -// } else { -// // callback(null, rentals.map(function(rental) { -// var arrayOfMovies = []; -// -// var allMovies = rentals.map(function(rental) { -// console.log(rental) -// db.query("select * from movies where id=$1", [rental.movie_id], function(error, movie) { -// // console.log(rental.movie_id) -// // new Movie(rental); -// return arrayOfMovies.push(new Movie(movie[0])) -// console.log(arrayOfMovies) -// // console.log(movie[0].title) -// // return arrayOfMovies; -// }); -// // console.log(arrayOfMovies) -// -// // return new Movie(movie[0]); -// }); -// console.log(allMovies) -// console.log(arrayOfMovies) -// // return arrayOfMovies; -// callback(null, arrayOfMovies) -// // console.log(arrayOfMovies) -// // callback(null, movie) -// }; -// }); -// }; - Cust.history = function(input, query, callback) { db.run("SELECT * FROM movies INNER JOIN rentals ON rentals.movie_id=movies.id WHERE rentals.customer_id=$1 and rentals.checked=$2 ORDER BY " + query + ";" , input, function(error, rentals) { // console.log(rentals) - if(error || !rentals) { + if(error || !rentals || rentals.length === 0) { callback(error || new Error("Could not retrieve customers rentals"), undefined); } else { callback(null, rentals.map(function(rental) { @@ -116,10 +67,10 @@ Cust.history = function(input, query, callback) { } // only attach this function if we're in test mode -if (app.get('env') === 'test') { - Cust.close_connection = function() { - console.log("closing connection") - db.end() - } -} +// if (app.get('env') === 'test') { +// Cust.close_connection = function() { +// console.log("closing connection") +// db.end() +// } +// } module.exports = Cust; diff --git a/spec/controllers/customers.spec.js b/spec/controllers/customers.spec.js index 1321de0e8..a43ea1b69 100644 --- a/spec/controllers/customers.spec.js +++ b/spec/controllers/customers.spec.js @@ -62,26 +62,26 @@ describe("CustomerController", function() { describe('.current', function(done) { it('returns a successful response', function(done) { - request.get(url("/5/current"), function(error, response, body) { + request.get(url("/1/current"), function(error, response, body) { expect(response.statusCode).toBe(200) done() }) }) it("returns JSON", function(done) { - request.get(url("/5/current"), function(error, response, body) { + request.get(url("/1/current"), function(error, response, body) { expect(response.headers['content-type']).toContain('application/json') done() }) }) it("should be an array of objects", function(done) { - request.get(url("/5/current"), function(error, response, body) { + request.get(url("/1/current"), function(error, response, body) { var data = JSON.parse(body) expect(typeof data).toEqual('object') for (var record of data) { - expect(Object.keys(record)).toEqual([ 'id', 'title', 'release_date', 'synopsis' ]) + expect(Object.keys(record)).toEqual([ 'id', 'title', 'search_title', 'overview', 'release_date', 'inventory', 'inventory_total', 'movie_id', 'customer_id', 'checked', 'rental_date', 'due_date', 'return_date' ]) } done() }) diff --git a/spec/models/customers.spec.js b/spec/models/customers.spec.js new file mode 100644 index 000000000..c2510923e --- /dev/null +++ b/spec/models/customers.spec.js @@ -0,0 +1,68 @@ +var app = require('../../app') +var db = app.get('db') +var Customer = require('../../models/customer') + +describe('Customer', function () { + afterEach(function () { + db.end() + }) + +// testing .all + describe('all', function () { + it('should return all customers', function (done) { + Customer.all(function (error, customers) { + expect(customers.length).toEqual(200) + done() + }) + }) + }) + +// testing .sort + describe('sort', function () { + it('should return customers sorted by name', function (done) { + Customer.sort('name', 1, 1, function (error, customers) { + expect(customers.length).toEqual(1) + done() + }) + }) + }) + + describe('find', function () { + it('Errors when fed bad info', function(done) { + Customer.find([10000,'true'], function(error, result) { + expect(error.message).toBe("Could not retrieve customers rentals") + expect(result).toEqual(null) + done() + }) + }) + }) + + describe('find', function () { + it('should return rentals that customer has checked out', function(done) { + Customer.find([1,'true'], function(error, customers) { + expect(customers.length).toEqual(7) + done() + }) + }) + }) + + describe('history', function () { + it('select customer history', function(done) { + Customer.history([1,'false'], "rentals.rental_date",function(error, customers) { + expect(customers.length).toEqual(5) + done() + }) + }) + }) + + describe('history', function () { + it('Errors when fed bad info', function(done) { + Customer.history([10000,'false'], "rentals.rental_date", function(error, result) { + expect(error.message).toBe("Could not retrieve customers rentals") + expect(result).toEqual(null) + done() + }) + }) + }) + +}) diff --git a/spec/models/rentals.spec.js b/spec/models/rentals.spec.js new file mode 100644 index 000000000..e020e8fd4 --- /dev/null +++ b/spec/models/rentals.spec.js @@ -0,0 +1,263 @@ +// var app = require('../../app') +// var db = app.get('db') +// var Rental = require('../../models/rentals') +// +// describe('Rental', function () { +// afterEach(function () { +// Rental.end() +// }) +// +// // testing .find_current +// describe('find_current', function () { +// it('Errors when fed bad info', function(done) { +// Rental.find_current('Fake column', function(error, result) { +// expect(error.message).toBe('invalid input syntax for integer: "Fake column"') +// expect(result).toEqual(null) +// done() +// }) +// }) +// }) +// +// describe('find_current', function () { +// it('returns an array', function(done) { +// Rental.find_current(1, function (error, result) { +// expect(error).toBe(null) +// expect(result).toEqual(jasmine.any(Array)) +// done() +// }) +// }) +// }) +// +// describe('find_current', function () { +// it('returns instances with correct keys', function(done) { +// Rental.find_current(2, function(error, result) { +// expect(error).toBe(null) +// expect(Object.keys(result[0])).toEqual(['id', 'customer_id', 'video_id', 'checkout_date', 'due_date', 'checkin_date', 'charge']) +// done() +// }) +// }) +// }) +// +// describe('find_current', function () { +// it('returns instances where the checkin_date is null', function(done) { +// Rental.find_current(2, function(error, result) { +// expect(error).toBe(null) +// expect(result[0]['checkin_date']).toBe(null) +// done() +// }) +// }) +// }) +// +// describe('find_current', function () { +// it('returns instances for one customer only', function(done) { +// Rental.find_current(2, function(error, result) { +// expect(error).toBe(null) +// for (var instance of result) { +// expect(instance['customer_id']).toBe(2) +// } +// done() +// }) +// }) +// }) +// +// // testing .find_history +// describe('find_history', function () { +// it('Errors when fed bad info', function(done) { +// Rental.find_history('Fake column', function(error, result) { +// expect(error.message).toBe('invalid input syntax for integer: "Fake column"') +// expect(result).toEqual(null) +// done() +// }) +// }) +// }) +// +// describe('find_history', function () { +// it('returns an array', function(done) { +// Rental.find_history(1, function (error, result) { +// expect(error).toBe(null) +// expect(result).toEqual(jasmine.any(Array)) +// done() +// }) +// }) +// }) +// +// describe('find_history', function () { +// it('returns instances with correct keys', function(done) { +// Rental.find_history(1, function(error, result) { +// expect(error).toBe(null) +// expect(Object.keys(result[0])).toEqual(['id', 'customer_id', 'video_id', 'checkout_date', 'due_date', 'checkin_date', 'charge']) +// done() +// }) +// }) +// }) +// +// describe('find_history', function () { +// it('returns instances where the checkin_date is not null', function(done) { +// Rental.find_history(1, function(error, result) { +// expect(error).toBe(null) +// expect(result[0]['checkin_date']).not.toBe(null) +// done() +// }) +// }) +// }) +// +// describe('find_history', function () { +// it('returns instances for one customer only', function(done) { +// Rental.find_history(1, function(error, result) { +// expect(error).toBe(null) +// for (var instance of result) { +// expect(instance['customer_id']).toBe(1) +// } +// done() +// }) +// }) +// }) +// +// // testing .overdue +// describe('overdue', function () { +// it('returns an array', function(done) { +// Rental.overdue(function (error, result) { +// expect(error).toBe(null) +// expect(result).toEqual(jasmine.any(Array)) +// done() +// }) +// }) +// }) +// +// describe('overdue', function () { +// it('returns instances with correct keys', function(done) { +// Rental.overdue(function(error, result) { +// expect(error).toBe(null) +// for (var instance of result) { +// expect(Object.keys(instance)).toEqual(['customer', 'video', 'checkout_date', 'due_date']) +// } +// done() +// }) +// }) +// }) +// +// describe('overdue', function () { +// it('returns instances where the checkin_date is not null', function(done) { +// Rental.overdue(function(error, result) { +// expect(error).toBe(null) +// for (var instance of result) { +// expect(instance['checkin_date']).not.toBe(null) +// } +// done() +// }) +// }) +// }) +// +// // testing .video_current +// describe('video_current', function () { +// it('returns an array', function(done) { +// Rental.video_current('Psycho', function (error, result) { +// expect(error).toBe(null) +// expect(result).toEqual(jasmine.any(Array)) +// done() +// }) +// }) +// }) +// +// describe('video_current', function () { +// it('returns instances with correct keys', function(done) { +// Rental.video_current('Psycho', function(error, result) { +// expect(error).toBe(null) +// for (var instance of result) { +// expect(Object.keys(instance)).toEqual(['id', 'name', 'registered_at', 'address', 'city', 'state', 'postal_code', 'phone', 'account_credit']) +// } +// done() +// }) +// }) +// }) +// +// describe('video_current', function () { +// it('returns correct customer', function(done) { +// Rental.video_current('Psycho', function(error, result) { +// expect(error).toBe(null) +// for (var instance of result) { +// expect(instance['id']).toBe(8) +// expect(instance['name']).toBe('Amanda Curtis') +// } +// done() +// }) +// }) +// }) +// +// // testing .find_video_history +// describe('find_video_history', function () { +// it('returns an array', function(done) { +// Rental.find_video_history('Psycho', 'name', function (error, result) { +// expect(error).toBe(null) +// expect(result).toEqual(jasmine.any(Array)) +// done() +// }) +// }) +// }) +// +// describe('find_video_history', function () { +// it('returns instances with correct keys', function(done) { +// Rental.find_video_history('Psycho', 'name', function(error, result) { +// expect(error).toBe(null) +// for (var instance of result) { +// expect(Object.keys(instance)).toEqual(['id', 'name', 'registered_at', 'address', 'city', 'state', 'postal_code', 'phone', 'account_credit']) +// } +// done() +// }) +// }) +// }) +// +// describe('find_video_history', function () { +// it('returns correct number of records', function(done) { +// Rental.find_video_history('Psycho', 'name', function(error, result) { +// expect(error).toBe(null) +// var iteration = 0 +// for (var instance of result) { +// iteration++ +// } +// expect(iteration).toEqual(3) +// done() +// }) +// }) +// }) +// +// describe('find_video_history', function () { +// it('orders by name', function(done) { +// Rental.find_video_history('Psycho', 'name', function(error, result) { +// expect(error).toBe(null) +// expect(result[0]['name']).toBe('Carolyn Chandler') +// done() +// }) +// }) +// }) +// +// describe('find_video_history', function () { +// it('orders by checkout_date', function(done) { +// Rental.find_video_history('Psycho', 'checkout_date', function(error, result) { +// expect(error).toBe(null) +// expect(result[0]['name']).toBe('Shelley Rocha') +// done() +// }) +// }) +// }) +// +// describe('checkout', function () { +// it('returns an array', function(done) { +// Rental.checkout('Psycho', 1, function (error, result) { +// expect(error).toBe(null) +// expect(typeof result).toEqual('object') +// done() +// }) +// }) +// }) +// +// describe('checkin', function () { +// it('returns an array', function(done) { +// Rental.checkin('Psycho', 1, function (error, result) { +// expect(error).toBe(null) +// expect(typeof result).toEqual('object') +// done() +// }) +// }) +// }) +// })