Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -48,4 +48,8 @@ public CommunityBoard(UUID writerId, String title, String content, String imgUrl
this.content = content;
this.imgUrl = imgUrl;
}

public boolean isWriter(UUID writerId) {
return this.writerId.equals(writerId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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 = getCommunityBoardById(id);

validateWriter(communityBoard, writerId);

communityBoard.markAsDeleted();

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;
}
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,92 @@
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.BeforeEach;
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;

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("커뮤니티 게시글 id로 게시글을 삭제한다.")
@Test
void deleteCommunityBoardWithId() {
//given
//when
deleteCommunityBoardService.deleteCommunityBoard(writerId, communityId);

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

@DisplayName("삭제된 커뮤니티 게시글의 id로 게시글을 삭제할 때 예외를 던진다.")
@Test
void deleteCommunityBoardWithDeletedId() {
//given
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
//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