Skip to content

Commit

Permalink
warmup done
Browse files Browse the repository at this point in the history
  • Loading branch information
NadaJrad committed Mar 15, 2024
1 parent 43932b3 commit 28eec43
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions 6_nodejs/week2/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,39 @@ app.get("/search", (req, res) => {
res.json(documents);
}
});

// GET /documents/:id
app.get("/documents/:id", (req, res) => {
const id = parseInt(req.params.id);
const document = documents.find(doc => doc.id === id);
if (document) {
res.json(document);
} else {
res.status(404).send("Document not found");
}
});

// POST /search
app.post("/search", (req, res) => {
const query = req.query.q;
const fields = req.body.fields;

if (query && fields) {
res.status(400).send("Both query parameter and fields in body cannot be provided simultaneously.");
} else if (query) {
const matchedDocuments = documents.filter(doc => Object.values(doc).some(value => typeof value === 'string' && value.includes(query)));
res.json(matchedDocuments);
} else if (fields) {
const filteredDocuments = documents.filter(doc => {
for (const field in fields) {
if (doc[field] !== fields[field]) {
return false;
}
}
return true;
});
res.json(filteredDocuments);
} else {
res.json(documents);
}
});

0 comments on commit 28eec43

Please sign in to comment.