-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create endpoint to get articles by url
- Loading branch information
Showing
8 changed files
with
542 additions
and
11 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,44 @@ | ||
const { DB } = include("db/"); | ||
|
||
exports.get_articles = async (req, res) => { | ||
const { id, url } = req.query; | ||
|
||
// Ensure at least one of `id` or `url` is provided | ||
if ( | ||
(!id || (Array.isArray(id) && id.length === 0)) && | ||
(!url || (Array.isArray(url) && url.length === 0)) | ||
) { | ||
return res | ||
.status(400) | ||
.json({ error: "At least one 'id' or 'url' must be provided." }); | ||
} | ||
|
||
// Normalize `id` and `url` into arrays | ||
const idList = id ? (Array.isArray(id) ? id : [id]).map(Number) : []; | ||
const urlList = url ? (Array.isArray(url) ? url : [url]) : []; | ||
|
||
try { | ||
const results = await DB.blog.any( | ||
` | ||
SELECT a.id, a.url, a.article_type, a.title, a.iso3, a.posted_date, a.posted_date_str, a.parsed_date, a.language, a.created_at, c.html_content, | ||
regexp_replace( | ||
regexp_replace(COALESCE(b.content, c.html_content), E'\\n', ' ', 'g'), | ||
E'<iframe[^>]*>.*?</iframe>', | ||
'', | ||
'gi' | ||
) AS content | ||
FROM articles a | ||
JOIN article_content b ON b.article_id = a.id | ||
JOIN article_html_content c ON c.article_id = a.id | ||
WHERE (array_length($1::int[], 1) > 0 AND a.id = ANY ($1::int[])) | ||
OR (array_length($2::text[], 1) > 0 AND a.url = ANY ($2::text[])) | ||
`, | ||
[idList, urlList] | ||
); | ||
|
||
return res.status(200).json(results); | ||
} catch (err) { | ||
console.error("Database query failed:", err); | ||
return res.status(500).json({ error: "An unexpected error occurred." }); | ||
} | ||
}; |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.