Skip to content
Merged
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
31 changes: 20 additions & 11 deletions src/domains/keyword/keywordController.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,32 @@ const getArticlesByCompany = async (req, res) => {
};

const getArticleCounts = async (req, res) => {
const { keyword } = req.params;
console.log('개수 조회 키워드: ', keyword);
const keywords = req.query.keywords ? req.query.keywords.split(',') : [];

if (!keyword) {
return res.status(400).json({ error: "Keyword is required" });
if (keywords.length === 0) {
return res.status(400).json({ error: "At least one keyword is required" });
}

console.log('개수 조회 키워드: ', keywords);

try {
const count = await fetchArticleCounts(keyword);
if (count === null) {
return res.status(404).json({ error: "Keyword not found in stats" });
}
// 여러 키워드를 병렬로 조회
const results = await Promise.all(
keywords.map(keyword => fetchArticleCounts(keyword))
);

// 응답 데이터 정리
const response = Object.fromEntries(
keywords.map((keyword, index) => [
keyword,
results[index] || { error: "Keyword not found in stats" }
])
);

res.status(200).json(count);
res.status(200).json(response);
} catch (error) {
console.error("Error fetching article count:", error.message);
res.status(500).json({ error: "Failed to fetch article count" });
console.error("Error fetching article counts:", error.message);
res.status(500).json({ error: "Failed to fetch article counts" });
}
};

Expand Down