Skip to content

Commit

Permalink
[FEAT] 커뮤니티 게시물 삭제 API (#356)
Browse files Browse the repository at this point in the history
* [FEAT] 커뮤니티 게시물 삭제 API #355

* [FIX] 커뮤니티 게시물 삭제 시 community_category_post 테이블 데이터도 삭제 #355

* [FIX] statueCode 200에서 204로 변경
  • Loading branch information
yubinquitous authored Jun 12, 2024
1 parent 3f8bf9e commit 625da1e
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 6 deletions.
39 changes: 39 additions & 0 deletions functions/api/routes/community/communityPostDELETE.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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 DELETE /community/:communityPostId
* @desc 커뮤니티 게시글 삭제
* @access Private
*/

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

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

const post = await communityDB.getCommunityPostById(dbConnection, communityPostId);
if (!post) {
return res
.status(statusCode.NOT_FOUND)
.send(util.fail(statusCode.NOT_FOUND, responseMessage.NO_COMMUNITY_POST));
}
if (post.userId !== userId) {
return res
.status(statusCode.FORBIDDEN)
.send(util.fail(statusCode.FORBIDDEN, responseMessage.FORBIDDEN));
}

await communityDB.deleteCommunityPostById(dbConnection, communityPostId);
await communityDB.deleteCommunityCategoryPostByPostId(dbConnection, communityPostId);

res
.status(statusCode.NO_CONTENT)
.send(util.success(statusCode.NO_CONTENT, responseMessage.DELETE_COMMUNITY_POST_SUCCESS));
});
29 changes: 29 additions & 0 deletions functions/api/routes/community/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,33 @@ router.post(
*/
);

router.delete(
'/:communityPostId',
checkUser,
[...communityValidator.deleteCommunityPostValidator, validate],
require('./communityPostDELETE'),
/**
* #swagger.summary = "커뮤니티 게시글 삭제"
* #swagger.parameters['communityPostId'] = {
in: 'path',
description: '커뮤니티 게시글 아이디',
type: 'number',
required: true
}
* #swagger.responses[204] = {
description: "커뮤니티 게시글 삭제 성공",
content: {
"application/json": {
schema:{
$ref: "#/components/schemas/responseDeleteCommunityPostSchema"
}
}
}
}
* #swagger.responses[400]
* #swagger.responses[403]
* #swagger.responses[404]
*/
);

module.exports = router;
1 change: 1 addition & 0 deletions functions/constants/responseMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ module.exports = {
ALREADY_REPORTED_POST: '이미 신고한 게시글',
READ_COMMUNITY_CATEGORY_POSTS_SUCCESS: '커뮤니티 카테고리별 게시글 조회 성공',
REPORT_COMMUNITY_POST_SUCCESS: '커뮤니티 게시글 신고 성공',
DELETE_COMMUNITY_POST_SUCCESS: '커뮤니티 게시글 삭제 성공',

// 서버 상태 체크
HEALTH_CHECK_SUCCESS: '서버 상태 정상',
Expand Down
7 changes: 7 additions & 0 deletions functions/constants/swagger/schemas/communitySchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ const requestCommunityReportSchema = {
$communityPostId: 1,
};

const responseDeleteCommunityPostSchema = {
$status: 204,
$success: true,
$message: '커뮤니티 게시글 삭제 성공',
};

module.exports = {
responseCommunityCategorySchema,
responseCommunityPostsDetailSchema,
Expand All @@ -112,4 +118,5 @@ module.exports = {
requestCreateCommunityPostSchema,
responseCommunityReportSchema,
requestCommunityReportSchema,
responseDeleteCommunityPostSchema,
};
44 changes: 41 additions & 3 deletions functions/db/community.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ const reportCommunityPost = async (client, userId, communityPostId) => {
WHERE id = $1
RETURNING *
`,
[communityPostId]
[communityPostId],
);
if (!existingCommunityPosts[0]) return existingCommunityPosts[0];
const { rows: communityPostReports } = await client.query(
Expand All @@ -189,10 +189,45 @@ const reportCommunityPost = async (client, userId, communityPostId) => {
($1, $2)
RETURNING *
`,
[userId, communityPostId]
[userId, communityPostId],
);
return convertSnakeToCamel.keysToCamel(communityPostReports[0]);
}
};

const getCommunityPostById = async (client, communityPostId) => {
const { rows } = await client.query(
`SELECT *
FROM community_post
WHERE id = $1 AND is_deleted = FALSE
`,
[communityPostId],
);
return convertSnakeToCamel.keysToCamel(rows[0]);
};

const deleteCommunityPostById = async (client, communityPostId) => {
const { rows } = await client.query(
`
UPDATE community_post
SET is_deleted = TRUE
WHERE id = $1
`,
[communityPostId],
);
return convertSnakeToCamel.keysToCamel(rows[0]);
};

const deleteCommunityCategoryPostByPostId = async (client, communityPostId) => {
const { rows } = await client.query(
`
UPDATE community_category_post
SET is_deleted = TRUE
WHERE community_post_id = $1
`,
[communityPostId],
);
return convertSnakeToCamel.keysToCamel(rows[0]);
};

module.exports = {
getCommunityPostDetail,
Expand All @@ -207,4 +242,7 @@ module.exports = {
getCommunityCategoryPostsCount,
getCommunityCategoryPostsById,
reportCommunityPost,
getCommunityPostById,
deleteCommunityPostById,
deleteCommunityCategoryPostByPostId,
};
14 changes: 11 additions & 3 deletions functions/middlewares/validator/communityValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,21 @@ const getCommunityCategoryPostsValidator = [
];

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

const deleteCommunityPostValidator = [
param('communityPostId')
.notEmpty()
.isInt({ min: 1 })
.withMessage('Invalid communityPostId field'),
];

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

0 comments on commit 625da1e

Please sign in to comment.