Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
116 commits
Select commit Hold shift + click to select a range
7ff12ab
Generated node framework & customer & movie controllers & added node_…
QuartzBrandi Sep 15, 2015
4303a89
finished zomg it works endpoint
MisShellyMac Sep 15, 2015
ac26111
Setup routes for zomg; changed users to customers & added movies.js.
QuartzBrandi Sep 15, 2015
7db7a93
Added pseudo-code for movies.
QuartzBrandi Sep 15, 2015
a1b0467
Psuedo code for routes
MisShellyMac Sep 17, 2015
ac1b5c0
Created utils folder for schema and seed data.
QuartzBrandi Sep 17, 2015
a352697
Added db folder to gitignore.
QuartzBrandi Sep 17, 2015
04ba619
Set up schema for database.
QuartzBrandi Sep 17, 2015
39c2136
Added sqlite3 to package.json.
QuartzBrandi Sep 17, 2015
34db915
Added schema for all tables & added seed data from json files.
QuartzBrandi Sep 17, 2015
63bdc81
Added custom scripts to packages.json file.
QuartzBrandi Sep 17, 2015
330220a
Set up mocha with phony test.
QuartzBrandi Sep 17, 2015
70e23f7
WIP testing
MisShellyMac Sep 17, 2015
08a81b6
WIP tring to get movies to show by title, index and all
MisShellyMac Sep 18, 2015
8ff761a
WIP: Added customers models & controllers & tests. Still working on.
QuartzBrandi Sep 18, 2015
4c1cc20
WIP database.js
MisShellyMac Sep 18, 2015
d5dc439
Added functions to database.js.
QuartzBrandi Sep 18, 2015
724dd72
Merge branch 'bw+mm/master' of github.com:MisShellyMac/C3Projects--Vi…
QuartzBrandi Sep 18, 2015
a04660c
Added ability to look at movies/title/title/inventory
MisShellyMac Sep 18, 2015
2624083
Added npm-debug to gitignore.
QuartzBrandi Sep 18, 2015
cf12c46
WIP: Working on customers controller index, got it working, now will …
QuartzBrandi Sep 18, 2015
e7a4126
Still WIP for customers controller.
QuartzBrandi Sep 18, 2015
fc64972
Merge branch 'bw+mm/master' of github.com:MisShellyMac/C3Projects--Vi…
QuartzBrandi Sep 18, 2015
1e064ba
Merge pull request #1 from MisShellyMac/customer_tests/bw/master
QuartzBrandi Sep 18, 2015
9d10e49
Sorting Movies by Release Date Possible at movies/release_date endpoint
MisShellyMac Sep 18, 2015
8a7613c
Added seeds for rentals.
QuartzBrandi Sep 21, 2015
1ef1dd6
Removed duplicate.
QuartzBrandi Sep 21, 2015
36dda1a
WIP: Customer functions & routes.
QuartzBrandi Sep 21, 2015
6b9cff1
Removed npm-debug, put the file in the gitignore.
QuartzBrandi Sep 21, 2015
d3f9c9d
Got show method to work. Displaying currently checked out movies & re…
QuartzBrandi Sep 21, 2015
4ac69eb
Removed commented out code.
QuartzBrandi Sep 21, 2015
a364a4c
added ability to search by page and number and to see current and pas…
MisShellyMac Sep 21, 2015
a8ee714
Added to seed one record.
QuartzBrandi Sep 21, 2015
ab0646f
Merge branch 'bw+mm/master' of github.com:MisShellyMac/C3Projects--Vi…
QuartzBrandi Sep 21, 2015
76c9eb3
Merge pull request #2 from MisShellyMac/seed/bew/master
QuartzBrandi Sep 21, 2015
85071ab
Merge branch 'michelle' into bw+mm/master
MisShellyMac Sep 21, 2015
fd73407
Removed a lot of my commented-out code.
QuartzBrandi Sep 21, 2015
3f4e2b2
Don't need var data since there's a callback returning the results.
QuartzBrandi Sep 21, 2015
9f66aeb
WIP: Sort by functions for customers.
QuartzBrandi Sep 21, 2015
a4abb9f
Made change to seed file and updated readme
MisShellyMac Sep 21, 2015
6da432f
WIP: Working on a single method for the all the sortby functions.
QuartzBrandi Sep 21, 2015
276b930
Finished basic functionality of customers functions; still need to fo…
QuartzBrandi Sep 21, 2015
935042e
Corrected a path.
QuartzBrandi Sep 21, 2015
07eabc2
Added 2 endpoints for overdue and current rentals
MisShellyMac Sep 21, 2015
f700517
WIP Rentals controller actions
MisShellyMac Sep 21, 2015
645a32b
Added rentals to routes.
QuartzBrandi Sep 21, 2015
1e6194d
Changed statement to include 'null' wording.
QuartzBrandi Sep 21, 2015
1889449
Got post route to work in rentals?
QuartzBrandi Sep 21, 2015
e645ff6
Fixing merge conflicts.
QuartzBrandi Sep 21, 2015
b68f2b9
Rentals.
QuartzBrandi Sep 21, 2015
f62f018
Fixed rentals; weird merge didn't flag rentals for the same changes.
QuartzBrandi Sep 21, 2015
8ec2e33
Merge pull request #3 from MisShellyMac/customers1/bew/master
QuartzBrandi Sep 21, 2015
8dd14cd
Removed some comments.
QuartzBrandi Sep 21, 2015
1a8f4d3
Realized there weren't specific columns that the API call wanted back…
QuartzBrandi Sep 21, 2015
61d0c58
See previous commit.
QuartzBrandi Sep 21, 2015
0cc4aa0
Added comment.
QuartzBrandi Sep 21, 2015
5771ccc
WIP: Working on fixing sort by for registered at.
QuartzBrandi Sep 21, 2015
41c9fc2
Added functionality for rentals
MisShellyMac Sep 22, 2015
4e4679d
Took out some comments.
QuartzBrandi Sep 22, 2015
9194fa8
Working on fixing the sort by registered date; works, but still a lot…
QuartzBrandi Sep 22, 2015
6ed49f1
Merge branch 'bw+mm/master' of github.com:MisShellyMac/C3Projects--Vi…
QuartzBrandi Sep 22, 2015
e1f1e8d
Merge pull request #4 from MisShellyMac/refactoring1.0/bew/master
QuartzBrandi Sep 22, 2015
e8f066b
WIP using models instead of controllers
MisShellyMac Sep 22, 2015
9e79aff
Fixed / Got Rentals POST request working\!
QuartzBrandi Sep 22, 2015
366200d
Merge branch 'bw+mm/master' of github.com:MisShellyMac/C3Projects--Vi…
QuartzBrandi Sep 22, 2015
4a58798
Removed comments from the create function & added error check.
QuartzBrandi Sep 22, 2015
e7b987a
Merge pull request #5 from MisShellyMac/post/bew/master
QuartzBrandi Sep 22, 2015
69572c4
Working on checkout and checkin functions for rentals. Troubleshootin…
QuartzBrandi Sep 22, 2015
e2524d3
Finished Rentals checkin/! Charge customer late fee if late.
QuartzBrandi Sep 22, 2015
628fcf2
WIP testing on movies endpoint
MisShellyMac Sep 23, 2015
917c89f
Deleted some old comments.
QuartzBrandi Sep 23, 2015
362f319
deleted commented out test that don't work
MisShellyMac Sep 23, 2015
9e3c86b
Working on (and trying to understand) tests for nodejs & customers.
QuartzBrandi Sep 23, 2015
186b7ef
Movies Controller Endpoint Tests
MisShellyMac Sep 23, 2015
cd4a426
Added some new npm commands.
QuartzBrandi Sep 23, 2015
43fa49e
Wrote an incorrect test for customers/:id\! Way to get involved and l…
QuartzBrandi Sep 23, 2015
14a5f3c
WIP rentals controllers
MisShellyMac Sep 23, 2015
d6d3568
Successfully tested each endpoint for the customers controller.
QuartzBrandi Sep 23, 2015
d4ebe71
Deleted extraneous console.log line 37
MisShellyMac Sep 23, 2015
b6cd397
Deleted some commented out code.
QuartzBrandi Sep 23, 2015
a0e2831
Merge branch 'bw+mm/master' of github.com:MisShellyMac/C3Projects--Vi…
QuartzBrandi Sep 23, 2015
a30dd56
Merge pull request #6 from MisShellyMac/customertests/bew/master
QuartzBrandi Sep 23, 2015
9d7d68d
Renamed test file names.
QuartzBrandi Sep 23, 2015
74c796d
Added tests for POST endpoint check_out.
QuartzBrandi Sep 23, 2015
e3a9beb
Working on customers tests.
QuartzBrandi Sep 23, 2015
7c32fea
Removed database test.
QuartzBrandi Sep 23, 2015
f2ab4b5
Added last endpoint test for check_in under rentals controller test
MisShellyMac Sep 23, 2015
5fc5add
fixed rentals/overdue test to passing
MisShellyMac Sep 24, 2015
5eaf464
Added seed data\!
QuartzBrandi Sep 24, 2015
4f11c3c
Merge branch 'bw+mm/master' of github.com:MisShellyMac/C3Projects--Vi…
QuartzBrandi Sep 24, 2015
6e69f76
Finished new endpoint and test for rentals/:title/available_inventory
MisShellyMac Sep 24, 2015
c37e7a9
Added SELECT title, overview, release_date, inventory to http://local…
MisShellyMac Sep 24, 2015
48d4b9a
Seperated customers/:id into two seperate API endpoints (customers/:i…
QuartzBrandi Sep 24, 2015
d898905
Edited the tests for the seperated endpoint of customers/:id.
QuartzBrandi Sep 24, 2015
3e39aed
Revised rewritten tests to pass.
QuartzBrandi Sep 24, 2015
7480395
Merge branch 'bw+mm/master' of github.com:MisShellyMac/C3Projects--Vi…
QuartzBrandi Sep 24, 2015
4817b61
Merge pull request #7 from MisShellyMac/bugfixes1.0/bew/master
QuartzBrandi Sep 24, 2015
69bb893
updated movies test file to accommadate for more data being returned
MisShellyMac Sep 24, 2015
dc77902
Removed public-facing/default Express page.
QuartzBrandi Sep 24, 2015
beeec27
Fixing getCheckedOutHistoryByTitle function; ordering correctly: id, …
QuartzBrandi Sep 24, 2015
5d0b7a5
Refactored the customers controller a bit.
QuartzBrandi Sep 24, 2015
fd611f1
Added another test for Rentals to make sure check_in updated the rent…
QuartzBrandi Sep 24, 2015
1033dda
Added another test for Rentals, testing if movie is returned and over…
QuartzBrandi Sep 25, 2015
63ffbdb
Added/Edited comments.
QuartzBrandi Sep 25, 2015
026ea6c
Merge pull request #8 from MisShellyMac/misc/bew/master
QuartzBrandi Sep 25, 2015
6c16386
Added functionality to deny rental if inventory is not enough to acco…
MisShellyMac Sep 25, 2015
018d8e2
Fixed bug with sort_by registered date only showing the first record …
QuartzBrandi Sep 25, 2015
f64c089
Updated test for rentals/check_out for appropriate json response
MisShellyMac Sep 25, 2015
164ffdf
Cleaned up the sort by registered date.
QuartzBrandi Sep 25, 2015
2124367
Refactored the sort by functions.
QuartzBrandi Sep 25, 2015
40a49ea
Merge branch 'bw+mm/master' of github.com:MisShellyMac/C3Projects--Vi…
QuartzBrandi Sep 25, 2015
b1a1677
Merge pull request #9 from MisShellyMac/refactor/bew/master
QuartzBrandi Sep 25, 2015
c4aeae4
Removed random console.logs lying around.
QuartzBrandi Sep 25, 2015
0bcb85a
Fixed some offset indenting.
QuartzBrandi Sep 25, 2015
3b2b47b
Fixed a test.
QuartzBrandi Sep 25, 2015
eadeedf
Added nyan command to package.json.
QuartzBrandi Sep 25, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
.DS_Store
/node_modules
/db
npm-debug.log
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ The API you build should have the following capabilities. The schema of your dat
- There is not an authentication requirement for this project; assume all users interacting with the API are video store employees.

### Customers
- Retrive a list of all customers
- Retrive a subset of customers
- Retrieve a list of all customers
- Retrieve a subset of customers
- Given a sort column, return _n_ customer records, offset by _p_ records (this will be used to create "pages" of customers)
- Sort columns are
- `name`
Expand All @@ -57,6 +57,10 @@ The API you build should have the following capabilities. The schema of your dat

### Movies
- Retrieve a list of all movies
- Look a movie up by title to see
- it's synopsis
- release date
- and inventory total
- Retrieve a subset of movies
- Given a sort column, return _n_ movie records, offset by _p_ records (this will be used to create "pages" of movies)
- Sort columns are
Expand All @@ -70,11 +74,7 @@ The API you build should have the following capabilities. The schema of your dat
- ordered by check out date

### Rental
- Look a movie up by title to see
- it's synopsis
- release date
- and inventory total
- Know if a movie has any inventory available to rent
- Know if a movie has available inventory to rent (diff than total inventory)
- See a list of customers that have _currently_ checked out any of the movie's inventory
- Given a customer's `id` and a movie's `title` ...
- "check out" one of the movie's inventory to the customer
Expand Down
64 changes: 64 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var movies = require('./routes/movies');
var customers = require('./routes/customers');
var rentals = require('./routes/rentals');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/customers', customers);
app.use('/movies', movies);
app.use('/rentals', rentals);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});


module.exports = app;
90 changes: 90 additions & 0 deletions bin/www
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('C3Projects--VideoStoreAPI:server');
var http = require('http');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
53 changes: 53 additions & 0 deletions controllers/customers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"use strict";

var customer_instance = require('../models/customers');
var Customer = new customer_instance;

function sortBy(the_function, sort_by, req, res) {
var number = req["query"]["n"];
var pages = req["query"]["p"];
the_function(sort_by, number, pages, function(err, result) {
return res.status(200).json(result);
});
}

function showRentals(the_function, req, res) {
var id = req["params"]["id"];
the_function(id, function(err, result) {
return res.status(200).json(result);
});
}

exports.customersController = {
// GET /customers
index: function(req, res) {
Customer.all(function(err, result) {
return res.status(200).json(result);
});
},

// GET /customers/by_name?n=XXX&p=XXX
showByName: function(req, res) {
sortBy(Customer.find_by_sorted, "name", req, res);
},

// GET /customers/by_registered_at?n=XXX&p=XXX
showByRegisteredAt: function(req, res) {
sortBy(Customer.find_by_sorted_date, "registered_at", req, res)
},

// GET /customers/by_postal_code?n=XXX&p=XXX
showByPostalCode: function(req, res) {
sortBy(Customer.find_by_sorted, "postal_code", req, res);
},

// GET /customers/:id/current
showCustomerCurrent: function(req, res) {
showRentals(Customer.find_current, req, res);
},

// GET /customers/:id/history
showCustomerHistory: function(req, res) {
showRentals(Customer.find_history, req, res);
}
};
134 changes: 134 additions & 0 deletions controllers/movies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
"use strict";

var sqlite3 = require('sqlite3').verbose();
var db_env = process.env.DB || 'development';
var db = new sqlite3.Database('db/' + db_env + '.db');
var Movie = require('../models/movies');

exports.moviesController = {

// GET /movies
getAllMovies: function(res) {
db.all("SELECT title, overview, release_date, inventory FROM movies", function(err, rows) {
if (err !== null) {
console.log(err);
}
res.status(200).json(rows);
});
},

// Get /movies/id/:id(synopsis, inventory, release_date)
getMovieById: function(id, res) {
db.all("SELECT title, overview, release_date, inventory FROM movies WHERE id=?", id, function(err, rows) {
if (err !== null) {
console.log(err);
}
res.status(200).json(rows);
});
},

// GET /movies/title/:title
getMovieByTitle: function(title, res) {
db.all("SELECT title, overview, release_date, inventory FROM movies WHERE title LIKE ?", title, function(err, rows) {
if (err !== null) {
console.log(err);
}
res.status(200).json(rows);
});
},

// GET /movies/title/:title/:inventory
getMovieByTitleInventory: function(title, res) {
db.all("SELECT title, inventory FROM movies WHERE title LIKE ?", title, function(err, rows) {
if (err !== null) {
console.log(err);
}
res.status(200).json(rows);
});
},

// GET /movies/release_date?n=XXX&p=XXX
getMoviesByReleaseDate: function(num, page, res) {
var callback = function(err, rows) {
if (err !== null) {
console.log(err);
}
res.status(200).json(rows);
};

// This is the case when no query parameters are given
if (num === undefined && page === undefined) {
db.all("SELECT title, overview, release_date, inventory FROM movies ORDER BY release_date DESC", callback);
}
// This is the case when only n is given
else if (page === undefined) {
db.all("SELECT title, overview, release_date, inventory FROM movies ORDER BY release_date DESC LIMIT ?", num, callback);
}
else {
// Assume both num and page are specified
db.all("SELECT title, release_date FROM movies WHERE id NOT IN ( SELECT id FROM movies ORDER BY release_date DESC LIMIT ?) ORDER BY release_date DESC LIMIT ?", (page - 1) * num, num, callback);
}
},

// GET /movies/title?n=XXX&p=XXX
getMoviesByTitle: function(num, page, res) {
var callback = function(err, rows) {
if (err !== null) {
console.log(err);
}
res.status(200).json(rows);
};

// This is the case when no query parameters are given
if (num === undefined && page === undefined) {
db.all("SELECT title, overview, release_date, inventory FROM movies ORDER BY title ASC", callback);
}
// This is the case when only n is given
else if (page === undefined) {
db.all("SELECT title, overview, release_date, inventory FROM movies ORDER BY title ASC LIMIT ?", num, callback);
}
else {
// Assume both num and page are specified
db.all("SELECT title, overview, release_date, inventory FROM movies WHERE id NOT IN ( SELECT id FROM movies ORDER BY title ASC LIMIT ?) ORDER BY title ASC LIMIT ?", (page - 1) * num, num, callback);
}
},

// GET /movies/title/:title/checked_out_current
getCheckedOutCustomersByTitle: function(title, res) {
db.all("SELECT customers.id, customers.name, customers.phone, rentals.check_out_date FROM customers INNER JOIN rentals ON customers.id = rentals.customer_id INNER JOIN movies ON movies.id = rentals.movie_id WHERE movies.title LIKE ? AND rentals.check_in_date IS NULL", title, function(err, rows) {
if (err !== null) {
console.log(err);
}
res.status(200).json(rows);
});
},

// GET /movies/title/:title/checked_out_history?ordered_by=XXX
// ordered_by id, name, check out date
getCheckedOutHistoryByTitle: function(title, ordered_by, res) {

var order = "customers.id";
if (ordered_by == "id") {
order = "customers.id";
}
else if (ordered_by == "name")
order = "customers.name";
else if (ordered_by == "check_out_date" || ordered_by == "checkout_date")
order = "rentals.check_out_date";
else if (ordered_by !== undefined)
order = ordered_by;

var statement =
"SELECT customers.id, customers.name, customers.phone, rentals.check_out_date \
FROM customers INNER JOIN rentals ON customers.id = rentals.customer_id INNER JOIN movies ON movies.id = rentals.movie_id \
WHERE movies.title LIKE ? AND rentals.check_in_date IS NOT NULL ORDER BY " + order;

// SELECT customers.id, customers.name, customers.phone, rentals.check_out_date FROM customers INNER JOIN rentals ON customers.id = rentals.customer_id INNER JOIN movies ON movies.id = rentals.movie_id WHERE movies.title LIKE 'psycho' AND rentals.check_in_date IS NOT NULL ORDER BY customers.id;
db.all(statement, title, function(err, rows) {
if (err !== null) {
console.log(err);
}
res.status(200).json(rows);
});
}
}
Loading