Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
updated function getCustomerContactInfoByCustomerId. now working
  • Loading branch information
JamesMessina committed May 6, 2021
commit 59dba851dbde18c0a86d04322852330f8f988ebf
25 changes: 19 additions & 6 deletions controllers/customers.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,37 @@ function getAllCustomerContactInfo(req, res){

function getCustomerContactInfoByCustomerId(req, res){
let customerId = req.params.customer_Id;
console.log('in the customer contact info function/path fetching customer Id', customerId);
console.log('in the customer contact info path fetching customer Id', customerId);


let sql = `SELECT customers.customer_id, customers.first_name, customers.last_name, address.phone, customers.email, address.address, address.district
let sql = `SELECT customers.customer_id, customers.first_name, customers.last_name, address.phone, customers.email, address.address, city.city, address.district, country.country
FROM address
JOIN customers
ON customers.address_id = address.address_id
ON customers.address_id = address.address_id
JOIN city
ON address.city_id = city.city_id
JOIN country
ON city.country_id = country.country_id
WHERE customer_id = ?
ORDER BY customer_id`
GROUP BY customer_id`

let replacements = [customerId];
sql = mysql.format(sql, replacements);

pool.query(sql, function(err, results){
if(err){
console.error()
console.error('Internal Server Error', err);
res.status(500).send('Internal Server Error Occured');
}else if(results.length === 0){
console.log('customer and corresponding contact info not found for customer id' + customerId);
res.status(404).send('Customer and corresponding contact info not found for customer id ' + customerId);
}else if(results.length > 1){
console.log('400 Bad Request');
res.status(400).send('More than 1 customer found with the same ID. Bad Request, refine search')
}else{
res.json(results)
}
})

}

module.exports = { getCustomers, getCustomersByStoreId, getCustomerContactInfoByCustomerId, getAllCustomerContactInfo }
2 changes: 1 addition & 1 deletion routes/customers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ router.get('/customers/:storeId', customersController.getCustomersByStoreId);

router.get('/customersContactInfo', customersController.getAllCustomerContactInfo);

router.get('/customerContactInfo/:customer_Id', customersController.getCustomerContactInfoByCustomerId);
router.get('/customersContactInfo/:customer_Id', customersController.getCustomerContactInfoByCustomerId);

module.exports = router;