Skip to content

Commit

Permalink
Merge pull request #45 from giwoong01/feature/block
Browse files Browse the repository at this point in the history
Feat(#11 #18): Block 논리 삭제 기능 구현, Block 상태별 전체조회 Query 수정, Block, BlockRepository 테스트 코드 구현
  • Loading branch information
giwoong01 authored Jul 28, 2024
2 parents 0237e84 + 0f5cedb commit bef8958
Show file tree
Hide file tree
Showing 10 changed files with 341 additions and 3 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ dependencies {
implementation 'com.github.maricn:logback-slack-appender:1.4.0'

runtimeOnly 'com.mysql:mysql-connector-j'
runtimeOnly 'com.h2database:h2'

annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
Expand Down
13 changes: 12 additions & 1 deletion src/docs/asciidoc/block.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,15 @@ include::{snippets}/block/findById/path-parameters.adoc[]
==== 응답

include::{snippets}/block/findById/http-response.adoc[]
include::{snippets}/block/findById/response-fields.adoc[]
include::{snippets}/block/findById/response-fields.adoc[]

=== 블록 삭제

==== 요청

include::{snippets}/block/delete/http-request.adoc[]
include::{snippets}/block/delete/path-parameters.adoc[]

==== 응답

include::{snippets}/block/delete/http-response.adoc[]
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand Down Expand Up @@ -50,6 +51,12 @@ public RspTemplate<BlockListResDto> findByBlockWithProgress(@RequestParam(name =
blockService.findByBlockWithProgress(progress, PageRequest.of(page, size)));
}

@DeleteMapping("/{blockId}")
public RspTemplate<Void> delete(@PathVariable(name = "blockId") Long blockId) {
blockService.delete(blockId);
return new RspTemplate<>(HttpStatus.OK, "블록 삭제");
}

@GetMapping("/{blockId}")
public RspTemplate<BlockInfoResDto> findById(@PathVariable(name = "blockId") Long blockId) {
return new RspTemplate<>(HttpStatus.OK, "블록 상세보기", blockService.findById(blockId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,15 @@ public BlockInfoResDto findById(Long blockId) {
return BlockInfoResDto.of(block, member);
}

// 블록 삭제 (논리 삭제)
// 블록 삭제 유무 업데이트 (논리 삭제)
@Transactional
public void delete(Long blockId) {
// 로그인/회원가입 코드 완성 후 사용자 정보 받아올 예정
Member member = Member.builder().nickname("member").build();
Block block = blockRepository.findById(blockId).orElseThrow(BlockNotFoundException::new);

block.statusUpdate();
}

private Progress parseProgress(String progressString) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,8 @@ public void progressUpdate(Progress progress) {
this.progress = progress;
}

public void statusUpdate() {
this.status = (this.status == Status.A) ? Status.D : Status.A;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.transaction.annotation.Transactional;
import shop.kkeujeok.kkeujeokbackend.block.domain.Block;
import shop.kkeujeok.kkeujeokbackend.block.domain.Progress;
import shop.kkeujeok.kkeujeokbackend.global.entity.Status;

@Repository
@Transactional(readOnly = true)
Expand All @@ -31,7 +32,8 @@ public Page<Block> findByBlockWithProgress(Progress progress, Pageable pageable)

List<Block> blocks = queryFactory
.selectFrom(block)
.where(block.progress.eq(progress))
.where(block.progress.eq(progress)
.and(block.status.eq(Status.A)))
.orderBy(block.id.desc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doNothing;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.delete;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.patch;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
Expand Down Expand Up @@ -196,6 +198,27 @@ void setUp() {
.andExpect(status().isBadRequest());
}

@DisplayName("Delete 블록을 논리적으로 삭제합니다.")
@Test
void 블록_삭제() throws Exception {
// given
Long blockId = 1L;
doNothing().when(blockService).delete(anyLong());

// when & then
mockMvc.perform(delete("/api/blocks/{blockId}", blockId)
.accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andDo(document("block/delete",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint()),
pathParameters(
parameterWithName("blockId").description("블록 ID")
)
))
.andExpect(status().isOk());
}

@DisplayName("GET 블록을 상태별로 전체 조회합니다.")
@Test
void 블록_상태_전체_조회() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import shop.kkeujeok.kkeujeokbackend.block.domain.Progress;
import shop.kkeujeok.kkeujeokbackend.block.domain.repository.BlockRepository;
import shop.kkeujeok.kkeujeokbackend.block.exception.InvalidProgressException;
import shop.kkeujeok.kkeujeokbackend.global.entity.Status;

@ExtendWith(MockitoExtension.class)
class BlockServiceTest {
Expand All @@ -33,6 +34,7 @@ class BlockServiceTest {
private BlockService blockService;

private Block block;
private Block deleteBlock;
private BlockSaveReqDto blockSaveReqDto;
private BlockUpdateReqDto blockUpdateReqDto;

Expand All @@ -46,6 +48,14 @@ void setUp() {
.progress(blockSaveReqDto.progress())
.deadLine(blockSaveReqDto.deadLine())
.build();

deleteBlock = Block.builder()
.title(blockSaveReqDto.title())
.contents(blockSaveReqDto.contents())
.progress(blockSaveReqDto.progress())
.deadLine(blockSaveReqDto.deadLine())
.build();
deleteBlock.statusUpdate();
}

@DisplayName("블록을 저장합니다.")
Expand Down Expand Up @@ -135,6 +145,38 @@ void setUp() {
.isInstanceOf(InvalidProgressException.class);
}

@DisplayName("블록을 삭제 합니다.")
@Test
void 블록_삭제() {
// given
Long blockId = 1L;
when(blockRepository.findById(blockId)).thenReturn(Optional.of(block));

// when
blockService.delete(blockId);

// then
assertAll(() -> {
assertThat(block.getStatus()).isEqualTo(Status.D);
});
}

@DisplayName("삭제되었던 블록을 복구합니다.")
@Test
void 블록_복구() {
// given
Long blockId = 1L;
when(blockRepository.findById(blockId)).thenReturn(Optional.of(deleteBlock));

// when
blockService.delete(blockId);

// then
assertAll(() -> {
assertThat(deleteBlock.getStatus()).isEqualTo(Status.A);
});
}

@DisplayName("블록을 상세 봅니다.")
@Test
void 블록_상세보기() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package shop.kkeujeok.kkeujeokbackend.block.domain;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import shop.kkeujeok.kkeujeokbackend.global.entity.Status;

class BlockTest {
private String title;
private String contents;
private String deadLine;

private Block block;

@BeforeEach
void setUp() {
title = "title";
contents = "contents";
deadLine = "2024.07.27 22:34";

block = Block.builder()
.title(title)
.contents(contents)
.progress(Progress.NOT_STARTED)
.deadLine(deadLine)
.build();
}

@DisplayName("블록의 모든 값을 수정합니다.")
@Test
void 블록_수정() {
// given
String updateTitle = "updateTitle";
String updateContents = "updateContents";
String updateDeadLine = "updateDeadLine";

// when
block.update(updateTitle, updateContents, updateDeadLine);

// then
assertAll(() -> {
assertThat(block.getTitle()).isEqualTo(updateTitle);
assertThat(block.getContents()).isEqualTo(updateContents);
assertThat(block.getDeadLine()).isEqualTo(updateDeadLine);
});
}

@DisplayName("블록의 제목만 수정합니다.")
@Test
void 블록_제목_수정() {
// given
String updateTitle = "updateTitle";

// when
block.update(updateTitle, contents, deadLine);

// then
assertAll(() -> {
assertThat(block.getTitle()).isEqualTo(updateTitle);
assertThat(block.getContents()).isEqualTo(contents);
assertThat(block.getDeadLine()).isEqualTo(deadLine);
});
}

@DisplayName("블록의 내용만 수정합니다.")
@Test
void 블록_내용_수정() {
// given
String updateContents = "updateContents";

// when
block.update(title, updateContents, deadLine);

// then
assertAll(() -> {
assertThat(block.getTitle()).isEqualTo(title);
assertThat(block.getContents()).isEqualTo(updateContents);
assertThat(block.getDeadLine()).isEqualTo(deadLine);
});
}

@DisplayName("블록의 마감 기한만 수정합니다.")
@Test
void 블록_마감_기한_수정() {
// given
String updateDeadLine = "2024.07.28 22:34";

// when
block.update(title, contents, updateDeadLine);

// then
assertAll(() -> {
assertThat(block.getTitle()).isEqualTo(title);
assertThat(block.getContents()).isEqualTo(contents);
assertThat(block.getDeadLine()).isEqualTo(updateDeadLine);
});
}

@DisplayName("블록의 진행 상태를 수정합니다.")
@Test
void 블록_진행_상태_수정() {
// given
Progress progress = Progress.IN_PROGRESS;

// when
block.progressUpdate(progress);

// then
assertThat(block.getProgress()).isEqualTo(progress);
}

@DisplayName("블록의 논리 삭제 상태를 수정합니다.")
@Test
void 블록_논리_삭제_수정() {
// given
assertThat(block.getStatus()).isEqualTo(Status.A);

// when
block.statusUpdate();

// then
assertThat(block.getStatus()).isEqualTo(Status.D);

// when
block.statusUpdate();

// then
assertThat(block.getStatus()).isEqualTo(Status.A);
}

}
Loading

0 comments on commit bef8958

Please sign in to comment.