-
Notifications
You must be signed in to change notification settings - Fork 13
Ania and Suze #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
2eaf465
9140fac
fa4ce05
e03e453
c841893
a7e7d95
d18f5b6
dadef92
5a3475e
d522894
852f807
bacbd69
635b981
3f45045
abc06c2
9c4d9a7
1b78096
661e237
db9f160
2255b0b
3841493
7b66c21
c55332f
4a043b3
0f16284
137c11e
06a287e
271d97f
24b3e0f
1de9c8f
e465000
b8c9e89
c9a803d
9ba25d6
2af017c
9c93b1a
70e3005
ccd6bbc
93acdf2
e367891
e2a630d
ed3d48d
a5fe028
543c4fb
12ecba9
9cf6b95
d070933
7cd4de3
3f3f0d5
706c7da
ecd858f
1ce38eb
aad4b53
55fffe5
e9fdace
98ae357
ef5e344
2728048
802c19b
080e432
13d1809
a2833c2
11aa91b
85b4f51
03cf03f
e746491
08fe5f6
1a61a2b
fbffdad
87cd727
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| 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) | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do |
||
| 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) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like this |
||
| 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) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment above regarding |
||
| 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 | ||
| 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; |
| 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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's always good practice to remove any |
||
|
|
||
| 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; | ||
| 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; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| SELECT | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| 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" | ||
| } | ||
| ] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice looking JSON! 👍 |
||
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment.
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? :)