Skip to content

Conversation

@roza8765
Copy link

No description provided.

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.

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?

Copy link

@zoltan-gal zoltan-gal left a 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){

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))

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])

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.

Comment on lines +112 to +118
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));

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

Comment on lines +122 to +123
//////// 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) {

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!");

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

Comment on lines +186 to +189
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));

Choose a reason for hiding this comment

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

well done here!

Comment on lines +195 to +203
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));

Choose a reason for hiding this comment

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

Well done!

Comment on lines +214 to +229
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));
}
});

Choose a reason for hiding this comment

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

Nice!

Comment on lines +237 to +239
.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));

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants