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

Develop #50

Merged
merged 18 commits into from
Sep 25, 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
9 changes: 4 additions & 5 deletions src/controller/keyword.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,11 @@ const getTopKeywords = async (req: Request, res: Response, next: NextFunction) =
try {
const topReactionImage: string = await MemeService.getTopReactionImage(keyword);
return { ...keyword, topReactionImage } as IKeywordWithImage;
} catch (error) {
logger.error(
`Error retrieving top reaction image for keyword: ${JSON.stringify(keyword._id)}`,
error,
} catch (err) {
throw new CustomError(
`Error retrieving top reaction image for keyword: ${JSON.stringify(keyword._id)}: ${err.message}`,
HttpCode.INTERNAL_SERVER_ERROR,
);
throw new CustomError(`Failed to get top reaction image`, HttpCode.INTERNAL_SERVER_ERROR);
}
},
);
Expand Down
4 changes: 2 additions & 2 deletions src/controller/keywordCategory.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const updateKeywordCategory = async (req: CustomRequest, res: Response, next: Ne
updateInfo,
);
logger.info(`Updated category with ID ${req.params.categoryName}`);
return res.json(createSuccessResponse(HttpCode.OK, 'Update KeywordCategor', updatedCategory));
return res.json(createSuccessResponse(HttpCode.OK, 'Updated KeywordCategory', updatedCategory));
} catch (err) {
return next(new CustomError(err.message, err.status || HttpCode.INTERNAL_SERVER_ERROR));
}
Expand All @@ -40,7 +40,7 @@ const deleteKeywordCategory = async (req: CustomRequest, res: Response, next: Ne
const category = req.requestedKeywordCategory;
try {
await KeywordCategoryService.deleteKeywordCategory(category.name);
return res.json(createSuccessResponse(HttpCode.OK, 'Deleted KeywordCategor', true));
return res.json(createSuccessResponse(HttpCode.OK, 'Deleted KeywordCategory', true));
} catch (err) {
return next(new CustomError(err.message, err.status || HttpCode.INTERNAL_SERVER_ERROR));
}
Expand Down
72 changes: 67 additions & 5 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 All @@ -24,7 +27,7 @@ const getMeme = async (req: Request, res: Response, next: NextFunction) => {
}

if (!mongoose.Types.ObjectId.isValid(memeId)) {
return next(new CustomError(`'memeId' is not a valid ObjectId`, HttpCode.BAD_REQUEST));
return next(new CustomError(`${memeId} is not a valid ObjectId`, HttpCode.BAD_REQUEST));
}

try {
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,21 +226,48 @@ 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);
}
};

const createMemeReaction = async (req: CustomRequest, res: Response, next: NextFunction) => {
const user = req.requestedUser;
const meme = req.requestedMeme;
const { count = 1 } = req.body;

try {
const result: boolean = await MemeService.createMemeInteraction(
user,
meme,
InteractionType.REACTION,
count,
);
return res.json(createSuccessResponse(HttpCode.CREATED, 'Create Meme Reaction', result));
} catch (err) {
Expand Down Expand Up @@ -304,5 +364,7 @@ export {
deleteMemeSave,
updateMeme,
getMemeWithKeywords,
searchMemeList,
searchMemeListByKeyword,
searchMemeListBySearchTerm,
};
2 changes: 1 addition & 1 deletion src/middleware/requestedInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const getRequestedMemeInfo = async (
}

if (!mongoose.Types.ObjectId.isValid(memeId)) {
return next(new CustomError(`'memeId' is not a valid ObjectId`, HttpCode.BAD_REQUEST));
return next(new CustomError(`${memeId} is not a valid ObjectId`, HttpCode.BAD_REQUEST));
}

const meme: IMemeDocument = await getMeme(memeId);
Expand Down
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