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

deviceId 헤더 적용, API 엔드포인트 변경, 목록 조회 API 페이지네이션 적용 #25

Merged
merged 17 commits into from
Jul 6, 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
29 changes: 25 additions & 4 deletions src/controller/meme.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,33 @@ const getTodayMemeList = async (req: Request, res: Response, next: NextFunction)
}
};

const searchMemeByKeyword = async (req: CustomRequest, res: Response, next: NextFunction) => {
const searchMemeListByKeyword = async (req: CustomRequest, res: Response, next: NextFunction) => {
const keyword = req.requestedKeyword;

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 MemeService.searchMemeByKeyword(keyword);
return res.json(createSuccessResponse(HttpCode.OK, 'Search meme by keyword', memeList));
const memeList = await MemeService.searchMemeByKeyword(page, size, keyword);
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, 'Search meme list by keyword', data));
} catch (err) {
return next(new CustomError(err.message, err.status));
}
Expand Down Expand Up @@ -287,5 +308,5 @@ export {
deleteMemeSave,
updateMeme,
getMemeWithKeywords,
searchMemeByKeyword,
searchMemeListByKeyword,
};
33 changes: 27 additions & 6 deletions src/controller/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,50 @@ const getUser = async (req: CustomRequest, res: Response, next: NextFunction) =>
}
};

const getLastSeenMeme = async (req: CustomRequest, res: Response, next: NextFunction) => {
const getLastSeenMemes = async (req: CustomRequest, res: Response, next: NextFunction) => {
const user = req.requestedUser;

try {
const memeList = await UserService.getLastSeenMeme(user);
const memeList = await UserService.getLastSeenMemes(user);
return res.json(createSuccessResponse(HttpCode.OK, 'Get Last Seen Meme', memeList));
} catch (err) {
return next(new CustomError(err.message, err.status));
}
};

const getSavedMeme = async (req: CustomRequest, res: Response, next: NextFunction) => {
const getSavedMemes = 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.getSavedMeme(user);
return res.json(createSuccessResponse(HttpCode.OK, 'Get saved Meme List', memeList));
const memeList = await UserService.getSavedMemes(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 saved Meme List', data));
} catch (err) {
return next(new CustomError(err.message, err.status));
}
};

export { getUser, createUser, getLastSeenMeme, getSavedMeme };
export { getUser, createUser, getLastSeenMemes, getSavedMemes };

function getLevel(watch: number, reaction: number, share: number): number {
let level = 1;
Expand Down
8 changes: 4 additions & 4 deletions src/middleware/requestedInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ export const getRequestedUserInfo = async (
res: Response,
next: NextFunction,
) => {
const deviceId = req.params?.deviceId || req.body?.deviceId || null;
const deviceId = req.headers['x-device-id'] as string;

if (_.isNull(deviceId)) {
return next(new CustomError(`'deviceId' should be provided`, HttpCode.BAD_REQUEST));
if (!deviceId) {
return next(new CustomError(`'x-device-id' header should be provided`, HttpCode.BAD_REQUEST));
}

const user = await getUser(deviceId);

if (_.isNull(user)) {
if (!user) {
return next(new CustomError(`user(${deviceId}) does not exist`, HttpCode.NOT_FOUND));
}

Expand Down
33 changes: 18 additions & 15 deletions src/routes/keywordCategory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ const router = express.Router();
* /api/keywordCategory:
* post:
* tags: [KeywordCategory]
* summary: 키워드 카테고리 생성
* description: 키워드 카테고리를 생성합니다
* summary: 키워드 카테고리 생성 (백오피스)
* description: 키워드 카테고리를 생성합니다 (백오피스)
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* properties:
* name: { type: string }
* name:
* type: string
* example: 대학
* responses:
* '200':
* description: 키워드 카테고리 생성
Expand All @@ -48,10 +50,10 @@ const router = express.Router();
* properties:
* _id:
* type: string
* example: "5f5f5f5f5f5f5f5f5f5f5f5f"
* example: "66898279850f512db15ee832"
* name:
* type: string
* example: "meme"
* example: "대학"
* isRecommend:
* type: boolean
* example: true
Expand Down Expand Up @@ -107,14 +109,15 @@ router.post('/', validateCategoryDuplication, createKeywordCategory);
* /api/keywordCategory/{categoryName}:
* get:
* tags: [KeywordCategory]
* summary: 키워드 카테고리
* description: 키워드 카테고리를 반환합니다
* summary: 키워드 카테고리 조회 (백오피스)
* description: 키워드 카테고리를 조회한다.
* parameters:
* - in: path
* name: categoryName
* required: true
* schema:
* type: string
* example: 대학생
* description: 키워드 카테고리
* responses:
* 200:
Expand All @@ -138,10 +141,10 @@ router.post('/', validateCategoryDuplication, createKeywordCategory);
* properties:
* _id:
* type: string
* example: "5f5f5f5f5f5f5f5f5f5f5f5f"
* example: "66898279850f512db15ee832"
* name:
* type: string
* example: "meme"
* example: "대학생"
* isRecommend:
* type: boolean
* example: false
Expand Down Expand Up @@ -191,8 +194,8 @@ router.post('/', validateCategoryDuplication, createKeywordCategory);
* example: Internal server error
* put:
* tags: [KeywordCategory]
* summary: 키워드 카테고리 수정
* description: 키워드 카테고리를 수정합니다
* summary: 키워드 카테고리 수정 (백오피스)
* description: 키워드 카테고리를 수정한다. (백오피스)
* parameters:
* - in: path
* name: categoryName
Expand All @@ -210,7 +213,7 @@ router.post('/', validateCategoryDuplication, createKeywordCategory);
* name: { type: string }
* responses:
* 200:
* description: 키워드 카테고리를 수정해야다.
* description: 키워드 카테고리 수정 성공
* content:
* application/json:
* schema:
Expand All @@ -230,7 +233,7 @@ router.post('/', validateCategoryDuplication, createKeywordCategory);
* properties:
* _id:
* type: string
* example: "5f5f5f5f5f5f5f5f5f5f5f5f"
* example: "66898279850f512db15ee832"
* name:
* type: string
* example: "meme"
Expand Down Expand Up @@ -286,8 +289,8 @@ router.post('/', validateCategoryDuplication, createKeywordCategory);
* example: null
* delete:
* tags: [KeywordCategory]
* summary: 키워드 카테고리 삭제
* description: 키워드 카테고리를 삭제합니다
* summary: 키워드 카테고리 삭제 (백오피스)
* description: 키워드 카테고리를 삭제합니다 (백오피스)
* parameters:
* - in: path
* name: categoryName
Expand Down
Loading