diff --git a/src/controller/meme.controller.ts b/src/controller/meme.controller.ts index f4d4626..5339935 100644 --- a/src/controller/meme.controller.ts +++ b/src/controller/meme.controller.ts @@ -164,22 +164,22 @@ const getAllMemeList = async (req: CustomRequest, res: Response, next: NextFunct } }; -const getTodayMemeList = 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) || 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.`, + `Invalid 'size' parameter. Today Meme List max size is 20.`, HttpCode.BAD_REQUEST, ), ); } 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, + getLatestMemeList, getAllMemeList, createMeme, createMemeSave, diff --git a/src/routes/meme.ts b/src/routes/meme.ts index 2c2bb4f..f3eedda 100644 --- a/src/routes/meme.ts +++ b/src/routes/meme.ts @@ -5,7 +5,7 @@ import { getMemeWithKeywords, updateMeme, createMeme, - getTodayMemeList, + getLatestMemeList, 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, getLatestMemeList); // 오늘의 추천 밈 (5개) /** * @swagger diff --git a/src/service/meme.service.ts b/src/service/meme.service.ts index edcdb37..2336a47 100644 --- a/src/service/meme.service.ts +++ b/src/service/meme.service.ts @@ -60,27 +60,27 @@ async function getMemeWithKeywords( } } -async function getTodayMemeList( - limit: number = 5, +async function getLatestCreatedMeme( + limit: number = 20, user: IUserDocument, ): Promise { try { - const todayMemeList = await MemeModel.find( - { isDeleted: false, isTodayMeme: true }, - { isDeleted: 0 }, - ).lean(); + const latestMemeList = await MemeModel.find({ isDeleted: false }, { isDeleted: 0 }) + .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..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,15 +27,15 @@ describe("[GET] '/api/meme/recommend-memes' ", () => { await UserModel.deleteMany({}); }); - it('should return list of recommend-memes - default size: 5', async () => { - const mockDatas = createMockData(totalCount, 5, 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) .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(20); }); it('should return list of recommend-memes - customize size', async () => { @@ -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 };