Skip to content

Commit

Permalink
Update: Added nodemon and SwaggerJS
Browse files Browse the repository at this point in the history
  • Loading branch information
hoangsonww committed Sep 4, 2024
1 parent 8bd69ad commit 50882f2
Show file tree
Hide file tree
Showing 30 changed files with 1,254 additions and 1,350 deletions.
24 changes: 24 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"printWidth": 160,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"trailingComma": "es5",
"bracketSpacing": true,
"arrowParens": "avoid",
"overrides": [
{
"files": "*.html",
"options": {
"parser": "html"
}
},
{
"files": "*.js",
"options": {
"parser": "babel"
}
}
]
}
21 changes: 10 additions & 11 deletions backend/config/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ const mongoose = require('mongoose');
require('dotenv').config();

const connectDB = async () => {
try {
await mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log('MongoDB connected...');
}
catch (err) {
console.error(err.message);
process.exit(1);
}
try {
await mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log('MongoDB connected...');
} catch (err) {
console.error(err.message);
process.exit(1);
}
};

module.exports = connectDB;
14 changes: 7 additions & 7 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ const PORT = process.env.PORT || 5000;

// Database Connection
mongoose
.connect(process.env.MONGO_URI, { })
.then(() => console.log('MongoDB Connected'))
.catch((err) => console.log(err));
.connect(process.env.MONGO_URI, {})
.then(() => console.log('MongoDB Connected'))
.catch(err => console.log(err));

// Middleware
app.use(cors());
Expand All @@ -32,8 +32,8 @@ app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));

// Seed database on startup
seedDB().then(() => {
// Start Server after seeding
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});
// Start Server after seeding
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});
});
3 changes: 1 addition & 2 deletions backend/routes/checkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ router.post('/create-order', async (req, res) => {
await new Promise(resolve => setTimeout(resolve, 3000));

res.status(201).json({ message: 'Order created successfully!' });
}
catch (error) {
} catch (error) {
console.error('Error creating order:', error);
res.status(500).json({ error: 'Failed to create order' });
}
Expand Down
9 changes: 3 additions & 6 deletions backend/routes/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ router.get('/', async (req, res) => {
}));

res.json(formattedProducts);
}
catch (err) {
} catch (err) {
res.status(500).send('Server error');
}
});
Expand Down Expand Up @@ -90,8 +89,7 @@ router.get('/:id', async (req, res) => {
return res.status(404).send('Product not found');
}
res.json(product);
}
catch (err) {
} catch (err) {
res.status(500).send('Server error');
}
});
Expand Down Expand Up @@ -123,8 +121,7 @@ router.get('/category/:category', async (req, res) => {
try {
const products = await Product.find({ category: req.params.category });
res.json(products);
}
catch (err) {
} catch (err) {
res.status(500).send('Server error');
}
});
Expand Down
7 changes: 2 additions & 5 deletions backend/routes/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,12 @@ router.get('/', async (req, res) => {
const query = req.query.q;

const products = await Product.find({
$or: [
{ name: { $regex: query, $options: 'i' } },
{ description: { $regex: query, $options: 'i' } }
]
$or: [{ name: { $regex: query, $options: 'i' } }, { description: { $regex: query, $options: 'i' } }],
});

res.json(products);
} catch (error) {
console.error("Error searching products:", error);
console.error('Error searching products:', error);
res.status(500).json({ error: 'An error occurred during the search.' });
}
});
Expand Down
Loading

0 comments on commit 50882f2

Please sign in to comment.