diff --git a/app.js b/app.js index f0579b1dc..6cb46b80a 100644 --- a/app.js +++ b/app.js @@ -4,10 +4,16 @@ 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(); +module.exports = app; + +// database setup +var connectionString = "postgres://localhost/video_store_api_" + app.get('env'); +var db = massive.connectSync({connectionString: connectionString}); +app.set('db', db); // view engine setup app.set('views', path.join(__dirname, 'views')); @@ -23,6 +29,15 @@ app.use(express.static(path.join(__dirname, 'public'))); 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'); @@ -53,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 new file mode 100644 index 000000000..5cdb15a6d --- /dev/null +++ b/controllers/customers_controller.js @@ -0,0 +1,63 @@ +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) + } + }); + }, + + sortBy: function(req, res, next) { + var n = req.query.n; + var p = req.query.p; + var field = String(req.params.field) + + Customer.sortBy(field, 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(customers) + } + }); + + }, + + 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) { + 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/controllers/movies_controller.js b/controllers/movies_controller.js new file mode 100644 index 000000000..ce41f854c --- /dev/null +++ b/controllers/movies_controller.js @@ -0,0 +1,82 @@ +var Movie = require("../models/movie"); +var Customer = require("../models/customer"); +var Rental = require("../models/rental"); + +var MoviesController = { + listMovies: 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); + } + }); + }, + + 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) + } + }); + }, + + current: function(req, res, next) { + var movie = req.params.title; + + 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); + } + }) + }, + + history: function(req, res, next) { + var movie = req.params.title; + var field = req.params.field; + + Movie.find_customers_by_movie_title_history([movie, 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/controllers/rentals_controller.js b/controllers/rentals_controller.js new file mode 100644 index 000000000..f9e4dcfda --- /dev/null +++ b/controllers/rentals_controller.js @@ -0,0 +1,90 @@ +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) + } + }) + }, + + 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); + } + }) + }, + + 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) { + console.log("in createCheckOut method") + if(error) { + var err = new Error("Rental checkout failed"); + err.status = 404; + next(err); + } else { + res.json("rental_checkout works"); + } + }) + }, + + 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) { + console.log("in RETURN method") + if(error) { + var err = new Error("Rental return failed"); + err.status = 404; + next(err); + } else { + 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/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/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/db/seeds/rentals.json b/db/seeds/rentals.json new file mode 100644 index 000000000..510780baf --- /dev/null +++ b/db/seeds/rentals.json @@ -0,0 +1,54 @@ +[ +{ +"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" +}, + +{ +"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": "2016-02-10", +"due_date": "2016-02-20" +}, + +{ +"movie_id": 6, +"customer_id": 6, +"checkout_date": "2016-06-16", +"due_date": "2016-06-26" +}, + +{ +"movie_id": 7, +"customer_id": 9, +"checkout_date": "2015-08-11", +"due_date": "2015-08-21", +"return_date": "2015-08-28" +} +] diff --git a/db/setup/schema.sql b/db/setup/schema.sql new file mode 100644 index 000000000..e603ec108 --- /dev/null +++ b/db/setup/schema.sql @@ -0,0 +1,41 @@ +DROP TABLE IF EXISTS rentals; +DROP TABLE IF EXISTS movies; +DROP TABLE IF EXISTS customers; +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 */ +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 decimal +); + +CREATE INDEX customers_name ON customers (name); + +/* 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/models/customer.js b/models/customer.js new file mode 100644 index 000000000..243131f93 --- /dev/null +++ b/models/customer.js @@ -0,0 +1,80 @@ +var app = require("../app"); +var db = app.get("db"); + +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 + this.due_date = customer.due_date + this.checkout_date = customer.checkout_date + this.title = customer.title +} +module.exports = Customer; + +var Movie = require("../models/movie"); + +// 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); + })); + }; + }); +}; + +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 { + 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 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, 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 { + callback(null, movies.map(function(movie) { + return new Movie(movie); + })); + } + }); +} + + +module.exports = Customer; diff --git a/models/movie.js b/models/movie.js new file mode 100644 index 000000000..eac27038a --- /dev/null +++ b/models/movie.js @@ -0,0 +1,91 @@ +var app = require("../app"); +var db = app.get("db"); + + +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; + 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) { + // 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); + })); + }; + }); +}; + +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 { + callback(null, movies.map(function(movie) { + return new Movie(movie) + })); + }; + }); +}; + +// Movie.find = function(title, callback) { +// 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 { +// callback(null, new Movie(movie)) +// } +// }); +// }; + +Movie.find_customers_by_movie_title = function(title, callback) { + db.movie.customers_by_movie_title([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); + })); + } + }); +}; + +Movie.find_customers_by_movie_title_history = function(fields, 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 ORDER BY $2;", fields, 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); + })); + } + }); +} + + + +// 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() +// } +// } diff --git a/models/rental.js b/models/rental.js new file mode 100644 index 000000000..e92a74882 --- /dev/null +++ b/models/rental.js @@ -0,0 +1,163 @@ +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; + this.overview = rental.overview; + this.inventory = rental.inventory; + this.release_date = rental.release_date; +} + +module.exports = Rental; +var Customer = require("../models/customer"); +var Movie = require("../models/movie") + +Rental.all = function (title, callback) { + 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) { + if(error || !rentals) { + callback(error || new Error("Could not retrieve rentals"), undefined); + } else { + callback(null, rentals.map(function (rental) { + return new Rental(rental); + })); + } + }); +}; + +// 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] + 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); + })); + } + }); +}; + +Rental.createCheckOut = function(title, customer_id, callback) { + console.log("in rental model", title, customer_id) + Rental.find(title, function(error, movie) { + + 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) + console.log(error, movie) + db.rentals.saveSync({customer_id: customer_id, movie_id: movie[0].id, checkout_date: today, due_date: dueDate}) + console.log(today) + console.log(dueDate) + console.log(movie[0].id) + console.log(customer_id) + // remove money from account + 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[0].id], function(error, result) { + if (error) { + return callback(error); + } else { + return callback(null, result) + }; + }); + }; + }); + }); +}; + +Rental.returnRental = function(title, customer_id, callback) { + console.log("in rental model", title, customer_id) + 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); + } + }); + }); + }) +}; +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/package.json b/package.json index d39b26403..15cd2f8e7 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; ./node_modules/.bin/jasmine-node --captureExceptions --verbose spec/", + "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" }, "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" @@ -20,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..d6bdb6b30 --- /dev/null +++ b/routes/customers.js @@ -0,0 +1,19 @@ +var express = require('express'); +var router = express.Router(); + +var Controller = require('../controllers/customers_controller') + +// GET customers // +router.get('/', Controller.getCustomers) + +// GET customers/sort/name?n=10&p=2 +router.get('/sort/:field', Controller.sortBy) + +// GET customers/:id/current +router.get('/:id/current', Controller.customerCurrent) + +// GET customers/:id/history +router.get('/:id/history', Controller.customerHistory) + + +module.exports = router diff --git a/routes/index.js b/routes/index.js index 06cfc1137..520e874bd 100644 --- a/routes/index.js +++ b/routes/index.js @@ -6,4 +6,38 @@ 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 +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 #provide customerid and movie title +POST rentals/:title/return #provide customerid and movie title +GET rentals/overdue + +*/ + module.exports = router; diff --git a/routes/movies.js b/routes/movies.js new file mode 100644 index 000000000..fdfbedb02 --- /dev/null +++ b/routes/movies.js @@ -0,0 +1,10 @@ +var express = require('express'); +var router = express.Router(); +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/:field', MoviesController.history) + +module.exports = router; diff --git a/routes/rentals.js b/routes/rentals.js new file mode 100644 index 000000000..c31d710b3 --- /dev/null +++ b/routes/rentals.js @@ -0,0 +1,26 @@ +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) +// +// // 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) +// + +module.exports = router; 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() + }) + }) + + + +}) diff --git a/spec/models/movie.spec.js b/spec/models/movie.spec.js new file mode 100644 index 000000000..ca7e0e39d --- /dev/null +++ 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 new file mode 100644 index 000000000..d80c30bdf --- /dev/null +++ b/tasks/load_schema.js @@ -0,0 +1,21 @@ +var massive = require('massive') + +var count = 0 + +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() + } + }) +} + +setup("development"); +setup("test"); diff --git a/tasks/seed_data.js b/tasks/seed_data.js new file mode 100644 index 000000000..95ab3f5ee --- /dev/null +++ b/tasks/seed_data.js @@ -0,0 +1,62 @@ +var massive = require('massive') +var connectionString = "postgres://localhost/video_store_api_development" + + +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 seedRentals = "rentals.json" +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 +var rentals_done = false + +seedData(seedMovies, db.movies, function(){ + movies_done = true + if (customers_done && rentals_done === true) { + process.exit() + } +}) +seedData(seedCustomers, db.customers, function() { + customers_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() + } +})