Skip to content

Commit

Permalink
[FEAT] 커뮤니티 게시글 신고하기 API (#349)
Browse files Browse the repository at this point in the history
* [FEAT] 커뮤니티 게시글 신고하기 validator 구현

* [DOCS] 커뮤니티 게시글 신고하기 response message 추가

* [FEAT] 커뮤니티 게시글 신고하기 query 작성

* [FEAT] 커뮤니티 게시글 신고하기 controller 구현

* [CHORE] validator id 최솟값 1로 변경
  • Loading branch information
HYOSITIVE authored Apr 23, 2024
1 parent fefb4f3 commit 442401d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
22 changes: 20 additions & 2 deletions functions/api/routes/community/communityReportPOST.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
const util = require('../../../lib/util');
const statusCode = require('../../../constants/statusCode');
const responseMessage = require('../../../constants/responseMessage');
const db = require('../../../db/db');
const { communityDB } = require('../../../db');
const asyncWrapper = require('../../../lib/asyncWrapper');

/**
* @route POST /community/reports
* @desc 커뮤니티 게시글 신고
* @access Private
*/

module.exports = async (req, res) => {
module.exports = asyncWrapper(async (req, res) => {
const { userId } = req.user;
const { communityPostId } = req.body;
};

const dbConnection = await db.connect(req);
req.dbConnection = dbConnection;

const communityPostReport = await communityDB.reportCommunityPost(dbConnection, userId, communityPostId);
if (!communityPostReport) {
return res.status(statusCode.BAD_REQUEST).send(util.fail(statusCode.BAD_REQUEST, responseMessage.NO_COMMUNITY_POST))
}

res.status(statusCode.CREATED).send(util.success(statusCode.CREATED, responseMessage.REPORT_COMMUNITY_POST_SUCCESS))
});
1 change: 1 addition & 0 deletions functions/api/routes/community/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ router.post(
router.post(
'/reports',
checkUser,
[...communityValidator.reportCommunityPostValidator, validate],
require('./communityReportPOST'),
/**
* #swagger.summary = "커뮤니티 게시글 신고"
Expand Down
1 change: 1 addition & 0 deletions functions/constants/responseMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ module.exports = {
NO_PAGE: '존재하지 않는 페이지',
READ_COMMUNITY_CATEGORIES_SUCCESS: '커뮤니티 카테고리 조회 성공',
READ_COMMUNITY_CATEGORY_POSTS_SUCCESS: '커뮤니티 카테고리별 게시글 조회 성공',
REPORT_COMMUNITY_POST_SUCCESS: '커뮤니티 게시글 신고 성공',

// 서버 상태 체크
HEALTH_CHECK_SUCCESS: '서버 상태 정상',
Expand Down
25 changes: 25 additions & 0 deletions functions/db/community.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,30 @@ const getCommunityCategoryPostsById = async (
return convertSnakeToCamel.keysToCamel(rows);
};

const reportCommunityPost = async (client, userId, communityPostId) => {
const { rows: existingCommunityPosts } = await client.query(
`
UPDATE community_post
SET reported_count = reported_count + 1
WHERE id = $1
RETURNING *
`,
[communityPostId]
);
if (!existingCommunityPosts[0]) return existingCommunityPosts[0];
const { rows: communityPostReports } = await client.query(
`
INSERT INTO community_post_report_user
(report_user_id, community_post_id)
VALUES
($1, $2)
RETURNING *
`,
[userId, communityPostId]
);
return convertSnakeToCamel.keysToCamel(communityPostReports[0]);
}

module.exports = {
getCommunityPostDetail,
getCommunityPosts,
Expand All @@ -168,4 +192,5 @@ module.exports = {
getCommunityPostsCount,
getCommunityCategoryPostsCount,
getCommunityCategoryPostsById,
reportCommunityPost
};
5 changes: 5 additions & 0 deletions functions/middlewares/validator/communityValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,13 @@ const getCommunityCategoryPostsValidator = [
.withMessage('Invalid communityCategoryId field'),
];

const reportCommunityPostValidator = [
body('communityPostId').isInt({ min: 1 }).notEmpty().withMessage('Invalid communityPostId')
]

module.exports = {
createCommunityPostValidator,
getCommunityPostsValidator,
getCommunityCategoryPostsValidator,
reportCommunityPostValidator
};

0 comments on commit 442401d

Please sign in to comment.