Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
208 commits
Select commit Hold shift + click to select a range
2c10b8d
Adds initial documentation.
catchingash Sep 17, 2015
14e42dd
express generated a node skeleton
lindseyreno Sep 17, 2015
6276595
added db/
lindseyreno Sep 17, 2015
653ca47
added sqlite3 and removed jade and serve-favicon
lindseyreno Sep 17, 2015
276e107
not using view stuff
lindseyreno Sep 17, 2015
9a61351
moved seed files to their own folder
lindseyreno Sep 17, 2015
7150bcc
created schema script
lindseyreno Sep 17, 2015
6f56b77
Moved seed data into utils directory.
catchingash Sep 17, 2015
c973206
created rentals seed data
lindseyreno Sep 18, 2015
748fd68
Adds seed script.
catchingash Sep 18, 2015
77b5725
Adds command line commands 'cause I'm lazy.
catchingash Sep 18, 2015
d838c20
Fixes issues with our commented-out implementation of #seedRentals.
catchingash Sep 18, 2015
c862887
deleted comments
lindseyreno Sep 18, 2015
691d6d7
initial test setup and tests for #create
lindseyreno Sep 18, 2015
e236d92
created database #create, still wip
lindseyreno Sep 18, 2015
8c042cf
Created Movie model + made Database#create work for all model types.
catchingash Sep 18, 2015
2b1557c
Minor updates.
catchingash Sep 18, 2015
4f327d2
removed line used for testing
lindseyreno Sep 18, 2015
4f62a78
created a rentals model
lindseyreno Sep 18, 2015
116b391
created tests for rentals model, tests currently pass
lindseyreno Sep 18, 2015
2b48609
Small refactors.
catchingash Sep 18, 2015
67a1e2b
Merge pull request #1 from catchingash/anw/db#all/anw+lr
lindseyreno Sep 20, 2015
5cf50bb
Merge branch 'anw+lr/master' of github.com:catchingash/C3Projects--Vi…
lindseyreno Sep 20, 2015
f86e83c
Adds Database#all + test.
catchingash Sep 21, 2015
b1c6092
Updates error message to be more descriptive.
catchingash Sep 21, 2015
092f71b
Adds Database#findBy; Adds movie#findBy test.
catchingash Sep 21, 2015
4aa6340
Minor changes.
catchingash Sep 21, 2015
279c9c5
added update function
lindseyreno Sep 21, 2015
110d194
added test for update function
lindseyreno Sep 21, 2015
3684979
Merge pull request #2 from catchingash/anw/db#all/anw+lr
lindseyreno Sep 21, 2015
b52ead0
Merge branch 'anw+lr/master' of github.com:catchingash/C3Projects--Vi…
catchingash Sep 21, 2015
46ca675
Updates test to be more specific.
catchingash Sep 21, 2015
9476e48
changed the name of #update to #return and changed the sql statement …
lindseyreno Sep 21, 2015
6c0d51d
Merge pull request #3 from catchingash/anw/db#findBy/anw+lr
lindseyreno Sep 21, 2015
7a912c5
fixed merge conflicts
lindseyreno Sep 21, 2015
dbf7de1
Adds Customer model
catchingash Sep 21, 2015
93d8eb8
Adds test for Customer#create
catchingash Sep 21, 2015
393bdce
added sortBy function that can select all or a specific number of rec…
lindseyreno Sep 21, 2015
2f28e42
added tests for sortBy function
lindseyreno Sep 21, 2015
1250c21
Adds #resetCustomersTable to Customer beforeEach
catchingash Sep 21, 2015
6d1e62a
Adds tests for Customer#create validation checks.
catchingash Sep 21, 2015
919d0d2
Adds test for Customer#all
catchingash Sep 21, 2015
d98a8d9
function names match now
lindseyreno Sep 21, 2015
2c16889
Merge pull request #4 from catchingash/lr/rentals
catchingash Sep 21, 2015
dc79a21
Merge branch 'anw+lr/master' of github.com:catchingash/C3Projects--Vi…
lindseyreno Sep 21, 2015
575648c
Adds tests for Customer#findBy.
catchingash Sep 21, 2015
b9bdfc1
Merge branch 'anw+lr/master' of github.com:catchingash/C3Projects--Vi…
catchingash Sep 21, 2015
aef5b31
Merge pull request #6 from catchingash/anw/CustomerModel/anw+lr
lindseyreno Sep 21, 2015
743ce0d
changed variables to question marks
lindseyreno Sep 21, 2015
ab0aa31
changed return function name to checkIn because of reserved key words
lindseyreno Sep 21, 2015
b0912ea
Merge pull request #5 from catchingash/lr/sorted_by
catchingash Sep 21, 2015
45ec474
Changed models/rentals.js --> models/rental.js
catchingash Sep 21, 2015
3080500
Renamed rentals_test.js --> rental_test.js
catchingash Sep 21, 2015
9948334
Renamed models/movies.js --> models/movie.js
catchingash Sep 21, 2015
b39f09f
Renamed movies_test.js --> movie_test.js
catchingash Sep 21, 2015
167b918
Merge pull request #8 from catchingash/anw/update_model_names/anw+lr
lindseyreno Sep 21, 2015
2038017
Bug Fix: if there's an err during create, our res code will break.
catchingash Sep 21, 2015
c2ec13b
changed if statement in sortBy function to allow for different variat…
lindseyreno Sep 21, 2015
f7441d5
Adds this.columnNames attribute to all models.
catchingash Sep 21, 2015
7ba27a4
Capitalizes 'integer' to match the other column types.
catchingash Sep 21, 2015
016dd0d
Adds missing ;
catchingash Sep 21, 2015
a331b62
Comments out unused `require`
catchingash Sep 21, 2015
c5564e5
added validMovieData to DRY things up a bit and added test for movie …
lindseyreno Sep 21, 2015
99b958f
Removes unnecessary #finalize lines (we don't use #prepare anymore)
catchingash Sep 21, 2015
e3a98f2
added test for uniqueness of movie title
lindseyreno Sep 21, 2015
f3628be
made 'integer' uppercase to be consistent
lindseyreno Sep 21, 2015
8b218fa
added test for movie inventory defaulting to zero
lindseyreno Sep 21, 2015
222a2d5
Adds 'id' to columnNames attribute.
catchingash Sep 21, 2015
6adab6f
Adds a check that parameters are valid before making a db query.
catchingash Sep 21, 2015
a7e19ee
Merge pull request #9 from catchingash/anw/update_model_names/anw+lr
lindseyreno Sep 21, 2015
4334e9b
Merge pull request #10 from catchingash/movie_model_validation_tests/lr
catchingash Sep 21, 2015
ca4eaef
Fixes typo.
catchingash Sep 21, 2015
439d935
created validRentalData for reuse
lindseyreno Sep 21, 2015
9684f86
Merge pull request #11 from catchingash/anw/add_columnNames_attribute…
lindseyreno Sep 21, 2015
24b3d7e
Merge branch 'anw+lr/master' of github.com:catchingash/C3Projects--Vi…
lindseyreno Sep 21, 2015
b6d4a93
fixed typo and made #checkIn better
lindseyreno Sep 21, 2015
7cda11a
Adds tests for #findBy: confirms that the param is valid.
catchingash Sep 21, 2015
8a8c39f
added test for #all
lindseyreno Sep 21, 2015
4a3c05f
added tests for #findBy
lindseyreno Sep 21, 2015
ee1a535
added tests for #sortBy
lindseyreno Sep 21, 2015
71b83e5
Merge pull request #12 from catchingash/anw/add_columnNames_attribute…
lindseyreno Sep 22, 2015
5c591b1
Merge branch 'anw+lr/master' of github.com:catchingash/C3Projects--Vi…
lindseyreno Sep 22, 2015
d540cc1
added validation tests for #create
lindseyreno Sep 22, 2015
0761d4e
Adds sqlite as a command because I'm lazy.
catchingash Sep 22, 2015
2e38ade
Adds customer#movies + associated tests.
catchingash Sep 22, 2015
a43f237
Merge pull request #13 from catchingash/update_rental_tests/lr
catchingash Sep 22, 2015
67c9141
Merge branch 'anw+lr/master' of github.com:catchingash/C3Projects--Vi…
catchingash Sep 22, 2015
b6dc6eb
added tests for #sortBy
lindseyreno Sep 22, 2015
63fddbf
Merge pull request #14 from catchingash/add_sortBy_tests_to_customer/lr
catchingash Sep 22, 2015
a0d0570
Adds customer#rentals + one test.
catchingash Sep 22, 2015
0804a9b
Merge branch 'anw+lr/master' of github.com:catchingash/C3Projects--Vi…
catchingash Sep 22, 2015
11fbe38
added offset to #sortBy
lindseyreno Sep 22, 2015
ec04bca
added test to #sortBy for added offset and added another customer to …
lindseyreno Sep 22, 2015
12ffd9c
changed undefined to null
lindseyreno Sep 22, 2015
0e29e13
fixed 3 #sortBy tests that broke when offset was added to #sortBy
lindseyreno Sep 22, 2015
cd02884
fixed broken #sortBy tests
lindseyreno Sep 22, 2015
250bcde
fixed broken #sortBy tests
lindseyreno Sep 22, 2015
b9ca91e
added to test seed data and added test for offset for #sortBy
lindseyreno Sep 22, 2015
1594c12
added to test seed data and added test for offset in #sortBy
lindseyreno Sep 22, 2015
7ee8add
Merge pull request #15 from catchingash/anw/customer#movies/anw+lr
lindseyreno Sep 22, 2015
9c507f8
Merge branch 'anw+lr/master' of github.com:catchingash/C3Projects--Vi…
lindseyreno Sep 22, 2015
8915a43
Merge pull request #16 from catchingash/add_paging/lr
catchingash Sep 22, 2015
6ea924c
added #customersCurrent to get a list of customers who currently have…
lindseyreno Sep 22, 2015
a959472
added test for #customersCurrent and functions to reset the customers…
lindseyreno Sep 22, 2015
c45ba4c
added checkout_date to test for #customersCurrent
lindseyreno Sep 22, 2015
590dcac
added checkout_date to the statement
lindseyreno Sep 22, 2015
f674ef6
added #customersPast
lindseyreno Sep 22, 2015
821252d
changed id to rentals_id in #customersCurrent, added tests for #custo…
lindseyreno Sep 22, 2015
f953aa0
nested #customersCurrent and #customersPast describe blocks so they c…
lindseyreno Sep 22, 2015
9b13ecc
Merge branch 'anw+lr/master' of github.com:catchingash/C3Projects--Vi…
lindseyreno Sep 22, 2015
0abe1c6
Merge pull request #17 from catchingash/movie_customers_current/lr
catchingash Sep 23, 2015
be7860b
Adds a db:test:reset command to the node package manager. 'Cause I'm …
catchingash Sep 23, 2015
b04b597
added test for #overdue and added seed data
lindseyreno Sep 23, 2015
dd6fae3
fixed checkout_date
lindseyreno Sep 23, 2015
928042e
added #overdue to get a list of customers with overdue movies
lindseyreno Sep 23, 2015
ad5bb1a
Merge pull request #18 from catchingash/anw/customer#movies/anw+lr
lindseyreno Sep 23, 2015
5e76c50
Merge branch 'anw+lr/master' of github.com:catchingash/C3Projects--Vi…
lindseyreno Sep 23, 2015
403f914
Merge branch 'anw+lr/master' of github.com:catchingash/C3Projects--Vi…
lindseyreno Sep 23, 2015
36222a2
Refactors how we reset/seed the test db.
catchingash Sep 23, 2015
9bc7be0
Merge pull request #19 from catchingash/overdue/lr
catchingash Sep 23, 2015
c0e64b6
Merge branch 'anw+lr/master' of github.com:catchingash/C3Projects--Vi…
catchingash Sep 23, 2015
91395de
fixed sortBy so it is actually sorting
lindseyreno Sep 23, 2015
ea2ad68
Merge pull request #20 from catchingash/anw/refactor_dbCleaner/anw+lr
lindseyreno Sep 23, 2015
7cb35de
Merge branch 'anw+lr/master' of github.com:catchingash/C3Projects--Vi…
lindseyreno Sep 23, 2015
f676f9c
Fix: changes customer date format in the seed data.
catchingash Sep 23, 2015
bbb4eff
fixed merge conflicts
lindseyreno Sep 23, 2015
6723f10
Merge pull request #22 from catchingash/anw/bug_fix/anw+lr
lindseyreno Sep 23, 2015
4bd5aa4
Merge pull request #21 from catchingash/sortBy_bug_fix/lr
catchingash Sep 23, 2015
305fb4c
Adds SuperTest to package.json.
catchingash Sep 23, 2015
165d350
Removes unused files + unused lines from app.js.
catchingash Sep 23, 2015
b8c9cab
WIP -- Adds CustomersController#index setup + limited testing.
catchingash Sep 23, 2015
d35d424
Merge pull request #23 from catchingash/anw/customers#index/anw+lr
lindseyreno Sep 23, 2015
9f59a1a
Adds a few tests, plus uses asynchronous testing.
catchingash Sep 23, 2015
4697501
Merge pull request #24 from catchingash/anw/customers#index/anw+lr
catchingash Sep 23, 2015
c8c8c5c
added if statement to index to handle status = 'overdue'
lindseyreno Sep 23, 2015
353166e
added tests for /customers?status=overdue and changed back to synchro…
lindseyreno Sep 23, 2015
c9fa22e
Updated documentation to not be so deceiving.
catchingash Sep 23, 2015
ba512d0
Merge branch 'anw+lr/master' of github.com:catchingash/C3Projects--Vi…
catchingash Sep 23, 2015
32f7135
added route for /movies/
lindseyreno Sep 23, 2015
95eabcd
removed only
lindseyreno Sep 23, 2015
ba87bcd
added movie controller
lindseyreno Sep 23, 2015
eb8f2c8
added movie route
lindseyreno Sep 23, 2015
25d761d
added tests for get /movies with no query
lindseyreno Sep 23, 2015
51ada49
added test for /movies with optional sort option
lindseyreno Sep 24, 2015
f2c14ed
added if statement to handle optional sort query
lindseyreno Sep 24, 2015
14aacd6
rewrote index to handle all query options
lindseyreno Sep 24, 2015
adcd69e
added tests for /movies with sort and n, and /movies with sort and n …
lindseyreno Sep 24, 2015
d2c7e22
fixed #overdue since seed data had rentals become overdue today
lindseyreno Sep 24, 2015
2414d19
removed .only
lindseyreno Sep 24, 2015
82f263e
removed leading &'s
lindseyreno Sep 24, 2015
d567462
Bug fix: handled if invalid input for n provided to #sortyBy.
catchingash Sep 24, 2015
3aa3b87
changed error and added handling of n or p if NaN
lindseyreno Sep 24, 2015
12a71a2
added more tests to cover edge cases
lindseyreno Sep 24, 2015
17368ea
Adds '/customer?sort' endpoint + options + testing.
catchingash Sep 24, 2015
02c2965
changed assertion in #sorBy to match change in code
lindseyreno Sep 24, 2015
16055db
changed error status code and added error message
lindseyreno Sep 24, 2015
cbe0bb3
Merge pull request #26 from catchingash/anw/customers#index/anw+lr
lindseyreno Sep 24, 2015
6236beb
fixed merge conflict
lindseyreno Sep 24, 2015
a2273ff
Merge pull request #27 from catchingash/get_movies/lr
catchingash Sep 24, 2015
d2afbd5
Adds framework for RentalsController + testing.
catchingash Sep 24, 2015
e59a24e
Merge pull request #28 from catchingash/anw/rentals_controller/anw+lr
lindseyreno Sep 24, 2015
781c363
Fixed typo
catchingash Sep 24, 2015
e122599
Merge pull request #29 from catchingash/anw/rentals_controller/anw+lr
lindseyreno Sep 24, 2015
0843220
added route for finding a movie by the title
lindseyreno Sep 24, 2015
313a9a8
added show function
lindseyreno Sep 24, 2015
8e2368b
added tests for /movies/:title endpoint
lindseyreno Sep 24, 2015
8694767
Merge branch 'anw+lr/master' of github.com:catchingash/C3Projects--Vi…
lindseyreno Sep 24, 2015
b998cf5
made describe more specific in GET /dog
lindseyreno Sep 24, 2015
0ede181
updated keys for overdue
lindseyreno Sep 24, 2015
2b6a7c5
updated overdue statement to return rental_id and changed id to custo…
lindseyreno Sep 24, 2015
27f6788
fixed typo and changed parameter to 'title'
lindseyreno Sep 24, 2015
36eda1c
removed numMoviesSeeded if it isn't being used
lindseyreno Sep 24, 2015
79d6e39
Merge pull request #30 from catchingash/movies_by_title_endpoint/lr
catchingash Sep 24, 2015
24da861
Merge pull request #31 from catchingash/overdue_fix/lr
catchingash Sep 24, 2015
ef79c98
added test for updating/checking in a rental
lindseyreno Sep 25, 2015
417eb93
added route for put /:title for updating/checking in a rental
lindseyreno Sep 25, 2015
b4cf869
added update to the rentals controller
lindseyreno Sep 25, 2015
df33c50
changed test for #checkIn to include customer_id as a parameter
lindseyreno Sep 25, 2015
6662d1d
changed #checkIn to include customer_id as a parameter and in the sta…
lindseyreno Sep 25, 2015
48f0408
changed test for put /:customer_id to use customer_id in the params a…
lindseyreno Sep 25, 2015
c7361b6
changed the put route to use customer_id instead of title
lindseyreno Sep 25, 2015
dac9dbb
changed update to include the customer_id as a parameter of #checkIn
lindseyreno Sep 25, 2015
0a817fe
updated documentation of checking in a rental to reflect changes in t…
lindseyreno Sep 25, 2015
6098a57
changed n and p in the statements for #sortBy to ?
lindseyreno Sep 25, 2015
b19f1eb
Merge pull request #33 from catchingash/change_sortBy/lr
catchingash Sep 25, 2015
3c66c2a
Merge pull request #32 from catchingash/update_rental/lr
catchingash Sep 25, 2015
4be5be4
changed rental movie_title seed data to match movie seed data and add…
lindseyreno Sep 25, 2015
6cdb2f6
added function numAvail to determine the number of movies available t…
lindseyreno Sep 25, 2015
d808a21
Merge pull request #34 from catchingash/numAvail/lr
catchingash Sep 25, 2015
eac24db
added tests for new endpoint /movies/:title/customers with query options
lindseyreno Sep 25, 2015
6041dfb
added route for /movies/:title/customers
lindseyreno Sep 25, 2015
916006c
added customers function for new /movies/:title/customers endpoint
lindseyreno Sep 25, 2015
9b6dc2d
had to change ORDER BY ? to the parameter or it would not actually or…
lindseyreno Sep 25, 2015
e1deb54
added requirements for /movies/:title/customers
lindseyreno Sep 25, 2015
52a4b50
Merge branch 'anw+lr/master' of github.com:catchingash/C3Projects--Vi…
lindseyreno Sep 25, 2015
91a6717
Removed unused `require sqlite`s.
catchingash Sep 25, 2015
b03f6a0
Adds Rental#checkOut model method + green tests
catchingash Sep 25, 2015
96c1dcf
Adds Rentals#create + green tests
catchingash Sep 25, 2015
3a93c23
Merge branch 'anw+lr/master' of github.com:catchingash/C3Projects--Vi…
catchingash Sep 25, 2015
633a03c
Fixes merge typo.
catchingash Sep 25, 2015
0f1cec7
Merge pull request #35 from catchingash/movies_title_customers/lr
catchingash Sep 25, 2015
be50a20
Merge branch 'anw+lr/master' of github.com:catchingash/C3Projects--Vi…
catchingash Sep 25, 2015
36b484e
Merge pull request #36 from catchingash/anw/rentals_controller/anw+lr
lindseyreno Sep 25, 2015
f603b35
changed test for GET /Jaws to include num_available of movie for rent
lindseyreno Sep 26, 2015
05db641
added the results of the numAvail function to the results of the find…
lindseyreno Sep 26, 2015
ad95f1a
changed the #overdue test as a seed rental became overdue today
lindseyreno Sep 26, 2015
6795760
Merge pull request #37 from catchingash/fix_movie_title/lr
lindseyreno Sep 26, 2015
32ef483
Adds 'customers/:id/rentals' + green tests!
catchingash Sep 26, 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
32 changes: 32 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,33 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git-
node_modules

# Debug log from npm
npm-debug.log

.DS_Store
db/
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,5 @@ The API you build should have the following capabilities. The schema of your dat
- We will use [Mocha](https://mochajs.org/) for tests.
- There isn't a coverage requirement for this project, beyond demonstrating that every endpoint is covered by some manner of tests.

#### Run once prior to running tests
`DB=test npm run db:schema`
52 changes: 52 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var app = express();

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')));

// routes
app.use('/customers', require('./routes/customers'));
app.use('/movies', require('./routes/movies'));
app.use('/rentals', require('./routes/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);
}
44 changes: 44 additions & 0 deletions controllers/customers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"use strict";

var Customer = require('../models/customer');

var Controller = {
index: function(req, res, next) {
if (req.query.status == 'overdue') {
new Customer().overdue(Controller.sendJSON.bind(res));
} else if (req.query.sort) {
new Customer().sortBy(req.query.sort, req.query.n, req.query.p, Controller.sendJSON.bind(res));
} else {
new Customer().all(Controller.sendJSON.bind(res));
}
},

rentals: function(req, res, next) {
new Customer().rentals(req.params.id, Controller.formatRentalsSendJON.bind(res));
},

formatRentalsSendJON: function(err, res) {
var results = { current: [], past: [] };

for (var i = 0; i < res.length; i++) {
if (res[i].return_date) {
results.past.push(res[i]);
} else {
results.current.push(res[i]);
}
}

Controller.sendJSON.call(this, err, results);
},

sendJSON: function(err, res) {
if (err) {
var status = err.status == 400 ? 400 : 500;
this.status(status).json(err.message);
} else {
this.status(200).json(res);
}
}
}

module.exports = Controller;
49 changes: 49 additions & 0 deletions controllers/movies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"use strict";

var Movie = require('../models/movie');

var Controller = {
index: function(req, res, next) {
if (req.query.sort) {
new Movie().sortBy(req.query.sort, req.query.n, req.query.p, Controller.sendJSON.bind(res));
} else {
new Movie().all(Controller.sendJSON.bind(res));
}
},

show: function(req, res, next) {
new Movie().findBy('title', req.params.title, Controller.formatThenSendJSON.bind(res));
},

formatThenSendJSON: function(err, results) {
new Movie().numAvail(results[0].title, function(err, otherResults) {
results = results[0];
results.num_available = otherResults[0].num_available;

Controller.sendJSON.apply(this, [err, results]);

}.bind(this));

},

customers: function(req, res, next) {
if (req.query.status == 'current') {
new Movie().customersCurrent(req.params.title, Controller.sendJSON.bind(res));
} else if (req.query.status == 'past') {
new Movie().customersPast(req.params.title, req.query.sort, Controller.sendJSON.bind(res));
} else {
res.status(400).json("Bad request");
}
},

sendJSON: function(err, res) {
if (err) {
var status = err.status == 400 ? 400 : 500;
this.status(status).json(err.message);
} else {
this.status(200).json(res);
}
}
}

module.exports = Controller;
25 changes: 25 additions & 0 deletions controllers/rentals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"use strict";

var Rental = require('../models/rental');

var Controller = {
create: function(req, res, next) {
var rental = new Rental()
rental.checkOut(req.body, Controller.sendJSON.bind(res));
},

update: function(req, res, next) {
new Rental().checkIn(req.body.movie_title, req.body.return_date, req.params.customer_id, Controller.sendJSON.bind(res))
},

sendJSON: function(err, res) {
if (err) {
var status = err.status == 400 ? 400 : 500;
this.status(status).json(err.message);
} else {
this.status(200).json(res);
}
}
}

module.exports = Controller;
69 changes: 69 additions & 0 deletions documentation.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
### Customers
GET '/customers'
// => [{<customer>}, {<customer>}] <-- includes customer attributes
- See a list of customers with overdue movies
// ?&status=overdue
- Retrive a subset of customers
- Given a sort column, return _n_ customer records, offset by _p_ records (this will be used to create "pages" of customers)
// ?&n=12&p=2&sort=_____
// sort options: name, registered_at, postal_code
- Sort columns are
- `name`
- `registered_at`
- `postal_code`
GET 'customers/:id/rentals'
- Given a customers `id`...
- List the movies they _currently_ have checked out
+ include rental id
- List the movies a customer has checked out in the past
+ include rental id
- ordered by check out date
- includes return date

### Movies
GET '/movies'
// => [{<movie>}, {<movie>}] <-- includes movie attributes
// OPTIONAL
// ?n=12&p=2&sort=_____
// sort options: title, release_date
- Retrieve a list of all movies
- 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
- `title`
- `release_date`
GET 'movies/:title/customers'
// => {<movie>} <-- title is REQUIRED TO BE unique
// ?status=____&sort=_____
// status options: current, past
// status required for this endpoint
// sort options: id, name, checkout_date
// sort required for status past
- Given a movies `title`...
+ include rental ids
- Get a list of customers that have _currently_ checked out a copy of the film
- See a list of customers that have _currently_ checked out any of the movie's inventory

- Get a list of customers that have checked out a copy _in the past_
- ordered by customer `id`
- ordered by customer `name`
- ordered by check out date

GET '/movies/:title'
// => {<movie>} <-- title is REQUIRED TO BE unique
- Look a movie up by title to see
- its synopsis
- release date
- and inventory total
- Know if a movie has any inventory available to rent (num avail, num checked out, which/whatever)

### Rental
- Given a customer's `id` and a movie's `title` ...
POST '/rentals'
// in the body, include customer/movie details
- "check out" one of the movies inventory to the customer
- Establish a return date
- Charge the customers account (cost up to you)
PUT '/rentals/:customer_id'
- "check in" one of customers rentals
- return the movie to its inventory
Loading