Skip to content
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

Nodejs week2/parisa #215

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions class30-homework
Submodule class30-homework added at e774a9
Binary file added databases/week2/Screenshot 2025-02-12 185047.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 15 additions & 0 deletions nodejs/week2/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import express from 'express'
import { searchRouter } from "./search.js"
const app = express();
const port = process.env.PORT || 3000;

// Support parsing JSON requests
app.use(express.json());
app.use("/search", searchRouter);
app.get("/", (req, res) => {
res.send("This is a search engine");
});

app.listen(port, () => {
console.log(`Listening on port ${port}`);
});
13 changes: 13 additions & 0 deletions nodejs/week2/documents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[
{
"id": 1,
"name": "Used Apple Airpods",
"price": "50",
"description": "Battery life is not great"
},
{
"id": 2,
"type": "doc",
"value": "hello world"
}
]
50 changes: 50 additions & 0 deletions nodejs/week2/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import express from "express";
import documents from "./documents.json" with { type: 'json' };

export const searchRouter = express.Router();

// GET: Fetch all documents or filter by query
searchRouter.get("/", (req, res) => {
const query = req.query.q;

if (!query) {
res.status(200).json(documents);
return
}

const filteredResults = documents.filter(item =>
item.value?.toLowerCase().includes(query.toLowerCase())
);

res.status(200).json(filteredResults);
});

// GET: Fetch a document by ID
searchRouter.get('/:id', (req, res) => {
const id = Number(req.params.id);
const document = documents.find(item => item.id === id);

if (!document) {
res.status(404).json({ message: "Not Found. Check your ID and try again." });
return
}

res.status(200).json(document);
});

// POST: Filter documents based on provided fields
searchRouter.post("/", (req, res) => {
const { fields } = req.body;

if (!fields || typeof fields !== "object") {
res.status(400).json({ message: "Invalid request body. Expected an object with fields." });
return
}

const filteredDocuments = documents.filter(item => {
Object.entries(fields).every(([key, value]) => item[key] === value);
return
});

res.status(200).json(filteredDocuments);
});