generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 103
WM5 | ADNIYA YOUSAF | MODULE DATABASE | E-COMMERCE-API | WEEK 4 #164
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
Open
adniyaYousaf
wants to merge
8
commits into
CodeYourFuture:main
Choose a base branch
from
adniyaYousaf:e-commerce-API
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1f368b1
completed the big spender
adniyaYousaf 40dea4a
Set up the db connection and config the jest
adniyaYousaf f2ffac7
add test that get all products and get the searched product
adniyaYousaf 4a2527f
got customer by id
adniyaYousaf 99e851a
Added: new test to add new customer in customers table
adniyaYousaf f6f5880
complete 4 post endpoint tests
adniyaYousaf ed889aa
fix: test 7
adniyaYousaf 34a2d53
fix: sql injection
adniyaYousaf 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
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 |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| DB_HOST=localhost | ||
| DB_PORT=5432 | ||
| DB_USER=adniya | ||
| DB_PASSWORD=ad6057nya510 | ||
| DB_NAME=cyf-ecommerce | ||
| PORT=3000 |
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,101 @@ | ||
| const express = require("express"); | ||
| import express from "express"; | ||
| import 'dotenv/config'; | ||
| import pg from "pg" | ||
| const { Pool } = pg; | ||
| const app = express(); | ||
| // 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 | ||
| app.use(express.json()); | ||
|
|
||
| module.exports = app; | ||
| const db = new Pool({ | ||
| host: process.env.DB_HOST, | ||
| port: process.env.DB_PORT, | ||
| user: process.env.DB_USER, | ||
| password: process.env.DB_PASSWORD, | ||
| database: process.env.DB_NAME | ||
| }); | ||
|
|
||
| app.get('/products', async (req, res) => { | ||
| try { | ||
| const result = await db.query('select p.product_name, pa.unit_price ,s.supplier_name from products p inner join product_availability pa on (p.id = pa.prod_id) inner join suppliers s on (s.id =pa.supp_id);'); | ||
| res.send(result.rows); | ||
| } catch (error) { | ||
| res.send(500); | ||
| } | ||
| }) | ||
|
|
||
| app.get('/products/:name', async (req, res) => { | ||
| console.log(req.params.name); | ||
| try { | ||
| const name = req.params.name; | ||
| const result = await db.query(`SELECT product_name FROM products WHERE product_name ILIKE '%${name}%';`); | ||
| res.status(200).send(result.rows); | ||
| } catch (error) { | ||
| res.status(400).send(`failed to get searched name`); | ||
| } | ||
| }); | ||
|
|
||
| app.get('/customers/:customerId', async (req, res) => { | ||
| try { | ||
| const result = await db.query('select * from customers where id= $1;', [req.params.customerId]) | ||
| res.status(200).send(result.rows); | ||
| } catch (error) { | ||
| res.status(400).send('Customer not found!') | ||
| } | ||
| }) | ||
|
|
||
| app.post('/customers', async (req, res) => { | ||
| try { | ||
| await db.query(`INSERT INTO customers (name, address, city, country) | ||
| VALUES($1,$2, $3, $4)`, [req.body.name, req.body.address, req.body.city, req.body.country]); | ||
| res.status(201).send('Added the customer successfully'); | ||
| } catch (error) { | ||
| res.status(500).send(`failed to add new customer! ${error}`) | ||
| } | ||
| }) | ||
|
|
||
| app.post('/products', async (req, res) => { | ||
| try { | ||
| await db.query(`INSERT INTO products ( product_name) | ||
| VALUES($1)`, [req.body.product_name]); | ||
| res.status(201).send('Added the new product successfully'); | ||
| } catch (error) { | ||
| res.status(500).send(`failed to add new products! ${error}`); | ||
| } | ||
| }) | ||
|
|
||
| app.post('/availability', async (req, res) => { | ||
| try { | ||
| if (req.body.unit_price > 0) { | ||
| await db.query(`INSERT INTO product_availability (prod_id, supp_id, unit_price) | ||
| VALUES((select id from products where id= $1 ), $2, $3)`, [req.body.prod_id, req.body.supp_id, req.body.unit_price]); | ||
| res.status(201).send('Price cant be below zero!'); | ||
| } else { | ||
| res.status(404).send('Added the new entry in product availability successfully'); | ||
| } | ||
| } catch (error) { | ||
| res.status(500).send(`failed to add new entry in product availability! ${error}`) | ||
| } | ||
| }) | ||
|
|
||
| app.post('/customers/:customerId/orders', async (req, res) => { | ||
| const result = db.query(`select id from customers where id= $1`, [req.params.customerId]); | ||
|
|
||
| if (result !== null) { | ||
| try { | ||
| await db.query(`INSERT INTO orders (order_date, order_reference, customer_id) | ||
| VALUES($1, $2, (select id from customers where id= $3 ))`, [req.body.order_date, req.body.order_reference, req.params.customerId]); | ||
| res.status(201).send('Successfully added the order!'); | ||
| } catch (error) { | ||
| res.status(500).send('Failed to add order!') | ||
| } | ||
| } else { | ||
| res.status(400).send('Customer ID is not Valid') | ||
| } | ||
| }) | ||
|
|
||
| const port = process.env.PORT; | ||
|
|
||
| app.listen(port, () => { | ||
| console.log(`Server listening to port ${port}!`); | ||
| }) | ||
|
|
||
| export default app; | ||
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 |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export default { | ||
| transform: {}, | ||
| }; |
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.
Check failure
Code scanning / SonarCloud
Database queries should not be vulnerable to injection attacks