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

검색 기능 구현 #45

Merged
merged 9 commits into from
Sep 25, 2024
68 changes: 64 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: ${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: ${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,34 @@ 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,
) => {
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 +362,7 @@ export {
deleteMemeSave,
updateMeme,
getMemeWithKeywords,
searchMemeList,
searchMemeListByKeyword,
searchMemeListBySearchTerm,
};
2 changes: 1 addition & 1 deletion src/model/meme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const MemeSchema: Schema = new Schema(
keywordIds: { type: [Types.ObjectId], ref: 'Keyword', required: true, default: [] },
image: { type: String, required: true },
reaction: { type: Number, required: true, default: 0 },
source: { type: String, required: true },
source: { type: String, default: '' },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

피그마에서는 필수로 표시되어있던 거 같은데 상관없나 ???

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앗 이게 백오피스때문에 필수 빼뒀던 것 같은데.. 다시 required 옵션 추가했어요~

isTodayMeme: { type: Boolean, requried: true, default: false },
isDeleted: { type: Boolean, required: true, default: false },
},
Expand Down
Loading