Skip to content

Commit 35ce7ff

Browse files
authored
Merge pull request #35 from TWC-codeit/feat/#12-articles
fix: articles GET ์‹œ API ๋‹ค์ค‘ ์ฟผ๋ฆฌ ์ ์šฉ
2 parents 6269ca3 + b33cb99 commit 35ce7ff

File tree

3 files changed

+42
-37
lines changed

3 files changed

+42
-37
lines changed
Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
11
const { fetchArticlesByKeyword, fetchArticlesByCompany, fetchArticleCounts, fetchKeywords } = require("./keywordService");
22
const redis = require("../../config/redis.js"); // Redis ํด๋ผ์ด์–ธํŠธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
33

4-
const getArticlesByKeyword = async (req, res) => {
5-
const { keyword } = req.params;
6-
console.log('์กฐํšŒ ํ‚ค์›Œ๋“œ: ', keyword);
4+
const getArticlesByKeywords = async (req, res) => {
5+
const keywords = req.query.keywords ? req.query.keywords.split(',') : [];
76

8-
if (!keyword) {
9-
return res.status(400).json({ error: "Keyword is required" });
7+
if (keywords.length === 0) {
8+
return res.status(400).json({ error: "At least one keyword is required" });
109
}
1110

11+
console.log('์กฐํšŒ ํ‚ค์›Œ๋“œ:', keywords);
12+
1213
try {
13-
const articles = await fetchArticlesByKeyword(keyword);
14-
res.status(200).json({ keyword, articles });
14+
// ์—ฌ๋Ÿฌ ํ‚ค์›Œ๋“œ๋ฅผ ๋ณ‘๋ ฌ๋กœ ์กฐํšŒ
15+
const results = await Promise.all(
16+
keywords.map(async (keyword) => {
17+
const articles = await fetchArticlesByKeyword(keyword);
18+
return { keyword, articles };
19+
})
20+
);
21+
22+
// ํ‚ค์›Œ๋“œ๋ณ„ ๊ธฐ์‚ฌ ๋ฆฌ์ŠคํŠธ ์ •๋ฆฌ
23+
const response = Object.fromEntries(
24+
results.map(({ keyword, articles }) => [keyword, articles])
25+
);
26+
27+
res.status(200).json(response);
1528
} catch (error) {
1629
console.error("Error fetching articles:", error.message);
1730
res.status(500).json({ error: "Failed to fetch articles" });
1831
}
1932
};
2033

34+
2135
const getArticlesByCompany = async (req, res) => {
2236
const { keyword, company } = req.params;
2337
console.log("[getArticlesByCompany] ์š”์ฒญ๋œ ํ‚ค์›Œ๋“œ:", keyword, "์–ธ๋ก ์‚ฌ:", company);
@@ -40,32 +54,23 @@ const getArticlesByCompany = async (req, res) => {
4054
};
4155

4256
const getArticleCounts = async (req, res) => {
43-
const keywords = req.query.keywords ? req.query.keywords.split(',') : [];
57+
const { keyword } = req.params;
58+
console.log('๊ฐœ์ˆ˜ ์กฐํšŒ ํ‚ค์›Œ๋“œ: ', keyword);
4459

45-
if (keywords.length === 0) {
46-
return res.status(400).json({ error: "At least one keyword is required" });
60+
if (!keyword) {
61+
return res.status(400).json({ error: "Keyword is required" });
4762
}
4863

49-
console.log('๊ฐœ์ˆ˜ ์กฐํšŒ ํ‚ค์›Œ๋“œ: ', keywords);
50-
5164
try {
52-
// ์—ฌ๋Ÿฌ ํ‚ค์›Œ๋“œ๋ฅผ ๋ณ‘๋ ฌ๋กœ ์กฐํšŒ
53-
const results = await Promise.all(
54-
keywords.map(keyword => fetchArticleCounts(keyword))
55-
);
56-
57-
// ์‘๋‹ต ๋ฐ์ดํ„ฐ ์ •๋ฆฌ
58-
const response = Object.fromEntries(
59-
keywords.map((keyword, index) => [
60-
keyword,
61-
results[index] || { error: "Keyword not found in stats" }
62-
])
63-
);
65+
const count = await fetchArticleCounts(keyword);
66+
if (count === null) {
67+
return res.status(404).json({ error: "Keyword not found in stats" });
68+
}
6469

65-
res.status(200).json(response);
70+
res.status(200).json(count);
6671
} catch (error) {
67-
console.error("Error fetching article counts:", error.message);
68-
res.status(500).json({ error: "Failed to fetch article counts" });
72+
console.error("Error fetching article count:", error.message);
73+
res.status(500).json({ error: "Failed to fetch article count" });
6974
}
7075
};
7176

@@ -81,4 +86,4 @@ const getKeywords = async (req, res) => {
8186
};
8287

8388

84-
module.exports = { getArticlesByKeyword, getArticlesByCompany, getArticleCounts, getKeywords };
89+
module.exports = { getArticlesByKeywords, getArticlesByCompany, getArticleCounts, getKeywords };

โ€Žsrc/domains/keyword/keywordRoutes.jsโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const router = express.Router();
88
router.get('/articles/count', getArticleCounts);
99

1010
// ํŠน์ • ํ‚ค์›Œ๋“œ๋ฅผ ํฌํ•จํ•œ ์–ธ๋ก ์‚ฌ ๋ณ„ ๊ธฐ์‚ฌ ๋ชฉ๋ก ์กฐํšŒ
11-
router.get('/articles/:keyword', getArticlesByKeyword);
11+
router.get('/articles', getArticlesByKeyword);
1212

1313
router.get('/articles/:keyword/:company', getArticlesByCompany);
1414

โ€Žsrc/domains/keyword/keywordService.jsโ€Ž

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,20 @@ const { normalizeData } = require("./redisUtils");
33

44
const fetchArticlesByKeyword = async (keyword) => {
55
try {
6-
// ํ‚ค ์กด์žฌ ์—ฌ๋ถ€ ํ™•์ธ
7-
const key = `keyword:${keyword}:company_articles:*`;
8-
const companyKeys = await redisClient.keys(key);
9-
console.log(companyKeys)
6+
const keyPattern = `keyword:${keyword}:company_articles:*`;
7+
const companyKeys = await redisClient.keys(keyPattern);
8+
109
if (!companyKeys || companyKeys.length === 0) {
11-
console.log(`Key does not exist: ${key}`);
10+
console.log(`Key does not exist: ${keyPattern}`);
1211
return {};
1312
}
13+
1414
const articlesByCompany = {};
1515

1616
await Promise.all(
1717
companyKeys.map(async (companyKey) => {
1818
const company = companyKey.split(":").pop();
19-
const type = await redisClient.type(companyKey); // `TYPE` ๋ช…๋ น์–ด ํ˜ธ์ถœ
20-
21-
// ํ‚ค์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž… ํ™•์ธ
19+
const type = await redisClient.type(companyKey);
2220

2321
let data = [];
2422
if (type === "string") {
@@ -35,13 +33,15 @@ const fetchArticlesByKeyword = async (keyword) => {
3533
articlesByCompany[company] = data;
3634
})
3735
);
36+
3837
return articlesByCompany;
3938
} catch (err) {
4039
console.error("Error fetching articles by keyword:", err);
4140
throw err;
4241
}
4342
};
4443

44+
4545
const fetchArticlesByCompany = async (keyword, company) => {
4646
console.log(`์กฐํšŒ ํ‚ค์›Œ๋“œ: ${keyword}, ์–ธ๋ก ์‚ฌ: ${company}`);
4747

0 commit comments

Comments
ย (0)