generated from HackYourFuture-CPH/hyf-assignment-template
-
Notifications
You must be signed in to change notification settings - Fork 1
warm up done #237
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
Open
siderdk
wants to merge
1
commit into
main
Choose a base branch
from
nodejs-week2/soheib
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
warm up done #237
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import express from 'express' | ||
import bodyParser from 'body-parser'; | ||
import fs from 'fs'; | ||
|
||
|
||
const documents = JSON.parse(fs.readFileSync('./document.json', 'utf-8')); | ||
|
||
const app = express(); | ||
const port = 3600; | ||
|
||
// Support parsing JSON requests | ||
app.use(express.json()); | ||
|
||
app.use(bodyParser.json()); | ||
app.use(bodyParser.urlencoded({ extended: true })); | ||
|
||
|
||
//get all documents | ||
app.get('/search', (req, res) => { | ||
|
||
if(!req.query.q){ | ||
console.log(`No query specified. sending all documents`); | ||
return res.json(documents); | ||
}; | ||
const query = req.query.q.toLowerCase(); | ||
const result = documents.filter((document) => { | ||
return Object.values(document).some(value => value.toString().toLowerCase().includes(query)); | ||
}); | ||
console.log(`Search for ${query} returned ${result.length} results`); | ||
return res.json(result); | ||
|
||
}); | ||
|
||
//GET /documents/:id | ||
app.get('/documents/:id', (req, res) => { | ||
const id = req.params.id; | ||
const result = documents.find(document => document.id == id); //I know I need to parse the id and stick to string equality. but this is just a warm up. can I get away with this? | ||
if (!result) { | ||
return res.status(404).json({ error: 'Document not found' }); | ||
} | ||
console.log(`Document with id ${id} found`); | ||
return res.json(result); | ||
}); | ||
|
||
|
||
//POST /search | ||
app.post('/search', (req, res) => { | ||
const query = req.query.q ? req.query.q.toLowerCase() : null; | ||
const fields = req.body.fields || null; | ||
//when both query and fields are provided | ||
if (fields && query) { | ||
return res.status(400).json({ | ||
message: "Both query parameter 'q' and 'fields' in body cannot be provided at the same time." | ||
}); | ||
} | ||
|
||
//when neither query nor fields are provided | ||
if (!query && !fields) { | ||
return res.status(400).json({ | ||
message: "Please provide either query parameter 'q' or 'fields' in body." | ||
}); | ||
} | ||
|
||
let result; | ||
|
||
//when only fields is provided | ||
if (fields) { | ||
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.
|
||
result = documents.filter((document) => { | ||
return Object.keys(fields).every((field) => { | ||
return document[field] && document[field].toString().toLowerCase() === fields[field].toString().toLowerCase(); | ||
}); | ||
}); | ||
console.log(`Search in fields ${JSON.stringify(fields)} returned ${result.length} results`); | ||
|
||
} | ||
|
||
//when only query is provided | ||
if (query) { | ||
result = documents.filter((document) => { | ||
return Object.values(document).some((value) => { | ||
return value.toString().toLowerCase().includes(query); | ||
}); | ||
}); | ||
console.log(`Search for ${query} returned ${result.length} results`); | ||
} | ||
return res.json(result); | ||
|
||
}); | ||
|
||
|
||
app.get("/", (req, res) => { | ||
res.send("This is a search engine"); | ||
}); | ||
|
||
app.listen(port, () => { | ||
console.log(`Listening on port ${port}`); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I like your comment😄
yes, you can do anything you want.