Skip to content

Commit eb31125

Browse files
authored
Merge branch 'main' into feature/fetchAllProducts
2 parents b28d3af + 1da0123 commit eb31125

5 files changed

Lines changed: 34 additions & 7 deletions

File tree

scripts/seed.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@ async function seedDB() {
2828
await Product.deleteMany();
2929
await Product.insertMany(seedProducts);
3030
console.log("✅ Seed data inserted");
31-
process.exit();
3231
} catch (err) {
3332
console.error("❌ Error seeding DB", err);
3433
process.exit(1);
3534
}
3635
}
3736

38-
seedDB();
37+
export default seedDB;

server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import app from './src/app.js';
22

3-
const PORT = process.env.PORT || 3000;
3+
const PORT = process.env.PORT || 5000;
44

55
app.listen(PORT, () => {
66
console.log(`Server is running on port ${PORT}`);

src/app.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import express from 'express';
22
import cors from 'cors';
33
import productRoutes from './routes/product.routes.js';
4+
import seedDB from '../scripts/seed.js';
45

56
const app = express();
67

8+
seedDB();
79
app.use(cors());
810
app.use(express.json());
911

12+
app.get('/',(req,res)=>{
13+
res.send("Welcome to Homepage");
14+
})
15+
1016
// Routes
1117
app.use('/api/products', productRoutes);
1218

src/controllers/product.controller.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import Product from '../models/product.model.js';
1+
import mongoose from "mongoose";
2+
import Product from "../models/product.model.js";
3+
24

35
/**
46
* Get all products from the database
57
* @route GET /api/products
68
* @returns {Array} products - Array of product objects or empty array if no products
79
*/
8-
export const getAllProducts = async (req, res) => {
10+
const getAllProducts = async (req, res) => {
911
try {
1012
// Fetch all products from the database
1113
const products = await Product.find({});
@@ -21,3 +23,21 @@ export const getAllProducts = async (req, res) => {
2123
});
2224
}
2325
};
26+
27+
const getProductById = async(req,res)=>{
28+
//fetches ID from request parameters
29+
const id = req.params.id;
30+
if(!mongoose.Types.ObjectId.isValid(id)){
31+
//checks for validity of the id
32+
return res.status(400).json({error: "Invalid ID format"});
33+
}
34+
// fetches the product details if id is valid & exists
35+
const product = await Product.findById(id);
36+
if(!product){
37+
//sends 404 error if product not found
38+
return res.status(404).json({message:"Product not found"});
39+
}
40+
return res.json(product);
41+
}
42+
43+
export {getAllProducts, getProductById}

src/routes/product.routes.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import express from 'express';
2-
import { getAllProducts } from '../controllers/product.controller.js';
2+
import { getAllProducts, getProductById } from '../controllers/product.controller.js';
33

44
const router = express.Router();
55

66
// Fetch all products
77
router.get('/', getAllProducts);
88

9-
export default router;
9+
router.get('/:id',getProductById);
10+
11+
export default router

0 commit comments

Comments
 (0)