-
Notifications
You must be signed in to change notification settings - Fork 140
Node eval #179
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?
Node eval #179
Changes from all commits
3b7cf05
5ef95bc
5cd3b83
e9e8d7c
e4f70e7
3b484a6
7703604
3af7186
adc712c
b1f284d
727dd62
76b4dfd
c064025
bd1cc35
db9ca2f
17ba198
912ffe7
c28a769
8490211
8bd6c15
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,8 +3,9 @@ const bodyParser = require('body-parser'); | |
| const jwt = require('jsonwebtoken'); | ||
| const swaggerUi = require('swagger-ui-express'); | ||
| const YAML = require('yamljs'); | ||
| const swaggerDocument = YAML.load('./swagger.yaml'); // Replace './swagger.yaml' with the path to your Swagger file | ||
| const swaggerDocument = YAML.load('./swagger.yaml'); | ||
| const app = express(); | ||
| require("dotenv").config(); | ||
|
|
||
| app.use(bodyParser.json()); | ||
|
|
||
|
|
@@ -13,6 +14,8 @@ const users = require('../initial-data/users.json'); | |
| const brands = require('../initial-data/brands.json'); | ||
| const products = require('../initial-data/products.json'); | ||
|
|
||
| const jwtSecret = process.env.jwtSecret; | ||
|
|
||
| // Error handling | ||
| app.use((err, req, res, next) => { | ||
| console.error(err.stack); | ||
|
|
@@ -28,4 +31,104 @@ app.listen(PORT, () => { | |
| console.log(`Server running on port ${PORT}`); | ||
| }); | ||
|
|
||
| // Middleware for authentication | ||
| const authenticateToken = (req, res, next) => { | ||
| const token = req.headers['authorization']; | ||
| if (!token) return res.status(401).json({ error: 'Unauthorized' }); | ||
|
|
||
| jwt.verify(token, jwtSecret, (err, user) => { | ||
| if (err) return res.status(401).json({ error: 'Unauthorized' }); | ||
| req.user = user; | ||
| next(); | ||
| }); | ||
| }; | ||
|
|
||
| // Routes | ||
| app.get('/brands', (req, res) => { | ||
| res.json(brands); | ||
| }); | ||
|
|
||
| app.get('/brands/:id/products', (req, res) => { | ||
| const brandId = req.params.id; | ||
| const brandProducts = products.filter(product => product.categoryId === brandId); | ||
| if (brandProducts.length === 0) { | ||
| return res.status(404).json({ error: 'Brand not found' }); | ||
| } | ||
| res.json(brandProducts); | ||
| }); | ||
|
|
||
| app.get('/products', (req, res) => { | ||
| res.json(products); | ||
| }); | ||
|
|
||
| app.post('/login', (req, res) => { | ||
| if (req.body.username === undefined || req.body.password === undefined) { | ||
| return res.status(400).json({ error: "Username and password are required" }); | ||
| } | ||
| const { username, password } = req.body; | ||
| const user = users.find( | ||
| (u) => u.login.username === username && u.login.password === password | ||
| ); | ||
| if (!user) { | ||
| return res.status(401).json({ error: "Invalid credentials" }); | ||
| } | ||
| const token = jwt.sign({ username: user.login.username }, jwtSecret, { | ||
| expiresIn: "1h", | ||
| }); | ||
| res.json({ token }); | ||
| }); | ||
|
|
||
| app.get('/me/cart', authenticateToken, (req, res) => { | ||
| const userCart = users.find(u => u.login.username === req.user.username).cart; | ||
| if (!userCart) { | ||
| return res.status(401).json({ error: "Unauthorized" }); | ||
| } | ||
| res.json(userCart); | ||
| }); | ||
|
|
||
| app.post('/me/cart', authenticateToken, (req, res) => { | ||
| const { id } = req.body; | ||
| const userCart = users.find((u) => u.login.username === req.user.username).cart; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the user isn’t found, your code will throw an error on .cart access. |
||
| if (!userCart) { | ||
| return res.status(401).json({ error: "Unauthorized" }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is 404, not 401. |
||
| } | ||
| const product = products.find((item) => item.id === id); | ||
| const existingProduct = userCart.find((item) => item.id === id); | ||
| if (existingProduct) { | ||
| existingProduct.quantity += 1; | ||
| } else { | ||
| userCart.push({ ...product, quantity: 1 }); | ||
| } | ||
| res.json(userCart); | ||
| }); | ||
|
|
||
| app.delete('/me/cart/:productId', authenticateToken, (req, res) => { | ||
| const productId = req.params.productId; | ||
| const user = users.find(u => u.id === req.user.id); | ||
| if (!user) { | ||
| return res.status(401).json({ error: 'Unauthorized' }); | ||
| } | ||
| const productIndex = user.cart.findIndex(item => item.id === productId); | ||
| if (productIndex === -1) { | ||
| return res.status(404).json({ error: 'Product not found in cart' }); | ||
| } | ||
| user.cart.splice(productIndex, 1); | ||
| res.json(user.cart); | ||
| }); | ||
|
|
||
| app.post('/me/cart/:productId', authenticateToken, (req, res) => { | ||
| const productId = req.params.id; | ||
| const { quantity } = req.body; | ||
| const user = users.find(u => u.id === req.user.id); | ||
| if (!user) { | ||
| return res.status(401).json({ error: 'Unauthorized' }); | ||
| } | ||
| const product = user.cart.find(item => item.productId === productId); | ||
| if (!product) { | ||
| return res.status(404).json({ error: 'Product not found in cart' }); | ||
| } | ||
| product.quantity = quantity; | ||
| res.json(user.cart); | ||
| }); | ||
|
|
||
| module.exports = app; | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
beware your code alignment