Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.somemore.community.domain;

import com.somemore.community.dto.request.CommunityBoardUpdateRequestDto;
import com.somemore.global.common.BaseEntity;
import static lombok.AccessLevel.PROTECTED;

Expand Down Expand Up @@ -52,4 +53,10 @@ public CommunityBoard(UUID writerId, String title, String content, String imgUrl
public boolean isWriter(UUID writerId) {
return this.writerId.equals(writerId);
}

public void updateWith(CommunityBoardUpdateRequestDto dto, String imgUrl) {
this.title = dto.title();
this.content = dto.content();
this.imgUrl = imgUrl;
}
}
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,52 @@
package com.somemore.community.service.command;

import com.somemore.community.domain.CommunityBoard;
import com.somemore.community.dto.request.CommunityBoardUpdateRequestDto;
import com.somemore.community.repository.CommunityBoardRepository;
import com.somemore.community.usecase.command.UpdateCommunityBoardUseCase;
import com.somemore.global.exception.BadRequestException;
import com.somemore.location.usecase.command.UpdateLocationUseCase;
import com.somemore.recruitboard.domain.RecruitBoard;
import com.somemore.recruitboard.domain.RecruitStatus;
import com.somemore.recruitboard.dto.request.RecruitBoardLocationUpdateRequestDto;
import com.somemore.recruitboard.dto.request.RecruitBoardUpdateRequestDto;
import com.somemore.recruitboard.repository.RecruitBoardRepository;
import com.somemore.recruitboard.usecase.command.UpdateRecruitBoardUseCase;
import java.time.LocalDateTime;
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
@@ -0,0 +1,13 @@
package com.somemore.community.usecase.command;

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
Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
UNAUTHORIZED_COMMUNITY_BOARD("해당 게시글에 권한이 없습니다."),
NOT_EXISTS_LOCATION("존재하지 않는 위치 ID 입니다."),
NOT_FOUND("%s를 찾을 수 없습니다."),
UNAUTHORIZED("%s에 대한 권한이 없습니다."),

이렇게 사용하고 아래에서 메시지 게터에 도메인이나 맥락에 대해서 동적으로 파라미터를 사용하는 방법은 어떨까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다 같이 얘기해보면 좋을 거 같아요!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOT_EXISTS_RECRUIT_BOARD("존재하지 않는 봉사 모집글 ID 입니다."),
UNAUTHORIZED_RECRUIT_BOARD("자신이 작성한 봉사 모집글이 아닙니다."),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.somemore.community.service.command;

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.command.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);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ThrowingCallable 이거 좋은것 같아요.
when 에서 에러가 발생하는 동작을 이렇게 뺄수있군요..

//then
assertThatExceptionOfType(BadRequestException.class)
.isThrownBy(callable)
.withMessage(ExceptionMessage.UNAUTHORIZED_COMMUNITY_BOARD.getMessage());
}
}