From cd381dbdec77bb16ca970bd19f259e6c2815c22a Mon Sep 17 00:00:00 2001 From: Hyun-git Date: Fri, 4 Oct 2024 13:35:43 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=EC=B5=9C=EC=8B=A0=EC=88=9C?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=B3=B4=EC=97=AC=EC=A3=BC=EA=B2=8C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controller/meme.controller.ts | 12 ++++++------ src/routes/meme.ts | 4 ++-- src/service/meme.service.ts | 20 ++++++++++---------- test/meme/get-recommend-meme-list.test.ts | 4 ++-- 4 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/controller/meme.controller.ts b/src/controller/meme.controller.ts index f4d4626..92db0d2 100644 --- a/src/controller/meme.controller.ts +++ b/src/controller/meme.controller.ts @@ -164,11 +164,11 @@ const getAllMemeList = async (req: CustomRequest, res: Response, next: NextFunct } }; -const getTodayMemeList = async (req: CustomRequest, res: Response, next: NextFunction) => { +const getLatestMeme = async (req: CustomRequest, res: Response, next: NextFunction) => { const user = req.requestedUser; - const size = parseInt(req.query.size as string) || 5; + const size = parseInt(req.query.size as string) || 20; - if (size > 5) { + if (size > 20) { return next( new CustomError( `Invalid 'size' parameter. Today Meme List max size is 5.`, @@ -178,8 +178,8 @@ const getTodayMemeList = async (req: CustomRequest, res: Response, next: NextFun } try { - const todayMemeList = await MemeService.getTodayMemeList(size, user); - return res.json(createSuccessResponse(HttpCode.OK, 'Get today meme list', todayMemeList)); + const latestMemeList = await MemeService.getLatestCreatedMeme(size, user); + return res.json(createSuccessResponse(HttpCode.OK, 'Get today meme list', latestMemeList)); } catch (err) { return next(new CustomError(err.message, err.status)); } @@ -374,7 +374,7 @@ const deleteMemeSave = async (req: CustomRequest, res: Response, next: NextFunct export { getMeme, - getTodayMemeList, + getLatestMeme, getAllMemeList, createMeme, createMemeSave, diff --git a/src/routes/meme.ts b/src/routes/meme.ts index 2c2bb4f..6935f12 100644 --- a/src/routes/meme.ts +++ b/src/routes/meme.ts @@ -5,7 +5,7 @@ import { getMemeWithKeywords, updateMeme, createMeme, - getTodayMemeList, + getLatestMeme, getAllMemeList, createMemeShare, createMemeSave, @@ -299,7 +299,7 @@ router.get('/list', getRequestedUserInfo, getAllMemeList); // meme 목록 전체 * type: null * example: null */ -router.get('/recommend-memes', getRequestedUserInfo, getTodayMemeList); // 오늘의 추천 밈 (5개) +router.get('/recommend-memes', getRequestedUserInfo, getLatestMeme); // 오늘의 추천 밈 (5개) /** * @swagger diff --git a/src/service/meme.service.ts b/src/service/meme.service.ts index edcdb37..4dc1d92 100644 --- a/src/service/meme.service.ts +++ b/src/service/meme.service.ts @@ -60,27 +60,27 @@ async function getMemeWithKeywords( } } -async function getTodayMemeList( +async function getLatestCreatedMeme( limit: number = 5, user: IUserDocument, ): Promise { try { - const todayMemeList = await MemeModel.find( - { isDeleted: false, isTodayMeme: true }, - { isDeleted: 0 }, - ).lean(); + const latestMemeList = await MemeModel.find({ isDeleted: false }, { isDeleted: false }) + .limit(limit) + .sort({_id: -1}) + .lean(); - const memeList = await getMemeListWithKeywordsAndisSavedAndisReaction(user, todayMemeList); + const memeList = await getMemeListWithKeywordsAndisSavedAndisReaction(user, latestMemeList); - const memeIds = todayMemeList.map((meme) => meme._id); + const memeIds = latestMemeList.map((meme) => meme._id); logger.info( - `Get all today meme list(${todayMemeList.length}) - memeIds(${memeIds}), limit(${limit})`, + `Get all latest meme list(${latestMemeList.length}) - memeIds(${memeIds}), limit(${limit})`, ); return memeList; } catch (err) { throw new CustomError( - `Failed to get today meme list: ${err.message}`, + `Failed to get latest meme list: ${err.message}`, HttpCode.INTERNAL_SERVER_ERROR, ); } @@ -370,7 +370,6 @@ export { updateMeme, deleteMeme, deleteMemeSave, - getTodayMemeList, getAllMemeList, getMemeListWithKeywordsAndisSavedAndisReaction, deleteKeywordOfMeme, @@ -378,4 +377,5 @@ export { searchMemeByKeyword, searchMemeBySearchTerm, getTopReactionImage, + getLatestCreatedMeme }; diff --git a/test/meme/get-recommend-meme-list.test.ts b/test/meme/get-recommend-meme-list.test.ts index e5f35a9..9842f08 100644 --- a/test/meme/get-recommend-meme-list.test.ts +++ b/test/meme/get-recommend-meme-list.test.ts @@ -26,14 +26,14 @@ describe("[GET] '/api/meme/recommend-memes' ", () => { }); it('should return list of recommend-memes - default size: 5', async () => { - const mockDatas = createMockData(totalCount, 5, keywordIds); + const mockDatas = createMockData(totalCount, 10, keywordIds); await MemeModel.insertMany(mockDatas); const response = await request(app) .get('/api/meme/recommend-memes') .set('x-device-id', 'deviceId'); expect(response.statusCode).toBe(200); - expect(response.body.data.length).toBe(5); + expect(response.body.data.length).toBe(10); }); it('should return list of recommend-memes - customize size', async () => { From 92d0809b57a9b1514dc5ac187608cc197b3e479e Mon Sep 17 00:00:00 2001 From: Hyun-git Date: Fri, 4 Oct 2024 13:36:58 +0900 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20=EA=B0=9C=EC=88=98=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/meme/get-recommend-meme-list.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/meme/get-recommend-meme-list.test.ts b/test/meme/get-recommend-meme-list.test.ts index 9842f08..d554be7 100644 --- a/test/meme/get-recommend-meme-list.test.ts +++ b/test/meme/get-recommend-meme-list.test.ts @@ -26,14 +26,14 @@ describe("[GET] '/api/meme/recommend-memes' ", () => { }); it('should return list of recommend-memes - default size: 5', async () => { - const mockDatas = createMockData(totalCount, 10, keywordIds); + const mockDatas = createMockData(totalCount, 20, keywordIds); await MemeModel.insertMany(mockDatas); const response = await request(app) .get('/api/meme/recommend-memes') .set('x-device-id', 'deviceId'); expect(response.statusCode).toBe(200); - expect(response.body.data.length).toBe(10); + expect(response.body.data.length).toBe(20); }); it('should return list of recommend-memes - customize size', async () => { From 0d794438cbfcef807771347141467a383eade07c Mon Sep 17 00:00:00 2001 From: Hyun-git Date: Fri, 4 Oct 2024 16:12:52 +0900 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20=EC=BD=94=EB=93=9C=EB=A6=AC=EB=B7=B0?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controller/meme.controller.ts | 6 +++--- src/routes/meme.ts | 4 ++-- src/service/meme.service.ts | 4 ++-- test/meme/get-recommend-meme-list.test.ts | 12 +++++++----- test/util/meme.mock.ts | 1 + 5 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/controller/meme.controller.ts b/src/controller/meme.controller.ts index 92db0d2..5339935 100644 --- a/src/controller/meme.controller.ts +++ b/src/controller/meme.controller.ts @@ -164,14 +164,14 @@ const getAllMemeList = async (req: CustomRequest, res: Response, next: NextFunct } }; -const getLatestMeme = async (req: CustomRequest, res: Response, next: NextFunction) => { +const getLatestMemeList = async (req: CustomRequest, res: Response, next: NextFunction) => { const user = req.requestedUser; const size = parseInt(req.query.size as string) || 20; if (size > 20) { return next( new CustomError( - `Invalid 'size' parameter. Today Meme List max size is 5.`, + `Invalid 'size' parameter. Today Meme List max size is 20.`, HttpCode.BAD_REQUEST, ), ); @@ -374,7 +374,7 @@ const deleteMemeSave = async (req: CustomRequest, res: Response, next: NextFunct export { getMeme, - getLatestMeme, + getLatestMemeList, getAllMemeList, createMeme, createMemeSave, diff --git a/src/routes/meme.ts b/src/routes/meme.ts index 6935f12..f3eedda 100644 --- a/src/routes/meme.ts +++ b/src/routes/meme.ts @@ -5,7 +5,7 @@ import { getMemeWithKeywords, updateMeme, createMeme, - getLatestMeme, + getLatestMemeList, getAllMemeList, createMemeShare, createMemeSave, @@ -299,7 +299,7 @@ router.get('/list', getRequestedUserInfo, getAllMemeList); // meme 목록 전체 * type: null * example: null */ -router.get('/recommend-memes', getRequestedUserInfo, getLatestMeme); // 오늘의 추천 밈 (5개) +router.get('/recommend-memes', getRequestedUserInfo, getLatestMemeList); // 오늘의 추천 밈 (5개) /** * @swagger diff --git a/src/service/meme.service.ts b/src/service/meme.service.ts index 4dc1d92..2336a47 100644 --- a/src/service/meme.service.ts +++ b/src/service/meme.service.ts @@ -61,11 +61,11 @@ async function getMemeWithKeywords( } async function getLatestCreatedMeme( - limit: number = 5, + limit: number = 20, user: IUserDocument, ): Promise { try { - const latestMemeList = await MemeModel.find({ isDeleted: false }, { isDeleted: false }) + const latestMemeList = await MemeModel.find({ isDeleted: false }, { isDeleted: 0 }) .limit(limit) .sort({_id: -1}) .lean(); diff --git a/test/meme/get-recommend-meme-list.test.ts b/test/meme/get-recommend-meme-list.test.ts index d554be7..a2ff377 100644 --- a/test/meme/get-recommend-meme-list.test.ts +++ b/test/meme/get-recommend-meme-list.test.ts @@ -1,4 +1,5 @@ import request from 'supertest'; +import { Types } from 'mongoose'; import app from '../../src/app'; import { KeywordModel } from '../../src/model/keyword'; @@ -8,11 +9,12 @@ import { createMockData as createKeywordMockData } from '../util/keyword.mock'; import { createMockData } from '../util/meme.mock'; import { mockUser } from '../util/user.mock'; -const totalCount = 10; +const totalCount = 40; let keywordIds = []; describe("[GET] '/api/meme/recommend-memes' ", () => { beforeEach(async () => { + const ObjectIdList = Array.from({ length: totalCount }, () => new Types.ObjectId()); const keywordMockDatas = createKeywordMockData(5); const createdKeywords = await KeywordModel.insertMany(keywordMockDatas); keywordIds = createdKeywords.map((k) => k._id); @@ -25,8 +27,8 @@ describe("[GET] '/api/meme/recommend-memes' ", () => { await UserModel.deleteMany({}); }); - it('should return list of recommend-memes - default size: 5', async () => { - const mockDatas = createMockData(totalCount, 20, keywordIds); + it('should return list of recommend-memes - default size: 20', async () => { + const mockDatas = createMockData(totalCount, 40, keywordIds); await MemeModel.insertMany(mockDatas); const response = await request(app) @@ -49,8 +51,8 @@ describe("[GET] '/api/meme/recommend-memes' ", () => { expect(response.body.data.length).toBe(customizedTodayMemeCount); }); - it('should not return list of recommend-memes - customize size: bigger than limit(5)', async () => { - const customizedTodayMemeCount = 10; + it('should not return list of recommend-memes - customize size: bigger than limit(20)', async () => { + const customizedTodayMemeCount = 30; const mockDatas = createMockData(totalCount, customizedTodayMemeCount, keywordIds); await MemeModel.insertMany(mockDatas); diff --git a/test/util/meme.mock.ts b/test/util/meme.mock.ts index ed3ed58..462c810 100644 --- a/test/util/meme.mock.ts +++ b/test/util/meme.mock.ts @@ -20,6 +20,7 @@ const createMockData = ( ): IMeme[] => { const result: IMeme[] = []; + console.log(size); for (let i = 0; i < size; i++) { const mockData = { ...memeMockData };