-
-
Notifications
You must be signed in to change notification settings - Fork 102
Mickey Haile | London-9 | Database | week 2 #43
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,215 @@ | ||
| const express = require("express"); | ||
| const app = express(); | ||
| var bodyParser = require("body-parser"); | ||
| // Your code to run the server should go here | ||
| // Don't hardcode your DB password in the code or upload it to GitHub! Never ever do this ever. | ||
| // Use environment variables instead: | ||
| // https://www.codementor.io/@parthibakumarmurugesan/what-is-env-how-to-set-up-and-run-a-env-file-in-node-1pnyxw9yxj | ||
| require("dotenv").config(); | ||
| const { Pool } = require("pg"); | ||
|
|
||
| DB_HOST=localhost | ||
| DB_PORT=5432 | ||
| DB_DATABASE=cyf_ecommerce | ||
| DB_USERNAME=postgres | ||
| DB_PASSWORD="" | ||
| const db = new Pool({ | ||
| user: process.env.DB_USERNAME, | ||
| host: process.env.DB_HOST, | ||
| database: process.env.DB_DATABASE, | ||
| password: process.env.DB_PASSWORD, | ||
| port: process.env.DB_PORT, | ||
| }); | ||
| app.use(express.json()); | ||
| app.get("/products", (req, res) => { | ||
| db.query( | ||
| "SELECT p.product_name as name, pa.unit_price as price, s.supplier_name as supplierName FROM products p INNER JOIN product_availability pa on (p.id = pa.prod_id) INNER JOIN suppliers s on (pa.supp_id = s.id)", | ||
| (error, result) => { | ||
| if (error) { | ||
| console.error(error); | ||
| res.status(500).json({ error: "Internal Server Error" }); | ||
| } else { | ||
| // res.json(result.rows); | ||
| // const products = result.rows.map(({ name, price, suppliername }) => ({ | ||
| // name, | ||
| // price, | ||
| // supplierName: suppliername, | ||
| // })); | ||
| res.status(200).json(result.rows); | ||
| } | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| app.get("/products/search", (req, res) => { | ||
| const { name } = req.query; | ||
| db.query( | ||
| "SELECT p.product_name as name, pa.unit_price as price, s.supplier_name as supplierName FROM products p INNER JOIN product_availability pa on (p.id = pa.prod_id) INNER JOIN suppliers s on (pa.supp_id = s.id) WHERE p.product_name ILIKE $1", | ||
| [`%${name}%`], | ||
| (error, result) => { | ||
| if (error) { | ||
| console.error(error); | ||
| res.status(500).json({ error: "Internal Server Error" }); | ||
| } else { | ||
| const products = result.rows.map(({ name, price, suppliername }) => ({ | ||
| name, | ||
| price, | ||
| supplierName: suppliername, | ||
| })); | ||
| res.status(200).json(products); | ||
| } | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| app.get("/customers/:id", (req, res) => { | ||
| const custId = parseInt(req.params.id); | ||
| db.query( | ||
| "SELECT * FROM customers WHERE id = $1", | ||
| [custId], | ||
| (error, result) => { | ||
| if (error) { | ||
| console.error(error); | ||
| res.status(500).json({ error: "Internal Server Error" }); | ||
| } else { | ||
| const customer = result.rows[0]; | ||
| if (customer) { | ||
| res.status(200).json(customer); | ||
| } else { | ||
| res.status(404).json({ error: `Customer with id ${id} not found` }); | ||
| } | ||
| } | ||
| } | ||
| ); | ||
| }); | ||
| app.post("/customers", (req, res) => { | ||
| const newName = req.body.name; | ||
| const newAdress = req.body.adress; | ||
| const newCity= req.body.city; | ||
| const newCountry = req.body.country; | ||
|
|
||
|
|
||
| db.query( | ||
| "INSERT INTO customers (name, address, city, country) VALUES ($1, $2, $3, $4) RETURNING *", | ||
| [newName, newAdress, newCity, newCountry], | ||
| (error, result) => { | ||
| if (error) { | ||
| console.error(error); | ||
| res.status(500).json({ error: "Internal Server Error" }); | ||
| } else { | ||
| const customer = result.rows[0]; | ||
| res.status(201).json(customer); | ||
| } | ||
| } | ||
| ); | ||
| }); | ||
| app.post("/products", (req, res) => { | ||
| const productName = req.body; | ||
| db.query( | ||
| "INSERT INTO products (product_name) VALUES ($1) RETURNING *", | ||
| [productName], | ||
| (error, result) => { | ||
| if (error) { | ||
| console.error(error); | ||
| res.status(500).json({ error: "Internal Server Error" }); | ||
| } else { | ||
| const product = result.rows[0]; | ||
| res.status(201).json(product); | ||
| } | ||
| } | ||
| ); | ||
| }); | ||
|
|
||
| app.post("/availability", (req, res) => { | ||
|
|
||
| newPrice = req.body.price; | ||
| newProductid = req.body.productId; | ||
| newSupplierid = req.body.supplierId; | ||
| // const { price, productId, supplierId } = req.body; | ||
|
|
||
| // Check if the price is a positive integer | ||
| if (!Number.isInteger(newPrice) || newPrice <= 0) { | ||
| res.status(400).json({ error: "Price must be a positive integer" }); | ||
| return; | ||
| } | ||
|
|
||
| db.query( | ||
| "SELECT * FROM products WHERE id = $1", | ||
| [newProductid], | ||
| (error, result) => { | ||
| if (error) { | ||
| console.error(error); | ||
| res.status(500).json({ error: "Internal Server Error" }); | ||
| } else if (result.rows.length === 0) { | ||
| res.status(404).json({ error: "Product not found" }); | ||
| } else { | ||
| // Check if the supplier exists | ||
| db.query( | ||
| "SELECT * FROM suppliers WHERE id = $1", | ||
| [newSupplierid], | ||
| (error, result) => { | ||
| if (error) { | ||
| console.error(error); | ||
| res.status(500).json({ error: "Internal Server Error" }); | ||
| } else if (result.rows.length === 0) { | ||
| res.status(404).json({ error: "Supplier not found" }); | ||
| } else { | ||
| // Insert the new availability | ||
| db.query( | ||
| "INSERT INTO product_availability (prod_id, supp_id, unit_price) VALUES ($1, $2, $3) RETURNING *", | ||
| [newProductid, newSupplierid, newPrice], | ||
| (error, result) => { | ||
| if (error) { | ||
| console.error(error); | ||
| res.status(500).json({ error: "Internal Server Error" }); | ||
| } else { | ||
| const availability = result.rows[0]; | ||
| res.status(201).json(availability); | ||
| } | ||
| } | ||
| ); | ||
| } | ||
| } | ||
| ); | ||
| } | ||
| } | ||
| ); | ||
| }); | ||
| app.post("/orders", (req, res) => { | ||
| const { orderDate, referenceNumber, customerId } = req.body; | ||
|
|
||
| // Check if the customer ID is valid | ||
| db.query( | ||
| "SELECT * FROM customers WHERE id = $1", | ||
| [customerId], | ||
| (error, result) => { | ||
| if (error) { | ||
| console.error(error); | ||
| res.status(500).json({ error: "Internal Server Error" }); | ||
| } else { | ||
| const customer = result.rows[0]; | ||
| if (customer) { | ||
| db.query( | ||
| "INSERT INTO orders (customer_id, order_date, reference_number) VALUES ($1, $2, $3) RETURNING *", | ||
| [customerId, orderDate, referenceNumber], | ||
| (error, result) => { | ||
| if (error) { | ||
| console.error(error); | ||
| res.status(500).json({ error: "Internal Server Error" }); | ||
| } else { | ||
| const order = result.rows[0]; | ||
| res.status(201).json(order); | ||
| } | ||
| } | ||
| ); | ||
| } else { | ||
| res.status(404).json({ error: "Invalid customer ID" }); | ||
| } | ||
| } | ||
| } | ||
| ); | ||
| }); | ||
| app.listener = app.listen(3000, function () { | ||
| console.log(`Server is listening on port ${3000}`); | ||
| }); | ||
| module.exports = app; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 like the error handling, however careful with this as not every error is a 500 error; it could be a bad request or invalid credentials or something like that.