-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [FEAT] user profile dummy image url 추가 * [FEAT] 커뮤니티 게시글 상세조회 API * [FIX] createdAt 형식 포맷팅 * [FIX] db query 결과 반환 형식 수정 * [FIX] createdAt format 수정 * [WIP] 커뮤니티 카테고리별 게시글 조회 구현 중 * [FEAT] 커뮤니티 카테고리별 게시글 조회 API 구현 * [FIX] communityCategoryId 검증 * [FIX] 불필요한 주석과 isLastPage 변수 삭제 * [FIX] getComunityCategoryPostsValidator, getCommunityCategoryPostsById로 변경 --------- Co-authored-by: jokj624 <[email protected]>
- Loading branch information
1 parent
f2a14eb
commit fefb4f3
Showing
6 changed files
with
148 additions
and
10 deletions.
There are no files selected for viewing
74 changes: 72 additions & 2 deletions
74
functions/api/routes/community/communityCategoryPostsGET.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,79 @@ | ||
const dayjs = require('dayjs'); | ||
const customParseFormat = require('dayjs/plugin/customParseFormat'); | ||
const util = require('../../../lib/util'); | ||
const statusCode = require('../../../constants/statusCode'); | ||
const responseMessage = require('../../../constants/responseMessage'); | ||
const asyncWrapper = require('../../../lib/asyncWrapper'); | ||
const db = require('../../../db/db'); | ||
const { communityDB } = require('../../../db'); | ||
const dummyImages = require('../../../constants/dummyImages'); | ||
/** | ||
* @route GET /community/categories/:communityCategoryId | ||
* @desc 커뮤니티 카테고리별 게시글 조회 | ||
* @access Private | ||
*/ | ||
|
||
module.exports = async (req, res) => { | ||
module.exports = asyncWrapper(async (req, res) => { | ||
const { userId } = req.user; | ||
const { page, limit } = req.query; | ||
}; | ||
const { communityCategoryId } = req.params; | ||
|
||
const dbConnection = await db.connect(req); | ||
req.dbConnection = dbConnection; | ||
|
||
dayjs().format(); | ||
dayjs.extend(customParseFormat); | ||
|
||
// category id가 존재하지 않는 경우 | ||
const isExistingCategory = await communityDB.isExistingCategory( | ||
dbConnection, | ||
communityCategoryId, | ||
); | ||
if (!isExistingCategory) { | ||
return res | ||
.status(statusCode.NOT_FOUND) | ||
.send(util.fail(statusCode.NOT_FOUND, responseMessage.NO_CATEGORY)); | ||
} | ||
|
||
const totalItemCount = await communityDB.getCommunityCategoryPostsCount( | ||
dbConnection, | ||
userId, | ||
communityCategoryId, | ||
); | ||
const totalPageCount = Math.ceil(totalItemCount / limit); | ||
const currentPage = +page; | ||
|
||
// 요청한 페이지가 존재하지 않는 경우 | ||
if (page > totalPageCount) { | ||
return res | ||
.status(statusCode.NOT_FOUND) | ||
.send(util.fail(statusCode.NOT_FOUND, responseMessage.NO_PAGE)); | ||
} | ||
|
||
const offset = (page - 1) * limit; | ||
const communityCategoryPosts = await communityDB.getCommunityCategoryPostsById( | ||
dbConnection, | ||
userId, | ||
communityCategoryId, | ||
limit, | ||
offset, | ||
); | ||
// 각 게시글의 createdAt 형식 변경 및 프로필 이미지 추가 | ||
const result = await Promise.all( | ||
communityCategoryPosts.map((communityPost) => { | ||
communityPost.createdAt = dayjs(`${communityPost.createdAt}`).format('YYYY. MM. DD'); | ||
communityPost.profileImage = dummyImages.user_profile_dummy; | ||
return communityPost; | ||
}), | ||
); | ||
|
||
res.status(statusCode.OK).send( | ||
util.success(statusCode.OK, responseMessage.READ_COMMUNITY_CATEGORY_POSTS_SUCCESS, { | ||
posts: result, | ||
currentPage, | ||
totalPageCount, | ||
totalItemCount, | ||
isLastPage: currentPage === totalPageCount, | ||
}), | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters