Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
104 commits
Select commit Hold shift + click to select a range
9f2e721
Fresh node install.
sallyamoore Sep 15, 2015
e606316
Merge branch 'lw+sm/master' of github.com:sallyamoore/C3Projects--Vid…
sallyamoore Sep 15, 2015
03673d6
Added scaffolding notes so we both have access to them.
sallyamoore Sep 15, 2015
ae08ed1
Set up practice route and controller to return zomg.
sallyamoore Sep 15, 2015
fcb55f5
Added database.js, movies controller, and movies routes files.
sallyamoore Sep 17, 2015
eeec08d
Added mocha for testing, wrote a coupel preliminary tests, wrote init…
sallyamoore Sep 17, 2015
b4c36e9
Created schema and seed functionality for customers table, then seeded.
sallyamoore Sep 17, 2015
2b7f39b
moved seed files
sallyamoore Sep 17, 2015
aa9440c
added number available to movie schema
wangg131 Sep 17, 2015
eead190
added foreign keys to rental tables
wangg131 Sep 17, 2015
fea1b24
Merge pull request #1 from sallyamoore/rental-setup
wangg131 Sep 17, 2015
bc2e17b
Implemented a few more tests. WIP movies table initial tests.
sallyamoore Sep 17, 2015
03adb91
reorganized file structure and updated package.json
wangg131 Sep 18, 2015
64b73a3
WIP relationship between movies model and controller
wangg131 Sep 18, 2015
d3e40d7
First endpoint (to get all movies) is working. YAY.
sallyamoore Sep 18, 2015
da3013e
Set up schema in test.db and wrote test for /movies endpoint.
sallyamoore Sep 18, 2015
a0176d1
Added endpoint to select column and return movie info for n movies of…
sallyamoore Sep 18, 2015
9b57fe7
created endpoints for all customers and subset of customers
wangg131 Sep 18, 2015
f4aad09
tests for customer
wangg131 Sep 18, 2015
0168cea
wip rental checkout
wangg131 Sep 18, 2015
8a12d82
WIP checkout functionality.
sallyamoore Sep 19, 2015
8cf32b0
WIP post to rentals. Not working yet. Moving on to something else til…
sallyamoore Sep 21, 2015
56f26e5
Merge pull request #2 from sallyamoore/sm-checkout
sallyamoore Sep 21, 2015
c94aa5f
cleaning up a bit of the unfinished checkout stuff.
sallyamoore Sep 21, 2015
5675827
not customers rentals.. got a list of customers that has checked out …
wangg131 Sep 21, 2015
4f0f534
Merge pull request #3 from sallyamoore/customers_rentals
sallyamoore Sep 21, 2015
dce88c8
Added endpoint to checkout history for a given customer id.
sallyamoore Sep 21, 2015
be6c43e
fixed subset queries for movies and customers so all columns are retu…
sallyamoore Sep 22, 2015
4e73359
Reformatted dates in rental seed file. Created endpoint for list of c…
sallyamoore Sep 22, 2015
8483a13
look up movie by title
wangg131 Sep 22, 2015
228a157
customers by movie history ordered
wangg131 Sep 22, 2015
9269e25
started some wip tests for movies_by_customer_history
wangg131 Sep 22, 2015
6c7db67
added different sort columns for movie's history
wangg131 Sep 22, 2015
d3f97ae
Merge pull request #4 from sallyamoore/sm-sort-cust-and-movies
sallyamoore Sep 22, 2015
37a4e27
Merge pull request #5 from sallyamoore/sm-overdue-movies
sallyamoore Sep 22, 2015
b5bed27
movie history and info
wangg131 Sep 22, 2015
b30fc88
sorting customers that previously checked out specified movie.
sallyamoore Sep 22, 2015
4840bc8
mergin changes
wangg131 Sep 22, 2015
b134815
Merge pull request #6 from sallyamoore/get_movie_title
sallyamoore Sep 22, 2015
793096f
resolving merge conflicts.
sallyamoore Sep 22, 2015
d8fb238
Merge pull request #7 from sallyamoore/cust-movie-hist-sort
sallyamoore Sep 22, 2015
7038991
Refined endpoints to get customer who checked out specified movie in …
sallyamoore Sep 22, 2015
3d8bc2c
Post to checkout a movie. YAY
sallyamoore Sep 22, 2015
cb0e751
checkout out a rental subtracts account credit
wangg131 Sep 22, 2015
308db04
rental cost as variable
wangg131 Sep 22, 2015
2daf11b
Merge pull request #8 from sallyamoore/subtract-credit
sallyamoore Sep 22, 2015
e50d5a4
Finished check out functionality - updates customer credit and number…
sallyamoore Sep 23, 2015
43deb25
Merge pull request #9 from sallyamoore/sm-more-checkout
sallyamoore Sep 23, 2015
cb2054e
movie controller tests
wangg131 Sep 23, 2015
ad44ab6
movies controller tests wip
wangg131 Sep 23, 2015
5739bb8
separated controller and model tests wip
wangg131 Sep 23, 2015
08b8668
changed some test pathing issues
wangg131 Sep 23, 2015
f97fa59
Merge pull request #10 from sallyamoore/subtract-credit
sallyamoore Sep 23, 2015
63031e3
Customer model tests
sallyamoore Sep 23, 2015
c9768f1
Finished customer model tests. YAY
sallyamoore Sep 23, 2015
751fb58
Tested sort order of customers_overdue.
sallyamoore Sep 23, 2015
ca5de4f
movie controller tests wip
wangg131 Sep 23, 2015
74948e2
working movie controller tests
wangg131 Sep 23, 2015
b9c9895
Customer controller tests.
sallyamoore Sep 23, 2015
2b2f2f1
formatted controller tests
wangg131 Sep 23, 2015
aa864da
Merge pull request #11 from sallyamoore/subtract-credit
wangg131 Sep 23, 2015
857edf6
Completed controllers tests for all customer endpoints.
sallyamoore Sep 23, 2015
9fdaab3
Merge conflicts
sallyamoore Sep 23, 2015
d795bcf
Merging master onto branch.
sallyamoore Sep 23, 2015
6e35598
wip movie model tests and minor changes to controller tests
wangg131 Sep 23, 2015
134b593
movie model tests
wangg131 Sep 23, 2015
7b00ce9
Merge pull request #12 from sallyamoore/sm-test
sallyamoore Sep 23, 2015
c79a0c6
done with movie tests
wangg131 Sep 23, 2015
47f0376
mergin
wangg131 Sep 23, 2015
6db6743
Merge pull request #13 from sallyamoore/lw-movie-mod-tests
wangg131 Sep 23, 2015
3be0746
WIP check in movie
sallyamoore Sep 23, 2015
fc8892d
Resolving merge conflicts
sallyamoore Sep 23, 2015
25c3aff
Merge pull request #14 from sallyamoore/checkin
sallyamoore Sep 23, 2015
bdabfe4
check in working for now..
wangg131 Sep 24, 2015
7729e50
dev db adding forgot
wangg131 Sep 24, 2015
8008728
added back updated_id to check_in
wangg131 Sep 24, 2015
8ce7452
rental model tests, #check_out.
sallyamoore Sep 24, 2015
c288b81
finished model tests for rentals.
sallyamoore Sep 24, 2015
a14cf30
working on date formatting so that customers/overdue works correctly.
sallyamoore Sep 24, 2015
15060dd
Formatting dates. Extracted function into helpers/date_helper.
sallyamoore Sep 24, 2015
5189bd9
account credit cant go below zero
wangg131 Sep 24, 2015
1101e57
Merge pull request #15 from sallyamoore/sm-bugs
sallyamoore Sep 24, 2015
0b0cf51
mergin
wangg131 Sep 24, 2015
ed5281b
Merge pull request #16 from sallyamoore/lw-bugs
wangg131 Sep 24, 2015
60b4edf
updating rental functions to prevent execution of statements if one f…
sallyamoore Sep 24, 2015
3f9f36f
Now only updating movie num_available and customer#account_credit if …
sallyamoore Sep 24, 2015
4301e7e
Merge pull request #17 from sallyamoore/sm-inventory
sallyamoore Sep 25, 2015
0be8098
better rental controller tests
wangg131 Sep 25, 2015
a86b7f5
rental controller tests
wangg131 Sep 25, 2015
405de1d
deleted an unnecessary variable
wangg131 Sep 25, 2015
6461c37
rental patch test
wangg131 Sep 25, 2015
56356e2
test db
wangg131 Sep 25, 2015
519e657
Added constraints to inventory and num_available in movies. Cannot ch…
sallyamoore Sep 25, 2015
f4ee0c9
Merge pull request #18 from sallyamoore/rental-test
wangg131 Sep 25, 2015
e6257e6
fixing tests that failed b/c of changes to rentals model.
sallyamoore Sep 25, 2015
1b44276
Cleaning up stray deprecated code.
sallyamoore Sep 25, 2015
991e714
resolving merge conflicts
sallyamoore Sep 25, 2015
be563d5
Merge pull request #19 from sallyamoore/sm-inventory-4real
sallyamoore Sep 25, 2015
f0ab961
refactored model files into separate files
wangg131 Sep 25, 2015
7d754ed
mergin refactor
wangg131 Sep 25, 2015
72e5eff
Merge pull request #20 from sallyamoore/lw-refactor
wangg131 Sep 25, 2015
064a35d
Resetting the databases.
sallyamoore Sep 25, 2015
f5a456e
Fixed little things that broke in our tests when we merged.
sallyamoore Sep 25, 2015
39c8eb7
Adding back put route for rentals.
sallyamoore 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.DS_Store
node_modules
npm-debug.log
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('/movies', movies);
app.use('/customers', customers);
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')('app: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);
}
39 changes: 39 additions & 0 deletions controllers/customers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"use strict";
var Customer = require('../models/customer'); //class needs to be instantiated

exports.customersController = {
index: function(req, res) {
var db = new Customer();
db.find_all(function(err, result) {
return res.status(200).json(result);
});
},

by_column: function(req, res) {
var db = new Customer();
db.by_column(req.params.column, req.params.n, req.params.p, function(err, result) {
return res.status(200).json(result);
});
},

movies_by_customer_current: function(req, res) {
var db = new Customer();
db.movies_by_customer_current(req.params.customer_id, function(err, result) {
return res.status(200).json(result);
});
},

movies_by_customer_history: function(req, res) {
var db = new Customer();
db.movies_by_customer_history(req.params.customer_id, function(err, result) {
return res.status(200).json(result);
});
},

customers_overdue: function(req, res) {
var db = new Customer();
db.customers_overdue(function(err, result) {
return res.status(200).json(result);
});
}
};
12 changes: 12 additions & 0 deletions controllers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"use strict";

exports.indexController = {
zomg: function zomg(req,res) {
var results = {
zomg: "zomg it WORKED!",
moar: "zomg moar"
};

return res.status(200).json(results);
},
}
47 changes: 47 additions & 0 deletions controllers/movies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"use strict";
var Movie = require('../models/movie'); //class needs to be instantiated

exports.moviesController = {
index: function(req, res) {
var db = new Movie();
db.find_all(function(err, result) {

return res.status(200).json(result);
});
},

by_column: function(req, res) {
var db = new Movie();
db.by_column(req.params.column, req.params.n, req.params.p, function(err, result) {
return res.status(200).json(result);
});
},

customers_by_movie_current: function(req, res) {
var db = new Movie();
db.customers_by_movie_current(req.params.title, function(err, result) {
return res.status(200).json(result);
});
},

movie_info: function(req, res) {
var db = new Movie();
db.movie_info(req.params.title, function(err, result) {
return res.status(200).json(result);
});
},

customers_by_movie_history: function(req, res) {
var db = new Movie();
db.customers_by_movie_history(req.params.title, function(err, result) {
return res.status(200).json(result);
});
},

customers_by_movie_history_sorted: function(req, res) {
var db = new Movie();
db.customers_by_movie_history_sorted(req.params.title, req.params.table, req.params.column, function(err, result) {
return res.status(200).json(result);
});
}
};
27 changes: 27 additions & 0 deletions controllers/rentals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"use strict";
var Rental = require('../models/rental'); //class needs to be instantiated

var rentalsController = {
create: function(request, response, next) {
var customer_id = request.body.id,
movie_title = request.body.title,
db = new Rental();

db.check_out(customer_id, movie_title, function(err, result) {
return response.status(200).json(result);
});
},

update: function(request, response, next) {
var customer_id = request.body.id,
movie_title = request.body.title,
db = new Rental();

db.check_in(customer_id, movie_title, function(err, result) {
return response.status(200).json(result);
});
},

};

module.exports = rentalsController;
28 changes: 28 additions & 0 deletions database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"use strict";

var sqlite3 = require('sqlite3').verbose();
var db_env = process.env.DB || 'development';

var Database = {
find_all: function(callback) {
var db = new sqlite3.Database('db/' + db_env + '.db');
var statement = "SELECT * FROM " + this.table_name + ";";

db.all(statement, function(err, res) {
if (callback) callback(err, res);
db.close();
});
},

by_column: function(column, number, page, callback) {
var db = new sqlite3.Database('db/' + db_env + '.db');
var statement = "SELECT * FROM " + this.table_name + " ORDER BY " + column + " LIMIT " + number + " OFFSET " + page + ";";

db.all(statement, function(err, res) {
if (callback) callback(err, res);
db.close();
});
},
};

module.exports = Database;
Binary file added db/development.db
Binary file not shown.
Binary file added db/test.db
Binary file not shown.
15 changes: 15 additions & 0 deletions helpers/date_helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"use strict";

function format_date(days_to_add) {
var today = new Date(Date.now() + (days_to_add * 24 * 60 * 60 * 1000)),
today_dd = today.getDate(),
today_mm = today.getMonth() + 1,
today_yyyy = today.getFullYear(),
target_date;
if(today_dd < 10) { today_dd ='0'+ today_dd; }
if(today_mm < 10) { today_mm ='0'+ today_mm; }
target_date = today_yyyy + "-" + today_mm + "-" + today_dd;
return target_date;
}

module.exports = format_date;
46 changes: 46 additions & 0 deletions models/customer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"use strict";

var sqlite3 = require('sqlite3').verbose();
var db_env = process.env.DB || 'development';

function Customer() {
this.table_name = "customers";
}

Customer.prototype = require('../database');

// Called by Customers controller:
Customer.prototype.movies_by_customer_current = function(customer_id, callback) {
var db = new sqlite3.Database('db/' + db_env + '.db');
// all movies currently checked out by that customer
var statement = "SELECT 'movies'.* FROM movies INNER JOIN rentals ON movies.id = rentals.movie_id WHERE rentals.customer_id = (SELECT customers.id FROM customers WHERE customers.id = ? COLLATE NOCASE LIMIT 1) AND rentals.checked_out = 'true'; ";

db.all(statement, customer_id, function(err, rows) {
callback(err, rows);
db.close();
});
};

Customer.prototype.movies_by_customer_history = function(customer_id, callback) {
var db = new sqlite3.Database('db/' + db_env + '.db');
// all movies checked out by that customer in the past
var statement = "SELECT 'movies'.*, rentals.checkout_date, rentals.return_date FROM movies INNER JOIN rentals ON movies.id = rentals.movie_id WHERE rentals.customer_id = (SELECT customers.id FROM customers WHERE customers.id = ? COLLATE NOCASE LIMIT 1) AND rentals.checked_out = 'false' ORDER BY rentals.checkout_date; ";

db.all(statement, customer_id, function(err, rows) {
callback(err, rows);
db.close();
});
};

Customer.prototype.customers_overdue = function(callback) {
var db = new sqlite3.Database('db/' + db_env + '.db');
// all customers who currently have the movie checked out and past return_date
var statement = "SELECT * FROM customers INNER JOIN rentals ON customers.id = rentals.customer_id WHERE date(rentals.return_date) < date('now') AND rentals.checked_out = 'true' ORDER BY rentals.return_date; ";

db.all(statement, function(err, rows) {
callback(err, rows);
db.close();
});
};

module.exports = Customer;
Loading