Skip to content

Commit feae332

Browse files
authored
Merge pull request #94 from Team-NumberOne/feat/93
[feat/93] 게시글 삭제 api 구현
2 parents 89733da + 869d8a5 commit feae332

4 files changed

Lines changed: 26 additions & 1 deletion

File tree

src/main/kotlin/com/numberone/daepiro/domain/community/api/ArticleApiV1.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import io.swagger.v3.oas.annotations.media.Schema
1717
import io.swagger.v3.oas.annotations.tags.Tag
1818
import org.springframework.data.domain.Slice
1919
import org.springframework.http.MediaType
20+
import org.springframework.web.bind.annotation.DeleteMapping
2021
import org.springframework.web.bind.annotation.GetMapping
2122
import org.springframework.web.bind.annotation.ModelAttribute
2223
import org.springframework.web.bind.annotation.PathVariable
@@ -114,4 +115,12 @@ interface ArticleApiV1 {
114115
@RequestBody request: ReportRequest,
115116
): ApiResult<Unit>
116117

118+
@Operation(
119+
summary = "게시글 삭제",
120+
description = "게시글을 삭제합니다."
121+
)
122+
@DeleteMapping("/{id}")
123+
fun delete(
124+
@PathVariable("id") id: Long,
125+
): ApiResult<Unit>
117126
}

src/main/kotlin/com/numberone/daepiro/domain/community/controller/ArticleControllerV1.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,10 @@ class ArticleController(
8888
)
8989
return ApiResult.noContent(path = "/v1/articles/$id/report")
9090
}
91+
92+
override fun delete(id: Long): ApiResult<Unit> {
93+
val userId = SecurityContextUtils.getUserId()
94+
articleService.delete(userId, id)
95+
return ApiResult.ok(path = "/v1/articles/$id")
96+
}
9197
}

src/main/kotlin/com/numberone/daepiro/domain/community/service/ArticleService.kt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ class ArticleService(
186186
return@let authorVerifiedAddressIds.contains(article.address?.id)
187187
} ?: false
188188

189-
roots.forEach { it.children.removeIf { it.deletedAt != null }}
189+
roots.forEach { it.children.removeIf { it.deletedAt != null } }
190190

191191
return ArticleDetailResponse.of(
192192
article = article,
@@ -353,4 +353,13 @@ class ArticleService(
353353
article.increaseReportCount()
354354
)
355355
}
356+
357+
@Transactional
358+
fun delete(userId: Long, articleId: Long) {
359+
val article = articleRepository.findByIdOrThrow(articleId)
360+
if (article.authUser?.id != userId) {
361+
throw CustomException(CustomErrorContext.NOT_ARTICLE_AUTHOR)
362+
}
363+
articleRepository.delete(article)
364+
}
356365
}

src/main/kotlin/com/numberone/daepiro/global/exception/CustomErrorContext.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ enum class CustomErrorContext(
3838

3939
// 26xx: 게시글 관련 오류
4040
NOT_FOUND_ARTICLE(HttpStatus.NOT_FOUND, 2600, "게시글을 찾을 수 없습니다."),
41+
NOT_ARTICLE_AUTHOR(HttpStatus.BAD_REQUEST, 2601, "게시글 작성자가 아닙니다."),
4142

4243
// 27xx: 행동요령 관련 오류
4344
NOT_FOUND_TIP_TYPE(HttpStatus.NOT_FOUND, 2700, "행동요령 유형을 찾을 수 없습니다."),

0 commit comments

Comments
 (0)