11const { fetchArticlesByKeyword, fetchArticlesByCompany, fetchArticleCounts, fetchKeywords } = require ( "./keywordService" ) ;
22const 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+
2135const 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
4256const 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 } ;
0 commit comments