-
Notifications
You must be signed in to change notification settings - Fork 1
Feature/56 커뮤니티 게시글 수정 기능 #59
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
Changes from all commits
fa7ba99
5462f83
d23c3ec
25ce00c
8cb12c7
cb6251f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.somemore.community.dto.request; | ||
|
|
||
| import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
| import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
| import jakarta.validation.constraints.NotBlank; | ||
| import lombok.Builder; | ||
|
|
||
| @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) | ||
| @Builder | ||
| public record CommunityBoardUpdateRequestDto( | ||
| @Schema(description = "커뮤니티 게시글 제목", example = "11/29 OO도서관 봉사 같이 갈 사람 모집합니다.") | ||
| @NotBlank(message = "게시글 제목은 필수 값입니다.") | ||
| String title, | ||
| @Schema(description = "커뮤니티 게시글 내용", example = "저 포함 5명이 같이 가면 좋을 거 같아요") | ||
| @NotBlank(message = "게시글 내용은 필수 값입니다.") | ||
| String content | ||
| ) {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package com.somemore.community.service; | ||
|
|
||
| import com.somemore.community.domain.CommunityBoard; | ||
| import com.somemore.community.dto.request.CommunityBoardUpdateRequestDto; | ||
| import com.somemore.community.repository.CommunityBoardRepository; | ||
| import com.somemore.community.usecase.UpdateCommunityBoardUseCase; | ||
| import com.somemore.global.exception.BadRequestException; | ||
|
|
||
| import java.util.UUID; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import static com.somemore.global.exception.ExceptionMessage.*; | ||
|
|
||
|
|
||
| @RequiredArgsConstructor | ||
| @Transactional | ||
| @Service | ||
| public class UpdateCommunityBoardService implements UpdateCommunityBoardUseCase { | ||
|
|
||
| private final CommunityBoardRepository communityBoardRepository; | ||
|
|
||
| @Override | ||
| public void updateCommunityBoard(CommunityBoardUpdateRequestDto requestDto, Long communityBoardId, UUID writerId, String imgUrl) { | ||
| CommunityBoard communityBoard = getCommunityBoardById(communityBoardId); | ||
| validateWriter(communityBoard, writerId); | ||
| communityBoard.updateWith(requestDto, imgUrl); | ||
|
|
||
| communityBoardRepository.save(communityBoard); | ||
| } | ||
|
|
||
| private CommunityBoard getCommunityBoardById(Long id) { | ||
| return communityBoardRepository.findById(id) | ||
| .orElseThrow(() -> new BadRequestException(NOT_EXISTS_COMMUNITY_BOARD.getMessage())); | ||
| } | ||
|
|
||
| private void validateWriter(CommunityBoard communityBoard, UUID writerId) { | ||
| if (communityBoard.isWriter(writerId)) { | ||
| return; | ||
| } | ||
|
|
||
| throw new BadRequestException(UNAUTHORIZED_COMMUNITY_BOARD.getMessage()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| package com.somemore.community.usecase.command; | ||
| package com.somemore.community.usecase; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.somemore.community.usecase; | ||
|
|
||
| import com.somemore.community.dto.request.CommunityBoardUpdateRequestDto; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| public interface UpdateCommunityBoardUseCase { | ||
| void updateCommunityBoard( | ||
| CommunityBoardUpdateRequestDto requestDto, | ||
| Long communityBoardId, | ||
| UUID writerId, | ||
| String imgUrl); | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -10,7 +10,7 @@ public enum ExceptionMessage { | |||||||||
|
|
||||||||||
| NOT_EXISTS_CENTER("존재하지 않는 기관 ID 입니다."), | ||||||||||
| NOT_EXISTS_COMMUNITY_BOARD("존재하지 않는 게시글 입니다."), | ||||||||||
| UNAUTHORIZED_COMMUNITY_BOARD("게시글 삭제 권한이 없습니다."), | ||||||||||
| UNAUTHORIZED_COMMUNITY_BOARD("해당 게시글에 권한이 없습니다."), | ||||||||||
| NOT_EXISTS_LOCATION("존재하지 않는 위치 ID 입니다."), | ||||||||||
|
Comment on lines
+13
to
14
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
이렇게 사용하고 아래에서 메시지 게터에 도메인이나 맥락에 대해서 동적으로 파라미터를 사용하는 방법은 어떨까요?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 다 같이 얘기해보면 좋을 거 같아요!
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵 |
||||||||||
| NOT_EXISTS_RECRUIT_BOARD("존재하지 않는 봉사 모집글 ID 입니다."), | ||||||||||
| UNAUTHORIZED_RECRUIT_BOARD("자신이 작성한 봉사 모집글이 아닙니다."), | ||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| package com.somemore.community.service; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
|
|
||
| import com.somemore.IntegrationTestSupport; | ||
| import com.somemore.community.domain.CommunityBoard; | ||
| import com.somemore.community.dto.request.CommunityBoardCreateRequestDto; | ||
| import com.somemore.community.dto.request.CommunityBoardUpdateRequestDto; | ||
| import com.somemore.community.repository.CommunityBoardRepository; | ||
| import com.somemore.community.usecase.CreateCommunityBoardUseCase; | ||
| import com.somemore.global.exception.BadRequestException; | ||
| import com.somemore.global.exception.ExceptionMessage; | ||
| import org.assertj.core.api.ThrowableAssert; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
|
|
||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
|
|
||
| class UpdateCommunityBoardServiceTest extends IntegrationTestSupport { | ||
|
|
||
| @Autowired | ||
| private CreateCommunityBoardUseCase createCommunityBoardUseCase; | ||
| @Autowired | ||
| private CommunityBoardRepository communityBoardRepository; | ||
| @Autowired | ||
| private UpdateCommunityBoardService updateCommunityBoardService; | ||
|
|
||
| private UUID writerId; | ||
| private Long communityId; | ||
| private String imgUrl; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| CommunityBoardCreateRequestDto dto = CommunityBoardCreateRequestDto.builder() | ||
| .title("커뮤니티 테스트 제목") | ||
| .content("커뮤니티 테스트 내용") | ||
| .build(); | ||
|
|
||
| writerId = UUID.randomUUID(); | ||
| imgUrl = "https://image.test.url/123"; | ||
|
|
||
| communityId = createCommunityBoardUseCase.createCommunityBoard(dto, writerId, imgUrl); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown() { | ||
| communityBoardRepository.deleteAllInBatch(); | ||
| } | ||
|
|
||
| @DisplayName("커뮤니티 게시글을 수정한다.") | ||
| @Test | ||
| void updateCommunityBoard() { | ||
|
|
||
| //given | ||
| CommunityBoardUpdateRequestDto dto = CommunityBoardUpdateRequestDto.builder() | ||
| .title("수정된 커뮤니티 테스트 제목") | ||
| .content("수정된 커뮤니티 테스트 내용") | ||
| .build(); | ||
|
|
||
| String newImgUrl = "https://image.test.url/567"; | ||
|
|
||
| //when | ||
| updateCommunityBoardService.updateCommunityBoard(dto, communityId, writerId, newImgUrl); | ||
|
|
||
| //then | ||
| Optional<CommunityBoard> updatedCommunityBoard = communityBoardRepository.findById(communityId); | ||
| assertThat(updatedCommunityBoard).isNotNull(); | ||
| assertThat(updatedCommunityBoard.get().getId()).isEqualTo(communityId); | ||
| assertThat(updatedCommunityBoard.get().getWriterId()).isEqualTo(writerId); | ||
| assertThat(updatedCommunityBoard.get().getTitle()).isEqualTo("수정된 커뮤니티 테스트 제목"); | ||
| assertThat(updatedCommunityBoard.get().getContent()).isEqualTo("수정된 커뮤니티 테스트 내용"); | ||
| assertThat(updatedCommunityBoard.get().getImgUrl()).isEqualTo("https://image.test.url/567"); | ||
| } | ||
|
|
||
| @DisplayName("작성자가 아닌 id로 커뮤니티 게시글을 수정하고자 할 때 예외를 던진다.") | ||
| @Test | ||
| void updateCommunityBoardNotWriterId() { | ||
|
|
||
| //given | ||
| CommunityBoardUpdateRequestDto dto = CommunityBoardUpdateRequestDto.builder() | ||
| .title("수정된 커뮤니티 테스트 제목") | ||
| .content("수정된 커뮤니티 테스트 내용") | ||
| .build(); | ||
|
|
||
| //when | ||
| ThrowableAssert.ThrowingCallable callable = () -> updateCommunityBoardService.updateCommunityBoard(dto, communityId, UUID.randomUUID(), null); | ||
|
|
||
| //then | ||
| assertThatExceptionOfType(BadRequestException.class) | ||
| .isThrownBy(callable) | ||
| .withMessage(ExceptionMessage.UNAUTHORIZED_COMMUNITY_BOARD.getMessage()); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.