-
Notifications
You must be signed in to change notification settings - Fork 140
Sunglasses.io #182
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?
Sunglasses.io #182
Changes from all commits
2f626e7
719b52b
b30764a
80c3e2a
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -5,14 +5,118 @@ 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 app = express(); | ||||||
|
|
||||||
| const PORT = process.env.PORT || 3000; | ||||||
| const cors = require('cors'); | ||||||
| app.use(bodyParser.json()); | ||||||
| app.use(cors()); | ||||||
|
|
||||||
| // Importing the data from JSON files | ||||||
| const users = require('../initial-data/users.json'); | ||||||
| const brands = require('../initial-data/brands.json'); | ||||||
| const products = require('../initial-data/products.json'); | ||||||
|
|
||||||
| app.get('/api/products', (req, res) => { | ||||||
| res.status(200).json(products); | ||||||
| }); | ||||||
|
|
||||||
| app.get('/api/products/:id', (req, res) => { | ||||||
| const product = products.find(p => p.id === req.params.id); | ||||||
|
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. no need to be cheap, keep it readable
Suggested change
|
||||||
| if (!product) { | ||||||
| return res.status(404).json({ error: 'Product not found' }); | ||||||
| } | ||||||
| res.status(200).json(product); | ||||||
| }); | ||||||
|
|
||||||
| app.get('/api/users', (req, res) => { | ||||||
| res.status(200).json(users); | ||||||
|
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. alignment |
||||||
| }); | ||||||
|
|
||||||
| app.get('/api/users/:username', (req, res) => { | ||||||
| const user = users.find(u => u.login.username === req.params.username); | ||||||
|
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. user, don't use u |
||||||
| if (!user) { | ||||||
| return res.status(404).json({ error: 'User not found' }); | ||||||
| } | ||||||
| res.status(200).json(user); | ||||||
| }); | ||||||
|
|
||||||
| app.get('/api/users/:username/cart', (req, res) => { | ||||||
| const user = users.find(u => u.login.username === req.params.username); | ||||||
| if (!user) { | ||||||
| return res.status(404).json({ error: 'User not found' }); | ||||||
| } | ||||||
| res.status(200).json(user); | ||||||
| }); | ||||||
|
|
||||||
| // add item to a users cart | ||||||
| app.post('/api/users/:username/cart', (req, res) => { | ||||||
| const user = users.find(u => u.login.username === req.params.username); | ||||||
|
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. dry, anything you can do to reuse? |
||||||
| if (!user) { | ||||||
| return res.status(404).json({ error: 'User not found' }); | ||||||
| } | ||||||
|
|
||||||
| const { productId, quantity } = req.body; | ||||||
|
|
||||||
| // ValiDate the request | ||||||
| if (!productId || typeof quantity !== 'number' || quantity < 1) { | ||||||
| return res.status(400).json({ error: 'Invalid request body' }); | ||||||
| } | ||||||
|
|
||||||
| const product = products.find(p => p.id === productId); | ||||||
| if (!product) { | ||||||
| return res.status(404).json({ error: 'Product not found' }); | ||||||
| } | ||||||
|
|
||||||
| // check if product already exists in the cart | ||||||
| const existingItem = user.cart.find(item => item.productId === productId); | ||||||
|
|
||||||
| if (existingItem) { | ||||||
| // if exists, update the quantity | ||||||
| existingItem.quantity += quantity; | ||||||
| } else { | ||||||
| // If it doesn't, add it as a new item | ||||||
| user.cart.push({ productId, quantity }); | ||||||
| } | ||||||
|
|
||||||
| res.status(201).json({ message: 'Product added to cart' }); | ||||||
| }); | ||||||
|
|
||||||
| // Remove item from the user cart | ||||||
| app.delete('/api/users/:username/cart/product/:productId', (req, res) => { | ||||||
| const user = users.find(u => u.login.username === req.params.username); | ||||||
| if (!user) { | ||||||
| return res.status(404).json({ error: 'User not found' }); | ||||||
| } | ||||||
|
|
||||||
| const index = user.cart.findIndex(item => item.productId === req.params.productId); | ||||||
|
|
||||||
| if (index === -1) { | ||||||
| return res.status(404).json({ error: 'Product not in cart' }); | ||||||
| } | ||||||
|
|
||||||
| user.cart.splice(index, 1); | ||||||
| res.status(200).json({ message: 'Product removed from cart' }); | ||||||
| }); | ||||||
|
|
||||||
| app.post('/api/login', (req, res) => { | ||||||
| const { username, password } = req.body; | ||||||
|
|
||||||
| // Check if the fields are filled | ||||||
| if (!username || !password) { | ||||||
| return res.status(400).json({ error: 'Username and password are required' }); | ||||||
| } | ||||||
|
|
||||||
| // Find a user that matches both username and password | ||||||
| const user = users.find(u => u.login.username === username && u.login.password === password); | ||||||
|
|
||||||
| // If no user is found, return an error | ||||||
| if (!user) { | ||||||
| return res.status(401).json({ error: 'Invalid username or password' }); | ||||||
| } | ||||||
|
|
||||||
| // If match found, return a success message (no token for now) | ||||||
| res.status(200).json({ message: 'Login successful' }); | ||||||
|
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 not really authenticating, you need to return a token that then is sent on every call and every relevant api needs to check it. |
||||||
| }); | ||||||
|
|
||||||
| // Error handling | ||||||
| app.use((err, req, res, next) => { | ||||||
| console.error(err.stack); | ||||||
|
|
@@ -23,7 +127,7 @@ app.use((err, req, res, next) => { | |||||
| app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument)); | ||||||
|
|
||||||
| // Starting the server | ||||||
| const PORT = process.env.PORT || 3000; | ||||||
|
|
||||||
| app.listen(PORT, () => { | ||||||
| console.log(`Server running on port ${PORT}`); | ||||||
| }); | ||||||
|
|
||||||
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.
check your code alignment