Skip to content

Commit

Permalink
feat: 검색 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
seohyun0120 committed Sep 18, 2024
1 parent 37b24a8 commit 6d5e606
Show file tree
Hide file tree
Showing 3 changed files with 422 additions and 167 deletions.
70 changes: 66 additions & 4 deletions src/controller/meme.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import mongoose, { Types } from 'mongoose';
import CustomError from '../errors/CustomError';
import { HttpCode } from '../errors/HttpCode';
import { CustomRequest } from '../middleware/requestedInfo';
import { IKeywordDocument } from '../model/keyword';
import { IMemeCreatePayload, IMemeUpdatePayload } from '../model/meme';
import { InteractionType } from '../model/memeInteraction';
import { IUserDocument } from '../model/user';
import { getKeywordByName } from '../service/keyword.service';
import * as MemeService from '../service/meme.service';
import * as UserService from '../service/user.service';
import { logger } from '../util/logger';
Expand Down Expand Up @@ -166,9 +169,8 @@ const getTodayMemeList = async (req: CustomRequest, res: Response, next: NextFun
}
};

const searchMemeListByKeyword = async (req: CustomRequest, res: Response, next: NextFunction) => {
const searchMemeList = async (req: CustomRequest, res: Response, next: NextFunction) => {
const user = req.requestedUser;
const keyword = req.requestedKeyword;

const page = parseInt(req.query.page as string) || 1;
if (page < 1) {
Expand All @@ -180,6 +182,37 @@ const searchMemeListByKeyword = async (req: CustomRequest, res: Response, next:
return next(new CustomError(`Invalid 'size' parameter`, HttpCode.BAD_REQUEST));
}

const searchTerm = (req.query.q as string) || '';
try {
if (searchTerm) {
// 검색어로 검색하는 경우 (query-parameter)
logger.info('Search by searchTerm: %s', searchTerm);
const result = await searchMemeListBySearchTerm(page, size, searchTerm, user);
return res.json(createSuccessResponse(HttpCode.OK, 'Search meme list by searchTerm', result));
} else {
// 키워드로 검색하는 경우 (parameter)
const keyword = req.params?.name;
logger.info('Search by keyword: %s', keyword);
const requestedKeyword = await getKeywordByName(keyword);
if (_.isNull(requestedKeyword)) {
return next(
new CustomError(`Keyword name '${keyword}' does not exist`, HttpCode.NOT_FOUND),
);
}
const result = await searchMemeListByKeyword(page, size, requestedKeyword, user);
return res.json(createSuccessResponse(HttpCode.OK, 'Search meme list by keyword', result));
}
} catch (err) {
return next(new CustomError(err.message, err.status));
}
};

const searchMemeListByKeyword = async (
page: number,
size: number,
keyword: IKeywordDocument,
user: IUserDocument,
) => {
try {
const memeList = await MemeService.searchMemeByKeyword(page, size, keyword, user);
const data = {
Expand All @@ -193,9 +226,36 @@ const searchMemeListByKeyword = async (req: CustomRequest, res: Response, next:
memeList: memeList.data,
};

return res.json(createSuccessResponse(HttpCode.OK, 'Search meme list by keyword', data));
return data;
} catch (err) {
return next(new CustomError(err.message, err.status));
throw new CustomError(err.message, err.status);
}
};

const searchMemeListBySearchTerm = async (
page: number,
size: number,
saerchTerm: string,
user: IUserDocument,
) => {
logger.info(`검색어: ${saerchTerm}`);

try {
const memeList = await MemeService.searchMemeBySearchTerm(page, size, saerchTerm, user);
const data = {
pagination: {
total: memeList.total,
page: memeList.page,
perPage: size,
currentPage: memeList.page,
totalPages: memeList.totalPages,
},
memeList: memeList.data,
};

return data;
} catch (err) {
throw new CustomError(err.message, err.status);
}
};

Expand Down Expand Up @@ -304,5 +364,7 @@ export {
deleteMemeSave,
updateMeme,
getMemeWithKeywords,
searchMemeList,
searchMemeListByKeyword,
searchMemeListBySearchTerm,
};
Loading

0 comments on commit 6d5e606

Please sign in to comment.