Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add searchMemeByKeyword api #15

Merged
merged 3 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/controller/meme.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,23 @@ const getTodayMemeList = async (req: Request, res: Response, next: NextFunction)
}
};

export { getMeme, getTodayMemeList, getAllMemeList, createMeme, deleteMeme, updateMeme };
const searchMemeByKeyword = async (req: CustomRequest, res: Response, next: NextFunction) => {
const keyword = req.requestedKeyword;

try {
const memeList = await MemeService.searchMemeByKeyword(keyword);
return res.json(createSuccessResponse(HttpCode.OK, 'Search meme by keyword', memeList));
} catch (err) {
return next(new CustomError(err.message, err.status));
}
};

export {
getMeme,
getTodayMemeList,
getAllMemeList,
createMeme,
deleteMeme,
updateMeme,
searchMemeByKeyword,
};
2 changes: 1 addition & 1 deletion src/middleware/requestedInfo.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NextFunction, Request, Response } from 'express';
import _ from 'lodash';
import mongoose from 'mongoose';
import { IKeywordDocument } from 'src/model/keyword';
import { IKeywordDocument } from '../model/keyword';

import CustomError from '../errors/CustomError';
import { HttpCode } from '../errors/HttpCode';
Expand Down
5 changes: 4 additions & 1 deletion src/routes/meme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import {
createMeme,
getTodayMemeList,
getAllMemeList,
searchMemeByKeyword,
} from '../controller/meme.controller';
import { getRequestedMemeInfo } from '../middleware/requestedInfo';
import { getRequestedMemeInfo, getKeywordInfoByName } from '../middleware/requestedInfo';

const router = express.Router();

Expand All @@ -20,4 +21,6 @@ router.get('/:memeId', getMeme); // meme 조회
router.patch('/:memeId', getRequestedMemeInfo, updateMeme); // meme 수정
router.delete('/:memeId', getRequestedMemeInfo, deleteMeme); // meme 삭제

router.get('/search/:name', getKeywordInfoByName, searchMemeByKeyword);

export default router;
30 changes: 29 additions & 1 deletion src/service/meme.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import CustomError from '../errors/CustomError';
import { HttpCode } from '../errors/HttpCode';
import { IMemeCreatePayload, IMemeDocument, MemeModel } from '../model/meme';
import { logger } from '../util/logger';
import { IKeywordDocument } from 'src/model/keyword';
pjw5521 marked this conversation as resolved.
Show resolved Hide resolved

async function getMeme(memeId: string): Promise<IMemeDocument | null> {
try {
Expand Down Expand Up @@ -97,4 +98,31 @@ async function deleteMeme(memeId: Types.ObjectId): Promise<boolean> {
return true;
}

export { getMeme, createMeme, updateMeme, deleteMeme, getTodayMemeList, getAllMemeList };
async function searchMemeByKeyword(keyword: IKeywordDocument): Promise<IMemeDocument[]> {
try {
const memeList = await MemeModel.find(
{ keywordIds: keyword._id },
{ createdAt: 0, updatedAt: 0 },
)
.sort({ reaction: -1 })
.populate('keywordIds', 'name')
.lean();

return memeList;
} catch (err) {
logger.error(`Failed to search meme by keyword(${keyword})`, err.message);
throw new CustomError(
`Failed to search meme by keyword(${keyword})`,
HttpCode.INTERNAL_SERVER_ERROR,
);
}
}
export {
getMeme,
createMeme,
updateMeme,
deleteMeme,
getTodayMemeList,
getAllMemeList,
searchMemeByKeyword,
};