Skip to content

Commit

Permalink
isSaved(내파밈보관함 여부) 필드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyun-git committed Jul 13, 2024
1 parent 072c841 commit a6cbd81
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 124 deletions.
31 changes: 13 additions & 18 deletions src/controller/meme.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,17 @@ const getMeme = async (req: Request, res: Response, next: NextFunction) => {
}
};

const getMemeWithKeywords = async (req: Request, res: Response, next: NextFunction) => {
const memeId = req.params?.memeId || null;

if (_.isNull(memeId)) {
return next(new CustomError(`'memeId' field should be provided`, HttpCode.BAD_REQUEST));
}

if (!mongoose.Types.ObjectId.isValid(memeId)) {
return next(new CustomError(`'memeId' is not a valid ObjectId`, HttpCode.BAD_REQUEST));
}
const getMemeWithKeywords = async (req: CustomRequest, res: Response, next: NextFunction) => {
const user = req.requestedUser;
const meme = req.requestedMeme;

try {
const meme = await MemeService.getMemeWithKeywords(memeId);
if (_.isNull(meme)) {
return next(new CustomError(`Meme(${memeId}) not found.`, HttpCode.NOT_FOUND));
const ret = await MemeService.getMemeWithKeywords(user, meme);
if (_.isNull(ret)) {
return next(new CustomError(`Meme(${meme._id}) not found.`, HttpCode.NOT_FOUND));
}

logger.info(`Get meme with keywords - ${memeId})`);
logger.info(`Get meme with keywords - ${meme._id})`);
return res.json(createSuccessResponse(HttpCode.OK, 'Get Meme', meme));
} catch (err) {
return next(new CustomError(err.message, err.status));
Expand Down Expand Up @@ -120,7 +113,8 @@ const deleteMeme = async (req: CustomRequest, res: Response, next: NextFunction)
}
};

const getAllMemeList = async (req: Request, res: Response, next: NextFunction) => {
const getAllMemeList = async (req: CustomRequest, res: Response, next: NextFunction) => {
const user = req.requestedUser;
const page = parseInt(req.query.page as string) || 1;
if (page < 1) {
return next(new CustomError(`Invalid 'page' parameter`, HttpCode.BAD_REQUEST));
Expand All @@ -132,7 +126,7 @@ const getAllMemeList = async (req: Request, res: Response, next: NextFunction) =
}

try {
const memeList = await MemeService.getAllMemeList(page, size);
const memeList = await MemeService.getAllMemeList(page, size, user);

const data = {
pagination: {
Expand All @@ -151,7 +145,8 @@ const getAllMemeList = async (req: Request, res: Response, next: NextFunction) =
}
};

const getTodayMemeList = async (req: Request, res: Response, next: NextFunction) => {
const getTodayMemeList = async (req: CustomRequest, res: Response, next: NextFunction) => {
const user = req.requestedUser;
const size = parseInt(req.query.size as string) || 5;

if (size > 5) {
Expand All @@ -164,7 +159,7 @@ const getTodayMemeList = async (req: Request, res: Response, next: NextFunction)
}

try {
const todayMemeList = await MemeService.getTodayMemeList(size);
const todayMemeList = await MemeService.getTodayMemeList(size, user);
return res.json(createSuccessResponse(HttpCode.OK, 'Get today meme list', todayMemeList));
} catch (err) {
return next(new CustomError(err.message, err.status));
Expand Down
6 changes: 1 addition & 5 deletions src/model/meme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface IMemeGetResponse {
reaction: number;
source: string;
isTodayMeme: boolean;
isSaved: boolean; // 나의 파밈함 저장 여부
createdAt: Date;
updatedAt: Date;
isDeleted: boolean;
Expand All @@ -50,11 +51,6 @@ export interface IMemeDocument extends Document {
isDeleted: boolean;
}

// keywordIds로 조회한 keywords로 대체된 Meme 정보
export interface IMemeWithKeywords extends Omit<IMemeDocument, 'keywordIds'> {
keywords: string[];
}

const MemeSchema: Schema = new Schema(
{
title: { type: String, required: true },
Expand Down
18 changes: 15 additions & 3 deletions src/routes/meme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ const router = express.Router();
* isTodayMeme:
* type: boolean
* example: false
* isSaved:
* type: boolean
* example: true
* keywords:
* type: array
* items:
Expand Down Expand Up @@ -163,7 +166,7 @@ const router = express.Router();
* type: null
* example: null
*/
router.get('/list', getAllMemeList); // meme 목록 전체 조회 (페이지네이션)
router.get('/list', getRequestedUserInfo, getAllMemeList); // meme 목록 전체 조회 (페이지네이션)

/**
* @swagger
Expand Down Expand Up @@ -227,6 +230,9 @@ router.get('/list', getAllMemeList); // meme 목록 전체 조회 (페이지네
* type: string
* format: date-time
* example: "2024-06-29T19:05:55.638Z"
* isSaved:
* type: boolean
* example: true
* keywords:
* type: array
* items:
Expand Down Expand Up @@ -277,7 +283,7 @@ router.get('/list', getAllMemeList); // meme 목록 전체 조회 (페이지네
* type: null
* example: null
*/
router.get('/recommend-memes', getTodayMemeList); // 오늘의 추천 밈 (5개)
router.get('/recommend-memes', getRequestedUserInfo, getTodayMemeList); // 오늘의 추천 밈 (5개)

/**
* @swagger
Expand Down Expand Up @@ -475,6 +481,9 @@ router.post('/', createMeme); // meme 생성
* type: string
* format: date-time
* example: "2024-06-29T19:05:55.638Z"
* isSaved:
* type: boolean
* example: true
* keywords:
* type: array
* items:
Expand Down Expand Up @@ -544,7 +553,7 @@ router.post('/', createMeme); // meme 생성
* type: null
* example: null
*/
router.get('/:memeId', getMemeWithKeywords); // meme 조회
router.get('/:memeId', getRequestedUserInfo, getRequestedMemeInfo, getMemeWithKeywords); // meme 조회

/**
* @swagger
Expand Down Expand Up @@ -1381,6 +1390,9 @@ router.post('/:memeId/reaction', getRequestedUserInfo, getRequestedMemeInfo, cre
* isTodayMeme:
* type: boolean
* example: false
* isSaved:
* type: boolean
* example: true
* keywords:
* type: array
* items:
Expand Down
6 changes: 6 additions & 0 deletions src/routes/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@ router.get('/', getRequestedUserInfo, UserController.getUser); // user 조회
* isTodayMeme:
* type: boolean
* example: false
* isSaved:
* type: boolean
* example: false
* keywords:
* type: array
* items:
Expand Down Expand Up @@ -405,6 +408,9 @@ router.get('/saved-memes', getRequestedUserInfo, UserController.getSavedMemes);
* isTodayMeme:
* type: boolean
* example: false
* isSaved:
* type: boolean
* example: false
* keywords:
* type: array
* items:
Expand Down
Loading

0 comments on commit a6cbd81

Please sign in to comment.