Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
2eaf465
psuedo code for routes.
SuzHarrison Jun 14, 2016
9140fac
small change to routes file to require cust id and title for rentals
SuzHarrison Jun 14, 2016
fa4ce05
baseline completed. print json it works in json, at /zomg.
SuzHarrison Jun 14, 2016
e03e453
added scripts for db.
SuzHarrison Jun 14, 2016
c841893
made changes to schema.sql, package.json, and made tasks/seed_data.js…
SuzHarrison Jun 15, 2016
a7e7d95
added node run db:seed to the db:reset command in scripts package.json.
SuzHarrison Jun 15, 2016
d18f5b6
Created customers routes
aniagonzalez Jun 15, 2016
dadef92
added routes for movies and rentals. Added / routes to app.js file fo…
SuzHarrison Jun 16, 2016
5a3475e
small change to post (from get) to rentals return route.
SuzHarrison Jun 16, 2016
d522894
small change to customer route for history.
SuzHarrison Jun 16, 2016
852f807
added contollers folder and movies_controller.
SuzHarrison Jun 16, 2016
bacbd69
made changes to movies route file calling on controller functions.
SuzHarrison Jun 16, 2016
635b981
Added customer routes
aniagonzalez Jun 16, 2016
3f45045
changed movies route dynamic from name to title. Started redoing rent…
SuzHarrison Jun 16, 2016
abc06c2
Merge branch 'master' of github.com:SuzHarrison/VideoStoreAPI
SuzHarrison Jun 16, 2016
9c4d9a7
finished redoing the routes for rentals.
SuzHarrison Jun 16, 2016
1b78096
basics for the customers controller
aniagonzalez Jun 16, 2016
661e237
added mostly empty functions to movies_controller.js.
SuzHarrison Jun 16, 2016
db9f160
adding parameters to the new functions.
SuzHarrison Jun 16, 2016
2255b0b
small change to function name.
SuzHarrison Jun 16, 2016
3841493
Created customer model
aniagonzalez Jun 17, 2016
7b66c21
fixed circular app.get bug. added more to the movie model constructor.
SuzHarrison Jun 17, 2016
c55332f
Merge branch 'master' of github.com:SuzHarrison/VideoStoreAPI
aniagonzalez Jun 17, 2016
4a043b3
pushing changes to movie model. added this.title and this.id, as well…
SuzHarrison Jun 17, 2016
0f16284
export modules missing
aniagonzalez Jun 17, 2016
137c11e
Merge branch 'master' of github.com:SuzHarrison/VideoStoreAPI
aniagonzalez Jun 17, 2016
06a287e
added _development to all references to video_store_api in seed scrip…
SuzHarrison Jun 17, 2016
271d97f
Merge branch 'master' of github.com:SuzHarrison/VideoStoreAPI
aniagonzalez Jun 17, 2016
24b3e0f
Fixed typo
aniagonzalez Jun 17, 2016
1de9c8f
Fixed Customer model
aniagonzalez Jun 17, 2016
e465000
added properties to Movie model. now correctly getting json hash with…
SuzHarrison Jun 17, 2016
b8c9e89
Merge branch 'master' of github.com:SuzHarrison/VideoStoreAPI
SuzHarrison Jun 17, 2016
c9a803d
got Customers.all working
aniagonzalez Jun 17, 2016
9ba25d6
route with query for customers sort
aniagonzalez Jun 17, 2016
2af017c
working on movies query routes and model.
SuzHarrison Jun 17, 2016
9c93b1a
Merge branch 'master' of github.com:SuzHarrison/VideoStoreAPI
SuzHarrison Jun 17, 2016
70e3005
Customers can be sorted by name
aniagonzalez Jun 17, 2016
ccd6bbc
Merge branch 'master' of github.com:SuzHarrison/VideoStoreAPI
SuzHarrison Jun 17, 2016
93acdf2
customers sorted by name with limit and offset
aniagonzalez Jun 17, 2016
e367891
finally got sort by relase to work.
SuzHarrison Jun 17, 2016
e2a630d
Merge branch 'master' of github.com:SuzHarrison/VideoStoreAPI
SuzHarrison Jun 17, 2016
ed3d48d
Refactored sql query from run to find
aniagonzalez Jun 17, 2016
a5fe028
added to ability to sort release_date or title through sort. working …
SuzHarrison Jun 17, 2016
543c4fb
Seeded Rentals
aniagonzalez Jun 17, 2016
12ecba9
updated test script
aniagonzalez Jun 17, 2016
9cf6b95
Merge branch 'master' of github.com:SuzHarrison/VideoStoreAPI
aniagonzalez Jun 17, 2016
d070933
movie/title/current route enabled. starting to work on movie controller.
SuzHarrison Jun 17, 2016
7cd4de3
Merge branch 'master' of github.com:SuzHarrison/VideoStoreAPI
SuzHarrison Jun 17, 2016
3f3f0d5
commented out routes for editing
SuzHarrison Jun 20, 2016
706c7da
You can now get movie rentals for a specific customer:
aniagonzalez Jun 20, 2016
ecd858f
Customer current movies now only return movies with no return date
aniagonzalez Jun 20, 2016
1ce38eb
Customer history gives all rentals with checkout and return date
aniagonzalez Jun 20, 2016
aad4b53
movies. current now working.
SuzHarrison Jun 20, 2016
55fffe5
Merge branch 'master' of github.com:SuzHarrison/VideoStoreAPI
aniagonzalez Jun 20, 2016
e9fdace
Merge branch 'master' of github.com:SuzHarrison/VideoStoreAPI
SuzHarrison Jun 20, 2016
98ae357
broken movies history.
SuzHarrison Jun 20, 2016
ef5e344
saving routes uncommented now.
SuzHarrison Jun 20, 2016
2728048
fixed the circular app.js on customer and movie models. started model…
SuzHarrison Jun 20, 2016
802c19b
added db/movie/customers_by_movie.sql to extract the sql query from m…
SuzHarrison Jun 21, 2016
080e432
got the rentals by title route working.
SuzHarrison Jun 21, 2016
13d1809
changed the given data to the Rental.all by title.
SuzHarrison Jun 21, 2016
a2833c2
fixed the route and methods for finding all customers that have renat…
SuzHarrison Jun 21, 2016
11aa91b
starting the checkout route but not working.
SuzHarrison Jun 22, 2016
85b4f51
fixed the rentals controller so that information is stored correctly
aniagonzalez Jun 23, 2016
03cf03f
Set the return date for the rental checkout in db
aniagonzalez Jun 23, 2016
e746491
changed schema to accept decimal for account_credit in customers. add…
SuzHarrison Jun 23, 2016
08fe5f6
added function to rental models to charge for rental and change the i…
SuzHarrison Jun 23, 2016
1a61a2b
started the return rental function. Route uncommented but function n…
SuzHarrison Jun 24, 2016
fbffdad
fixed the return rental and the double callback
aniagonzalez Jun 24, 2016
87cd727
added overdue function.
SuzHarrison 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
20 changes: 16 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var massive = require('massive');

var routes = require('./routes/index');

var app = express();
module.exports = app;

// database setup
var connectionString = "postgres://localhost/video_store_api_" + app.get('env');
var db = massive.connectSync({connectionString: connectionString});
app.set('db', db);

// view engine setup
app.set('views', path.join(__dirname, 'views'));
Expand All @@ -23,6 +29,15 @@ app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);

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


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

var rentalsRoutes = require('./routes/rentals');
app.use('/rentals', rentalsRoutes);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
Expand Down Expand Up @@ -53,6 +68,3 @@ app.use(function(err, req, res, next) {
error: {}
});
});


module.exports = app;
63 changes: 63 additions & 0 deletions controllers/customers_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
var Customer = require("../models/customer");

var CustomersController = {

getCustomers: function(req, res, next) {
// giving a callback function to handle error or render view
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remember to remove any unnecessary comments before submitting your final PR. They are super helpful during development, though, huh? :)

Customer.all(function(error, customers) {
if(error) {
var err = new Error("Error retrieving customer list:\n" + error.message);
err.status = 500;
next(err);
} else {
res.json(customers)
}
});
},

sortBy: function(req, res, next) {
var n = req.query.n;
var p = req.query.p;
var field = String(req.params.field)

Copy link

@knaydee knaydee Jul 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do n, p, and field represent? I don't understand Express very well so maybe they are standard... I also don't know what sortBy is sorting by.

Customer.sortBy(field, n, p, function(error, customers) {
if(error) {
var err = new Error("Error retrieving customer list:\n" + error.message);
err.status = 500;
next(err);
} else {
res.json(customers)
}
});

},

customerCurrent: function(req, res, next) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this customerCurrent is returning movies for the current customer. Calling it currentCustomerMovies, or something along those lines, would be more descriptive.

var id = req.params.id

Customer.current([id], function(error, movies) {
if(error) {
var err = new Error("Error retrieving current movies for this customer:\n" + error.message);
err.status = 500;
next(err);
} else {
res.json(movies)
}
})
},

customerHistory: function(req, res, next) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above regarding currentCustomer. Is this finding the customer's movie history or finding historic movies?

var id = req.params.id

Customer.history([id], function(error, movies) {
if(error) {
var err = new Error("Error retrieving historic movies for this customer:\n" + error.message);
err.status = 500;
next(err);
} else {
res.json(movies)
}
})
}
}
module.exports = CustomersController
82 changes: 82 additions & 0 deletions controllers/movies_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
var Movie = require("../models/movie");
var Customer = require("../models/customer");
var Rental = require("../models/rental");

var MoviesController = {
listMovies: function(req, res, next) {
// giving a callback function to handle error or render view
Movie.all(function(error, movies) {
if(error) {
var err = new Error("Error retrieving movie list:\n" + error.message);
err.status = 500;
next(err);
} else {
res.json(movies)
// var locals = { movies: movies }
// res.render("movies/index", locals);
}
});
},

sortBy: function(req, res, next) {
var options = {
order: req.params.field,
limit: req.query.n,
offset: req.query.p
}

Movie.sortBy(options, function(error, movies) {
if(error) {
var err = new Error("Error retrieving sorted movie list:\n" + error.message);
err.status = 500;
next(err);
} else {
res.json(movies)
}
});
},

current: function(req, res, next) {
var movie = req.params.title;

Movie.find_customers_by_movie_title(movie, function(error, customers) {
if(error) {
var err = new Error("No such movie");
err.status = 404;
next(err);
} else {
var obj = {};
if (customers.length === 0) {
obj["status"] = 204;
} else {
obj["status"] = 200;
}
obj["customers"] = customers;
res.json(obj);
}
})
},

history: function(req, res, next) {
var movie = req.params.title;
var field = req.params.field;

Movie.find_customers_by_movie_title_history([movie, field], function(error, customers) {
if(error) {
var err = new Error("No such movie");
err.status = 404;
next(err);
} else {
var obj = {};
if (customers.length === 0) {
obj["status"] = 204;
} else {
obj["status"] = 200;
}
obj["customers"] = customers;
res.json(obj);
}
})
}
}
module.exports = MoviesController;
90 changes: 90 additions & 0 deletions controllers/rentals_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
var Movie = require("../models/movie");
var Customer = require("../models/customer");
var Rental = require("../models/rental");

var RentalsController = {

findMovie: function(req, res, next) {
var title = req.params.title;

Rental.all([title], function (error, rentals) {
if(error) {
var err = new Error("Error retrieving movie info:\n" + error.message);
err.status = 500;
next(err);
} else {
res.json(rentals)
}
})
},

sortBy: function(req, res, next) {
var movie = req.params.title;

Rental.customers_current_rentals(movie, function(error, customers) {
if(error) {
var err = new Error("No such customer");
err.status = 404;
next(err);
} else {
var obj = {};
if (customers.length === 0) {
obj["status"] = 204;
} else {
obj["status"] = 200;
}
obj["customers"] = customers;
res.json(obj);
}
})
},

checkout: function(req, res, next) {
var movie = req.params.title;
var customer_id = req.body.customer_id;
console.log(movie, customer_id);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's always good practice to remove any console.logs before submitting a final PR. Good for debugging though!


Rental.createCheckOut(movie, customer_id, function(error) {
console.log("in createCheckOut method")
if(error) {
var err = new Error("Rental checkout failed");
err.status = 404;
next(err);
} else {
res.json("rental_checkout works");
}
})
},

return: function(req, res, next) {
var movie = req.params.title;
var customer_id = req.body.customer_id;

console.log(movie, customer_id);
Rental.returnRental(movie, customer_id, function(error) {
console.log("in RETURN method")
if(error) {
var err = new Error("Rental return failed");
err.status = 404;
next(err);
} else {
res.json({returnRental: "Rental was properly returned"});
}
})
},

overdue: function(req, res, next) {
console.log("1st stop");
Rental.findOverdue(function(error, customers) {
if (error) {
var err = new Error("Error in finding overdue ");
err.status = 404;
next(err);
} else {
console.log("Final")
res.json(customers)
}
})
}
}
module.exports = RentalsController;
10 changes: 10 additions & 0 deletions db/movie/customers_by_movie_title.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
SELECT
customers.name,
customers.phone,
customers.account_credit
FROM customers
INNER JOIN rentals ON customers.id = rentals.customer_id
INNER JOIN movies ON rentals.movie_id = movies.id
WHERE
movies.title ILIKE $1 AND
rentals.return_date IS NULL;
10 changes: 10 additions & 0 deletions db/rental/customers_current_rentals.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
SELECT

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this query is the same as the previous one. I wonder if they're both necessary?

customers.name,
customers.phone,
customers.account_credit
FROM customers
INNER JOIN rentals ON customers.id = rentals.customer_id
INNER JOIN movies ON rentals.movie_id = movies.id
WHERE
movies.title ILIKE $1 AND
rentals.return_date IS NULL;
54 changes: 54 additions & 0 deletions db/seeds/rentals.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
[
{
"movie_id": 1,
"customer_id": 6,
"checkout_date": "2015-06-10",
"due_date": "2015-06-20",
"return_date": "2015-06-20"
},

{
"movie_id": 2,
"customer_id": 4,
"checkout_date": "2014-03-10",
"due_date": "2014-03-20",
"return_date": "2014-03-20"
},

{
"movie_id": 3,
"customer_id": 9,
"checkout_date": "2015-08-11",
"due_date": "2015-08-21"
},

{
"movie_id": 4,
"customer_id": 6,
"checkout_date": "2015-08-11",
"due_date": "2015-08-21",
"return_date": "2015-08-28"
},

{
"movie_id": 5,
"customer_id": 1,
"checkout_date": "2016-02-10",
"due_date": "2016-02-20"
},

{
"movie_id": 6,
"customer_id": 6,
"checkout_date": "2016-06-16",
"due_date": "2016-06-26"
},

{
"movie_id": 7,
"customer_id": 9,
"checkout_date": "2015-08-11",
"due_date": "2015-08-21",
"return_date": "2015-08-28"
}
]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice looking JSON! 👍

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

CREATE INDEX movies_title ON movies (title);
CREATE INDEX movies_release_date ON movies (release_date);

/*customers */
CREATE TABLE customers(
id serial PRIMARY KEY,
name text,
registered_at text,
address text,
city text,
state text,
postal_code integer,
phone text,
account_credit decimal

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing that this is for money. I wonder if it would better to use an integer and count in pennies? That is common practice.

);

CREATE INDEX customers_name ON customers (name);

/* rentals */
CREATE TABLE rentals(
id serial PRIMARY KEY,
movie_id integer references movies(id),
customer_id integer references customers(id),
checkout_date date,
due_date date,
return_date date
);

CREATE INDEX rentals_customer_id ON rentals (customer_id);
CREATE INDEX rentals_movie_id ON rentals (movie_id);
Loading