Skip to content

Commit

Permalink
Merge pull request #166 from themoment-team/feature/updateBoard
Browse files Browse the repository at this point in the history
게시글 수정 기능
  • Loading branch information
tlsgmltjd authored Jun 10, 2024
2 parents d86c3fa + 90f8aef commit a407213
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import team.themoment.gsmNetworking.domain.board.domain.BoardCategory
import team.themoment.gsmNetworking.domain.board.dto.BoardInfoDto
import team.themoment.gsmNetworking.domain.board.dto.BoardListDto
import team.themoment.gsmNetworking.domain.board.dto.BoardSaveDto
import team.themoment.gsmNetworking.domain.board.dto.BoardUpdateDto
import team.themoment.gsmNetworking.domain.board.service.*
import javax.validation.Valid
import javax.validation.constraints.Max
Expand All @@ -28,6 +29,7 @@ class BoardController (
private val saveBoardUseCase: SaveBoardUseCase,
private val queryBoardListUseCase: QueryBoardListUseCase,
private val queryBoardInfoUseCase: QueryBoardInfoUseCase,
private val updateBoardUseCase: UpdateBoardUseCase,
private val updatePinStatusUseCase: UpdatePinStatusUseCase,
private val authenticatedUserManager: AuthenticatedUserManager
) {
Expand All @@ -54,6 +56,15 @@ class BoardController (
return ResponseEntity.ok(queryBoardInfoUseCase.queryBoardInfo(boardId, authenticationId))
}

@PatchMapping("/{boardId}")
fun updateBoard(
@PathVariable boardId: Long,
@RequestBody boardUpdateDto: BoardUpdateDto
) : ResponseEntity<BoardInfoDto> {
val authenticationId = authenticatedUserManager.getName()
return ResponseEntity.ok(updateBoardUseCase.updateBoard(boardUpdateDto, boardId, authenticationId))
}

@PatchMapping("/pin/{boardId}")
fun updatePinStatus(
@PathVariable boardId: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ import team.themoment.gsmNetworking.common.domain.BaseIdTimestampEntity
import team.themoment.gsmNetworking.domain.comment.domain.Comment
import team.themoment.gsmNetworking.domain.like.domain.Like
import team.themoment.gsmNetworking.domain.user.domain.User
import java.time.LocalDateTime
import javax.persistence.*
import javax.persistence.FetchType.*

@Entity
@Table(name = "board")
class Board (

override val id: Long = 0,

override var createdAt: LocalDateTime = LocalDateTime.now(),

override var updatedAt: LocalDateTime = LocalDateTime.now(),

@Column(name = "title", length = 50)
val title: String,

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package team.themoment.gsmNetworking.domain.board.dto

import io.micrometer.core.lang.Nullable
import team.themoment.gsmNetworking.domain.board.domain.BoardCategory
import javax.persistence.EnumType
import javax.persistence.Enumerated
import javax.validation.constraints.*

data class BoardUpdateDto (
@field:NotBlank
@field:Size(max = 50)
val title: String,
@field:NotBlank
@field:Size(max = 1000)
val content: String,
@field:NotNull
@Enumerated(EnumType.STRING)
val boardCategory: BoardCategory
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package team.themoment.gsmNetworking.domain.board.service

import team.themoment.gsmNetworking.domain.board.dto.BoardInfoDto
import team.themoment.gsmNetworking.domain.board.dto.BoardUpdateDto

interface UpdateBoardUseCase {
fun updateBoard(updateBoardDto: BoardUpdateDto, boardId: Long, authenticationId: Long): BoardInfoDto
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import team.themoment.gsmNetworking.domain.board.domain.BoardCategory.*
import team.themoment.gsmNetworking.domain.board.dto.BoardInfoDto
import team.themoment.gsmNetworking.domain.board.dto.BoardListDto
import team.themoment.gsmNetworking.domain.board.dto.BoardSaveDto
import team.themoment.gsmNetworking.domain.board.dto.BoardUpdateDto
import team.themoment.gsmNetworking.domain.board.repository.BoardRepository
import team.themoment.gsmNetworking.domain.board.service.*
import team.themoment.gsmNetworking.domain.comment.domain.Comment
Expand Down Expand Up @@ -45,8 +46,9 @@ class BoardService (
) : SaveBoardUseCase,
QueryBoardListUseCase,
QueryBoardInfoUseCase,
UpdatePinStatusUseCase {

UpdatePinStatusUseCase,
UpdateBoardUseCase
{
@Transactional
override fun saveBoard(boardSaveDto: BoardSaveDto, authenticationId: Long): BoardListDto {
val currentUser = userRepository.findByAuthenticationId(authenticationId)
Expand Down Expand Up @@ -152,6 +154,8 @@ class BoardService (
)
}



private fun getFindComments(findComments: List<Comment>): List<CommentListDto> {
return findComments.map { CommentListDto(
commentId = it.id,
Expand Down Expand Up @@ -228,4 +232,51 @@ class BoardService (
board.isPinned = !board.isPinned
}

@Transactional
override fun updateBoard(updateBoardDto: BoardUpdateDto, boardId: Long, authenticationId: Long): BoardInfoDto {
val board = boardRepository.findById(boardId)
.orElseThrow { throw ExpectedException("존재하지않는 게시글입니다.", HttpStatus.NOT_FOUND) }

val currentUser = userRepository.findByAuthenticationId(authenticationId)
?: throw ExpectedException("유저를 찾을 수 없습니다.", HttpStatus.NOT_FOUND)

if (board.author.authenticationId != authenticationId)
throw ExpectedException("게시글의 작성자만 수정할 수 있습니다.", HttpStatus.BAD_REQUEST)

val updatedBoard = Board(
id = board.id,
createdAt = board.createdAt,
updatedAt = LocalDateTime.now(),
title = updateBoardDto.title,
content = updateBoardDto.content,
boardCategory = updateBoardDto.boardCategory,
author = currentUser,
comments = board.comments,
likes = board.likes,
isPinned = board.isPinned
)

val saveBoard = boardRepository.save(updatedBoard)

return BoardInfoDto(
id = saveBoard.id,
title = saveBoard.title,
content = saveBoard.content,
boardCategory = saveBoard.boardCategory,
author = AuthorDto(
name = saveBoard.author.name,
generation = saveBoard.author.generation,
profileUrl = saveBoard.author.profileUrl,
defaultImgNumber = saveBoard.author.defaultImgNumber
),
comments = getFindComments(saveBoard.comments),
likeCount = saveBoard.likes.size,
createdAt = saveBoard.createdAt,
isPinned = saveBoard.isPinned,
isLike = saveBoard.likes.stream().anyMatch {
like -> like.user == currentUser
}
)
}

}

0 comments on commit a407213

Please sign in to comment.