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 #21

Merged
merged 3 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions src/controller/keyword.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ const getTopKeywords = async (req: Request, res: Response, next: NextFunction) =
const limit = 6;
try {
const topKeywords = await KeywordService.getTopKeywords(limit);
// 키워드에 해당하는 밈 이미지 가져오기
const promises = topKeywords.map(async (keyword) => {
seohyun0120 marked this conversation as resolved.
Show resolved Hide resolved
try {
const topReactionImage = await MemeService.getTopReactionImage(keyword);
keyword["topReactionImage"] = topReactionImage;
} catch (error) {
console.error(`Error fetching reaction image for keyword "${keyword}":`, error);
seohyun0120 marked this conversation as resolved.
Show resolved Hide resolved
}
});

await Promise.all(promises);

logger.info(`Get top ${limit} keywords: ${JSON.stringify(topKeywords)}`);
return res.json(createSuccessResponse(HttpCode.OK, 'Get Top Keywords', topKeywords));
} catch (err) {
seohyun0120 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
7 changes: 5 additions & 2 deletions src/controller/meme.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,13 @@ const createMemeWatch = async (req: CustomRequest, res: Response, next: NextFunc

return res.json(createSuccessResponse(HttpCode.CREATED, 'Crate Meme Watch', result));
} else if (type == MemeWatchType.RECOMMEND) {
const memeRecommendWatch = await UserService.createMemeRecommendWatch(user, meme);
const [recommendMemeWatchCount, _]: [number, any] = await Promise.all([
UserService.createMemeRecommendWatch(user, meme),
UserService.updateLastSeenMeme(user, meme),
]);

return res.json(
createSuccessResponse(HttpCode.CREATED, `${type} Meme Watch`, memeRecommendWatch),
createSuccessResponse(HttpCode.CREATED, `${type} Meme Watch`, recommendMemeWatchCount),
);
} else {
return next(new CustomError(`Invalid 'type' parameter.`, HttpCode.BAD_REQUEST));
Expand Down
1 change: 1 addition & 0 deletions src/model/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface IUserInfos extends IUser {
reaction: number;
save: number;
level: number;
memeRecommendWatchCount: number;
}

export interface IUserDocument extends Document {
Expand Down
14 changes: 14 additions & 0 deletions src/service/meme.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,18 @@ async function deleteMemeSave(user: IUserDocument, meme: IMemeDocument): Promise
throw new CustomError(`Failed delete memeSave(${err.message})`, HttpCode.INTERNAL_SERVER_ERROR);
}
}
async function getTopReactionImage(keyword: IKeywordDocument): Promise<string> {
try {
const topReactionMeme = await MemeModel.findOne({ keywordIds: keyword._id }).sort({ reaction: -1 });

logger.info(`Get top reaction meme - keyword(${keyword.name}), meme(${topReactionMeme._id})`);
return topReactionMeme.image;
}catch (err) {
logger.error(`Failed get top reaction meme`, err.message);
throw new CustomError(`Failed get top reaction meme(${err.message})`, HttpCode.INTERNAL_SERVER_ERROR);
}
}


export {
getMeme,
Expand All @@ -263,4 +275,6 @@ export {
deleteKeywordOfMeme,
getMemeWithKeywords,
searchMemeByKeyword,
getTopReactionImage,
};

17 changes: 13 additions & 4 deletions src/service/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
IMemeRecommendWatchCreatePayload,
} from '../model/memeRecommendWatch';
import { logger } from '../util/logger';
import { startOfWeek, format } from 'date-fns';
import { startOfWeek } from 'date-fns';

async function getUser(deviceId: string): Promise<IUserDocument | null> {
try {
Expand Down Expand Up @@ -47,12 +47,21 @@ async function createUser(deviceId: string): Promise<IUserInfos> {
countInteractionType(InteractionType.SAVE),
]);

const todayWeekStart = startOfWeek(new Date(), { weekStartsOn: 1 });
const memeRecommendWatchList = await MemeRecommendWatchModel.find({
startDate: todayWeekStart,
deviceId: foundUser.deviceId,
isDeleted: false,
});
seohyun0120 marked this conversation as resolved.
Show resolved Hide resolved


return {
...foundUser.toObject(),
watch,
reaction,
save,
share,
memeRecommendWatchCount: memeRecommendWatchList.length,
level: 1,
};
}
Expand Down Expand Up @@ -156,7 +165,7 @@ async function getSavedMeme(user: IUserDocument): Promise<IMemeDocument[]> {
async function createMemeRecommendWatch(
user: IUserDocument,
meme: IMemeDocument,
): Promise<boolean> {
): Promise<number> {
try {
const todayWeekStart = startOfWeek(new Date(), { weekStartsOn: 1 });
const memeRecommendWatch = await MemeRecommendWatchModel.findOne({
Expand All @@ -176,7 +185,7 @@ async function createMemeRecommendWatch(
{ _id: memeRecommendWatch._id },
{ $set: updatePayload },
);
return true;
return updatePayload.memeIds.length;
}

const createPayload: IMemeRecommendWatchCreatePayload = {
Expand All @@ -187,7 +196,7 @@ async function createMemeRecommendWatch(

await MemeRecommendWatchModel.create(createPayload);

return true;
return 1;
} catch (err) {
logger.error(`Failed create memeRecommendWatch`, err.message);
throw new CustomError(
Expand Down