Skip to content

Commit

Permalink
Merge pull request #59 from mash-up-kr/feat/chagne-latest-meme
Browse files Browse the repository at this point in the history
최신순으로 보여주게 수정
  • Loading branch information
Hyun-git authored Oct 4, 2024
2 parents abf80ed + 0d79443 commit 8db5b4c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 26 deletions.
14 changes: 7 additions & 7 deletions src/controller/meme.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -374,7 +374,7 @@ const deleteMemeSave = async (req: CustomRequest, res: Response, next: NextFunct

export {
getMeme,
getTodayMemeList,
getLatestMemeList,
getAllMemeList,
createMeme,
createMemeSave,
Expand Down
4 changes: 2 additions & 2 deletions src/routes/meme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getMemeWithKeywords,
updateMeme,
createMeme,
getTodayMemeList,
getLatestMemeList,
getAllMemeList,
createMemeShare,
createMemeSave,
Expand Down Expand Up @@ -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
Expand Down
22 changes: 11 additions & 11 deletions src/service/meme.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,27 @@ async function getMemeWithKeywords(
}
}

async function getTodayMemeList(
limit: number = 5,
async function getLatestCreatedMeme(
limit: number = 20,
user: IUserDocument,
): Promise<IMemeGetResponse[]> {
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,
);
}
Expand Down Expand Up @@ -370,12 +370,12 @@ export {
updateMeme,
deleteMeme,
deleteMemeSave,
getTodayMemeList,
getAllMemeList,
getMemeListWithKeywordsAndisSavedAndisReaction,
deleteKeywordOfMeme,
getMemeWithKeywords,
searchMemeByKeyword,
searchMemeBySearchTerm,
getTopReactionImage,
getLatestCreatedMeme
};
14 changes: 8 additions & 6 deletions test/meme/get-recommend-meme-list.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import request from 'supertest';
import { Types } from 'mongoose';

import app from '../../src/app';
import { KeywordModel } from '../../src/model/keyword';
Expand All @@ -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);
Expand All @@ -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 () => {
Expand All @@ -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);

Expand Down
1 change: 1 addition & 0 deletions test/util/meme.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const createMockData = (
): IMeme[] => {
const result: IMeme[] = [];

console.log(size);
for (let i = 0; i < size; i++) {
const mockData = { ...memeMockData };

Expand Down

0 comments on commit 8db5b4c

Please sign in to comment.