-
Notifications
You must be signed in to change notification settings - Fork 44
Razan week3 #46
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?
Razan week3 #46
Conversation
| Add a new POST endpoint /customers to create a new customer. | ||
|
|
||
| - Add a new POST endpoint `/products` to create a new product. | ||
| Add a new POST endpoint /products to create a new product (with a product name, a price and a supplier id). Check that the price is a positive integer and that the supplier ID exists in the database, otherwise return an error. |
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.
Why did you change the task description?
zoltan-gal
left a comment
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.
There were a few mistakes but overall was great! Very nice job Razan! Solid demonstration of SQL queries. I really liked that you used the promise variation of the query method call instead of the callback version.
Some advice:
- please don't forget to keep a consistent indentation and code format. It was difficult to disentangle at some places which code blocks were embedded into another block and which wasn't.
- Try use more consistent messaging, put more information into the response messages like object ID affected, or details that have changed, or the reason of failure. It will help to detect issues when debugging.
| app.get("/customers/:customerId", function (req, res) { | ||
| const custId =Number(req.params.customerId); | ||
|
|
||
| if(custId<0){ |
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.
I don't think we need to check if id is smaller than zero. We have no such constraint. Technically a negative id is still valid.
|
|
||
| pool | ||
| .query("SELECT * FROM customers WHERE id=$1", [custId]) | ||
| .then((result) => res.json(result.rows)) |
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.
Negative scenario should be handled. what if a customer with given id doesn't exist.
| const newCountry = req.body.country; | ||
|
|
||
| pool | ||
| .query("SELECT * FROM customers WHERE name=$1", [newName]) |
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.
Why is it a problem if a user with the same name registered? It wasn't a criterion. The same name can occur multiple time.
| const query = | ||
| "INSERT INTO customers (name, address, city, country) VALUES ($1, $2, $3, $4)"; | ||
|
|
||
| pool | ||
| .query(query, [newName, newAddress, newCity,newCountry ]) | ||
| .then(() => res.send("Customer created!")) | ||
| .catch((e) => console.error(e)); |
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.
Well done. It's also worth including the ID for the newly created customer into the response. So that client will know what ID it got
| //////// Add a new POST endpoint /products to create a new product (with a product name, a price and a supplier id). Check that the price is a positive integer and that the supplier ID exists in the database, otherwise return an error./////// | ||
| app.post("/products", function (req, res) { |
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.
I think you mixed together two different tasks. Originally it was as follows:
Add a new POST endpoint /products to create a new product.
Add a new POST endpoint /availability to create a new product availability (with a price and a supplier id). Check that the price is a positive integer and that both the product and supplier ID's exist in the database, otherwise return an error.
| if (result.rows.length < 0) { | ||
| return res | ||
| .status(400) | ||
| .send("An customers doesn't exists!"); |
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.
The customer doesn't exist! also it's a good practice to include the ID into the message e.g The customer '$custId' doesn't exist
| pool | ||
| .query("UPDATE customers SET name=$2 , address =$3, city= $4, country= $5 WHERE id=$1 ", [ custId,newName, newAddress, newCity, newCountry]) | ||
| .then((result) => res.json(result.rows)) | ||
| .catch((e) => console.error(e)); |
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.
well done here!
| pool | ||
| .query("DELETE FROM order_items WHERE order_id =$1", [orderId]) | ||
| .then(() => { | ||
| pool | ||
| .query("DELETE FROM orders WHERE id=$1", [orderId]) | ||
| .then(() => res.send(`Customer ${orderId} deleted!`)) | ||
| .catch((e) => console.error(e)); | ||
| }) | ||
| .catch((e) => console.error(e)); |
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.
Well done!
| pool | ||
| .query("SELECT * FROM orders WHERE customer_id =$1", [customerId]) | ||
|
|
||
| .then((result) => { | ||
| if (result.rows.length > 0) { | ||
| return res | ||
| .status(400) | ||
| .send("This customer has order!"); | ||
| } else { | ||
|
|
||
| pool | ||
| .query("DELETE FROM customers WHERE id =$1", [customerId]) | ||
| .then(() => res.send(`Customer ${customerId} deleted!`)) | ||
| .catch((e) => console.error(e)); | ||
| } | ||
| }); |
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.
Nice!
| .query("SELECT orders.order_reference, orders.order_date, products.product_name, products.unit_price, suppliers.supplier_name , order_items.quantity FROM orders INNER JOIN order_items ON order_items.order_id = orders.id INNER JOIN products ON order_items.product_id = products.id INNER JOIN suppliers ON products.supplier_id = suppliers.id where customer_id=$1", [custId]) | ||
| .then((result) => res.json(result.rows)) | ||
| .catch((e) => console.error(e)); |
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.
Nearly correct. But unit_price should come from product_availability table, products table has no such column.
No description provided.