Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
45b942a
Added node_modules and db to gitignore
lo-k Sep 17, 2015
f58c016
first npm install.
lo-k Sep 17, 2015
81efc00
Added more boilerplate directories for db and utils. Moved seeds into…
lo-k Sep 17, 2015
03f48df
Created database connection with a StoreDatabase object and a .query …
lo-k Sep 17, 2015
ae4831a
Created schema file which will drop, reset and migrate our db tables/…
lo-k Sep 17, 2015
ce0e282
Wrote tests for database.js file
lo-k Sep 17, 2015
7e688b6
Wrote SQL statements and db.serialize loops to seed our db with movie…
lo-k Sep 17, 2015
57f8b53
Added md file to explain our endpoints. Lots of notes right now. Will…
lo-k Sep 17, 2015
8aa78ae
Started documentation for our endpoints.
amirahaile Sep 17, 2015
a9ca335
Saved mocha and sqlite3 as dependencies.
amirahaile Sep 17, 2015
b764406
Renamed the file for our routes.
amirahaile Sep 17, 2015
0cc727a
Added semicolons :|
amirahaile Sep 17, 2015
07a5cae
Specified db/development.db instead of just db/
amirahaile Sep 18, 2015
6bd59e5
Commented out index route and renamed user to customers.
amirahaile Sep 18, 2015
40c272c
Formatted comments.
amirahaile Sep 18, 2015
5cffc73
Created controllers directory and customer controller.
amirahaile Sep 18, 2015
a9d033c
Styling and structure for API endpoint documentation.
amirahaile Sep 18, 2015
523cf9a
Added a semicolon…
amirahaile Sep 18, 2015
7e747cf
Moved db.close() so that it waits for the query to happen before clos…
lo-k Sep 18, 2015
f28c04b
Renamed res to result for consistency and clarity
lo-k Sep 18, 2015
716ae6e
Refactoring how we are structuring our routes and controllers with ca…
lo-k Sep 18, 2015
45900db
Fixed merge conflict with customers.js.
amirahaile Sep 18, 2015
df7deaa
Got the GET all customers route and query functionality working in th…
lo-k Sep 18, 2015
fc46292
Can now query for a single customer's info
lo-k Sep 18, 2015
e391ee7
Created movies.js controller file with stubbed out functions.
lo-k Sep 18, 2015
5947c13
Created endpoints for './movies' and './movies/{:id}'.
amirahaile Sep 18, 2015
c48abf4
Added '/zomg' GET response per project baseline requirements.
lo-k Sep 18, 2015
5733896
WIP querying multiple times to movies db for customer renting and ren…
lo-k Sep 18, 2015
4f5a7bd
Merge pull request #2 from lo-k/kari/customer_movies_query
lo-k Sep 18, 2015
994b7ae
Sorts results by order_by query.
amirahaile Sep 19, 2015
5ec641f
Can now find a movie by its title.
amirahaile Sep 19, 2015
6c70ef8
Elaborated on endpoints.
amirahaile Sep 19, 2015
7dbf95c
Created rental seed data.
amirahaile Sep 21, 2015
8473ad6
Fixed movie title query. Returns fuzzy matches.
amirahaile Sep 21, 2015
7cbd37d
Merge pull request #3 from lo-k/adh/movie_queries_param
amirahaile Sep 21, 2015
1d70b5c
Added line to customers controller to order by id unless the query sp…
lo-k Sep 21, 2015
009aa0d
Callback Hell insanity and then we discovered join queries...
lo-k Sep 21, 2015
c0d18ae
Updated customer search query to pull in information about their rent…
lo-k Sep 21, 2015
de8597f
Added number & page query params for customer.
amirahaile Sep 21, 2015
4634877
Add pagination ability for customers search.
lo-k Sep 21, 2015
a10bf37
Added pagination feature to movies search.
lo-k Sep 21, 2015
bc98708
Created new endpoint to see rental history of a movie. Also can see a…
lo-k Sep 21, 2015
cc97537
Moved search query for rental information into a rentals controller w…
lo-k Sep 21, 2015
4eec6e2
Added new route for overdue rentals. Namespaced the rentals by title …
lo-k Sep 21, 2015
363eb6e
Merge branch 'kml+adh/master' of github.com:lo-k/C3Projects--VideoSto…
amirahaile Sep 22, 2015
9446cb5
WIP rentals_overdue controller function for the overdue endpoint.
lo-k Sep 22, 2015
d20b220
Merge branch 'kml+adh/master' of github.com:lo-k/C3Projects--VideoSto…
amirahaile Sep 22, 2015
9e84ffe
Finished route and controller function for retrieving overdue movies.
lo-k Sep 22, 2015
29a2163
Can now post a new rental record to our db. WIP on having it also upd…
lo-k Sep 22, 2015
47468de
Got the checkout_movie function to create a new rental record, update…
lo-k Sep 22, 2015
48fc8db
Merge branch 'kml+adh/master' of github.com:lo-k/C3Projects--VideoSto…
amirahaile Sep 22, 2015
2f7e6aa
Fixed post route for checkout_movie, which will pull in the customer'…
lo-k Sep 22, 2015
a4defaf
Wrote return_movie route and function which will update the movie's i…
lo-k Sep 22, 2015
e35c49b
Added order by return date desc order for a customer's rental return …
lo-k Sep 22, 2015
1a5a595
Updated json key to reflect that there could be multiple rental resul…
lo-k Sep 22, 2015
1926070
Merge branch 'kml+adh/master' of github.com:lo-k/C3Projects--VideoSto…
amirahaile Sep 22, 2015
d6bc6f8
WIP: Updating the endpoint documentation as needed.
amirahaile Sep 22, 2015
fa746e1
Installed supertest.
amirahaile Sep 23, 2015
80bd426
WIP: Updated customers tests and started movies test.
amirahaile Sep 23, 2015
a185724
WIP: Created rentals test file.
amirahaile Sep 23, 2015
07b8a01
Added db/test.db to our gitignore and included a note in our Readme t…
lo-k Sep 23, 2015
8644a3c
Removed db/test.db from git tracking
lo-k Sep 23, 2015
3b962f0
Updated instructions for setting up your test db file.
lo-k Sep 23, 2015
3acf51f
Changed db variable to be assigned a file path based on your env so t…
lo-k Sep 23, 2015
9d207ec
Wrote tests for customers controller endpoints.
lo-k Sep 23, 2015
3e96d4b
Commented out WIP db test.
lo-k Sep 23, 2015
1a30060
Commented out final closing bracket.
lo-k Sep 24, 2015
90b55a9
Wrote tests for movies controller all_movies endpoint.
lo-k Sep 24, 2015
f8eba26
Wrote tests for rest of movies controller endpoints. Removed unused m…
lo-k Sep 24, 2015
69c0671
Updated movies endpoint info in endpoints.md file.
lo-k Sep 24, 2015
a8d2a5c
Added description of endpoint for getting rental history for a given …
lo-k Sep 24, 2015
912a123
Removed console.logs
lo-k Sep 24, 2015
73a4d0c
Updated endpoints.md file for clearer order.
lo-k Sep 24, 2015
7754881
Wrote initial tests for rentals controller endpoints.
lo-k Sep 24, 2015
365d52d
Updated notes in endpoint.md file about GET /rentals/title/:title and…
lo-k Sep 24, 2015
85fec22
Wrote more tests for the rentals controller/endpoints.
lo-k Sep 25, 2015
860acdd
Pulling in movie_id with rental results for easier data use cases later.
lo-k Sep 25, 2015
1f3b158
Updated some json result formatting and descriptions to reflect our A…
lo-k Sep 25, 2015
aa047ee
Updated rentals test to include movie_id per the change in our rental…
lo-k 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/development.db
db/test.db
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ Kari & Amira

# Project: VideoStoreAPI

##Testing
To run our test base, you will need to setup your local test.db file with:
$ DB=test npm run reset

Then you can run our tests with:
$ npm test

##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.

We will use [NodeJS](https://nodejs.org/en/) to construct a RESTful API. The goal of this API is to quickly serve information about the store's video collection, customer information, and to update rental status. This repository provides two JSON datafiles to serve as the initial seeds for this system.
Expand Down
66 changes: 66 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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 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')));

var routes = require('./routes/index');
app.use('/', routes);

var customers = require('./routes/customers');
app.use('/customers', customers);

var movies = require('./routes/movies');
app.use('/movies', movies);

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

var Database = require('../database'); // Node module; Pulling in db object we made in a dif file

var customersController = {
// ALL CUSTOMERS SEARCH... "/customers?order_by=name"
// PAGINATION... "/customers?number=25&page=2" => customers 26-50
all_customers: function(req, callback) {
var column = req.query.order_by ? req.query.order_by : "id";

if (req.query.number && req.query.page) {
var limit = req.query.number;
var offset = req.query.page * limit - limit;
var statement = "SELECT * FROM customers ORDER BY " + column + " ASC LIMIT " + limit + " OFFSET " + offset + ";";
} else {
var statement = "SELECT * FROM customers ORDER BY " + column + " ASC;";
}

var db_env = process.env.DB || 'development',
db = new Database('db/' + db_env + '.db');

db.query(statement, function(err, result) {

var json_results = {
customers: result
};

callback(err, json_results);
});

},

customer: function(req, callback) {
var statement = "SELECT * FROM customers, rentals WHERE customers.id = " + req.params.id + " AND rentals.customer_id = " + req.params.id + " ORDER BY rentals.checkout_date ASC;";

var db_env = process.env.DB || 'development',
db = new Database('db/' + db_env + '.db');

db.query(statement, function(err, result) {

var customer_info = {
id: result[0].customer_id,
name: result[0].name,
registered_at: result[0].registered_at,
address: result[0].address,
city: result[0].city,
state: result[0].state,
postal_code: result[0].postal_code,
phone: result[0].phone,
account_credit: result[0].account_credit
};

var renting_movies = [];
var rented_movies = [];

for(var i = 0; i < result.length; i++) {
var movie = {
id: result[i].movie_id,
title: result[i].title,
checkout_date: result[i].checkout_date,
due_date: result[i].due_date,
return_date: result[i].return_date
};

if(movie.return_date == null) {
renting_movies.push(movie);
} else {
rented_movies.push(movie);
}
}

var json_results = {
account: customer_info,
renting: renting_movies,
rented: rented_movies
};

callback(err, json_results);
});
}

};

module.exports = customersController;
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 Database = require('../database'); // Node module; Pulling in db object we made in a dif file

var moviesController = {
// maybe move the var db = ... out here?
all_movies: function(req, callback) {
var column = req.query.order_by ? req.query.order_by : "id";

if (req.query.number && req.query.page) {
var limit = req.query.number;
var offset = req.query.page * limit - limit;
var statement = "SELECT * FROM movies ORDER BY " + column + " ASC LIMIT " + limit + " OFFSET " + offset + ";";
} else {
var statement = "SELECT * FROM movies ORDER BY " + column + " ASC;";
}

var db_env = process.env.DB || 'development',
db = new Database('db/' + db_env + '.db');

db.query(statement, function(err, result) {
var json_result = {
movies: result
};

callback(err, json_result);
});
},

movie: function(req, callback) {
var column = req.query.order_by ? req.query.order_by : "title";
var title = req.params.title
var statement = "SELECT * FROM movies WHERE title LIKE '%" + title + "%' ORDER BY " + column + " ASC;";

var db_env = process.env.DB || 'development',
db = new Database('db/' + db_env + '.db');

db.query(statement, function(err, result) {

var json_result = {
movie: result
};

callback(err, json_result);
});
},
};

module.exports = moviesController;
Loading