Skip to content

Commit

Permalink
Merge pull request #45 from mash-up-kr/feature/search
Browse files Browse the repository at this point in the history
검색 기능 구현
  • Loading branch information
Hyun-git authored Sep 25, 2024
2 parents 5b17148 + 6f8b237 commit 7007a78
Show file tree
Hide file tree
Showing 9 changed files with 737 additions and 168 deletions.
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 @@ -306,5 +364,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, required: true, default: '' },
isTodayMeme: { type: Boolean, requried: true, default: false },
isDeleted: { type: Boolean, required: true, default: false },
},
Expand Down
Loading

0 comments on commit 7007a78

Please sign in to comment.