From 2eaf465494a08549c146a17c54614fe107f15e29 Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Tue, 14 Jun 2016 14:55:31 -0700 Subject: [PATCH 01/58] psuedo code for routes. --- routes/index.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/routes/index.js b/routes/index.js index 06cfc1137..332c8f05c 100644 --- a/routes/index.js +++ b/routes/index.js @@ -6,4 +6,34 @@ router.get('/', function(req, res, next) { res.status(200).json({whatevs: 'whatevs!!!'}) }); +/* +customers +GET customers +GET customers/sort/name?n=10&p=2 +GET customers/sort/registered_at +GET customers/sort/postal_code + +GET customers/:id/current +GET customers/:id/history + +movies +GET movies +GET movies/sort/release-date?n=5&p=1 +GET movies/sort/title +GET movies/sort/release-date + +GET movies/:title/current +GET movies/:title/history/sort/name +GET movies/:title/history/sort/checkout-date + +rentals +GET rentals/:title +GET rentals/:title/customers +POST rentals/:title/checkout +POST rentals/:title/return +GET rentals/overdue + +*/ + + module.exports = router; From 9140faca6f1e420adf846992f16a8bf3ec886106 Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Tue, 14 Jun 2016 14:57:00 -0700 Subject: [PATCH 02/58] small change to routes file to require cust id and title for rentals --- routes/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routes/index.js b/routes/index.js index 332c8f05c..8870d72fb 100644 --- a/routes/index.js +++ b/routes/index.js @@ -29,8 +29,8 @@ GET movies/:title/history/sort/checkout-date rentals GET rentals/:title GET rentals/:title/customers -POST rentals/:title/checkout -POST rentals/:title/return +POST rentals/:title/checkout #provide customerid and movie title +POST rentals/:title/return #provide customerid and movie title GET rentals/overdue */ From fa4ce053e1fac8bf88ddb9d3040d3147cf391144 Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Tue, 14 Jun 2016 15:00:59 -0700 Subject: [PATCH 03/58] baseline completed. print json it works in json, at /zomg. --- routes/index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/routes/index.js b/routes/index.js index 8870d72fb..f8f55516c 100644 --- a/routes/index.js +++ b/routes/index.js @@ -6,6 +6,9 @@ router.get('/', function(req, res, next) { res.status(200).json({whatevs: 'whatevs!!!'}) }); +router.get('/zomg', function(req, res, next) { + res.status(200).json({it_works: 'it works!!!'}) +}); /* customers GET customers From e03e45364d8b11d85927b3a93f755b6afa39cf81 Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Tue, 14 Jun 2016 15:13:54 -0700 Subject: [PATCH 04/58] added scripts for db. --- package.json | 8 +++++++- routes/index.js | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index d39b26403..f564a8ded 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,12 @@ "private": true, "scripts": { "start": "nodemon ./bin/www", - "test": "clear; jasmine-node --verbose spec/" + "test": "clear; jasmine-node --verbose spec/", + "db:drop": "dropdb video_store_api", + "db:create": "createdb video_store_api", + "db:schema": "node tasks/load_schema.js", + "db:seed": "", + "db:reset": "npm run db:drop; npm run db:create; npm run db:schema" }, "dependencies": { "body-parser": "~1.13.2", @@ -12,6 +17,7 @@ "debug": "~2.2.0", "express": "~4.13.1", "jade": "~1.11.0", + "massive": "^2.3.0", "morgan": "~1.6.1", "sequelize": "^3.23.3", "serve-favicon": "~2.3.0" diff --git a/routes/index.js b/routes/index.js index f8f55516c..a146f6906 100644 --- a/routes/index.js +++ b/routes/index.js @@ -9,6 +9,7 @@ router.get('/', function(req, res, next) { router.get('/zomg', function(req, res, next) { res.status(200).json({it_works: 'it works!!!'}) }); + /* customers GET customers From c841893083273f50470f9f9237afd8619801a9cf Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Tue, 14 Jun 2016 21:41:44 -0700 Subject: [PATCH 05/58] made changes to schema.sql, package.json, and made tasks/seed_data.js. Seed data now loads, and includes logic to take care of asynchronous seeding for customers and movies. --- db/setup/schema.sql | 41 ++++++++++++++++++++++++++++++++++ package.json | 2 +- tasks/load_schema.js | 14 ++++++++++++ tasks/seed_data.js | 53 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 db/setup/schema.sql create mode 100644 tasks/load_schema.js create mode 100644 tasks/seed_data.js diff --git a/db/setup/schema.sql b/db/setup/schema.sql new file mode 100644 index 000000000..cb68c8750 --- /dev/null +++ b/db/setup/schema.sql @@ -0,0 +1,41 @@ +DROP TABLE IF EXISTS movies; +CREATE TABLE movies( + id serial PRIMARY KEY, + title text, + overview text, + release_date date, + inventory integer +); + +CREATE INDEX movies_title ON movies (title); +CREATE INDEX movies_release_date ON movies (release_date); + +/*customers */ +DROP TABLE IF EXISTS customers; +CREATE TABLE customers( + id serial PRIMARY KEY, + name text, + registered_at text, + address text, + city text, + state text, + postal_code integer, + phone text, + account_credit text +); + +CREATE INDEX customers_name ON customers (name); + +/* rentals */ +DROP TABLE IF EXISTS rentals; +CREATE TABLE rentals( + id serial PRIMARY KEY, + movie_id integer references movies(id), + customer_id integer references customers(id), + checkout_date date, + due_date date, + return_date date +); + +CREATE INDEX rentals_customer_id ON rentals (customer_id); +CREATE INDEX rentals_movie_id ON rentals (movie_id); diff --git a/package.json b/package.json index f564a8ded..64a128e00 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "db:drop": "dropdb video_store_api", "db:create": "createdb video_store_api", "db:schema": "node tasks/load_schema.js", - "db:seed": "", + "db:seed": "node tasks/seed_data.js", "db:reset": "npm run db:drop; npm run db:create; npm run db:schema" }, "dependencies": { diff --git a/tasks/load_schema.js b/tasks/load_schema.js new file mode 100644 index 000000000..95bb6c87b --- /dev/null +++ b/tasks/load_schema.js @@ -0,0 +1,14 @@ +var massive = require('massive') +var connectionString = "postgres://localhost/video_store_api" + +var db = massive.connectSync({connectionString : connectionString}) + +// setup below comes from folder name +db.setup.schema([], function(err, res) { + if(err) { + throw(new Error(err.message)) + } + + console.log("yay schema!") + process.exit() +}) diff --git a/tasks/seed_data.js b/tasks/seed_data.js new file mode 100644 index 000000000..de66d0d58 --- /dev/null +++ b/tasks/seed_data.js @@ -0,0 +1,53 @@ +var massive = require('massive') +var connectionString = "postgres://localhost/video_store_api" + + +var db = massive.connectSync({connectionString : connectionString}) + +// seeding below +// Define JSON File +// fs means file system + var fs = require("fs") + console.log("\n *STARTING* \n") +// Get content from file +var seedMovies = "movies.json" +var seedCustomers = "customers.json" + +var seedData = function(filename, table, callback) { + + var contents = fs.readFileSync("db/seeds/" + filename) + // Define to JSON type + var records = JSON.parse(contents) + // Get Value from JSON + var ongoing_saves = 0 + for (var record of records) { + // making an ongoing_saves to increment saved customers + ongoing_saves += 1 + table.save(record, function(err,inserted) { + // decrementing ongoing_saves as they are no longer ongoing. + ongoing_saves -= 1 + if(err) { + throw(new Error(err.message)) + } + // + if (ongoing_saves < 1) { + console.log("seeded " + filename + "!") + callback() + } + }) + } +} +var movies_done = false +var customers_done = false +seedData(seedMovies, db.movies, function(){ + movies_done = true + if (customers_done === true) { + process.exit() + } +}) +seedData(seedCustomers, db.customers, function() { + customers_done = true + if (movies_done === true) { + process.exit() + } +}) From a7e7d958fd6cfee6080cf4135d358ca2c89c752e Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Wed, 15 Jun 2016 15:10:38 -0700 Subject: [PATCH 06/58] added node run db:seed to the db:reset command in scripts package.json. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 64a128e00..38e672df4 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "db:create": "createdb video_store_api", "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", From d18f5b695513b24942133f07841c0dc1a939e3b4 Mon Sep 17 00:00:00 2001 From: Ania Gonzalez Date: Wed, 15 Jun 2016 15:49:27 -0700 Subject: [PATCH 07/58] Created customers routes --- app.js | 5 ++++- package.json | 14 +++++++++++++- routes/customers.js | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 routes/customers.js diff --git a/app.js b/app.js index f0579b1dc..0efc1a545 100644 --- a/app.js +++ b/app.js @@ -6,9 +6,9 @@ var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var routes = require('./routes/index'); - var app = express(); + // view engine setup app.set('views', path.join(__dirname, 'views')); app.set('view engine', 'jade'); @@ -23,6 +23,9 @@ app.use(express.static(path.join(__dirname, 'public'))); app.use('/', routes); +var customersRoutes = require('./routes/customers'); +app.use('/customers', customersRoutes); + // catch 404 and forward to error handler app.use(function(req, res, next) { var err = new Error('Not Found'); diff --git a/package.json b/package.json index 38e672df4..94eee6ce6 100644 --- a/package.json +++ b/package.json @@ -26,5 +26,17 @@ "jasmine-node": "^1.14.5", "nodemon": "^1.9.2", "request": "^2.72.0" - } + }, + "description": "The overall goal of this project is to create a system that a video store (remember those?) could use to track their inventory of rental videos and their collection of customers.", + "main": "app.js", + "repository": { + "type": "git", + "url": "git+https://github.com/SuzHarrison/VideoStoreAPI.git" + }, + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/SuzHarrison/VideoStoreAPI/issues" + }, + "homepage": "https://github.com/SuzHarrison/VideoStoreAPI#readme" } diff --git a/routes/customers.js b/routes/customers.js new file mode 100644 index 000000000..03a8e2571 --- /dev/null +++ b/routes/customers.js @@ -0,0 +1,36 @@ +var express = require('express'); +var router = express.Router(); + +// GET customers +router.get('/', function(req, res, next) { + res.status(200).json({title: 'List of Customers:'}) +}); + +// GET customers/sort/name?n=10&p=2 +router.get('/sort/', function(req, res, next) { + res.status(200).json({title: 'Sorted List of Customers:'}) +}); + +// GET customers/sort/registered_at +router.get('/sort/registered-at', function(req, res, next) { + res.status(200).json({title: 'List of Customers Sorted by Register Date:'}) +}); + +// GET customers/sort/postal_code +router.get('/sort/postal-code', function(req, res, next) { + res.status(200).json({title: 'List of Customers Sorted by Postal Code:'}) +}); + +// GET customers/:id/current +router.get('/:id/current', function(req, res, next) { + res.status(200).json({title: 'List of Movies the Customer Currently Has Checked-Out:'}) +}); + +// GET customers/:id/history +router.get('/:id/current', function(req, res, next) { + res.status(200).json({title: 'List of Movies the Customer Has Checked-Out in the Past:'}) +}); + + + +module.exports = router From dadef9206723c64190b2f8296aa5b3ae9090fede Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Thu, 16 Jun 2016 14:21:30 -0700 Subject: [PATCH 08/58] added routes for movies and rentals. Added / routes to app.js file for each. working at root. --- app.js | 5 +++++ routes/index.js | 2 +- routes/movies.js | 35 +++++++++++++++++++++++++++++++++++ routes/rentals.js | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 routes/movies.js create mode 100644 routes/rentals.js diff --git a/app.js b/app.js index 0efc1a545..a649faf2a 100644 --- a/app.js +++ b/app.js @@ -26,6 +26,11 @@ app.use('/', routes); var customersRoutes = require('./routes/customers'); app.use('/customers', customersRoutes); +var moviesRoutes = require('./routes/movies'); +app.use('/movies', moviesRoutes); + +var rentalsRoutes = require('./routes/rentals'); +app.use('/rentals', rentalsRoutes); // catch 404 and forward to error handler app.use(function(req, res, next) { var err = new Error('Not Found'); diff --git a/routes/index.js b/routes/index.js index a146f6906..520e874bd 100644 --- a/routes/index.js +++ b/routes/index.js @@ -10,6 +10,7 @@ router.get('/zomg', function(req, res, next) { res.status(200).json({it_works: 'it works!!!'}) }); + /* customers GET customers @@ -39,5 +40,4 @@ GET rentals/overdue */ - module.exports = router; diff --git a/routes/movies.js b/routes/movies.js new file mode 100644 index 000000000..f2bed7aa2 --- /dev/null +++ b/routes/movies.js @@ -0,0 +1,35 @@ +var express = require('express'); +var router = express.Router(); + +// GET movies +router.get('/', function(req, res, next) { + res.status(200).json({title: 'Movies:'}) +}); + +// GET movies/sort/release-date?n=5&p=1 +router.get('/sort/release-date', function(req, res, next) { + res.status(200).json({title: 'Sorted List of Movies by Release Date:'}) +}); + +// GET movies/sort/title +router.get('/sort/title', function(req, res, next) { + res.status(200).json({title: 'List of Movies by Title:'}) +}); + +// GET movies/:title/current +router.get('/:title/current', function(req, res, next) { + res.status(200).json({title: 'List of Customers that have currently checked out a copy of the film:'}) +}); + +// customers that have checked out a copy in the past (/movies/Jaws/history/sort/name) +// GET movies/:title/history/sort/name +router.get('/:title/history/sort/name', function(req, res, next) { + res.status(200).json({title: 'List of Movies the Customer Currently Has Checked-Out:'}) +}); + +// GET movies/:title/history/sort/checkout-date +router.get('/:title/history/sort/checkout-date', function(req, res, next) { + res.status(200).json({title: 'List of Movies the Customer Has Checked-Out in the Past:'}) +}); + +module.exports = router diff --git a/routes/rentals.js b/routes/rentals.js new file mode 100644 index 000000000..5771161cc --- /dev/null +++ b/routes/rentals.js @@ -0,0 +1,38 @@ +var express = require('express'); +var router = express.Router(); + + +// GET rentals +router.get('/', function(req, res, next) { + res.status(200).json({title: 'Rentals:'}) +}); +// GET rentals/:title +// Look a rental up by title +router.get('/:title', function(req, res, next) { + res.status(200).json({title: 'Rentals list by title:'}) +}); + +// GET rentals/:title/customers +// list of customers that have currently checked out any of the movie's inventory +router.get('/:title/customers', function(req, res, next) { + res.status(200).json({title: 'List of customers that have currently checked out any of the movie inventory:'}) +}); + +// POST rentals/:title/check-out +// #provide customerid and movie title +router.post('/:title/check-out', function(req, res, next) { + res.status(200).json({title: '"check out" one of a movies inventory to the customer:'}) +}); + +// POST rentals/:title/return +// #provide customerid and movie title +router.get('/:title/return', function(req, res, next) { + res.status(200).json({title: '"check in" one of a customers rentals:'}) +}); + +// GET rentals/overdue +router.get('/overdue', function(req, res, next) { + res.status(200).json({title: 'See a list of customers with overdue movies:'}) +}); + +module.exports = router From 5a3475effe41d5c7afd38419d2ca72a77faf26da Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Thu, 16 Jun 2016 14:22:26 -0700 Subject: [PATCH 09/58] small change to post (from get) to rentals return route. --- routes/rentals.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routes/rentals.js b/routes/rentals.js index 5771161cc..b75c19baa 100644 --- a/routes/rentals.js +++ b/routes/rentals.js @@ -26,7 +26,7 @@ router.post('/:title/check-out', function(req, res, next) { // POST rentals/:title/return // #provide customerid and movie title -router.get('/:title/return', function(req, res, next) { +router.post('/:title/return', function(req, res, next) { res.status(200).json({title: '"check in" one of a customers rentals:'}) }); From d522894e5cbb03aaacebc9ee954be1ee30fae432 Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Thu, 16 Jun 2016 14:28:41 -0700 Subject: [PATCH 10/58] small change to customer route for history. --- routes/customers.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routes/customers.js b/routes/customers.js index 03a8e2571..6f12760b9 100644 --- a/routes/customers.js +++ b/routes/customers.js @@ -27,7 +27,7 @@ router.get('/:id/current', function(req, res, next) { }); // GET customers/:id/history -router.get('/:id/current', function(req, res, next) { +router.get('/:id/history', function(req, res, next) { res.status(200).json({title: 'List of Movies the Customer Has Checked-Out in the Past:'}) }); From 852f8074ad47ed9ea1175abc5bae0354b3524d6d Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Thu, 16 Jun 2016 14:32:08 -0700 Subject: [PATCH 11/58] added contollers folder and movies_controller. --- controllers/movies_controller.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 controllers/movies_controller.js diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js new file mode 100644 index 000000000..e69de29bb From bacbd69b62b4c44787c045346815ee6d4b6da0df Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Thu, 16 Jun 2016 15:51:07 -0700 Subject: [PATCH 12/58] made changes to movies route file calling on controller functions. --- app.js | 5 ++++ controllers/movies_controller.js | 19 ++++++++++++++++ models/movie.js | 39 ++++++++++++++++++++++++++++++++ routes/movies.js | 37 +++++------------------------- 4 files changed, 69 insertions(+), 31 deletions(-) create mode 100644 models/movie.js diff --git a/app.js b/app.js index a649faf2a..2c32c6137 100644 --- a/app.js +++ b/app.js @@ -4,10 +4,15 @@ var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); +var massive = require('massive'); var routes = require('./routes/index'); var app = express(); +// database setup +var connectionString = "postgres://localhost/bank-accounts_" + app.get('env'); +var db = massive.connectSync({connectionString: connectionString}); +app.set('db', db); // view engine setup app.set('views', path.join(__dirname, 'views')); diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index e69de29bb..097d7936b 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -0,0 +1,19 @@ +var Movie = require("../models/movie"); + +var MoviesController = { + index: function(req, res, next) { + // giving a callback function to handle error or render view + Movie.all(function(error, movies) { + if(error) { + var err = new Error("Error retrieving movie list:\n" + error.message); + err.status = 500; + next(err); + } else { + res.json(movies) + // var locals = { movies: movies } + // res.render("movies/index", locals); + } + }); + }; + + module.exports = MoviesController; diff --git a/models/movie.js b/models/movie.js new file mode 100644 index 000000000..be2ebe0f5 --- /dev/null +++ b/models/movie.js @@ -0,0 +1,39 @@ +var app = require("../../app") +var db = app.get("db") + +// takes on parameter(callback)-then run db.accounts.find +Movie.all = function(callback) { + // then run db.accounts.find(no specific id or column - just another callback) + db.movies.find('*', function(error, movies) { + if(error || !movies) { + // handling any error + callback(error || new Error("Could not retrieve movies"), undefined); + } else { + // saying there was no error, accounts is an array and we map it + callback(null, movies.map(function(movie) { + // and return to a new instance of the account with id + return new Movie(movie.id); + })); + }; + }); +}; + +// Movie.find = function(id, callback) { +// db.movies.findOne({id: id}, function(error, movie) { +// if(error || !movie) { +// callback(error || new Error("Movie not found"), undefined); +// } else { +// callback(null, new Movie(movie.id)); +// } +// }) +// } + +// 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/routes/movies.js b/routes/movies.js index f2bed7aa2..dd222b269 100644 --- a/routes/movies.js +++ b/routes/movies.js @@ -1,35 +1,10 @@ var express = require('express'); var router = express.Router(); +var MoviesController = require('../controllers/movies_controller') -// GET movies -router.get('/', function(req, res, next) { - res.status(200).json({title: 'Movies:'}) -}); +router.get('/', MoviesController.listMovies) +router.get('/sort/:query', MoviesController.sortBy) +router.get('/:movie/current', MoviesController.current) +router.get('/:movie/history/sort/:by', MoviesController.sortByHistory) -// GET movies/sort/release-date?n=5&p=1 -router.get('/sort/release-date', function(req, res, next) { - res.status(200).json({title: 'Sorted List of Movies by Release Date:'}) -}); - -// GET movies/sort/title -router.get('/sort/title', function(req, res, next) { - res.status(200).json({title: 'List of Movies by Title:'}) -}); - -// GET movies/:title/current -router.get('/:title/current', function(req, res, next) { - res.status(200).json({title: 'List of Customers that have currently checked out a copy of the film:'}) -}); - -// customers that have checked out a copy in the past (/movies/Jaws/history/sort/name) -// GET movies/:title/history/sort/name -router.get('/:title/history/sort/name', function(req, res, next) { - res.status(200).json({title: 'List of Movies the Customer Currently Has Checked-Out:'}) -}); - -// GET movies/:title/history/sort/checkout-date -router.get('/:title/history/sort/checkout-date', function(req, res, next) { - res.status(200).json({title: 'List of Movies the Customer Has Checked-Out in the Past:'}) -}); - -module.exports = router +module.exports = router; From 635b981af79ccb8813db5fcf34d93dba2272267c Mon Sep 17 00:00:00 2001 From: Ania Gonzalez Date: Thu, 16 Jun 2016 16:02:40 -0700 Subject: [PATCH 13/58] Added customer routes --- controllers/customers_controller.js | 0 routes/customers.js | 29 +++++++++-------------------- 2 files changed, 9 insertions(+), 20 deletions(-) create mode 100644 controllers/customers_controller.js diff --git a/controllers/customers_controller.js b/controllers/customers_controller.js new file mode 100644 index 000000000..e69de29bb diff --git a/routes/customers.js b/routes/customers.js index 6f12760b9..6b2e7f8aa 100644 --- a/routes/customers.js +++ b/routes/customers.js @@ -1,36 +1,25 @@ var express = require('express'); var router = express.Router(); -// GET customers -router.get('/', function(req, res, next) { - res.status(200).json({title: 'List of Customers:'}) -}); +var Controller = require('../controllers/customers_controller') + +// GET customers // +router.get('/', Controller.getCustomers) // GET customers/sort/name?n=10&p=2 -router.get('/sort/', function(req, res, next) { - res.status(200).json({title: 'Sorted List of Customers:'}) -}); +router.get('/sort/name', Controller.sortName) // GET customers/sort/registered_at -router.get('/sort/registered-at', function(req, res, next) { - res.status(200).json({title: 'List of Customers Sorted by Register Date:'}) -}); +router.get('/sort/registered-at', Controller.sortRegistered) // GET customers/sort/postal_code -router.get('/sort/postal-code', function(req, res, next) { - res.status(200).json({title: 'List of Customers Sorted by Postal Code:'}) -}); +router.get('/sort/postal-code', Controller.sortPostal) // GET customers/:id/current -router.get('/:id/current', function(req, res, next) { - res.status(200).json({title: 'List of Movies the Customer Currently Has Checked-Out:'}) -}); +router.get('/:id/current', Controller.customerCurrent) // GET customers/:id/history -router.get('/:id/history', function(req, res, next) { - res.status(200).json({title: 'List of Movies the Customer Has Checked-Out in the Past:'}) -}); - +router.get('/:id/history', Controller.customerHistory) module.exports = router From 3f45045cf6fc2751cb91acd8b1bdf34aa55cfbff Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Thu, 16 Jun 2016 16:13:24 -0700 Subject: [PATCH 14/58] changed movies route dynamic from name to title. Started redoing rentals routes. Opened a rentals_controller.js. --- controllers/movies_controller.js | 3 ++- controllers/rentals_controller.js | 0 models/movie.js | 2 +- routes/movies.js | 6 ++--- routes/rentals.js | 39 +++++-------------------------- 5 files changed, 12 insertions(+), 38 deletions(-) create mode 100644 controllers/rentals_controller.js diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index 097d7936b..a64a17512 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -14,6 +14,7 @@ var MoviesController = { // res.render("movies/index", locals); } }); - }; + } +} module.exports = MoviesController; diff --git a/controllers/rentals_controller.js b/controllers/rentals_controller.js new file mode 100644 index 000000000..e69de29bb diff --git a/models/movie.js b/models/movie.js index be2ebe0f5..505fad75d 100644 --- a/models/movie.js +++ b/models/movie.js @@ -1,4 +1,4 @@ -var app = require("../../app") +var app = require("../app") var db = app.get("db") // takes on parameter(callback)-then run db.accounts.find diff --git a/routes/movies.js b/routes/movies.js index dd222b269..8db3c8041 100644 --- a/routes/movies.js +++ b/routes/movies.js @@ -1,10 +1,10 @@ var express = require('express'); var router = express.Router(); -var MoviesController = require('../controllers/movies_controller') +var MoviesController = require('../controllers/movies_controller.js'); router.get('/', MoviesController.listMovies) router.get('/sort/:query', MoviesController.sortBy) -router.get('/:movie/current', MoviesController.current) -router.get('/:movie/history/sort/:by', MoviesController.sortByHistory) +router.get('/:title/current', MoviesController.current) +router.get('/:title/history/sort/:by', MoviesController.sortByHistory) module.exports = router; diff --git a/routes/rentals.js b/routes/rentals.js index b75c19baa..e9a0d84b5 100644 --- a/routes/rentals.js +++ b/routes/rentals.js @@ -1,38 +1,11 @@ var express = require('express'); var router = express.Router(); +var RentalsController = require('../controllers/rentals_controller.js'); +/* GET customers listing. */ +router.get('/', RentalsController.index); -// GET rentals -router.get('/', function(req, res, next) { - res.status(200).json({title: 'Rentals:'}) -}); -// GET rentals/:title -// Look a rental up by title -router.get('/:title', function(req, res, next) { - res.status(200).json({title: 'Rentals list by title:'}) -}); +/* GET customers details. */ +/* router.get('/:id', RentalsController.show); */ -// GET rentals/:title/customers -// list of customers that have currently checked out any of the movie's inventory -router.get('/:title/customers', function(req, res, next) { - res.status(200).json({title: 'List of customers that have currently checked out any of the movie inventory:'}) -}); - -// POST rentals/:title/check-out -// #provide customerid and movie title -router.post('/:title/check-out', function(req, res, next) { - res.status(200).json({title: '"check out" one of a movies inventory to the customer:'}) -}); - -// POST rentals/:title/return -// #provide customerid and movie title -router.post('/:title/return', function(req, res, next) { - res.status(200).json({title: '"check in" one of a customers rentals:'}) -}); - -// GET rentals/overdue -router.get('/overdue', function(req, res, next) { - res.status(200).json({title: 'See a list of customers with overdue movies:'}) -}); - -module.exports = router +module.exports = router; From 9c4d9a7675ec7fdaf7db6b8f108a19885a043840 Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Thu, 16 Jun 2016 16:28:48 -0700 Subject: [PATCH 15/58] finished redoing the routes for rentals. --- routes/rentals.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/routes/rentals.js b/routes/rentals.js index e9a0d84b5..e9769775d 100644 --- a/routes/rentals.js +++ b/routes/rentals.js @@ -2,10 +2,24 @@ var express = require('express'); var router = express.Router(); var RentalsController = require('../controllers/rentals_controller.js'); -/* GET customers listing. */ -router.get('/', RentalsController.index); +// GET rentals/:title +// Look a rental up by title +router.get('/:title', RentalsController.findMovie) -/* GET customers details. */ -/* router.get('/:id', RentalsController.show); */ +// GET rentals/:title/customers +// list of customers that have currently checked out any of the movie's inventory +router.get('/:title/customers', RentalsController.sortBy) + +// POST rentals/:title/check-out +// #provide customerid and movie title +router.post('/:title/check-out', RentalsController.checkout) + +// POST rentals/:title/return +// #provide customerid and movie title +router.post('/:title/return', RentalsController.return) + +// GET rentals/overdue +// list of customers with overdue movies +router.get('/overdue', RentalsController.overdue) module.exports = router; From 1b78096189a4d587b47e72df0ea45b00b4c4a786 Mon Sep 17 00:00:00 2001 From: Ania Gonzalez Date: Thu, 16 Jun 2016 16:39:48 -0700 Subject: [PATCH 16/58] basics for the customers controller --- controllers/customers_controller.js | 38 +++++++++++++++++++++++++++++ models/customer.js | 0 2 files changed, 38 insertions(+) create mode 100644 models/customer.js diff --git a/controllers/customers_controller.js b/controllers/customers_controller.js index e69de29bb..e229cc79a 100644 --- a/controllers/customers_controller.js +++ b/controllers/customers_controller.js @@ -0,0 +1,38 @@ +var Customer = require("../models/customer"); + +var CustomersController = { + + getCustomers: function(req, res, next) { + // giving a callback function to handle error or render view + Customer.all(function(error, customers) { + if(error) { + var err = new Error("Error retrieving customer list:\n" + error.message); + err.status = 500; + next(err); + } else { + res.json(customers) + } + }); + }, + + sortName: function(req, res, next) { + + }, + + sortRegistered: function(req, res, next) { + + }, + + sortPostal: function(req, res, next) { + + }, + + customerCurrent: function(req, res, next) { + + }, + + customerHistory: function(req, res, next) { + + } + +module.exports = CustomersController diff --git a/models/customer.js b/models/customer.js new file mode 100644 index 000000000..e69de29bb From 661e237c5b2164af2211ed458e9902288490e8e3 Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Thu, 16 Jun 2016 16:50:17 -0700 Subject: [PATCH 17/58] added mostly empty functions to movies_controller.js. --- controllers/movies_controller.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index a64a17512..6b5a89710 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -1,7 +1,7 @@ var Movie = require("../models/movie"); var MoviesController = { - index: function(req, res, next) { + listMovies: function(req, res, next) { // giving a callback function to handle error or render view Movie.all(function(error, movies) { if(error) { @@ -14,7 +14,18 @@ var MoviesController = { // res.render("movies/index", locals); } }); + }, + + sortBy: function() { + + }, + + current: function() { + + }, + + sortedHistory: function() { + } } - - module.exports = MoviesController; +module.exports = MoviesController; From db9f16070012371922c8f05c1786208bb5a02362 Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Thu, 16 Jun 2016 16:55:34 -0700 Subject: [PATCH 18/58] adding parameters to the new functions. --- controllers/movies_controller.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index 6b5a89710..57d177fc1 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -1,4 +1,4 @@ -var Movie = require("../models/movie"); +var Movie = require("../models/movie.js"); var MoviesController = { listMovies: function(req, res, next) { @@ -16,15 +16,15 @@ var MoviesController = { }); }, - sortBy: function() { + sortBy: function(req, res, next) { }, - current: function() { + current: function(req, res, next) { }, - sortedHistory: function() { + sortedHistory: function(req, res, next) { } } From 2255b0b01d1071808279d56ed367ff2a412d6bce Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Thu, 16 Jun 2016 16:56:58 -0700 Subject: [PATCH 19/58] small change to function name. --- controllers/movies_controller.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index 57d177fc1..b70de87aa 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -24,7 +24,7 @@ var MoviesController = { }, - sortedHistory: function(req, res, next) { + sortByHistory: function(req, res, next) { } } From 3841493b729faf41425dcfa9b5b8f3c23012e61f Mon Sep 17 00:00:00 2001 From: Ania Gonzalez Date: Thu, 16 Jun 2016 17:01:22 -0700 Subject: [PATCH 20/58] Created customer model --- models/customer.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/models/customer.js b/models/customer.js index e69de29bb..646694ac9 100644 --- a/models/customer.js +++ b/models/customer.js @@ -0,0 +1,19 @@ +ar app = require("../app"); +var db = app.get("db"); + +// takes on parameter(callback)-then run db.accounts.find +Customer.all = function(callback) { + // then run db.accounts.find(no specific id or column - just another callback) + db.customers.find('*', function(error, customers) { + if(error || !customers) { + // handling any error + callback(error || new Error("Could not retrieve customers"), undefined); + } else { + // saying there was no error, accounts is an array and we map it + callback(null, customers.map(function(customer) { + // and return to a new instance of the account with id + return new Customer(customer.id); + })); + }; + }); +}; From 7b66c21436cf649c081903ee260d0a23667e0531 Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Thu, 16 Jun 2016 22:59:41 -0700 Subject: [PATCH 21/58] fixed circular app.get bug. added more to the movie model constructor. --- app.js | 5 ++-- controllers/customers_controller.js | 4 +-- controllers/movies_controller.js | 2 +- models/movie.js | 21 ++++++++++----- routes/rentals.js | 40 ++++++++++++++--------------- 5 files changed, 39 insertions(+), 33 deletions(-) diff --git a/app.js b/app.js index 2c32c6137..22eb3cedd 100644 --- a/app.js +++ b/app.js @@ -8,6 +8,7 @@ var massive = require('massive'); var routes = require('./routes/index'); var app = express(); +module.exports = app; // database setup var connectionString = "postgres://localhost/bank-accounts_" + app.get('env'); @@ -31,6 +32,7 @@ app.use('/', routes); var customersRoutes = require('./routes/customers'); app.use('/customers', customersRoutes); + var moviesRoutes = require('./routes/movies'); app.use('/movies', moviesRoutes); @@ -66,6 +68,3 @@ app.use(function(err, req, res, next) { error: {} }); }); - - -module.exports = app; diff --git a/controllers/customers_controller.js b/controllers/customers_controller.js index e229cc79a..bb642c65d 100644 --- a/controllers/customers_controller.js +++ b/controllers/customers_controller.js @@ -32,7 +32,7 @@ var CustomersController = { }, customerHistory: function(req, res, next) { - - } + } +} module.exports = CustomersController diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index b70de87aa..032e008a0 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -1,4 +1,4 @@ -var Movie = require("../models/movie.js"); +var Movie = require("../models/movie"); var MoviesController = { listMovies: function(req, res, next) { diff --git a/models/movie.js b/models/movie.js index 505fad75d..bb30483b5 100644 --- a/models/movie.js +++ b/models/movie.js @@ -1,10 +1,17 @@ var app = require("../app") var db = app.get("db") +var Movie = function(id) { + this.id = id, + this.title = title, + this.release_date = release_date, + this.inventory = inventory +} + // takes on parameter(callback)-then run db.accounts.find Movie.all = function(callback) { // then run db.accounts.find(no specific id or column - just another callback) - db.movies.find('*', function(error, movies) { + db.movies.find(function(error, movies) { if(error || !movies) { // handling any error callback(error || new Error("Could not retrieve movies"), undefined); @@ -29,11 +36,11 @@ Movie.all = function(callback) { // } // 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() - } -} +// if (app.get('env') === 'test') { +// Movie.close_connection = function() { +// console.log("closing connection") +// db.end() +// } +// } module.exports = Movie; diff --git a/routes/rentals.js b/routes/rentals.js index e9769775d..ecb5f9472 100644 --- a/routes/rentals.js +++ b/routes/rentals.js @@ -1,25 +1,25 @@ var express = require('express'); var router = express.Router(); var RentalsController = require('../controllers/rentals_controller.js'); - -// GET rentals/:title -// Look a rental up by title -router.get('/:title', RentalsController.findMovie) - -// GET rentals/:title/customers -// list of customers that have currently checked out any of the movie's inventory -router.get('/:title/customers', RentalsController.sortBy) - -// POST rentals/:title/check-out -// #provide customerid and movie title -router.post('/:title/check-out', RentalsController.checkout) - -// POST rentals/:title/return -// #provide customerid and movie title -router.post('/:title/return', RentalsController.return) - -// GET rentals/overdue -// list of customers with overdue movies -router.get('/overdue', RentalsController.overdue) +// +// // GET rentals/:title +// // Look a rental up by title +// router.get('/:title', RentalsController.findMovie) +// +// // GET rentals/:title/customers +// // list of customers that have currently checked out any of the movie's inventory +// router.get('/:title/customers', RentalsController.sortBy) +// +// // POST rentals/:title/check-out +// // #provide customerid and movie title +// router.post('/:title/check-out', RentalsController.checkout) +// +// // POST rentals/:title/return +// // #provide customerid and movie title +// router.post('/:title/return', RentalsController.return) +// +// // GET rentals/overdue +// // list of customers with overdue movies +// router.get('/overdue', RentalsController.overdue) module.exports = router; From 4a043b307d83720bf212978fca5a36803fcb4a81 Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Fri, 17 Jun 2016 09:52:03 -0700 Subject: [PATCH 22/58] pushing changes to movie model. added this.title and this.id, as well as release_date and inventory. --- models/movie.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/models/movie.js b/models/movie.js index bb30483b5..0a6618f3a 100644 --- a/models/movie.js +++ b/models/movie.js @@ -1,11 +1,11 @@ -var app = require("../app") -var db = app.get("db") +var app = require("../app"); +var db = app.get("db"); var Movie = function(id) { - this.id = id, - this.title = title, - this.release_date = release_date, - this.inventory = inventory + this.id = id; + this.title = title; + this.release_date = release_date; + this.inventory = inventory; } // takes on parameter(callback)-then run db.accounts.find From 0f16284d24473e0b9ab3669d66d4f5c3b9482d95 Mon Sep 17 00:00:00 2001 From: Ania Gonzalez Date: Fri, 17 Jun 2016 09:55:58 -0700 Subject: [PATCH 23/58] export modules missing --- models/customer.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/models/customer.js b/models/customer.js index 646694ac9..9f2da012c 100644 --- a/models/customer.js +++ b/models/customer.js @@ -17,3 +17,6 @@ Customer.all = function(callback) { }; }); }; + + +module.exports = Customer; From 06a287e09939abd2cbb2b8feede1c736aee3654f Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Fri, 17 Jun 2016 10:29:50 -0700 Subject: [PATCH 24/58] added _development to all references to video_store_api in seed scripts and in package.json. server wokin again. --- app.js | 2 +- package.json | 4 ++-- routes/movies.js | 6 +++--- tasks/load_schema.js | 2 +- tasks/seed_data.js | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app.js b/app.js index 22eb3cedd..6cb46b80a 100644 --- a/app.js +++ b/app.js @@ -11,7 +11,7 @@ var app = express(); module.exports = app; // database setup -var connectionString = "postgres://localhost/bank-accounts_" + app.get('env'); +var connectionString = "postgres://localhost/video_store_api_" + app.get('env'); var db = massive.connectSync({connectionString: connectionString}); app.set('db', db); diff --git a/package.json b/package.json index 94eee6ce6..6b5de1633 100644 --- a/package.json +++ b/package.json @@ -5,8 +5,8 @@ "scripts": { "start": "nodemon ./bin/www", "test": "clear; jasmine-node --verbose spec/", - "db:drop": "dropdb video_store_api", - "db:create": "createdb video_store_api", + "db:drop": "dropdb video_store_api_development", + "db:create": "createdb video_store_api_development", "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; npm run db:seed" diff --git a/routes/movies.js b/routes/movies.js index 8db3c8041..a596f1d15 100644 --- a/routes/movies.js +++ b/routes/movies.js @@ -3,8 +3,8 @@ var router = express.Router(); var MoviesController = require('../controllers/movies_controller.js'); router.get('/', MoviesController.listMovies) -router.get('/sort/:query', MoviesController.sortBy) -router.get('/:title/current', MoviesController.current) -router.get('/:title/history/sort/:by', MoviesController.sortByHistory) +// router.get('/sort/:query', MoviesController.sortBy) +// router.get('/:title/current', MoviesController.current) +// router.get('/:title/history/sort/:by', MoviesController.sortByHistory) module.exports = router; diff --git a/tasks/load_schema.js b/tasks/load_schema.js index 95bb6c87b..75fffcb22 100644 --- a/tasks/load_schema.js +++ b/tasks/load_schema.js @@ -1,5 +1,5 @@ var massive = require('massive') -var connectionString = "postgres://localhost/video_store_api" +var connectionString = "postgres://localhost/video_store_api_development" var db = massive.connectSync({connectionString : connectionString}) diff --git a/tasks/seed_data.js b/tasks/seed_data.js index de66d0d58..ee6322622 100644 --- a/tasks/seed_data.js +++ b/tasks/seed_data.js @@ -1,5 +1,5 @@ var massive = require('massive') -var connectionString = "postgres://localhost/video_store_api" +var connectionString = "postgres://localhost/video_store_api_development" var db = massive.connectSync({connectionString : connectionString}) From 24b3e0f2bd8e95f88598d056b799055f8d187318 Mon Sep 17 00:00:00 2001 From: Ania Gonzalez Date: Fri, 17 Jun 2016 10:31:40 -0700 Subject: [PATCH 25/58] Fixed typo Please enter the commit message for #your changes. Lines starting --- models/customer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/customer.js b/models/customer.js index 9f2da012c..6ee2e5283 100644 --- a/models/customer.js +++ b/models/customer.js @@ -1,4 +1,4 @@ -ar app = require("../app"); +var app = require("../app"); var db = app.get("db"); // takes on parameter(callback)-then run db.accounts.find From 1de9c8f753d13cabe28bf6ef5bc3e13eaa96f4d4 Mon Sep 17 00:00:00 2001 From: Ania Gonzalez Date: Fri, 17 Jun 2016 10:34:38 -0700 Subject: [PATCH 26/58] Fixed Customer model --- models/customer.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/models/customer.js b/models/customer.js index 6ee2e5283..dd70003a4 100644 --- a/models/customer.js +++ b/models/customer.js @@ -1,6 +1,19 @@ var app = require("../app"); var db = app.get("db"); + +var Customer = function(id) { + this.id = id + this.name = name + this.registered_at = registered_at + this.address = address + this.city = city + this.state = state + this.postal_code = postal_code + this.phone = phone + this.account_credit = account_credit +} + // takes on parameter(callback)-then run db.accounts.find Customer.all = function(callback) { // then run db.accounts.find(no specific id or column - just another callback) From e4650009a122accfcc144eccea084406652fe5cb Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Fri, 17 Jun 2016 10:49:51 -0700 Subject: [PATCH 27/58] added properties to Movie model. now correctly getting json hash with info. --- models/movie.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/models/movie.js b/models/movie.js index 0a6618f3a..29d60769c 100644 --- a/models/movie.js +++ b/models/movie.js @@ -1,11 +1,12 @@ var app = require("../app"); var db = app.get("db"); -var Movie = function(id) { - this.id = id; - this.title = title; - this.release_date = release_date; - this.inventory = inventory; +var Movie = function(movie) { + this.id = movie.id; + this.title = movie.title; + this.overview = movie.overview; + this.release_date = movie.release_date; + this.inventory = movie.inventory; } // takes on parameter(callback)-then run db.accounts.find @@ -19,7 +20,7 @@ Movie.all = function(callback) { // saying there was no error, accounts is an array and we map it callback(null, movies.map(function(movie) { // and return to a new instance of the account with id - return new Movie(movie.id); + return new Movie(movie); })); }; }); From c9a803dbad8f6acc5f77bdbed122f1b6d83ba070 Mon Sep 17 00:00:00 2001 From: Ania Gonzalez Date: Fri, 17 Jun 2016 10:51:07 -0700 Subject: [PATCH 28/58] got Customers.all working --- models/customer.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/models/customer.js b/models/customer.js index dd70003a4..842828637 100644 --- a/models/customer.js +++ b/models/customer.js @@ -2,22 +2,22 @@ var app = require("../app"); var db = app.get("db"); -var Customer = function(id) { - this.id = id - this.name = name - this.registered_at = registered_at - this.address = address - this.city = city - this.state = state - this.postal_code = postal_code - this.phone = phone - this.account_credit = account_credit +var Customer = function(customer) { + this.id = customer.id + this.name = customer.name + this.registered_at = customer.registered_at + this.address = customer.address + this.city = customer.city + this.state = customer.state + this.postal_code = customer.postal_code + this.phone = customer.phone + this.account_credit = customer.account_credit } // takes on parameter(callback)-then run db.accounts.find Customer.all = function(callback) { // then run db.accounts.find(no specific id or column - just another callback) - db.customers.find('*', function(error, customers) { + db.customers.find(function(error, customers) { if(error || !customers) { // handling any error callback(error || new Error("Could not retrieve customers"), undefined); @@ -25,7 +25,7 @@ Customer.all = function(callback) { // saying there was no error, accounts is an array and we map it callback(null, customers.map(function(customer) { // and return to a new instance of the account with id - return new Customer(customer.id); + return new Customer(customer); })); }; }); From 9ba25d6753df532a429bfb281a57bc91b3c70572 Mon Sep 17 00:00:00 2001 From: Ania Gonzalez Date: Fri, 17 Jun 2016 13:11:50 -0700 Subject: [PATCH 29/58] route with query for customers sort --- controllers/customers_controller.js | 12 ++++++++++++ routes/customers.js | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/controllers/customers_controller.js b/controllers/customers_controller.js index bb642c65d..0f4aea84e 100644 --- a/controllers/customers_controller.js +++ b/controllers/customers_controller.js @@ -16,6 +16,18 @@ var CustomersController = { }, sortName: function(req, res, next) { + var n = req.query.n; + var p = req.query.p; + + Customer.all(function(error, customers) { + if(error) { + var err = new Error("Error retrieving customer list:\n" + error.message); + err.status = 500; + next(err); + } else { + res.json(n) + } + }); }, diff --git a/routes/customers.js b/routes/customers.js index 6b2e7f8aa..91aff044d 100644 --- a/routes/customers.js +++ b/routes/customers.js @@ -7,7 +7,7 @@ var Controller = require('../controllers/customers_controller') router.get('/', Controller.getCustomers) // GET customers/sort/name?n=10&p=2 -router.get('/sort/name', Controller.sortName) +router.get('/sort/name*', Controller.sortName) // GET customers/sort/registered_at router.get('/sort/registered-at', Controller.sortRegistered) From 2af017c008b8eb48847c5149aef90ea6d46f0da1 Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Fri, 17 Jun 2016 13:13:51 -0700 Subject: [PATCH 30/58] working on movies query routes and model. --- controllers/movies_controller.js | 17 +++++++++++++++-- models/movie.js | 20 +++++++++++--------- routes/movies.js | 2 +- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index 032e008a0..e650efa93 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -17,8 +17,21 @@ var MoviesController = { }, sortBy: function(req, res, next) { - - }, + var n = req.query.n; + var p = req.query.p; + // giving a callback function to handle error or render view + Movie.all(field, function(error, movies) { + if(error) { + var err = new Error("Error retrieving sorted movie list:\n" + error.message); + err.status = 500; + next(err); + } else { + res.json(n) + // var locals = { movies: movies } + // res.render("movies/index", locals); + } + }) + }, current: function(req, res, next) { diff --git a/models/movie.js b/models/movie.js index 29d60769c..c57b1f482 100644 --- a/models/movie.js +++ b/models/movie.js @@ -26,15 +26,17 @@ Movie.all = function(callback) { }); }; -// Movie.find = function(id, callback) { -// db.movies.findOne({id: id}, function(error, movie) { -// if(error || !movie) { -// callback(error || new Error("Movie not found"), undefined); -// } else { -// callback(null, new Movie(movie.id)); -// } -// }) -// } +Movie.sortBy = function(field, callback) { + // first parameter is the + db.movies.run("SELECT * ???????????????????????", function(error, movies) { + // ("Select * From (Select Row_Number() Over (Order By " + order + ") As RowNum, *From movies) movies Where RowNum BETWEEN $1 AND $2;",input, function(error, movies) + if(error || !movie) { + callback(error || new Error("Movie not found"), undefined); + } else { + callback(null, new Movie(movie.id)); + } + }) +} // only attach this function if we're in test mode. // if (app.get('env') === 'test') { diff --git a/routes/movies.js b/routes/movies.js index a596f1d15..243734ce7 100644 --- a/routes/movies.js +++ b/routes/movies.js @@ -3,7 +3,7 @@ var router = express.Router(); var MoviesController = require('../controllers/movies_controller.js'); router.get('/', MoviesController.listMovies) -// router.get('/sort/:query', MoviesController.sortBy) +router.get('/sort/field*', MoviesController.sortBy) // router.get('/:title/current', MoviesController.current) // router.get('/:title/history/sort/:by', MoviesController.sortByHistory) From 70e300581a415115910b16199b1142fd470b4360 Mon Sep 17 00:00:00 2001 From: Ania Gonzalez Date: Fri, 17 Jun 2016 13:37:56 -0700 Subject: [PATCH 31/58] Customers can be sorted by name --- controllers/customers_controller.js | 4 ++-- models/customer.js | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/controllers/customers_controller.js b/controllers/customers_controller.js index 0f4aea84e..b79628fbb 100644 --- a/controllers/customers_controller.js +++ b/controllers/customers_controller.js @@ -19,13 +19,13 @@ var CustomersController = { var n = req.query.n; var p = req.query.p; - Customer.all(function(error, customers) { + Customer.sortByName([n, p], function(error, customers) { if(error) { var err = new Error("Error retrieving customer list:\n" + error.message); err.status = 500; next(err); } else { - res.json(n) + res.json(customers) } }); diff --git a/models/customer.js b/models/customer.js index 842828637..f9d2b4c9b 100644 --- a/models/customer.js +++ b/models/customer.js @@ -31,5 +31,17 @@ Customer.all = function(callback) { }); }; +Customer.sortByName = function(input, callback) { + db.run ("SELECT * FROM customers ORDER BY name;", function(error, customers) { + if(error || !customers) { + callback(error || new Error("Could not retrieve customers"), undefined); + } else { + callback(null, customers.map(function(customer) { + return new Customer(customer); + })); + }; + }); +} + module.exports = Customer; From 93acdf2291bd54fd89046ee2499c97a15d6a0bb0 Mon Sep 17 00:00:00 2001 From: Ania Gonzalez Date: Fri, 17 Jun 2016 14:40:44 -0700 Subject: [PATCH 32/58] customers sorted by name with limit and offset --- models/customer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/customer.js b/models/customer.js index f9d2b4c9b..2b9aa90b9 100644 --- a/models/customer.js +++ b/models/customer.js @@ -32,7 +32,7 @@ Customer.all = function(callback) { }; Customer.sortByName = function(input, callback) { - db.run ("SELECT * FROM customers ORDER BY name;", function(error, customers) { + db.run ("SELECT * FROM customers ORDER BY name LIMIT $1 OFFSET $2;", input, function(error, customers) { if(error || !customers) { callback(error || new Error("Could not retrieve customers"), undefined); } else { From e36789132dab35874c2c5760cbe5d2b50c021914 Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Fri, 17 Jun 2016 14:40:57 -0700 Subject: [PATCH 33/58] finally got sort by relase to work. --- controllers/movies_controller.js | 27 ++++++++++++++------------- models/movie.js | 19 ++++++++++--------- routes/movies.js | 2 +- 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index e650efa93..dc4fc8444 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -16,29 +16,30 @@ var MoviesController = { }); }, - sortBy: function(req, res, next) { + sortByRelease: function(req, res, next) { + // var type = req.params.query; var n = req.query.n; var p = req.query.p; - // giving a callback function to handle error or render view - Movie.all(field, function(error, movies) { + + Movie.sortByRelease([n, p], function(error, movies) { if(error) { var err = new Error("Error retrieving sorted movie list:\n" + error.message); err.status = 500; next(err); } else { - res.json(n) + res.json(movies) // var locals = { movies: movies } // res.render("movies/index", locals); } - }) - }, - - current: function(req, res, next) { - - }, - - sortByHistory: function(req, res, next) { + }); + }, - } + // current: function(req, res, next) { + // + // }, + // + // sortByHistory: function(req, res, next) { + // + // } } module.exports = MoviesController; diff --git a/models/movie.js b/models/movie.js index c57b1f482..96ee62e0d 100644 --- a/models/movie.js +++ b/models/movie.js @@ -26,17 +26,18 @@ Movie.all = function(callback) { }); }; -Movie.sortBy = function(field, callback) { +Movie.sortByRelease = function(input, callback) { // first parameter is the - db.movies.run("SELECT * ???????????????????????", function(error, movies) { - // ("Select * From (Select Row_Number() Over (Order By " + order + ") As RowNum, *From movies) movies Where RowNum BETWEEN $1 AND $2;",input, function(error, movies) - if(error || !movie) { - callback(error || new Error("Movie not found"), undefined); + db.run("SELECT * FROM movies ORDER BY release_date LIMIT $1 OFFSET $2;", input, function(error, movies) { + if(error || !movies) { + callback(error || new Error("Movies not found"), undefined); } else { - callback(null, new Movie(movie.id)); - } - }) -} + callback(null, movies.map(function(movie) { + return new Movie(movie) + })); + }; + }); +}; // only attach this function if we're in test mode. // if (app.get('env') === 'test') { diff --git a/routes/movies.js b/routes/movies.js index 243734ce7..eecd6554a 100644 --- a/routes/movies.js +++ b/routes/movies.js @@ -3,7 +3,7 @@ var router = express.Router(); var MoviesController = require('../controllers/movies_controller.js'); router.get('/', MoviesController.listMovies) -router.get('/sort/field*', MoviesController.sortBy) +router.get('/sort/release-date*', MoviesController.sortByRelease) // router.get('/:title/current', MoviesController.current) // router.get('/:title/history/sort/:by', MoviesController.sortByHistory) From ed3d48d3ffbc782070d742c09d894812baf37dad Mon Sep 17 00:00:00 2001 From: Ania Gonzalez Date: Fri, 17 Jun 2016 15:21:45 -0700 Subject: [PATCH 34/58] Refactored sql query from run to find --- controllers/customers_controller.js | 13 +++---------- models/customer.js | 8 ++++++-- routes/customers.js | 8 +------- 3 files changed, 10 insertions(+), 19 deletions(-) diff --git a/controllers/customers_controller.js b/controllers/customers_controller.js index b79628fbb..fbe5a99ce 100644 --- a/controllers/customers_controller.js +++ b/controllers/customers_controller.js @@ -15,11 +15,12 @@ var CustomersController = { }); }, - sortName: function(req, res, next) { + sortBy: function(req, res, next) { var n = req.query.n; var p = req.query.p; + var field = String(req.params.field) - Customer.sortByName([n, p], function(error, customers) { + Customer.sortBy(field, n, p, function(error, customers) { if(error) { var err = new Error("Error retrieving customer list:\n" + error.message); err.status = 500; @@ -31,14 +32,6 @@ var CustomersController = { }, - sortRegistered: function(req, res, next) { - - }, - - sortPostal: function(req, res, next) { - - }, - customerCurrent: function(req, res, next) { }, diff --git a/models/customer.js b/models/customer.js index 2b9aa90b9..98588db04 100644 --- a/models/customer.js +++ b/models/customer.js @@ -31,8 +31,12 @@ Customer.all = function(callback) { }); }; -Customer.sortByName = function(input, callback) { - db.run ("SELECT * FROM customers ORDER BY name LIMIT $1 OFFSET $2;", input, function(error, customers) { +Customer.sortBy = function(field, n, p, callback) { + db.customers.find({}, { + order: field, + limit: n, + offset: p + }, function(error, customers) { if(error || !customers) { callback(error || new Error("Could not retrieve customers"), undefined); } else { diff --git a/routes/customers.js b/routes/customers.js index 91aff044d..d6bdb6b30 100644 --- a/routes/customers.js +++ b/routes/customers.js @@ -7,13 +7,7 @@ var Controller = require('../controllers/customers_controller') router.get('/', Controller.getCustomers) // GET customers/sort/name?n=10&p=2 -router.get('/sort/name*', Controller.sortName) - -// GET customers/sort/registered_at -router.get('/sort/registered-at', Controller.sortRegistered) - -// GET customers/sort/postal_code -router.get('/sort/postal-code', Controller.sortPostal) +router.get('/sort/:field', Controller.sortBy) // GET customers/:id/current router.get('/:id/current', Controller.customerCurrent) From a5fe028fb5362c27798457d41a37f6211b1907e5 Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Fri, 17 Jun 2016 15:28:46 -0700 Subject: [PATCH 35/58] added to ability to sort release_date or title through sort. working on server. --- controllers/movies_controller.js | 33 ++++++++++++++++---------------- models/movie.js | 20 ++++++++++++++++--- routes/movies.js | 2 +- 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index dc4fc8444..4532224a8 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -15,23 +15,24 @@ var MoviesController = { } }); }, +sortBy: function(req, res, next) { + var options = { + order: req.params.field, + limit: req.query.n, + offset: req.query.p + } - sortByRelease: function(req, res, next) { - // var type = req.params.query; - var n = req.query.n; - var p = req.query.p; - - Movie.sortByRelease([n, p], function(error, movies) { - if(error) { - var err = new Error("Error retrieving sorted movie list:\n" + error.message); - err.status = 500; - next(err); - } else { - res.json(movies) - // var locals = { movies: movies } - // res.render("movies/index", locals); - } - }); + Movie.sortBy(options, function(error, movies) { + if(error) { + var err = new Error("Error retrieving sorted movie list:\n" + error.message); + err.status = 500; + next(err); + } else { + res.json(movies) + // var locals = { movies: movies } + // res.render("movies/index", locals); + } + }); }, // current: function(req, res, next) { diff --git a/models/movie.js b/models/movie.js index 96ee62e0d..16a45ac5e 100644 --- a/models/movie.js +++ b/models/movie.js @@ -26,9 +26,22 @@ Movie.all = function(callback) { }); }; -Movie.sortByRelease = function(input, callback) { - // first parameter is the - db.run("SELECT * FROM movies ORDER BY release_date LIMIT $1 OFFSET $2;", input, function(error, movies) { +// Movie.sortByRelease = function(input, callback) { +// // first parameter is the +// db.run("SELECT * FROM movies ORDER BY release_date LIMIT $1 OFFSET $2;", input, function(error, movies) { +// if(error || !movies) { +// callback(error || new Error("Movies not found"), undefined); +// } else { +// callback(null, movies.map(function(movie) { +// return new Movie(movie) +// })); +// }; +// }); +// }; + +Movie.sortBy = function(options, callback) { + // first parameter is the info from movie controller which was [type, n, p] + db.movies.find({}, options, function(error, movies) { if(error || !movies) { callback(error || new Error("Movies not found"), undefined); } else { @@ -39,6 +52,7 @@ Movie.sortByRelease = function(input, callback) { }); }; + // only attach this function if we're in test mode. // if (app.get('env') === 'test') { // Movie.close_connection = function() { diff --git a/routes/movies.js b/routes/movies.js index eecd6554a..57e69815e 100644 --- a/routes/movies.js +++ b/routes/movies.js @@ -3,7 +3,7 @@ var router = express.Router(); var MoviesController = require('../controllers/movies_controller.js'); router.get('/', MoviesController.listMovies) -router.get('/sort/release-date*', MoviesController.sortByRelease) +router.get('/sort/:field', MoviesController.sortBy) // router.get('/:title/current', MoviesController.current) // router.get('/:title/history/sort/:by', MoviesController.sortByHistory) From 543c4fb57f1782dbd12e575c084f9a6435e27956 Mon Sep 17 00:00:00 2001 From: Ania Gonzalez Date: Fri, 17 Jun 2016 16:12:24 -0700 Subject: [PATCH 36/58] Seeded Rentals --- db/seeds/rentals.json | 57 +++++++++++++++++++++++++++++++++++++++++++ tasks/seed_data.js | 13 ++++++++-- 2 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 db/seeds/rentals.json diff --git a/db/seeds/rentals.json b/db/seeds/rentals.json new file mode 100644 index 000000000..735cb7c85 --- /dev/null +++ b/db/seeds/rentals.json @@ -0,0 +1,57 @@ +[ +{ +"movie_id": 1, +"customer_id": 6, +"checkout_date": "2015-06-10", +"due_date": "2015-06-20", +"return_date": "2015-06-20" +}, + +{ +"movie_id": 2, +"customer_id": 4, +"checkout_date": "2014-03-10", +"due_date": "2014-03-20", +"return_date": "2014-03-20" +}, + +{ +"movie_id": 3, +"customer_id": 9, +"checkout_date": "2015-08-11", +"due_date": "2015-08-21", +"return_date": "2015-08-28" +}, + +{ +"movie_id": 4, +"customer_id": 6, +"checkout_date": "2015-08-11", +"due_date": "2015-08-21", +"return_date": "2015-08-28" +}, + +{ +"movie_id": 5, +"customer_id": 1, +"checkout_date": "2015-02-10", +"due_date": "2015-02-20", +"return_date": "2015-02-27" +}, + +{ +"movie_id": 6, +"customer_id": 6, +"checkout_date": "2015-10-10", +"due_date": "2015-10-20", +"return_date": "2015-10-20" +}, + +{ +"movie_id": 7, +"customer_id": 9, +"checkout_date": "2015-08-11", +"due_date": "2015-08-21", +"return_date": "2015-08-28" +} +] diff --git a/tasks/seed_data.js b/tasks/seed_data.js index ee6322622..95ab3f5ee 100644 --- a/tasks/seed_data.js +++ b/tasks/seed_data.js @@ -10,6 +10,7 @@ var db = massive.connectSync({connectionString : connectionString}) var fs = require("fs") console.log("\n *STARTING* \n") // Get content from file +var seedRentals = "rentals.json" var seedMovies = "movies.json" var seedCustomers = "customers.json" @@ -39,15 +40,23 @@ var seedData = function(filename, table, callback) { } var movies_done = false var customers_done = false +var rentals_done = false + seedData(seedMovies, db.movies, function(){ movies_done = true - if (customers_done === true) { + if (customers_done && rentals_done === true) { process.exit() } }) seedData(seedCustomers, db.customers, function() { customers_done = true - if (movies_done === true) { + if (movies_done && rentals_done === true) { + process.exit() + } +}) +seedData(seedRentals, db.rentals, function(){ + rentals_done = true + if (customers_done && movies_done === true) { process.exit() } }) From 12ecba96982e5cada7e82e8ade53f1a6302ac7c6 Mon Sep 17 00:00:00 2001 From: Ania Gonzalez Date: Fri, 17 Jun 2016 16:14:11 -0700 Subject: [PATCH 37/58] updated test script --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6b5de1633..8248111f6 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "private": true, "scripts": { "start": "nodemon ./bin/www", - "test": "clear; jasmine-node --verbose spec/", + "test": "clear; ./node_modules/.bin/jasmine-node --captureExceptions --verbose spec/", "db:drop": "dropdb video_store_api_development", "db:create": "createdb video_store_api_development", "db:schema": "node tasks/load_schema.js", From d070933378943a19e8cbcad2b598335b2dd8361f Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Fri, 17 Jun 2016 16:27:32 -0700 Subject: [PATCH 38/58] movie/title/current route enabled. starting to work on movie controller. --- controllers/movies_controller.js | 59 ++++++++++++++++++++++---------- routes/movies.js | 2 +- 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index 4532224a8..3dc2adb6b 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -1,4 +1,6 @@ var Movie = require("../models/movie"); +var Customer = require("../models/customer"); +var Rental = require("../models/rental"); var MoviesController = { listMovies: function(req, res, next) { @@ -15,29 +17,48 @@ var MoviesController = { } }); }, -sortBy: function(req, res, next) { - var options = { - order: req.params.field, - limit: req.query.n, - offset: req.query.p - } - Movie.sortBy(options, function(error, movies) { - if(error) { - var err = new Error("Error retrieving sorted movie list:\n" + error.message); - err.status = 500; - next(err); - } else { - res.json(movies) - // var locals = { movies: movies } - // res.render("movies/index", locals); + sortBy: function(req, res, next) { + var options = { + order: req.params.field, + limit: req.query.n, + offset: req.query.p } - }); + + Movie.sortBy(options, function(error, movies) { + if(error) { + var err = new Error("Error retrieving sorted movie list:\n" + error.message); + err.status = 500; + next(err); + } else { + res.json(movies) + // var locals = { movies: movies } + // res.render("movies/index", locals); + } + }); }, - // current: function(req, res, next) { - // - // }, + current: function(req, res, next) { + var movie = req.params.title; + + Movie.find(movie, function(error, search_movie) + if(error) { + var err = new Error("No such movie"); + err.status = 404; + next(err); + } else { + Rentals.find(function(error, movie) { + res.render("rentals/show", { + movie: { + id: movie.id, + } + }); + }); + } + }); + ) + + }, // // sortByHistory: function(req, res, next) { // diff --git a/routes/movies.js b/routes/movies.js index 57e69815e..5f4f43001 100644 --- a/routes/movies.js +++ b/routes/movies.js @@ -4,7 +4,7 @@ var MoviesController = require('../controllers/movies_controller.js'); router.get('/', MoviesController.listMovies) router.get('/sort/:field', MoviesController.sortBy) -// router.get('/:title/current', MoviesController.current) +router.get('/:title/current', MoviesController.current) // router.get('/:title/history/sort/:by', MoviesController.sortByHistory) module.exports = router; From 3f3f0d5fd9ff57ee52997b430c793e7c79bd323c Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Mon, 20 Jun 2016 10:21:50 -0700 Subject: [PATCH 39/58] commented out routes for editing --- controllers/movies_controller.js | 46 +++++++++++++++----------------- routes/movies.js | 2 +- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index 3dc2adb6b..2a681b407 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -1,6 +1,6 @@ var Movie = require("../models/movie"); -var Customer = require("../models/customer"); -var Rental = require("../models/rental"); +// var Customer = require("../models/customer"); +// var Rental = require("../models/rental"); var MoviesController = { listMovies: function(req, res, next) { @@ -32,33 +32,31 @@ var MoviesController = { next(err); } else { res.json(movies) - // var locals = { movies: movies } - // res.render("movies/index", locals); } }); }, - current: function(req, res, next) { - var movie = req.params.title; - - Movie.find(movie, function(error, search_movie) - if(error) { - var err = new Error("No such movie"); - err.status = 404; - next(err); - } else { - Rentals.find(function(error, movie) { - res.render("rentals/show", { - movie: { - id: movie.id, - } - }); - }); - } - }); - ) + // current: function(req, res, next) { + // var movie = req.params.title; + // + // Movie.find(movie, function(error, search_movie) + // if(error) { + // var err = new Error("No such movie"); + // err.status = 404; + // next(err); + // } else { + // Rentals.find(function(error, movie) { + // res.render("rentals/show", { + // movie: { + // id: movie.id, + // } + // }); + // }); + // } + // }); + // ) - }, + // }, // // sortByHistory: function(req, res, next) { // diff --git a/routes/movies.js b/routes/movies.js index 5f4f43001..57e69815e 100644 --- a/routes/movies.js +++ b/routes/movies.js @@ -4,7 +4,7 @@ var MoviesController = require('../controllers/movies_controller.js'); router.get('/', MoviesController.listMovies) router.get('/sort/:field', MoviesController.sortBy) -router.get('/:title/current', MoviesController.current) +// router.get('/:title/current', MoviesController.current) // router.get('/:title/history/sort/:by', MoviesController.sortByHistory) module.exports = router; From 706c7da44c9accdd26d931a5ab9854756315a8eb Mon Sep 17 00:00:00 2001 From: Ania Gonzalez Date: Mon, 20 Jun 2016 11:27:40 -0700 Subject: [PATCH 40/58] You can now get movie rentals for a specific customer: --- controllers/customers_controller.js | 10 ++++++++++ models/customer.js | 15 ++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/controllers/customers_controller.js b/controllers/customers_controller.js index fbe5a99ce..46de2db38 100644 --- a/controllers/customers_controller.js +++ b/controllers/customers_controller.js @@ -33,7 +33,17 @@ var CustomersController = { }, customerCurrent: function(req, res, next) { + var id = req.params.id + Customer.current([id], function(error, movies) { + if(error) { + var err = new Error("Error retrieving current movies for this customer:\n" + error.message); + err.status = 500; + next(err); + } else { + res.json(movies) + } + }) }, customerHistory: function(req, res, next) { diff --git a/models/customer.js b/models/customer.js index 98588db04..580a53033 100644 --- a/models/customer.js +++ b/models/customer.js @@ -1,5 +1,6 @@ var app = require("../app"); var db = app.get("db"); +var Movie = require("../models/movie"); var Customer = function(customer) { @@ -43,7 +44,19 @@ Customer.sortBy = function(field, n, p, callback) { callback(null, customers.map(function(customer) { return new Customer(customer); })); - }; + } + }); +} + +Customer.current = function([id], callback) { + db.run ("SELECT rentals.customer_id, rentals.movie_id, movies.title FROM rentals INNER JOIN movies ON rentals.movie_id = movies.id WHERE customer_id = $1;", [id], function(error, movies) { + if(error || !movies) { + callback(error || new Error("Could not retrieve customer movies"), undefined); + } else { + callback(null, movies.map(function(movie) { + return new Movie(movie); + })); + } }); } From ecd858fe0dc3cd8b6eed8bcc9fc26c85fd73f62c Mon Sep 17 00:00:00 2001 From: Ania Gonzalez Date: Mon, 20 Jun 2016 13:00:03 -0700 Subject: [PATCH 41/58] Customer current movies now only return movies with no return date --- db/seeds/rentals.json | 10 ++++------ models/customer.js | 12 ++++++++++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/db/seeds/rentals.json b/db/seeds/rentals.json index 735cb7c85..a206846a0 100644 --- a/db/seeds/rentals.json +++ b/db/seeds/rentals.json @@ -34,17 +34,15 @@ { "movie_id": 5, "customer_id": 1, -"checkout_date": "2015-02-10", -"due_date": "2015-02-20", -"return_date": "2015-02-27" +"checkout_date": "2016-02-10", +"due_date": "2016-02-20" }, { "movie_id": 6, "customer_id": 6, -"checkout_date": "2015-10-10", -"due_date": "2015-10-20", -"return_date": "2015-10-20" +"checkout_date": "2016-06-16", +"due_date": "2016-06-26" }, { diff --git a/models/customer.js b/models/customer.js index 580a53033..a6b2bb173 100644 --- a/models/customer.js +++ b/models/customer.js @@ -49,6 +49,18 @@ Customer.sortBy = function(field, n, p, callback) { } Customer.current = function([id], callback) { + db.run ("SELECT rentals.customer_id, rentals.movie_id, movies.title FROM rentals INNER JOIN movies ON rentals.movie_id = movies.id WHERE customer_id = $1 AND return_date IS NULL;", [id], function(error, movies) { + if(error || !movies) { + callback(error || new Error("Could not retrieve customer movies"), undefined); + } else { + callback(null, movies.map(function(movie) { + return new Movie(movie); + })); + } + }); +} + +Customer.history = function([id], callback) { db.run ("SELECT rentals.customer_id, rentals.movie_id, movies.title FROM rentals INNER JOIN movies ON rentals.movie_id = movies.id WHERE customer_id = $1;", [id], function(error, movies) { if(error || !movies) { callback(error || new Error("Could not retrieve customer movies"), undefined); From 1ce38eb6d24c20005ff38cba6e1419f6bb4a2343 Mon Sep 17 00:00:00 2001 From: Ania Gonzalez Date: Mon, 20 Jun 2016 13:20:51 -0700 Subject: [PATCH 42/58] Customer history gives all rentals with checkout and return date --- controllers/customers_controller.js | 10 ++++++++++ models/customer.js | 2 +- models/movie.js | 2 ++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/controllers/customers_controller.js b/controllers/customers_controller.js index 46de2db38..5cdb15a6d 100644 --- a/controllers/customers_controller.js +++ b/controllers/customers_controller.js @@ -47,7 +47,17 @@ var CustomersController = { }, customerHistory: function(req, res, next) { + var id = req.params.id + Customer.history([id], function(error, movies) { + if(error) { + var err = new Error("Error retrieving historic movies for this customer:\n" + error.message); + err.status = 500; + next(err); + } else { + res.json(movies) + } + }) } } module.exports = CustomersController diff --git a/models/customer.js b/models/customer.js index a6b2bb173..8a499d22d 100644 --- a/models/customer.js +++ b/models/customer.js @@ -61,7 +61,7 @@ Customer.current = function([id], callback) { } Customer.history = function([id], callback) { - db.run ("SELECT rentals.customer_id, rentals.movie_id, movies.title FROM rentals INNER JOIN movies ON rentals.movie_id = movies.id WHERE customer_id = $1;", [id], function(error, movies) { + db.run ("SELECT rentals.customer_id, rentals.movie_id, movies.title, rentals.checkout_date, rentals.return_date FROM rentals INNER JOIN movies ON rentals.movie_id = movies.id WHERE customer_id = $1 ORDER BY rentals.checkout_date;", [id], function(error, movies) { if(error || !movies) { callback(error || new Error("Could not retrieve customer movies"), undefined); } else { diff --git a/models/movie.js b/models/movie.js index 16a45ac5e..9bbae06e2 100644 --- a/models/movie.js +++ b/models/movie.js @@ -7,6 +7,8 @@ var Movie = function(movie) { this.overview = movie.overview; this.release_date = movie.release_date; this.inventory = movie.inventory; + this.checkout_date = movie.checkout_date; + this.return_date = movie.return_date; } // takes on parameter(callback)-then run db.accounts.find From aad4b53719b18a8ec95f43396973562a3c402412 Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Mon, 20 Jun 2016 14:18:00 -0700 Subject: [PATCH 43/58] movies. current now working. --- controllers/movies_controller.js | 45 ++++++++++++++------------------ models/movie.js | 41 ++++++++++++++++++++--------- routes/movies.js | 2 +- 3 files changed, 49 insertions(+), 39 deletions(-) diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index 2a681b407..0c17b4550 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -1,5 +1,5 @@ var Movie = require("../models/movie"); -// var Customer = require("../models/customer"); +var Customer = require("../models/customer"); // var Rental = require("../models/rental"); var MoviesController = { @@ -36,30 +36,25 @@ var MoviesController = { }); }, - // current: function(req, res, next) { - // var movie = req.params.title; - // - // Movie.find(movie, function(error, search_movie) - // if(error) { - // var err = new Error("No such movie"); - // err.status = 404; - // next(err); - // } else { - // Rentals.find(function(error, movie) { - // res.render("rentals/show", { - // movie: { - // id: movie.id, - // } - // }); - // }); - // } - // }); - // ) + current: function(req, res, next) { + var movie = req.params.title; - // }, - // - // sortByHistory: function(req, res, next) { - // - // } + Movie.find_customers_by_movie_title(movie, function(error, customers) { + if(error) { + var err = new Error("No such movie"); + err.status = 404; + next(err); + } else { + var obj = {}; + if (customers.length === 0) { + obj["status"] = 204; + } else { + obj["status"] = 200; + } + obj["customers"] = customers; + res.json(obj); + } + }) + } } module.exports = MoviesController; diff --git a/models/movie.js b/models/movie.js index 16a45ac5e..7424a898e 100644 --- a/models/movie.js +++ b/models/movie.js @@ -1,5 +1,6 @@ var app = require("../app"); var db = app.get("db"); +var Customers = require("../models/customer"); var Movie = function(movie) { this.id = movie.id; @@ -26,19 +27,6 @@ Movie.all = function(callback) { }); }; -// Movie.sortByRelease = function(input, callback) { -// // first parameter is the -// db.run("SELECT * FROM movies ORDER BY release_date LIMIT $1 OFFSET $2;", input, function(error, movies) { -// if(error || !movies) { -// callback(error || new Error("Movies not found"), undefined); -// } else { -// callback(null, movies.map(function(movie) { -// return new Movie(movie) -// })); -// }; -// }); -// }; - Movie.sortBy = function(options, callback) { // first parameter is the info from movie controller which was [type, n, p] db.movies.find({}, options, function(error, movies) { @@ -52,6 +40,33 @@ Movie.sortBy = function(options, callback) { }); }; +// Movie.find = function(title, callback) { +// db.movies.findMovieByTitle({title: title}, function(error, movie) { +// if(error || !movie) { +// callback(error || new Error("No such movie by title"), undefined); +// } else { +// callback(null, new Movie(movie)) +// } +// }); +// }; + +Movie.find_customers_by_movie_title = function(title, callback) { + db.run("SELECT customers.name, customers.phone, customers.account_credit FROM customers INNER JOIN rentals ON customers.id = rentals.customer_id INNER JOIN movies ON rentals.movie_id = movies.id WHERE movies.title ILIKE $1 AND rentals.return_date IS NULL;", [title], function(error, customers) { + if(error || !customers) { + callback(error || new Error("Could not find customers"), undefined); + } else { + callback(null, customers.map(function(customer) { + return new Customers(customer); + })); + } + }); +} + +// 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 + // only attach this function if we're in test mode. // if (app.get('env') === 'test') { diff --git a/routes/movies.js b/routes/movies.js index 57e69815e..5f4f43001 100644 --- a/routes/movies.js +++ b/routes/movies.js @@ -4,7 +4,7 @@ var MoviesController = require('../controllers/movies_controller.js'); router.get('/', MoviesController.listMovies) router.get('/sort/:field', MoviesController.sortBy) -// router.get('/:title/current', MoviesController.current) +router.get('/:title/current', MoviesController.current) // router.get('/:title/history/sort/:by', MoviesController.sortByHistory) module.exports = router; From 98ae35746a3f0cb1c6c5e734294921569b26aeba Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Mon, 20 Jun 2016 14:46:51 -0700 Subject: [PATCH 44/58] broken movies history. --- controllers/movies_controller.js | 24 +++++++++++++++++++++++- models/movie.js | 12 ++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index 0c17b4550..8ea8f4355 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -55,6 +55,28 @@ var MoviesController = { res.json(obj); } }) - } + }, + + history: function(req, res, next) { + var movie = req.params.title; + var field = req.params.field; + + Movie.find_customers_by_movie_title_history([field], function(error, customers) { + if(error) { + var err = new Error("No such movie"); + err.status = 404; + next(err); + } else { + var obj = {}; + if (customers.length === 0) { + obj["status"] = 204; + } else { + obj["status"] = 200; + } + obj["customers"] = customers; + res.json(obj); + } + }) + } } module.exports = MoviesController; diff --git a/models/movie.js b/models/movie.js index 9dc5085c1..b4033ba5f 100644 --- a/models/movie.js +++ b/models/movie.js @@ -64,6 +64,18 @@ Movie.find_customers_by_movie_title = function(title, callback) { }); } +Movie.find_customers_by_movie_title_history = function([field], callback) { + db.run("SELECT customers.name, customers.phone, customers.account_credit FROM customers INNER JOIN rentals ON customers.id = rentals.customer_id INNER JOIN movies ON rentals.movie_id = movies.id WHERE movies.title ILIKE 'Jaws' ORDER BY $1;", [field], function(error, customers) { + if(error || !customers) { + callback(error || new Error("Could not find customers"), undefined); + } else { + callback(null, customers.map(function(customer) { + return new Customers(customer); + })); + } + }); +} + // 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 From ef5e344af058b4e958c6594c7f97529b7e082e37 Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Mon, 20 Jun 2016 14:52:03 -0700 Subject: [PATCH 45/58] saving routes uncommented now. --- routes/movies.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routes/movies.js b/routes/movies.js index 5f4f43001..fdfbedb02 100644 --- a/routes/movies.js +++ b/routes/movies.js @@ -5,6 +5,6 @@ var MoviesController = require('../controllers/movies_controller.js'); router.get('/', MoviesController.listMovies) router.get('/sort/:field', MoviesController.sortBy) router.get('/:title/current', MoviesController.current) -// router.get('/:title/history/sort/:by', MoviesController.sortByHistory) +router.get('/:title/history/sort/:field', MoviesController.history) module.exports = router; From 2728048bb2de82a623e8e155ce06cc6f5ce0d45f Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Mon, 20 Jun 2016 16:30:48 -0700 Subject: [PATCH 46/58] fixed the circular app.js on customer and movie models. started model movie spec.js --- controllers/movies_controller.js | 4 ++-- models/customer.js | 6 ++++-- models/movie.js | 28 ++++++++++++++++++++-------- spec/models/movie.spec.js | 0 4 files changed, 26 insertions(+), 12 deletions(-) create mode 100644 spec/models/movie.spec.js diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index 8ea8f4355..76ae69ad5 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -1,5 +1,5 @@ var Movie = require("../models/movie"); -var Customer = require("../models/customer"); +// var Customer = require("../models/customer"); // var Rental = require("../models/rental"); var MoviesController = { @@ -61,7 +61,7 @@ var MoviesController = { var movie = req.params.title; var field = req.params.field; - Movie.find_customers_by_movie_title_history([field], function(error, customers) { + Movie.find_customers_by_movie_title_history([movie, field], function(error, customers) { if(error) { var err = new Error("No such movie"); err.status = 404; diff --git a/models/customer.js b/models/customer.js index 8a499d22d..3fb7d0166 100644 --- a/models/customer.js +++ b/models/customer.js @@ -1,9 +1,8 @@ var app = require("../app"); var db = app.get("db"); -var Movie = require("../models/movie"); - var Customer = function(customer) { + console.log(customer) this.id = customer.id this.name = customer.name this.registered_at = customer.registered_at @@ -14,6 +13,9 @@ var Customer = function(customer) { this.phone = customer.phone this.account_credit = customer.account_credit } +module.exports = Customer; + +var Movie = require("../models/movie"); // takes on parameter(callback)-then run db.accounts.find Customer.all = function(callback) { diff --git a/models/movie.js b/models/movie.js index b4033ba5f..ee38f5485 100644 --- a/models/movie.js +++ b/models/movie.js @@ -1,6 +1,6 @@ var app = require("../app"); var db = app.get("db"); -var Customers = require("../models/customer"); + var Movie = function(movie) { this.id = movie.id; @@ -11,6 +11,8 @@ var Movie = function(movie) { this.checkout_date = movie.checkout_date; this.return_date = movie.return_date; } +module.exports = Movie; +var Customer = require("../models/customer"); // takes on parameter(callback)-then run db.accounts.find Movie.all = function(callback) { @@ -58,19 +60,31 @@ Movie.find_customers_by_movie_title = function(title, callback) { callback(error || new Error("Could not find customers"), undefined); } else { callback(null, customers.map(function(customer) { - return new Customers(customer); + return new Customer(customer); })); } }); -} +}; -Movie.find_customers_by_movie_title_history = function([field], callback) { - db.run("SELECT customers.name, customers.phone, customers.account_credit FROM customers INNER JOIN rentals ON customers.id = rentals.customer_id INNER JOIN movies ON rentals.movie_id = movies.id WHERE movies.title ILIKE 'Jaws' ORDER BY $1;", [field], function(error, customers) { +Movie.find_customers_by_movie_title_history = function(fields, callback) { + console.log("CUSTOMERS:", Customer) + db.run("SELECT customers.name, customers.phone, customers.account_credit FROM customers INNER JOIN rentals ON customers.id = rentals.customer_id INNER JOIN movies ON rentals.movie_id = movies.id WHERE movies.title ILIKE $1 ORDER BY $2;", fields, function(error, customers) { if(error || !customers) { callback(error || new Error("Could not find customers"), undefined); } else { + var my_customers = customers.map(function(customer) { + + var my_customer = new Customer(customer) + return my_customer + // console.log(my_customer) + }) + + console.log("after the map!") + console.log(my_customers) + // callback(null, my_customers) + callback(null, customers.map(function(customer) { - return new Customers(customer); + return new Customer(customer); })); } }); @@ -89,5 +103,3 @@ Movie.find_customers_by_movie_title_history = function([field], callback) { // db.end() // } // } - -module.exports = Movie; diff --git a/spec/models/movie.spec.js b/spec/models/movie.spec.js new file mode 100644 index 000000000..e69de29bb From 802c19bad6605100d1c6652393746f5a917aa427 Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Tue, 21 Jun 2016 09:19:01 -0700 Subject: [PATCH 47/58] added db/movie/customers_by_movie.sql to extract the sql query from movies model. added to load_schema to load test db. changed some seed data. --- db/movie/customers_by_movie_title.sql | 10 ++++++ db/seeds/rentals.json | 3 +- db/setup/schema.sql | 4 +-- models/customer.js | 1 - models/movie.js | 18 +---------- package.json | 4 +-- spec/models/movie.spec.js | 44 +++++++++++++++++++++++++++ tasks/load_schema.js | 27 ++++++++++------ 8 files changed, 77 insertions(+), 34 deletions(-) create mode 100644 db/movie/customers_by_movie_title.sql diff --git a/db/movie/customers_by_movie_title.sql b/db/movie/customers_by_movie_title.sql new file mode 100644 index 000000000..26411e176 --- /dev/null +++ b/db/movie/customers_by_movie_title.sql @@ -0,0 +1,10 @@ +SELECT + customers.name, + customers.phone, + customers.account_credit +FROM customers + INNER JOIN rentals ON customers.id = rentals.customer_id + INNER JOIN movies ON rentals.movie_id = movies.id +WHERE + movies.title ILIKE $1 AND + rentals.return_date IS NULL; diff --git a/db/seeds/rentals.json b/db/seeds/rentals.json index a206846a0..510780baf 100644 --- a/db/seeds/rentals.json +++ b/db/seeds/rentals.json @@ -19,8 +19,7 @@ "movie_id": 3, "customer_id": 9, "checkout_date": "2015-08-11", -"due_date": "2015-08-21", -"return_date": "2015-08-28" +"due_date": "2015-08-21" }, { diff --git a/db/setup/schema.sql b/db/setup/schema.sql index cb68c8750..334f82fcf 100644 --- a/db/setup/schema.sql +++ b/db/setup/schema.sql @@ -1,4 +1,6 @@ +DROP TABLE IF EXISTS rentals; DROP TABLE IF EXISTS movies; +DROP TABLE IF EXISTS customers; CREATE TABLE movies( id serial PRIMARY KEY, title text, @@ -11,7 +13,6 @@ CREATE INDEX movies_title ON movies (title); CREATE INDEX movies_release_date ON movies (release_date); /*customers */ -DROP TABLE IF EXISTS customers; CREATE TABLE customers( id serial PRIMARY KEY, name text, @@ -27,7 +28,6 @@ CREATE TABLE customers( CREATE INDEX customers_name ON customers (name); /* rentals */ -DROP TABLE IF EXISTS rentals; CREATE TABLE rentals( id serial PRIMARY KEY, movie_id integer references movies(id), diff --git a/models/customer.js b/models/customer.js index 3fb7d0166..dde57f78b 100644 --- a/models/customer.js +++ b/models/customer.js @@ -2,7 +2,6 @@ var app = require("../app"); var db = app.get("db"); var Customer = function(customer) { - console.log(customer) this.id = customer.id this.name = customer.name this.registered_at = customer.registered_at diff --git a/models/movie.js b/models/movie.js index ee38f5485..c70d533d6 100644 --- a/models/movie.js +++ b/models/movie.js @@ -55,7 +55,7 @@ Movie.sortBy = function(options, callback) { // }; Movie.find_customers_by_movie_title = function(title, callback) { - db.run("SELECT customers.name, customers.phone, customers.account_credit FROM customers INNER JOIN rentals ON customers.id = rentals.customer_id INNER JOIN movies ON rentals.movie_id = movies.id WHERE movies.title ILIKE $1 AND rentals.return_date IS NULL;", [title], function(error, customers) { + db.movie.customers_by_movie_title([title], function(error, customers) { if(error || !customers) { callback(error || new Error("Could not find customers"), undefined); } else { @@ -67,22 +67,10 @@ Movie.find_customers_by_movie_title = function(title, callback) { }; Movie.find_customers_by_movie_title_history = function(fields, callback) { - console.log("CUSTOMERS:", Customer) db.run("SELECT customers.name, customers.phone, customers.account_credit FROM customers INNER JOIN rentals ON customers.id = rentals.customer_id INNER JOIN movies ON rentals.movie_id = movies.id WHERE movies.title ILIKE $1 ORDER BY $2;", fields, function(error, customers) { if(error || !customers) { callback(error || new Error("Could not find customers"), undefined); } else { - var my_customers = customers.map(function(customer) { - - var my_customer = new Customer(customer) - return my_customer - // console.log(my_customer) - }) - - console.log("after the map!") - console.log(my_customers) - // callback(null, my_customers) - callback(null, customers.map(function(customer) { return new Customer(customer); })); @@ -90,10 +78,6 @@ Movie.find_customers_by_movie_title_history = function(fields, callback) { }); } -// 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 // only attach this function if we're in test mode. diff --git a/package.json b/package.json index 8248111f6..15cd2f8e7 100644 --- a/package.json +++ b/package.json @@ -5,8 +5,8 @@ "scripts": { "start": "nodemon ./bin/www", "test": "clear; ./node_modules/.bin/jasmine-node --captureExceptions --verbose spec/", - "db:drop": "dropdb video_store_api_development", - "db:create": "createdb video_store_api_development", + "db:drop": "dropdb video_store_api_development && dropdb video_store_api_test", + "db:create": "createdb video_store_api_development && createdb video_store_api_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; npm run db:seed" diff --git a/spec/models/movie.spec.js b/spec/models/movie.spec.js index e69de29bb..ca7e0e39d 100644 --- a/spec/models/movie.spec.js +++ b/spec/models/movie.spec.js @@ -0,0 +1,44 @@ +var app = require("../../app"); +var db = app.get("db"); + +var Movie = require('../../models/movie') + +describe('Movie', function () { + var title1 = "The Exorcist"; + var overview1 = "12-year-old Regan MacNeil begins to adapt an explicit new personality as strange events befall the local area of Georgetown. Her mother becomes torn between science and superstition in a desperate bid to save her daughter, and ultimately turns to her last hope: Father Damien Karras, a troubled priest who is struggling with his own faith."; + var inventory1 = 7; + + afterEach(function () { + db.end() + }) + + describe('#all', function () { + it('should return a list of all the movies and respective data', function (done) { + Movie.all(function (error, movies) { + expect(error).toBeNull + expect(movies.length).toEqual(100) + }) + done() + }) + + it('should return an array', function (done) { + Movie.all(function (error, movies) { + expect(error).toBeNull + expect(movies.isArray).toEqual(true) + }) + done() + }) + }) + + describe('#sortBy', function () { + it('should return a sorted list of movies', function (done) { + Movie.sortBy("title", 1, 2, function (error, movies) { + expect(movies.length).toEqual(1) + expect(movies[0].title).toEqual(title1) + expect(movies[0].overview).toEqual(overview1) + expect(movies[0].inventory).toEqual(inventory1) + }) + done() + }) + }) +}) diff --git a/tasks/load_schema.js b/tasks/load_schema.js index 75fffcb22..d80c30bdf 100644 --- a/tasks/load_schema.js +++ b/tasks/load_schema.js @@ -1,14 +1,21 @@ var massive = require('massive') -var connectionString = "postgres://localhost/video_store_api_development" -var db = massive.connectSync({connectionString : connectionString}) +var count = 0 -// setup below comes from folder name -db.setup.schema([], function(err, res) { - if(err) { - throw(new Error(err.message)) - } +var setup = function(environment) { + var connectionString = "postgres://localhost/video_store_api_" + environment + var db = massive.connectSync({connectionString : connectionString}) + db.setup.schema([], function(err, res) { + if(err) { + throw(new Error(err.message)) + } + count ++ + console.log("yay " + environment + " schema!") + if(count == 2) { + process.exit() + } + }) +} - console.log("yay schema!") - process.exit() -}) +setup("development"); +setup("test"); From 080e4324f1b8cae3d0ecc097e563a67d26966550 Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Tue, 21 Jun 2016 14:18:04 -0700 Subject: [PATCH 48/58] got the rentals by title route working. --- controllers/movies_controller.js | 4 ++-- controllers/rentals_controller.js | 23 +++++++++++++++++++++++ models/rental.js | 31 +++++++++++++++++++++++++++++++ routes/rentals.js | 2 +- 4 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 models/rental.js diff --git a/controllers/movies_controller.js b/controllers/movies_controller.js index 76ae69ad5..ce41f854c 100644 --- a/controllers/movies_controller.js +++ b/controllers/movies_controller.js @@ -1,6 +1,6 @@ var Movie = require("../models/movie"); -// var Customer = require("../models/customer"); -// var Rental = require("../models/rental"); +var Customer = require("../models/customer"); +var Rental = require("../models/rental"); var MoviesController = { listMovies: function(req, res, next) { diff --git a/controllers/rentals_controller.js b/controllers/rentals_controller.js index e69de29bb..1e6467a41 100644 --- a/controllers/rentals_controller.js +++ b/controllers/rentals_controller.js @@ -0,0 +1,23 @@ +var Movie = require("../models/movie"); +var Customer = require("../models/customer"); +var Rental = require("../models/rental"); + +var RentalsController = { + + findMovie: function(req, res, next) { + var title = req.params.title; + + Rental.all([title], function (error, rentals) { + if(error) { + var err = new Error("Error retrieving movie info:\n" + error.message); + err.status = 500; + next(err); + } else { + res.json(rentals) + } + }) + } + + +} +module.exports = RentalsController; diff --git a/models/rental.js b/models/rental.js new file mode 100644 index 000000000..38d6e6033 --- /dev/null +++ b/models/rental.js @@ -0,0 +1,31 @@ +var app = require("../app"); +var db = app.get("db"); + +var Rental = function(rental) { + this.id = rental.id; + this.title = rental.title; + this.name = rental.name; + this.checkout_date = rental.checkout_date; + this.due_date = rental.due_date; + this.return_date = rental.return_date; +} + +module.exports = Rental; +var Customer = require("../models/customer"); +var Movie = require("../models/movie") + +Rental.all = function (title, callback) { + console.log(title) + db.run("select * from (select * from rentals, movies where rentals.movie_id=movies.id) as movie_rentals where movie_rentals.title = $1 order by due_date;", title, 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) { + return new Rental(rental); + })); + } + }); + + +}; diff --git a/routes/rentals.js b/routes/rentals.js index ecb5f9472..91db39198 100644 --- a/routes/rentals.js +++ b/routes/rentals.js @@ -4,7 +4,7 @@ var RentalsController = require('../controllers/rentals_controller.js'); // // // GET rentals/:title // // Look a rental up by title -// router.get('/:title', RentalsController.findMovie) +router.get('/:title', RentalsController.findMovie) // // // GET rentals/:title/customers // // list of customers that have currently checked out any of the movie's inventory From 13d1809049f67dcb20f3e350caebbc5d9a035e02 Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Tue, 21 Jun 2016 15:54:35 -0700 Subject: [PATCH 49/58] changed the given data to the Rental.all by title. --- models/rental.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/models/rental.js b/models/rental.js index 38d6e6033..08f3f4237 100644 --- a/models/rental.js +++ b/models/rental.js @@ -8,6 +8,9 @@ var Rental = function(rental) { this.checkout_date = rental.checkout_date; this.due_date = rental.due_date; this.return_date = rental.return_date; + this.overview = rental.overview; + this.inventory = rental.inventory; + this.release_date = rental.release_date; } module.exports = Rental; @@ -15,17 +18,13 @@ var Customer = require("../models/customer"); var Movie = require("../models/movie") Rental.all = function (title, callback) { - console.log(title) db.run("select * from (select * from rentals, movies where rentals.movie_id=movies.id) as movie_rentals where movie_rentals.title = $1 order by due_date;", title, 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) { return new Rental(rental); - })); + })); } }); - - }; From a2833c2e5b869ecc46ae95695ea86204401c6050 Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Tue, 21 Jun 2016 16:48:06 -0700 Subject: [PATCH 50/58] fixed the route and methods for finding all customers that have renatls currently out for specific. --- controllers/rentals_controller.js | 23 ++++++++++++++++++++++- db/rental/customers_current_rentals.sql | 10 ++++++++++ models/rental.js | 25 +++++++++++++++++++++++++ routes/rentals.js | 2 +- 4 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 db/rental/customers_current_rentals.sql diff --git a/controllers/rentals_controller.js b/controllers/rentals_controller.js index 1e6467a41..77c1f455e 100644 --- a/controllers/rentals_controller.js +++ b/controllers/rentals_controller.js @@ -16,7 +16,28 @@ var RentalsController = { res.json(rentals) } }) - } + }, + + sortBy: function(req, res, next) { + var movie = req.params.title; + + Rental.customers_current_rentals(movie, function(error, customers) { + if(error) { + var err = new Error("No such customer"); + err.status = 404; + next(err); + } else { + var obj = {}; + if (customers.length === 0) { + obj["status"] = 204; + } else { + obj["status"] = 200; + } + obj["customers"] = customers; + res.json(obj); + } + }) + }, } diff --git a/db/rental/customers_current_rentals.sql b/db/rental/customers_current_rentals.sql new file mode 100644 index 000000000..26411e176 --- /dev/null +++ b/db/rental/customers_current_rentals.sql @@ -0,0 +1,10 @@ +SELECT + customers.name, + customers.phone, + customers.account_credit +FROM customers + INNER JOIN rentals ON customers.id = rentals.customer_id + INNER JOIN movies ON rentals.movie_id = movies.id +WHERE + movies.title ILIKE $1 AND + rentals.return_date IS NULL; diff --git a/models/rental.js b/models/rental.js index 08f3f4237..7cec8006a 100644 --- a/models/rental.js +++ b/models/rental.js @@ -28,3 +28,28 @@ Rental.all = function (title, callback) { } }); }; + +Rental.sortBy = function(options, callback) { + // first parameter is the info from movie controller which was [type, n, p] + db.rentals.find({}, options, function(error, rentals) { + if(error || !rentals) { + callback(error || new Error("Rentals not found"), undefined); + } else { + callback(null, rentals.map(function(rental) { + return new Rental(rental) + })); + }; + }); +}; + +Rental.customers_current_rentals = function(title, callback) { + db.rental.customers_current_rentals([title], function(error, customers) { + if(error || !customers) { + callback(error || new Error("Could not find customers"), undefined); + } else { + callback(null, customers.map(function(customer) { + return new Customer(customer); + })); + } + }); +}; diff --git a/routes/rentals.js b/routes/rentals.js index 91db39198..f00fc88ad 100644 --- a/routes/rentals.js +++ b/routes/rentals.js @@ -8,7 +8,7 @@ router.get('/:title', RentalsController.findMovie) // // // GET rentals/:title/customers // // list of customers that have currently checked out any of the movie's inventory -// router.get('/:title/customers', RentalsController.sortBy) +router.get('/:title/customers', RentalsController.sortBy) // // // POST rentals/:title/check-out // // #provide customerid and movie title From 11aa91b28a92a9886054ca00989bf2922a184fbb Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Wed, 22 Jun 2016 14:01:42 -0700 Subject: [PATCH 51/58] starting the checkout route but not working. --- controllers/rentals_controller.js | 13 +++++++++++++ models/rental.js | 17 +++++++++++++++++ routes/rentals.js | 2 +- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/controllers/rentals_controller.js b/controllers/rentals_controller.js index 77c1f455e..cd7deba34 100644 --- a/controllers/rentals_controller.js +++ b/controllers/rentals_controller.js @@ -39,6 +39,19 @@ var RentalsController = { }) }, + checkout: function(req, res, next) { + var movie = req.params.title; + var customer_id = req.body.customer_id; + Rental.createCheckOut(movie, customer_id, function(error) { + if(error) { + var err = new Error("Rental checkout failed"); + err.status = 404; + next(err); + } else { + res.json(rental); + } + }) + } } module.exports = RentalsController; diff --git a/models/rental.js b/models/rental.js index 7cec8006a..8e5ad8a8c 100644 --- a/models/rental.js +++ b/models/rental.js @@ -28,6 +28,17 @@ Rental.all = function (title, callback) { } }); }; +Rental.find = function (title, callback) { + db.movies.search({columns:["title"], term: title}, function (error, movies) { + if(error || !movies) { + callback(error || new Error("Movies not found"), undefined); + } else { + callback(null, movies.map(function(movie) { + return new Movie(movie) + })); + }; + }); +}; Rental.sortBy = function(options, callback) { // first parameter is the info from movie controller which was [type, n, p] @@ -53,3 +64,9 @@ Rental.customers_current_rentals = function(title, callback) { } }); }; + +Rental.createCheckOut = function(title, customer_id, callback) { + Rental.find(title, function(error, movie) { + db.rentals.saveSync({customer_id: customer_id, movie_id: movie.id, checkout_date: (new Date())}) + }); +}; diff --git a/routes/rentals.js b/routes/rentals.js index f00fc88ad..b48d90006 100644 --- a/routes/rentals.js +++ b/routes/rentals.js @@ -12,7 +12,7 @@ router.get('/:title/customers', RentalsController.sortBy) // // // POST rentals/:title/check-out // // #provide customerid and movie title -// router.post('/:title/check-out', RentalsController.checkout) +router.post('/:title/check-out', RentalsController.checkout) // // // POST rentals/:title/return // // #provide customerid and movie title From 85b4f5182763c90086fa7d936ad5c907a88545db Mon Sep 17 00:00:00 2001 From: Ania Gonzalez Date: Thu, 23 Jun 2016 10:04:24 -0700 Subject: [PATCH 52/58] fixed the rentals controller so that information is stored correctly --- controllers/rentals_controller.js | 7 +++-- models/rental.js | 6 +++- spec/models/customer.spec.js | 48 +++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 spec/models/customer.spec.js diff --git a/controllers/rentals_controller.js b/controllers/rentals_controller.js index cd7deba34..c64943016 100644 --- a/controllers/rentals_controller.js +++ b/controllers/rentals_controller.js @@ -42,14 +42,17 @@ var RentalsController = { checkout: function(req, res, next) { var movie = req.params.title; var customer_id = req.body.customer_id; + console.log(movie, customer_id); - Rental.createCheckOut(movie, customer_id, function(error) { + Rental.createCheckOut(movie, customer_id, function(error) { //should we add something after error, movie? rental? + console.log("in createCheckOut method") if(error) { var err = new Error("Rental checkout failed"); err.status = 404; next(err); } else { - res.json(rental); + console.log("rental is:", rental) + res.json({"checkout": "it was ok"}); } }) } diff --git a/models/rental.js b/models/rental.js index 8e5ad8a8c..b86233d80 100644 --- a/models/rental.js +++ b/models/rental.js @@ -66,7 +66,11 @@ Rental.customers_current_rentals = function(title, callback) { }; Rental.createCheckOut = function(title, customer_id, callback) { + console.log("in rental model", title, customer_id) Rental.find(title, function(error, movie) { - db.rentals.saveSync({customer_id: customer_id, movie_id: movie.id, checkout_date: (new Date())}) + console.log(movie) + console.log("movie_id is:", movie[0].id) + var in10days = new Date(today.getTime() + (10 * 24 * 60 * 60 * 1000)) + db.rentals.saveSync({customer_id: customer_id, movie_id: movie[0].id, checkout_date: (new Date()), due_date: in10days}) }); }; diff --git a/spec/models/customer.spec.js b/spec/models/customer.spec.js new file mode 100644 index 000000000..ef634cf8e --- /dev/null +++ b/spec/models/customer.spec.js @@ -0,0 +1,48 @@ +var app = require("../../app"); +var db = app.get("db"); + +var Customer = require('../../models/customer') + +describe('Customer', function () { + var customer1 = "Ania Gonzalez" + var city1 = "Sammamish" + + afterEach(function () { + // delete all the customers I created + db.end() + }) + + describe('#all', function() { + it('should return all the customers in db', function (done) { + Customer.all(function (error, customers) { + expect(error).toBeNull + expect(customers.length).toEqual(200) + }) + done() + }) + + it('should return an array', function (done) { + Customer.all(function (error, customers) { + expect(error).toBeNull + expect(customers.isArray).toEqual(true) + }) + done() + }) + + }) + + describe('#sortBy', function () { + it('should return a sorted list of customers', function (done) { + Customer.sortBy("name", 1, 2, function (error, customers) { + expect(customers.length).toEqual(1) + // expect(customers[0].title).toEqual(title1) + // expect(customers[0].overview).toEqual(overview1) + // expect(customers[0].inventory).toEqual(inventory1) + }) + done() + }) + }) + + + +}) From 03cf03fb125c8fc27c992ffdceb43bb100a9b487 Mon Sep 17 00:00:00 2001 From: Ania Gonzalez Date: Thu, 23 Jun 2016 11:24:38 -0700 Subject: [PATCH 53/58] Set the return date for the rental checkout in db --- controllers/rentals_controller.js | 4 ++-- models/rental.js | 15 +++++++++++---- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/controllers/rentals_controller.js b/controllers/rentals_controller.js index c64943016..9c164e966 100644 --- a/controllers/rentals_controller.js +++ b/controllers/rentals_controller.js @@ -44,7 +44,7 @@ var RentalsController = { var customer_id = req.body.customer_id; console.log(movie, customer_id); - Rental.createCheckOut(movie, customer_id, function(error) { //should we add something after error, movie? rental? + Rental.createCheckOut(movie, customer_id, function(error, rental_checkout) { console.log("in createCheckOut method") if(error) { var err = new Error("Rental checkout failed"); @@ -52,7 +52,7 @@ var RentalsController = { next(err); } else { console.log("rental is:", rental) - res.json({"checkout": "it was ok"}); + res.json(rental_checkout); } }) } diff --git a/models/rental.js b/models/rental.js index b86233d80..327b6a74f 100644 --- a/models/rental.js +++ b/models/rental.js @@ -68,9 +68,16 @@ Rental.customers_current_rentals = function(title, callback) { Rental.createCheckOut = function(title, customer_id, callback) { console.log("in rental model", title, customer_id) Rental.find(title, function(error, movie) { - console.log(movie) - console.log("movie_id is:", movie[0].id) - var in10days = new Date(today.getTime() + (10 * 24 * 60 * 60 * 1000)) - db.rentals.saveSync({customer_id: customer_id, movie_id: movie[0].id, checkout_date: (new Date()), due_date: in10days}) + // console.log(movie) + // console.log("movie_id is:", movie[0].id) + var today = new Date(); + var now = new Date() + var dueDate = new Date(now) + var numberOfDaysToRent = 10; + dueDate.setDate(dueDate.getDate() + numberOfDaysToRent); + console.log("date returnDate is:", dueDate) + db.rentals.saveSync({customer_id: customer_id, movie_id: movie[0].id, checkout_date: today, due_date: dueDate}) + //take out money + //modify inventory }); }; From e74649111a4a0beec28c02b0de7481c6a62c84ab Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Thu, 23 Jun 2016 14:26:09 -0700 Subject: [PATCH 54/58] changed schema to accept decimal for account_credit in customers. added logic in model to charge for the movie. --- db/setup/schema.sql | 2 +- models/rental.js | 30 +++++++++++++++++++++++------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/db/setup/schema.sql b/db/setup/schema.sql index 334f82fcf..e603ec108 100644 --- a/db/setup/schema.sql +++ b/db/setup/schema.sql @@ -22,7 +22,7 @@ CREATE TABLE customers( state text, postal_code integer, phone text, - account_credit text + account_credit decimal ); CREATE INDEX customers_name ON customers (name); diff --git a/models/rental.js b/models/rental.js index 327b6a74f..57501dc24 100644 --- a/models/rental.js +++ b/models/rental.js @@ -68,16 +68,32 @@ Rental.customers_current_rentals = function(title, callback) { Rental.createCheckOut = function(title, customer_id, callback) { console.log("in rental model", title, customer_id) Rental.find(title, function(error, movie) { - // console.log(movie) - // console.log("movie_id is:", movie[0].id) + var today = new Date(); - var now = new Date() - var dueDate = new Date(now) + var now = new Date(); + var dueDate = new Date(now); var numberOfDaysToRent = 10; dueDate.setDate(dueDate.getDate() + numberOfDaysToRent); console.log("date returnDate is:", dueDate) + db.rentals.saveSync({customer_id: customer_id, movie_id: movie[0].id, checkout_date: today, due_date: dueDate}) - //take out money - //modify inventory - }); + // remove money from account + console.log(customer_id) + db.run("UPDATE customers SET account_credit=account_credit-3.0 WHERE id=$1;", [customer_id], function (error, result) { + console.log(error, result) + if (error) { + return callback(error); + } + // else { + // //modify inventory + // db.run("UPDATE movies SET inventory=inventory-1 WHERE id=$1;", [movie_id], function(error, result) { + // if (error) { + // console.log(error) + // return callback(error); + // } + // }) + + // }; + }); + }); }; From 08fe5f68d9b3908a1ae08ec67dcf88a84e4fd47c Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Thu, 23 Jun 2016 16:31:03 -0700 Subject: [PATCH 55/58] added function to rental models to charge for rental and change the inventory in the database. --- controllers/rentals_controller.js | 2 +- models/rental.js | 32 ++++++++++++++++++------------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/controllers/rentals_controller.js b/controllers/rentals_controller.js index 9c164e966..ab86b3bfd 100644 --- a/controllers/rentals_controller.js +++ b/controllers/rentals_controller.js @@ -51,7 +51,7 @@ var RentalsController = { err.status = 404; next(err); } else { - console.log("rental is:", rental) + console.log("rental is:", rental_checkout) res.json(rental_checkout); } }) diff --git a/models/rental.js b/models/rental.js index 57501dc24..bd57fed8e 100644 --- a/models/rental.js +++ b/models/rental.js @@ -75,25 +75,31 @@ Rental.createCheckOut = function(title, customer_id, callback) { var numberOfDaysToRent = 10; dueDate.setDate(dueDate.getDate() + numberOfDaysToRent); console.log("date returnDate is:", dueDate) - + console.log(error, movie) db.rentals.saveSync({customer_id: customer_id, movie_id: movie[0].id, checkout_date: today, due_date: dueDate}) - // remove money from account + console.log(today) + console.log(dueDate) + console.log(movie[0].id) console.log(customer_id) + // remove money from account + // console.log(movie_id) + // console.log(customer_id) db.run("UPDATE customers SET account_credit=account_credit-3.0 WHERE id=$1;", [customer_id], function (error, result) { console.log(error, result) if (error) { return callback(error); - } - // else { - // //modify inventory - // db.run("UPDATE movies SET inventory=inventory-1 WHERE id=$1;", [movie_id], function(error, result) { - // if (error) { - // console.log(error) - // return callback(error); - // } - // }) - - // }; + } else { + //modify inventory + db.run("UPDATE movies SET inventory=inventory-1 WHERE id=$1;", [movie[0].id], function(error, result) { + if (error) { + console.log("DAD") + console.log(error) + return callback(error); + } else { + return callback(null, result) + }; + }); + }; }); }); }; From 1a61a2bac7665fce82a8900ec24d9afdffb322b1 Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Thu, 23 Jun 2016 21:48:17 -0700 Subject: [PATCH 56/58] started the return rental function. Route uncommented but function not working yet. urrently stuck on error: throw new Error('Can\'t set headers after they are sent.'); --- controllers/rentals_controller.js | 18 ++++++++++++++++++ models/rental.js | 26 ++++++++++++++++++++++---- routes/rentals.js | 2 +- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/controllers/rentals_controller.js b/controllers/rentals_controller.js index ab86b3bfd..05aa714c4 100644 --- a/controllers/rentals_controller.js +++ b/controllers/rentals_controller.js @@ -55,6 +55,24 @@ var RentalsController = { res.json(rental_checkout); } }) + }, + + return: function(req, res, next) { + var movie = req.params.title; + var customer_id = req.body.customer_id; + + console.log(movie, customer_id); + Rental.returnRental(movie, customer_id, function(error, rental_return) { + console.log("in RETURN method") + if(error) { + var err = new Error("Rental return failed"); + err.status = 404; + next(err); + } else { + console.log("rental to return is:", rental_return) + res.json(rental_return); + } + }) } } module.exports = RentalsController; diff --git a/models/rental.js b/models/rental.js index bd57fed8e..fdfb0f9f4 100644 --- a/models/rental.js +++ b/models/rental.js @@ -28,6 +28,7 @@ Rental.all = function (title, callback) { } }); }; + Rental.find = function (title, callback) { db.movies.search({columns:["title"], term: title}, function (error, movies) { if(error || !movies) { @@ -82,8 +83,6 @@ Rental.createCheckOut = function(title, customer_id, callback) { console.log(movie[0].id) console.log(customer_id) // remove money from account - // console.log(movie_id) - // console.log(customer_id) db.run("UPDATE customers SET account_credit=account_credit-3.0 WHERE id=$1;", [customer_id], function (error, result) { console.log(error, result) if (error) { @@ -92,8 +91,6 @@ Rental.createCheckOut = function(title, customer_id, callback) { //modify inventory db.run("UPDATE movies SET inventory=inventory-1 WHERE id=$1;", [movie[0].id], function(error, result) { if (error) { - console.log("DAD") - console.log(error) return callback(error); } else { return callback(null, result) @@ -103,3 +100,24 @@ Rental.createCheckOut = function(title, customer_id, callback) { }); }); }; + +Rental.returnRental = function(title, customer_id, callback) { + console.log("in rental model", title, customer_id) + Rental.find(title, function(error, movie) { + db.rentals.update({customer_id: customer_id, movie_id: movie[0].id, return_date: null, checkout_date: null}, function(error, checked_out) { + if(error) { + callback(error, undefined); + } else { + callback(null, checked_out); + } + }); + + db.run("UPDATE movies SET inventory=inventory+1 WHERE id=$1;", [movie[0].id], function(error, result) { + if (error) { + return callback(error); + } else { + return callback(null, result) + }; + }); + }); +}; diff --git a/routes/rentals.js b/routes/rentals.js index b48d90006..29bd9e5a6 100644 --- a/routes/rentals.js +++ b/routes/rentals.js @@ -16,7 +16,7 @@ router.post('/:title/check-out', RentalsController.checkout) // // // POST rentals/:title/return // // #provide customerid and movie title -// router.post('/:title/return', RentalsController.return) +router.post('/:title/return', RentalsController.return) // // // GET rentals/overdue // // list of customers with overdue movies From fbffdadf43ab976a8f7a48ecd5fe0a27db8e9b47 Mon Sep 17 00:00:00 2001 From: Ania Gonzalez Date: Fri, 24 Jun 2016 10:22:46 -0700 Subject: [PATCH 57/58] fixed the return rental and the double callback --- controllers/rentals_controller.js | 10 +++----- models/movie.js | 4 ++- models/rental.js | 42 +++++++++++++++---------------- 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/controllers/rentals_controller.js b/controllers/rentals_controller.js index 05aa714c4..0958a2301 100644 --- a/controllers/rentals_controller.js +++ b/controllers/rentals_controller.js @@ -44,15 +44,14 @@ var RentalsController = { var customer_id = req.body.customer_id; console.log(movie, customer_id); - Rental.createCheckOut(movie, customer_id, function(error, rental_checkout) { + Rental.createCheckOut(movie, customer_id, function(error) { console.log("in createCheckOut method") if(error) { var err = new Error("Rental checkout failed"); err.status = 404; next(err); } else { - console.log("rental is:", rental_checkout) - res.json(rental_checkout); + res.json("rental_checkout works"); } }) }, @@ -62,15 +61,14 @@ var RentalsController = { var customer_id = req.body.customer_id; console.log(movie, customer_id); - Rental.returnRental(movie, customer_id, function(error, rental_return) { + Rental.returnRental(movie, customer_id, function(error) { console.log("in RETURN method") if(error) { var err = new Error("Rental return failed"); err.status = 404; next(err); } else { - console.log("rental to return is:", rental_return) - res.json(rental_return); + res.json({returnRental: "Rental was properly returned"}); } }) } diff --git a/models/movie.js b/models/movie.js index c70d533d6..eac27038a 100644 --- a/models/movie.js +++ b/models/movie.js @@ -45,7 +45,9 @@ Movie.sortBy = function(options, callback) { }; // Movie.find = function(title, callback) { -// db.movies.findMovieByTitle({title: title}, function(error, movie) { +// console.log("in movie.find....title:", title) +// db.movies.findOne({title: title}, function(error, movie) { +// console.log("movies in movie.find is....", movie) // if(error || !movie) { // callback(error || new Error("No such movie by title"), undefined); // } else { diff --git a/models/rental.js b/models/rental.js index fdfb0f9f4..a2390bea0 100644 --- a/models/rental.js +++ b/models/rental.js @@ -29,17 +29,17 @@ Rental.all = function (title, callback) { }); }; -Rental.find = function (title, callback) { - db.movies.search({columns:["title"], term: title}, function (error, movies) { - if(error || !movies) { - callback(error || new Error("Movies not found"), undefined); - } else { - callback(null, movies.map(function(movie) { - return new Movie(movie) - })); - }; - }); -}; +// Rental.find = function (title, callback) { +// db.rentals.find({movie_id: movie_id, customer_id: customer_id}, function (error, rentals) { +// if(error || !rentals) { +// callback(error || new Error("Rentals not found"), undefined); +// } else { +// callback(null, rentals.map(function(rental) { +// return new Rental(rental) +// })); +// }; +// }); +// }; Rental.sortBy = function(options, callback) { // first parameter is the info from movie controller which was [type, n, p] @@ -103,21 +103,19 @@ Rental.createCheckOut = function(title, customer_id, callback) { Rental.returnRental = function(title, customer_id, callback) { console.log("in rental model", title, customer_id) - Rental.find(title, function(error, movie) { - db.rentals.update({customer_id: customer_id, movie_id: movie[0].id, return_date: null, checkout_date: null}, function(error, checked_out) { + db.movies.search({columns: ["title"], term: title}, function(error, movies) { + console.log("movies from movie.find is:", movies) + db.rentals.find({movie_id: movies[0].id, customer_id: customer_id}, function(error, rentals) { + console.log("rentals from rental.find is", rentals) + db.rentals.updateSync({id: rentals[0].id, return_date: new Date()}, function(error, checked_out) { + db.run("UPDATE movies SET inventory=inventory+1 WHERE id=$1;", [movies[0].id]); + console.log("check if return date is there:", rentals) if(error) { callback(error, undefined); } else { callback(null, checked_out); } + }); }); - - db.run("UPDATE movies SET inventory=inventory+1 WHERE id=$1;", [movie[0].id], function(error, result) { - if (error) { - return callback(error); - } else { - return callback(null, result) - }; - }); - }); + }) }; From 87cd7274c9c36259dafe5212104d7e4af445381f Mon Sep 17 00:00:00 2001 From: Suzanne Harrison Date: Fri, 24 Jun 2016 14:42:35 -0700 Subject: [PATCH 58/58] added overdue function. --- controllers/rentals_controller.js | 14 +++++++++++ models/customer.js | 3 +++ models/rental.js | 42 +++++++++++++++++++++++++++++++ routes/rentals.js | 7 +++--- 4 files changed, 63 insertions(+), 3 deletions(-) diff --git a/controllers/rentals_controller.js b/controllers/rentals_controller.js index 0958a2301..f9e4dcfda 100644 --- a/controllers/rentals_controller.js +++ b/controllers/rentals_controller.js @@ -71,6 +71,20 @@ var RentalsController = { res.json({returnRental: "Rental was properly returned"}); } }) + }, + + overdue: function(req, res, next) { + console.log("1st stop"); + Rental.findOverdue(function(error, customers) { + if (error) { + var err = new Error("Error in finding overdue "); + err.status = 404; + next(err); + } else { + console.log("Final") + res.json(customers) + } + }) } } module.exports = RentalsController; diff --git a/models/customer.js b/models/customer.js index dde57f78b..243131f93 100644 --- a/models/customer.js +++ b/models/customer.js @@ -11,6 +11,9 @@ var Customer = function(customer) { this.postal_code = customer.postal_code this.phone = customer.phone this.account_credit = customer.account_credit + this.due_date = customer.due_date + this.checkout_date = customer.checkout_date + this.title = customer.title } module.exports = Customer; diff --git a/models/rental.js b/models/rental.js index a2390bea0..e92a74882 100644 --- a/models/rental.js +++ b/models/rental.js @@ -119,3 +119,45 @@ Rental.returnRental = function(title, customer_id, callback) { }); }) }; +Rental.findOverdue = function(callback) { + var today = new Date(); + console.log("infind OVerdue function", today) + db.run("SELECT customers.name, rentals.checkout_date, rentals.due_date, movies.title FROM customers INNER JOIN rentals ON customers.id=rentals.customer_id INNER JOIN movies ON rentals.movie_id = movies.id WHERE rentals.return_date IS NULL AND rentals.due_date < $1;", [today], function(error, customers) { + if(error || !customers) { + callback(error || new Error("Could not retrieve customers"), undefined); + } else { + callback(null, customers.map(function(customer) { + return new Customer(customer); + })); + } + }) +} + + +// Rental.findOverdue = function(callback) { +// console.log("This is before db.search for rentals:") +// db.rentals.find({return_date: null}, function(error, rentals) { +// console.log("in db.rentals.find ", rentals) +// // look for all the rentals where the due_date is less than today +// callback(null, rentals.map (function (rental) { +// var today = new Date(); +// console.log("Testing what rental is ", rental) +// var cust_id = rental.customer_id +// if(rental.due_date < today) { +// console.log("Type is ", typeof cust_id) +// console.log("RENTAL CUST ID =", rental.customer_id) +// db.run("SELECT customers.id, customers.name FROM customers WHERE id=$1;", [cust_id], function (error, customers) { +// if(error || !customers) { +// callback(error || new Error("Could not find customers"), undefined); +// } else { +// callback(null, customers.map(function(customer){ +// console.log("This is customer[0] is ", customer); +// return new Customer(customer) +// })) +// } +// }) +// } +// }) +// }) +// )} +// } diff --git a/routes/rentals.js b/routes/rentals.js index 29bd9e5a6..c31d710b3 100644 --- a/routes/rentals.js +++ b/routes/rentals.js @@ -2,6 +2,10 @@ var express = require('express'); var router = express.Router(); var RentalsController = require('../controllers/rentals_controller.js'); // +// // GET rentals/overdue +// // list of customers with overdue movies +router.get('/overdue', RentalsController.overdue) + // // GET rentals/:title // // Look a rental up by title router.get('/:title', RentalsController.findMovie) @@ -18,8 +22,5 @@ router.post('/:title/check-out', RentalsController.checkout) // // #provide customerid and movie title router.post('/:title/return', RentalsController.return) // -// // GET rentals/overdue -// // list of customers with overdue movies -// router.get('/overdue', RentalsController.overdue) module.exports = router;