Skip to content

Commit

Permalink
Merge pull request #56 from mash-up-kr/fix/registerdMeme
Browse files Browse the repository at this point in the history
feat: add pagination to registered-memes api
  • Loading branch information
Hyun-git authored Oct 1, 2024
2 parents fcffbcf + d44b851 commit 90ed934
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 64 deletions.
25 changes: 23 additions & 2 deletions src/controller/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,30 @@ const getSavedMemeList = async (req: CustomRequest, res: Response, next: NextFun
const getRegisteredMemeList = 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));
}

const size = parseInt(req.query.size as string) || 10;
if (size < 1) {
return next(new CustomError(`Invalid 'size' parameter`, HttpCode.BAD_REQUEST));
}

try {
const memeList = await UserService.getRegisteredMemeList(user);
return res.json(createSuccessResponse(HttpCode.OK, 'Get Registered Meme', memeList));
const memeList = await UserService.getRegisteredMemeList(page, size, user);

const data = {
pagination: {
total: memeList.total,
page: memeList.page,
perPage: size,
currentPage: memeList.page,
totalPages: memeList.totalPages,
},
memeList: memeList.data,
};
return res.json(createSuccessResponse(HttpCode.OK, 'Get Registered Meme', data));
} catch (err) {
return next(new CustomError(err.message, err.status));
}
Expand Down
146 changes: 88 additions & 58 deletions src/routes/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -497,82 +497,112 @@ router.get('/recent-memes', getRequestedUserInfo, UserController.getLastSeenMeme
* /api/user/registered-memes:
* get:
* tags: [User]
* summary: 사용자가 등록한 밈 정보 조회
* description: 사용자가 등록한 밈 정보 조회
* summary: 사용자가 등록한 밈 목록 조회 (페이지네이션)
* description: 사용자가 등록한 밈 목록 조회
* parameters:
* - in: query
* name: page
* schema:
* type: number
* example: 1
* description: 현재 페이지 번호 (기본값 1)
* - in: query
* name: size
* schema:
* type: number
* example: 10
* description: 한 번에 조회할 밈 개수 (기본값 10)
* - name: x-device-id
* in: header
* description: 유저의 고유한 deviceId
* required: true
* type: string
* responses:
* 200:
* description: 등록한 밈 목록
* description: 사용자가 등록한 밈 목록 조회
* content:
* application/json:
* schema:
* type: object
* properties:
* status:
* type: string
* example: "success"
* example: success
* code:
* type: integer
* example: 200
* message:
* type: string
* example: "Get Registered Meme"
* example: Get Registered Meme List
* data:
* type: array
* items:
* type: object
* properties:
* _id:
* type: string
* example: "66f412741cdc0e3efad1c24e"
* deviceId:
* type: string
* example: "test1"
* title:
* type: string
* example: "폰보는 루피"
* image:
* type: string
* example: "https://ppac-meme.s3.ap-northeast-2.amazonaws.com/1727271540074"
* reaction:
* type: integer
* example: 0
* source:
* type: string
* example: "google"
* isTodayMeme:
* type: boolean
* example: false
* createdAt:
* type: string
* format: date-time
* example: "2024-09-25T13:39:00.179Z"
* updatedAt:
* type: string
* format: date-time
* example: "2024-09-25T13:39:00.179Z"
* keywords:
* type: array
* items:
* type: object
* properties:
* _id:
* type: string
* example: "667ff3d1239eeaf78630a283"
* name:
* type: string
* example: "행복"
* isSaved:
* type: boolean
* example: true
* isReaction:
* type: boolean
* example: false
* type: object
* properties:
* pagination:
* type: object
* properties:
* total:
* type: integer
* example: 2
* page:
* type: integer
* example: 1
* perPage:
* type: integer
* example: 10
* currentPage:
* type: integer
* example: 1
* totalPages:
* type: integer
* example: 1
* memeList:
* type: array
* items:
* type: object
* properties:
* _id:
* type: string
* example: "66f412741cdc0e3efad1c24e"
* image:
* type: string
* example: "https://ppac-meme.s3.ap-northeast-2.amazonaws.com/1727271540074"
* title:
* type: string
* example: "폰보는 루피"
* source:
* type: string
* example: "구글 이미지"
* reaction:
* type: integer
* example: 10
* watch:
* type: integer
* example: 500
* createdAt:
* type: string
* format: date-time
* example: "2024-09-25T13:39:00.179Z"
* updatedAt:
* type: string
* format: date-time
* example: "2024-09-25T13:39:00.179Z"
* keywords:
* type: array
* items:
* type: object
* properties:
* _id:
* type: string
* example: "667ff3d1239eeaf78630a283"
* name:
* type: string
* example: "행복"
* isSaved:
* type: boolean
* example: true
* isReaction:
* type: boolean
* example: false
* 400:
* description: 잘못된 요청
* content:
Expand All @@ -582,7 +612,7 @@ router.get('/recent-memes', getRequestedUserInfo, UserController.getLastSeenMeme
* properties:
* status:
* type: string
* example: "error"
* example: error
* code:
* type: integer
* example: 400
Expand All @@ -600,13 +630,13 @@ router.get('/recent-memes', getRequestedUserInfo, UserController.getLastSeenMeme
* properties:
* status:
* type: string
* example: "error"
* example: error
* code:
* type: integer
* example: 500
* message:
* type: string
* example: "Failed get registered meme list"
* example: "Failed to get registered meme list"
* data:
* type: null
*/
Expand Down
22 changes: 18 additions & 4 deletions src/service/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,25 +251,39 @@ async function createMemeRecommendWatch(user: IUserDocument, meme: IMemeDocument
}
}

async function getRegisteredMemeList(user: IUserDocument): Promise<IMemeGetResponse[]> {
async function getRegisteredMemeList(
page: number,
size: number,
user: IUserDocument,
): Promise<{ total: number; page: number; totalPages: number; data: IMemeGetResponse[] }> {
try {
const deviceId = user.deviceId;
const totalRegisteredMemes = await MemeModel.countDocuments({ deviceId, isDeleted: false });

const myMemeList = await MemeModel.find({ deviceId, isDeleted: false }, { isDeleted: 0 })
.sort({ createdAt: -1 })
.skip((page - 1) * size)
.limit(size)
.lean();

const myMemeListInfo = await MemeService.getMemeListWithKeywordsAndisSavedAndisReaction(
user,
myMemeList,
);

logger.info(
`Get registeredMemeList - deviceId(${user.deviceId}), memeList(${getRegisteredMemeList})`,
`Get registeredMemeList - deviceId(${user.deviceId}), memeList count(${myMemeListInfo.length})`,
);

return myMemeListInfo;
return {
total: totalRegisteredMemes,
page,
totalPages: Math.ceil(totalRegisteredMemes / size),
data: myMemeListInfo,
};
} catch (err) {
throw new CustomError(
`Failed get registered meme list(${err.message})`,
`Failed to get registered meme list(${err.message})`,
HttpCode.INTERNAL_SERVER_ERROR,
);
}
Expand Down

0 comments on commit 90ed934

Please sign in to comment.