Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
32b38bd
added js files for rentals, customers, movies routes
leahnp Jun 14, 2016
c58a53f
create index route to server it works json data
leahnp Jun 14, 2016
1e79dd1
added empty controllers
leahnp Jun 14, 2016
1146f58
added db dirs and files
leahnp Jun 14, 2016
05a8c0f
added controller scaffolding
leahnp Jun 14, 2016
2fed9bb
added routes to costumes and placeholder methods
leahnp Jun 14, 2016
923bffa
added routes to movies and placeholder methods
leahnp Jun 14, 2016
60a395b
completed routes and controller setup
leahnp Jun 14, 2016
ffb9db4
added scripts to package.json
leahnp Jun 14, 2016
7210590
create schema
leahnp Jun 14, 2016
609ea09
added vars to app js for db
leahnp Jun 14, 2016
017f585
create db and loaded schema
leahnp Jun 14, 2016
ac1de03
added seed js file to auto seed from our json data
leahnp Jun 15, 2016
1830723
deleted other file where we were trying to seed json data
leahnp Jun 15, 2016
1a9d7ad
seed success! seed js file working
leahnp Jun 15, 2016
f62cec9
no changes
leahnp Jun 16, 2016
5017a8d
Added Customer.all model method.
abrown152 Jun 16, 2016
2c1687c
Added Customer.all model and controller methods. Cleaned up funky code.
abrown152 Jun 16, 2016
94e3dd3
Added getMovie model and controller methods.
abrown152 Jun 16, 2016
0cb8c70
Structured the getRentals function and associated functions.
abrown152 Jun 16, 2016
7475951
EOD commit.
abrown152 Jun 16, 2016
66d9160
implemented customer sort
leahnp Jun 17, 2016
7272d8c
added sort movie functions
leahnp Jun 17, 2016
778a4b2
added rentals seed data
leahnp Jun 17, 2016
04a70b1
added function to return currently checked out videos for givien cust…
leahnp Jun 17, 2016
b245782
added get rental history for given id
leahnp Jun 17, 2016
481795d
working on returning data from movie and rentals models to get inventory
leahnp Jun 17, 2016
8f2444c
added instanbul
leahnp Jun 20, 2016
39b10c7
changed schema to include returned date
leahnp Jun 20, 2016
dec1418
added getpastchecked out method that returns past rentals and sorts b…
leahnp Jun 20, 2016
c948e37
added function to get all current customers who have checked out film…
leahnp Jun 20, 2016
273a5af
linked rentals & movies to use same function to return list of custom…
leahnp Jun 20, 2016
8a0f7ec
Break push.
abrown152 Jun 20, 2016
4f96be0
Slayed our own dragons.
abrown152 Jun 20, 2016
7cec0fa
working on getCheckout in rentals model, currently settime object is …
leahnp Jun 20, 2016
8546c07
fixed getCheck method to check out movie by name and customer id
leahnp Jun 21, 2016
43d3e46
make checkouts subtract 2 dollars from customer account balance
leahnp Jun 21, 2016
969b346
added returned method to return a rental
leahnp Jun 21, 2016
d2ac33e
working on getoverdue function, need to find object that will respond…
leahnp Jun 21, 2016
ff76112
getOverdue function.
abrown152 Jun 21, 2016
ec9af99
finished getoverdue with innerjoin query
leahnp Jun 21, 2016
101632c
got rentals/:title returning title, overview, release date, total inv…
leahnp Jun 22, 2016
25c8086
cleaned up commented out code and whitespace
leahnp Jun 22, 2016
35e9995
trying to setup test env everything is broken
leahnp Jun 22, 2016
59b985e
got test env config, can run npm run reset-test to create, schema, se…
leahnp Jun 22, 2016
c180749
changed dev db to be named _development
leahnp Jun 22, 2016
f55f4d0
Added basic customer controller tests.
abrown152 Jun 22, 2016
8983dfe
changed run test command, added tests to movies and rentals controllers
leahnp Jun 23, 2016
971e971
added test for customer model all method
leahnp Jun 23, 2016
17f324b
added test for customer sort model method
leahnp Jun 23, 2016
c1f3358
ignore coverage/ directory
leahnp Jun 24, 2016
106260f
switched views to render ejs, added docs ejs, route and controller
leahnp Jun 23, 2016
4c2b109
added json file, trying to find the best way to print it out in a view
leahnp Jun 23, 2016
7199638
changed rentals model error handling to be error or no results
leahnp Jun 23, 2016
dadc847
changed errors for no data found in db
leahnp Jun 23, 2016
383ddd9
changed error handling in customers controller to specify 500 or404
leahnp Jun 24, 2016
d838d4b
finished the rest of the error handling from the rentals model
leahnp Jun 24, 2016
d6d240a
finished the json docs for customers endpoint
leahnp Jun 24, 2016
367f88c
updated error handling for movies model when no data is found
leahnp Jun 24, 2016
2a6cc03
updated routes to handle for both no data and error 500
leahnp Jun 24, 2016
60d6347
finished docs for /movies endpoint
leahnp Jun 24, 2016
965f029
added html formatting for docs view to iterate through json file and …
leahnp Jun 24, 2016
62da41f
cleaned up commented out code and whitespace
leahnp Jun 24, 2016
148c82d
put .json route above view route to give higher priority
leahnp Jun 24, 2016
43b6264
fixed incorrect variable names and made = ==
leahnp Jun 24, 2016
af2d109
added two tests to movies model spec, checking valid and invalid movi…
leahnp Jun 24, 2016
955dace
Controller tests finished.
abrown152 Jun 24, 2016
4da180d
Added model tests.
abrown152 Jun 24, 2016
b2fc6f8
Finally got 90% test coverage. PR commit and byeeeeee
abrown152 Jun 24, 2016
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.DS_Store
coverage/
node_modules/
33 changes: 30 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,51 @@ var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var massive = require("massive");
var app = module.exports = express();

var routes = require('./routes/index');
// view engine setup
// app.set('views', path.join(__dirname, 'views'));
// app.set('view engine', 'ejs');

// var connectionString = "postgres://localhost/video_store"
var connectionString = "postgres://localhost/video_store_" + app.get('env');

// connect to Massive and get the db instance. You can safely use the
// convenience sync method here because its on app load
// you can also use loadSync - it's an alias
var massiveInstance = massive.connectSync({connectionString : connectionString})

// Set a reference to the massive instance on Express' app:
app.set('db', massiveInstance);

var app = express();

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

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
// this will terminate the request with an error 400 if the POST body
// doesn't contain valid json
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');
var customers = require('./routes/customers');
var movies = require('./routes/movies');
var rentals = require('./routes/rentals');



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) {
Expand Down
59 changes: 59 additions & 0 deletions controllers/customers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
var Customer = require("../models/customers_model");
var Rental = require("../models/rentals_model");


var CustomersController = {
getCustomers: function(req, res) {
Customer.all(function(error, customers) {
if(error=="Could not retrieve customers") {
res.status(404).send(error)
} else if (error) {
var err = "Please try again"
res.status(500).send(err)
} else {
res.json(customers)
}
})
},
// customer id, sort column, offest ?????
subsetCustomers: function(req, res) {
Customer.sort(req.params.column, req.query.p, req.query.n, function(error, data) {
if(error=="Could not retrieve customers") {
res.status(404).send(error)
} else if (error) {
var err = "Please try again"
res.status(500).send(err)
} else {
res.json(data)
}
})
},

// customer id, all rentals attached to customer id within data params
getCustomersCurrent: function(req, res) {
Rental.getCurrentRentals(req.params.customer_id, function(error, data) {
if(error=="No results from database") {
res.status(404).send(error)
} else if (error) {
var err = "Please try again"
res.status(500).send(err)
} else {
res.json(data)
}
})
},

getCustomersHistory: function(req, res) {
Rental.getPastRentals(req.params.customer_id, function(error, data) {
if(error=="No past rentals") {
res.status(404).send(error)
} else if (error) {
var err = "Please try again"
res.status(500).send(err)
} else {
res.json(data)
}
})
}
}
module.exports = CustomersController
16 changes: 16 additions & 0 deletions controllers/docs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var docs = require('../public/docs.json');

var DocsController = {
getDocs: function(req, res) {
res.render('docs', {
title: 'The Docs',
docs: docs
});
},

getJsonDocs: function(req, res) {
res.json(docs);
}
}

module.exports = DocsController
50 changes: 50 additions & 0 deletions controllers/movies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var Movie = require("../models/movies_model");
var Rental = require("../models/rentals_model");


MoviesController = {
getMovies: function(req, res) {
Movie.all(function(error, movies) {
if(error=="Could not retrieve movies") {
res.status(404).send(error)
} else if (error) {
var err = "Please try again"
res.status(500).send(err)
} else {
res.json(movies)
}
})
},

subsetMovies: function(req, res) {
Movie.sort(req.params.column, req.query.p, req.query.n, function(error, data) {
if(error=="Could not retrieve movies") {
res.status(404).send(error)
} else if (error) {
var err = "Please try again"
res.status(500).send(err)
} else {
res.json(data)
}
})
},

getRentalsCustomers: function(req, res) {
Rental.getCustomers(req.params.title, function(error, checked_out) {
if(error=="Could not retrieve movies") {
res.status(404).send(error)
} else if (error) {
var err = "Please try again"
res.status(500).send(err)
} else {
res.json(checked_out)
}
})
},

getMoviesHistory: function(req, res) {

}
}

module.exports = MoviesController
72 changes: 72 additions & 0 deletions controllers/rentals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
var Rental = require("../models/rentals_model");
var Movie = require("../models/movies_model");

RentalsController = {
getRentals: function(req, res) {
Movie.findMovie(req.params.title, function(error, movie) {
if(error) {
var err = new Error("No such movie");
err.status = 404;
} else {
Rental.getCurrentlyCheckedOut(movie, function(error, checked_out) {
var return_data = {
title:movie.title,
overview:movie.overview,
release_date:movie.release_date,
total_inventory:movie.inventory,
available_copies:(parseInt(movie.inventory))-(parseInt(checked_out))
}
res.json(return_data)
})
}

})
},

getRentalsCustomers: function(req, res) {
Rental.getCustomers(req.params.title, function(error, checked_out) {
if(error) {
var err = new Error("No one has that movie checked out");
err.status = 404;
} else {
res.json(checked_out)
}
})
},

getRentalsCheckOut: function(req, res, next) {
Rental.getCheckout(req.params.title, req.params.customerid, function(error, checked_out) {
if(error) {
var err = new Error("No one has that movie checked out");
err.status = 404;
} else {
res.json(checked_out)
}
});
},

getRentalsReturn: function(req, res) {
Rental.getReturn(req.params.id, function(error, returned) {
if(error) {
var err = new Error("No one has that movie checked out");
err.status = 404;
} else {
res.json(returned)
}
});

},

getRentalsOverdue: function(req, res) {
Rental.getOverdue(function(error, over_due) {
if(error) {
var err = new Error("Ooops");
err.status = 404;
} else {
res.json(over_due)
}
});
}
}

module.exports = RentalsController
42 changes: 42 additions & 0 deletions db/seeds/rentals.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[
{
"movie_id": 2,
"customer_id": 5,
"created_date": "1980-06-16",
"due_date": "1980-06-18",
"returned": true,
"returned_date": "1980-06-18T20:24:01.218Z"
},
{
"movie_id": 2,
"customer_id": 5,
"created_date": "1960-06-16",
"due_date": "2016-06-18T20:24:01.218Z",
"returned": false
},
{
"movie_id": 2,
"customer_id": 5,
"created_date": "1960-06-16",
"due_date": "1960-06-18",
"returned": true,
"returned_date": "1960-06-18T20:24:01.218Z"
},
{
"movie_id": 2,
"customer_id": 5,
"created_date": "1970-06-16",
"due_date": "1970-06-18",
"returned": true,
"returned_date": "1967-06-18T20:24:01.218Z"
},
{
"movie_id": 10,
"customer_id": 4,
"created_date": "1960-06-16",
"due_date": "1960-06-17",
"returned": false
}


]
32 changes: 32 additions & 0 deletions db/setup/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
DROP TABLE IF EXISTS movies;
CREATE TABLE movies(
id serial PRIMARY KEY,
title text,
overview text,
release_date date,
inventory integer
);

DROP TABLE IF EXISTS rentals;
CREATE TABLE rentals(
id serial PRIMARY KEY,
movie_id int,
customer_id int,
created_date date,
due_date date,
returned boolean,
returned_date date
);

DROP TABLE IF EXISTS customers;
CREATE TABLE customers(
id serial PRIMARY KEY,
name text,
registered_at timestamp,
address text,
city text,
state text,
postal_code text,
phone text,
account_credit decimal
);
37 changes: 37 additions & 0 deletions models/customers_model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var app = require("../app");
var db = app.get("db");

var Customer = function(id) {
this.id = id;
}

Customer.all = function(callback) {
db.customers.find(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.id);
}))
};
})
};

Customer.sort = function(column, p, n, callback) {
// sort by column, for n number of records starting at p
db.customers.find({}, {
order: column,
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 customer
}))
}
})
}

module.exports = Customer;
Loading