-
Notifications
You must be signed in to change notification settings - Fork 0
✨게시글 CRUD 구현 #7
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
Merged
✨게시글 CRUD 구현 #7
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/com/wafflestudio/team2server/DomainException.kt
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.wafflestudio.spring2025 | ||
|
|
||
| import org.springframework.http.HttpStatus | ||
| import org.springframework.http.HttpStatusCode | ||
|
|
||
| open class DomainException( | ||
| val errorCode: Int, | ||
| val httpErrorCode: HttpStatusCode = HttpStatus.INTERNAL_SERVER_ERROR, | ||
| val msg: String, | ||
| cause: Throwable? = null, | ||
| ) : RuntimeException(msg, cause) { | ||
| override fun toString(): String = "DomainException(msg='$msg', errorCode=$errorCode, httpErrorCode=$httpErrorCode)" | ||
| } |
54 changes: 54 additions & 0 deletions
54
src/main/kotlin/com/wafflestudio/team2server/article/ArticleException.kt
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package com.wafflestudio.team2server.article | ||
|
|
||
| import com.wafflestudio.spring2025.DomainException | ||
| import org.springframework.http.HttpStatus | ||
| import org.springframework.http.HttpStatusCode | ||
|
|
||
| sealed class ArticleException( | ||
| errorCode: Int, | ||
| httpStatusCode: HttpStatusCode, | ||
| msg: String, | ||
| cause: Throwable? = null, | ||
| ) : DomainException(errorCode, httpStatusCode, msg, cause) | ||
|
|
||
| class ArticleNotFoundException : | ||
| ArticleException( | ||
| errorCode = 0, | ||
| httpStatusCode = HttpStatus.NOT_FOUND, | ||
| msg = "Article not found", | ||
| ) | ||
|
|
||
| class ArticleBlankContentException : | ||
| ArticleException( | ||
| errorCode = 0, | ||
| httpStatusCode = HttpStatus.BAD_REQUEST, | ||
| msg = "Content must not be blank", | ||
| ) | ||
|
|
||
| class ArticleBlankAuthorException : | ||
| ArticleException( | ||
| errorCode = 0, | ||
| httpStatusCode = HttpStatus.BAD_REQUEST, | ||
| msg = "Author must not be blank", | ||
| ) | ||
|
|
||
| class ArticleBlankPublishedException : | ||
| ArticleException( | ||
| errorCode = 0, | ||
| httpStatusCode = HttpStatus.BAD_REQUEST, | ||
| msg = "PublishedAt must not be blank", | ||
| ) | ||
|
|
||
| class ArticleBlankOriginLinkException : | ||
| ArticleException( | ||
| errorCode = 0, | ||
| httpStatusCode = HttpStatus.BAD_REQUEST, | ||
| msg = "OriginLink must not be blank", | ||
| ) | ||
|
|
||
| class ArticleBlankTitleException : | ||
| ArticleException( | ||
| errorCode = 0, | ||
| httpStatusCode = HttpStatus.BAD_REQUEST, | ||
| msg = "Title must not be blank", | ||
| ) |
153 changes: 153 additions & 0 deletions
153
src/main/kotlin/com/wafflestudio/team2server/article/controller/ArticleController.kt
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| package com.wafflestudio.team2server.article.controller | ||
|
|
||
| import com.wafflestudio.team2server.article.dto.ArticlePagingRequest | ||
| import com.wafflestudio.team2server.article.dto.UpdateArticleRequest | ||
| import com.wafflestudio.team2server.article.dto.core.ArticleDto | ||
| import com.wafflestudio.team2server.article.dto.request.CreateArticleRequest | ||
| import com.wafflestudio.team2server.article.dto.response.ArticlePagingResponse | ||
| import com.wafflestudio.team2server.article.dto.response.CreateArticleResponse | ||
| import com.wafflestudio.team2server.article.service.ArticleService | ||
| import io.swagger.v3.oas.annotations.Operation | ||
| import io.swagger.v3.oas.annotations.Parameter | ||
| import io.swagger.v3.oas.annotations.responses.ApiResponse | ||
| import io.swagger.v3.oas.annotations.responses.ApiResponses | ||
| import io.swagger.v3.oas.annotations.tags.Tag | ||
| import org.springframework.http.ResponseEntity | ||
| import org.springframework.web.bind.annotation.DeleteMapping | ||
| import org.springframework.web.bind.annotation.GetMapping | ||
| import org.springframework.web.bind.annotation.PatchMapping | ||
| import org.springframework.web.bind.annotation.PathVariable | ||
| import org.springframework.web.bind.annotation.PostMapping | ||
| import org.springframework.web.bind.annotation.RequestBody | ||
| import org.springframework.web.bind.annotation.RestController | ||
| import java.time.Instant | ||
|
|
||
| @RestController | ||
| @Tag(name = "Article", description = "게시글 관리 API") | ||
| class ArticleController( | ||
| private val articleService: ArticleService, | ||
| ) { | ||
| @Operation(summary = "게시글 생성", description = "게시판에 글이 작성되는지 확인.") | ||
| @ApiResponses( | ||
| value = [ | ||
| ApiResponse(responseCode = "201", description = "게시글 생성"), | ||
| ApiResponse(responseCode = "400", description = "잘못된 요청(제목, 글쓴이, 원본링크, 글 작성시간 중 하나가 작성되지 않음)"), | ||
| ApiResponse(responseCode = "404", description = "게시판을 찾을 수 없음"), | ||
| ], | ||
| ) | ||
| @PostMapping("/api/v1/boards/{boardId}/articles") | ||
| fun create( | ||
| @Parameter( | ||
| description = "게시판 ID", | ||
| example = "1", | ||
| ) @PathVariable boardId: Long, | ||
| @RequestBody createArticleRequest: CreateArticleRequest, | ||
| ): ResponseEntity<CreateArticleResponse> { | ||
| val articleDto = | ||
| articleService.create( | ||
| content = createArticleRequest.content, | ||
| title = createArticleRequest.title, | ||
| author = createArticleRequest.author, | ||
| originLink = createArticleRequest.originLink, | ||
| publihedAt = createArticleRequest.publishedAt, | ||
| boardId = boardId, | ||
| ) | ||
| return ResponseEntity.ok(articleDto) | ||
| } | ||
|
|
||
| @Operation( | ||
| summary = "게시글 목록 조회", | ||
| description = """ | ||
| 게시판의 게시글을 페이지네이션 해서 가져옴 | ||
| 커서 기반 페이지 네이션 사용 | ||
| 정렬: 게시글 생성시간 내림차순 | ||
| """, | ||
| ) | ||
| @ApiResponses( | ||
| value = [ | ||
| ApiResponse(responseCode = "200", description = "게시글 목록 조회 성공"), | ||
| ApiResponse(responseCode = "404", description = "게시판을 찾을 수 없음"), | ||
| ], | ||
| ) | ||
| @GetMapping("/api/v1/boards/{boardId}/articles") | ||
| fun paging( | ||
| @Parameter(description = "게시판 ID", example = "1") | ||
| @PathVariable boardId: Long, | ||
| @RequestBody articlePagingRequest: ArticlePagingRequest, | ||
sehwanyi marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ): ResponseEntity<ArticlePagingResponse> { | ||
| val articlePagingResponse = | ||
| articleService.pageByBoardId( | ||
| boardId = boardId, | ||
| nextPublishedAt = articlePagingRequest.nextPublishedAt?.let { Instant.ofEpochMilli(it) }, | ||
| nextId = articlePagingRequest.nextId, | ||
| limit = articlePagingRequest.limit, | ||
| ) | ||
| return ResponseEntity.ok(articlePagingResponse) | ||
| } | ||
|
|
||
| @Operation(summary = "특정 게시글 조회", description = "게시글 ID로 게시글 상세 정보 조회") | ||
| @ApiResponses( | ||
| value = [ | ||
| ApiResponse(responseCode = "200", description = "특정 게시글 조회 성공"), | ||
| ApiResponse(responseCode = "404", description = "게시판을 찾을 수 없습니다."), | ||
| ], | ||
| ) | ||
| @GetMapping("/api/v1/articles/{articleId}") | ||
| fun get( | ||
| @Parameter( | ||
| description = "게시글 ID", | ||
| example = "1", | ||
| ) @PathVariable articleId: Long, | ||
| ): ResponseEntity<ArticleDto> { | ||
| val articleDto = articleService.get(articleId) | ||
| return ResponseEntity.ok(articleDto) | ||
| } | ||
|
|
||
| @Operation(summary = "특정 게시글 업데이트", description = "게시글 ID로 게시글 상세 정보 업데이트") | ||
| @ApiResponses( | ||
| value = [ | ||
| ApiResponse(responseCode = "200", description = "특정 게시글 업데이트 성공"), | ||
| ApiResponse(responseCode = "400", description = "잘못된 요청(제목, 글쓴이, 원본링크, 글 작성시간 중 하나가 작성되지 않음)"), | ||
| ApiResponse(responseCode = "404", description = "게시글을 찾을 수 없습니다."), | ||
| ], | ||
| ) | ||
| @PatchMapping("/api/v1/articles/{articleId}") | ||
| fun update( | ||
| @Parameter( | ||
| description = "게시글 ID", | ||
| example = "1", | ||
| ) | ||
| @PathVariable articleId: Long, | ||
| @RequestBody updateArticleRequest: UpdateArticleRequest, | ||
| ): ResponseEntity<ArticleDto> { | ||
| val articleDto = | ||
| articleService.update( | ||
| articleId = articleId, | ||
| content = updateArticleRequest.content, | ||
| author = updateArticleRequest.author, | ||
| originLink = updateArticleRequest.originLink, | ||
| title = updateArticleRequest.title, | ||
| publishedAt = updateArticleRequest.publishedAt, | ||
| ) | ||
| return ResponseEntity.ok(articleDto) | ||
| } | ||
|
|
||
| @Operation(summary = "특정 게시글 삭제", description = "게시글 ID로 게시글 삭제") | ||
| @ApiResponses( | ||
| value = [ | ||
| ApiResponse(responseCode = "204", description = "특정 게시글 삭제 성공"), | ||
| ApiResponse(responseCode = "404", description = "게시글을 찾을 수 없습니다."), | ||
| ], | ||
| ) | ||
| @DeleteMapping("/api/v1/articles/{articleId}") | ||
| fun delete( | ||
| @Parameter( | ||
| description = "게시글 ID", | ||
| example = "1", | ||
| ) | ||
| @PathVariable articleId: Long, | ||
| ): ResponseEntity<Unit> { | ||
| articleService.delete(articleId) | ||
| return ResponseEntity.noContent().build() | ||
| } | ||
| } | ||
7 changes: 7 additions & 0 deletions
7
src/main/kotlin/com/wafflestudio/team2server/article/dto/ArticlePaging.kt
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.wafflestudio.team2server.article.dto | ||
|
|
||
| data class ArticlePaging( | ||
| val nextPublishedAt: Long?, | ||
| val nextId: Long?, | ||
| val hasNext: Boolean, | ||
| ) |
47 changes: 47 additions & 0 deletions
47
src/main/kotlin/com/wafflestudio/team2server/article/dto/core/ArticleDto.kt
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package com.wafflestudio.team2server.article.dto.core | ||
|
|
||
| import com.wafflestudio.team2server.article.model.Article | ||
| import com.wafflestudio.team2server.article.model.ArticleWithBoard | ||
| import com.wafflestudio.team2server.board.dto.core.BoardDto | ||
| import com.wafflestudio.team2server.board.model.Board | ||
|
|
||
| data class ArticleDto( | ||
| val id: Long, | ||
| val board: BoardDto, | ||
| val content: String, | ||
| val author: String, | ||
| val originLink: String, | ||
| val title: String, | ||
| val publishedAt: Long, | ||
| val createdAt: Long, | ||
| val updatedAt: Long, | ||
| ) { | ||
| constructor(article: Article, board: Board) : this( | ||
| id = article.id!!, | ||
| board = BoardDto(board), | ||
| content = article.content, | ||
| author = article.author, | ||
| originLink = article.originLink, | ||
| title = article.title, | ||
| publishedAt = article.publishedAt.toEpochMilli(), | ||
| createdAt = article.createdAt!!.toEpochMilli(), | ||
| updatedAt = article.updatedAt!!.toEpochMilli(), | ||
| ) | ||
|
|
||
| constructor(articleWithBoard: ArticleWithBoard) : this( | ||
| id = articleWithBoard.id, | ||
| board = | ||
| BoardDto( | ||
| id = articleWithBoard.board!!.id, | ||
| name = articleWithBoard.board.name, | ||
| sourceUrl = articleWithBoard.board.sourceUrl, | ||
| ), | ||
| content = articleWithBoard.content, | ||
| author = articleWithBoard.author, | ||
| originLink = articleWithBoard.originLink, | ||
| title = articleWithBoard.title, | ||
| publishedAt = articleWithBoard.publishedAt.toEpochMilli(), | ||
| createdAt = articleWithBoard.createdAt.toEpochMilli(), | ||
| updatedAt = articleWithBoard.updatedAt.toEpochMilli(), | ||
| ) | ||
| } |
23 changes: 23 additions & 0 deletions
23
src/main/kotlin/com/wafflestudio/team2server/article/dto/request/ArticlePagingRequest.kt
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package com.wafflestudio.team2server.article.dto | ||
|
|
||
| import io.swagger.v3.oas.annotations.media.Schema | ||
|
|
||
| @Schema(description = "게시글 목록 커서 페이지네이션 요청") | ||
| data class ArticlePagingRequest( | ||
| @Schema( | ||
| description = "다음 페이지 커서( 게시글 생성 시간)", | ||
| required = false, | ||
| ) | ||
| val nextPublishedAt: Long? = null, | ||
| @Schema( | ||
| description = "다음 페이지 커서(게시글 ID)", | ||
| required = false, | ||
| ) | ||
| val nextId: Long? = null, | ||
| @Schema( | ||
| description = "페이지당 게시글 수", | ||
| example = "30", | ||
| required = false, | ||
| ) | ||
| val limit: Int = 30, | ||
| ) |
38 changes: 38 additions & 0 deletions
38
src/main/kotlin/com/wafflestudio/team2server/article/dto/request/CreateArticleRequest.kt
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package com.wafflestudio.team2server.article.dto.request | ||
|
|
||
| import io.swagger.v3.oas.annotations.media.Schema | ||
| import java.time.Instant | ||
|
|
||
| @Schema(description = "게시글 생성 요청") | ||
| data class CreateArticleRequest( | ||
| @Schema( | ||
| description = "게시글 제목", | ||
| example = "교내 장학금 신청 안내", | ||
| required = true, | ||
| ) | ||
| val title: String, | ||
| @Schema( | ||
| description = "게시글 본문 내용", | ||
| example = "mysnu에서 신청하세요...", | ||
| required = true, | ||
| ) | ||
| val content: String, | ||
| @Schema( | ||
| description = "기사 작성자", | ||
| example = "행정부", | ||
| required = true, | ||
| ) | ||
| val author: String, | ||
| @Schema( | ||
| description = "원문 기사 링크", | ||
| example = "https://www.mysnu/...", | ||
| required = true, | ||
| ) | ||
| val originLink: String, | ||
| @Schema( | ||
| description = "기사 실제 게시 시각", | ||
| example = "2026-01-01T12:00:00Z", | ||
| required = true, | ||
| ) | ||
| val publishedAt: Instant, | ||
| ) |
18 changes: 18 additions & 0 deletions
18
src/main/kotlin/com/wafflestudio/team2server/article/dto/request/UpdateArticleRequest.kt
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.wafflestudio.team2server.article.dto | ||
|
|
||
| import io.swagger.v3.oas.annotations.media.Schema | ||
| import java.time.Instant | ||
|
|
||
| @Schema(description = "게시글 수정 요청") | ||
| data class UpdateArticleRequest( | ||
| @Schema(description = "게시글 제목", example = "수정된 제목") | ||
| val title: String? = null, | ||
| @Schema(description = "게시글 내용", example = "수정된 내용") | ||
| val content: String? = null, | ||
| @Schema(description = "게시글 작성자", example = "다른 작성자") | ||
| val author: String? = null, | ||
| @Schema(description = "원문 게시글 링크", example = "www.youtube.com") | ||
| val originLink: String? = null, | ||
| @Schema(description = "게시글 작성 시각") | ||
| val publishedAt: Instant? = null, | ||
| ) |
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/com/wafflestudio/team2server/article/dto/response/ArticlePagingResponse.kt
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package com.wafflestudio.team2server.article.dto.response | ||
|
|
||
| import com.wafflestudio.team2server.article.dto.ArticlePaging | ||
| import com.wafflestudio.team2server.article.dto.core.ArticleDto | ||
|
|
||
| data class ArticlePagingResponse( | ||
| val data: List<ArticleDto>, | ||
| val paging: ArticlePaging, | ||
| ) |
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/com/wafflestudio/team2server/article/dto/response/CreateArticleResponse.kt
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package com.wafflestudio.team2server.article.dto.response | ||
|
|
||
| import com.wafflestudio.team2server.article.dto.core.ArticleDto | ||
|
|
||
| typealias CreateArticleResponse = ArticleDto |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.