Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
@@ -0,0 +1,40 @@
package com.somemore.community.service.command;

import com.somemore.community.domain.CommunityBoard;
import com.somemore.community.repository.CommunityBoardRepository;
import com.somemore.community.usecase.command.DeleteCommunityBoardUseCase;
import com.somemore.global.exception.BadRequestException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_COMMUNITY_BOARD;
import static com.somemore.global.exception.ExceptionMessage.UNAUTHORIZED_COMMUNITY_BOARD;

@RequiredArgsConstructor
@Transactional
@Service
class DeleteCommunityBoardService implements DeleteCommunityBoardUseCase {

private final CommunityBoardRepository communityBoardRepository;

@Override
public void deleteCommunityBoard(UUID writerId, Long id) {
CommunityBoard communityBoard = communityBoardRepository.findById(id)
Copy link
Collaborator

Choose a reason for hiding this comment

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

아래에 valiadteWriter처럼 메서드로 빼서 추상화 레벨 맞춰주는것도 좋아보여요!

Copy link
Collaborator Author

@ayoung-dev ayoung-dev Nov 26, 2024

Choose a reason for hiding this comment

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

넵! 수정하겠습니다

.orElseThrow(() -> new BadRequestException(NOT_EXISTS_COMMUNITY_BOARD.getMessage()));

validateWriter(communityBoard, writerId);

communityBoard.markAsDeleted();
}

private void validateWriter(CommunityBoard communityBoard, UUID writerId) {
if (communityBoard.getWriterId().equals(writerId)) {
return;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

communityBoard.getWriterId().equals(writerId)

이 부분은 CommunityBoard 메서드 호출해서 하는거 어떻게 생각하세요??
communityBoard.isWriter(writerId); 이런식으로요

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

TDA 감사합니다
수정하겠습니다!


throw new BadRequestException(UNAUTHORIZED_COMMUNITY_BOARD.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.somemore.community.usecase.command;

import java.util.UUID;

public interface DeleteCommunityBoardUseCase {
void deleteCommunityBoard(UUID writerId, Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
public enum ExceptionMessage {

NOT_EXISTS_CENTER("존재하지 않는 기관 ID 입니다."),
NOT_EXISTS_COMMUNITY_BOARD("삭제된 게시글 입니다."),
NOT_EXISTS_COMMUNITY_BOARD("존재하지 않는 게시글 입니다."),
UNAUTHORIZED_COMMUNITY_BOARD("게시글 삭제 권한이 없습니다."),
NOT_EXISTS_LOCATION("존재하지 않는 위치 ID 입니다."),
NOT_EXISTS_RECRUIT_BOARD("존재하지 않는 봉사 모집글 ID 입니다."),
UNAUTHORIZED_RECRUIT_BOARD("자신이 작성한 봉사 모집글이 아닙니다."),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
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.dto.request.CommunityBoardCreateRequestDto;
import com.somemore.community.repository.CommunityBoardRepository;
import com.somemore.community.usecase.command.CreateCommunityBoardUseCase;
import com.somemore.community.usecase.query.CommunityBoardQueryUseCase;
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.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.UUID;

class DeleteCommunityBoardServiceTest extends IntegrationTestSupport {
@Autowired
private DeleteCommunityBoardService deleteCommunityBoardService;
@Autowired
private CreateCommunityBoardUseCase createCommunityBoardUseCase;
@Autowired
private CommunityBoardQueryUseCase communityBoardQueryUseCase;
@Autowired
private CommunityBoardRepository communityBoardRepository;

@AfterEach
void tearDown() {
communityBoardRepository.deleteAllInBatch();
}

@DisplayName("커뮤니티 게시글 id로 게시글을 삭제한다.")
@Test
void deleteCommunityBoardWithId() {
//given
CommunityBoardCreateRequestDto dto = CommunityBoardCreateRequestDto.builder()
.title("커뮤니티 테스트 제목")
.content("커뮤니티 테스트 내용")
.build();

Copy link
Collaborator

@m-a-king m-a-king Nov 26, 2024

Choose a reason for hiding this comment

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

중복되는 부분이 눈에 띄는데 @BeforeEach를 사용해보는 것은 어떨까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

수정하겠습니다!

UUID writerId = UUID.randomUUID();
String imgUrl = "https://image.test.url/123";

Long communityId = createCommunityBoardUseCase.createCommunityBoard(dto, writerId, imgUrl);

//when
deleteCommunityBoardService.deleteCommunityBoard(writerId, communityId);

//then
assertThat(communityBoardQueryUseCase.getCommunityBoards()).isEmpty();
}

@DisplayName("삭제된 커뮤니티 게시글의 id로 게시글을 삭제할 때 예외를 던진다.")
@Test
void deleteCommunityBoardWithDeletedId() {
//given
CommunityBoardCreateRequestDto dto = CommunityBoardCreateRequestDto.builder()
.title("커뮤니티 테스트 제목")
.content("커뮤니티 테스트 내용")
.build();

UUID writerId = UUID.randomUUID();
String imgUrl = "https://image.test.url/123";

Long communityId = createCommunityBoardUseCase.createCommunityBoard(dto, writerId, imgUrl);
deleteCommunityBoardService.deleteCommunityBoard(writerId, communityId);

//when
ThrowableAssert.ThrowingCallable callable = () -> deleteCommunityBoardService.deleteCommunityBoard(writerId, communityId);

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

@DisplayName("작성자가 아닌 id로 커뮤니티 게시글을 삭제하고자 할 때 예외를 던진다.")
@Test
void deleteCommunityBoardWithNotWriterId() {
//given
CommunityBoardCreateRequestDto dto = CommunityBoardCreateRequestDto.builder()
.title("커뮤니티 테스트 제목")
.content("커뮤니티 테스트 내용")
.build();

UUID writerId = UUID.randomUUID();
String imgUrl = "https://image.test.url/123";

Long communityId = createCommunityBoardUseCase.createCommunityBoard(dto, writerId, imgUrl);

//when
ThrowableAssert.ThrowingCallable callable = () -> deleteCommunityBoardService.deleteCommunityBoard(UUID.randomUUID(), communityId);

//then
assertThatExceptionOfType(BadRequestException.class)
.isThrownBy(callable)
.withMessage(ExceptionMessage.UNAUTHORIZED_COMMUNITY_BOARD.getMessage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.somemore.community.dto.response.CommunityBoardGetResponseDto;
import com.somemore.community.repository.CommunityBoardRepository;
import com.somemore.community.usecase.command.CreateCommunityBoardUseCase;
import com.somemore.community.usecase.command.DeleteCommunityBoardUseCase;
import com.somemore.global.exception.BadRequestException;
import com.somemore.global.exception.ExceptionMessage;
import com.somemore.volunteer.domain.Volunteer;
Expand Down Expand Up @@ -37,6 +38,8 @@ class CommunityBoardQueryServiceTest extends IntegrationTestSupport {
@Autowired
CreateCommunityBoardUseCase createCommunityBoardUseCase;
@Autowired
DeleteCommunityBoardUseCase deleteCommunityBoardUseCase;
@Autowired
CommunityBoardQueryService communityBoardQueryService;

@AfterEach
Expand Down Expand Up @@ -209,7 +212,7 @@ void getCommunityBoardDetailWithDeletedId() {

Long communityId = createCommunityBoardUseCase.createCommunityBoard(dto1, savedVolunteer.getId(), imgUrl);

communityBoardRepository.deleteAllInBatch();
deleteCommunityBoardUseCase.deleteCommunityBoard(savedVolunteer.getId(), communityId);

//when
ThrowableAssert.ThrowingCallable callable = () -> communityBoardQueryService.getCommunityBoardDetail(communityId);
Expand Down