Skip to content

Commit

Permalink
feat: (#95) 해당 게시글 조기 마감 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
tjdtls690 committed Jul 20, 2023
1 parent 24057e5 commit e65bfab
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down Expand Up @@ -62,7 +63,7 @@ public ResponseEntity<Void> save(
}

@Operation(summary = "게시글 투표 선택지 통계 조회", description = "게시글 특정 투표 선택지에 대한 통계를 조회한다.")
@ApiResponses({
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "게시글 투표 선택지 통계 조회 성공"),
@ApiResponse(responseCode = "400", description = "게시글 투표 옵션이 게시글에 속하지 않아 조회 실패"),
@ApiResponse(responseCode = "404", description = "존재하지 않는 게시글이거나 게시글 투표 옵션")
Expand All @@ -76,5 +77,17 @@ public ResponseEntity<VoteOptionStatisticsResponse> getVoteOptionStatistics(
return ResponseEntity.ok(response);
}

@Operation(summary = "게시글 조기 마감", description = "게시글을 조기 마감한다.")
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "게시물이 조기 마감 되었습니다."),
@ApiResponse(responseCode = "400", description = "잘못된 입력입니다."),
@ApiResponse(responseCode = "500", description = "인터넷 서버 오류입니다.")
})
@PatchMapping("/{id}/close")
public ResponseEntity<Void> postClosedEarly(@PathVariable final Long id) {
postService.postClosedEarlyById(id);
return ResponseEntity.ok().build();
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,7 @@ private void validatePostOption(PostOption postOption) {
}
}

public void closedEarly() {
this.deadline = LocalDateTime.now();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,11 @@ private String groupAgeRange(final String ageRange) {
return ageRange;
}

@Transactional
public void postClosedEarlyById(final Long id) {
final Post post = postRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("해당 게시글은 존재하지 않습니다."));

post.closedEarly();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,20 @@ void getVoteOptionStatistics() {
assertThat(result).usingRecursiveComparison().isEqualTo(response);
}

@Test
@DisplayName("게시글을 조기 마감 합니다")
void postClosedEarly() {
// given
long postId = 1L;

// when
ExtractableResponse<MockMvcResponse> response = RestAssuredMockMvc.given().log().all()
.when().patch("/posts/" + postId + "/close")
.then().log().all()
.extract();

// then
assertThat(response.statusCode()).isEqualTo(HttpStatus.OK.value());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ class PostTest {
@DisplayName("여러 Category를 전달하면 Post와 매핑되어 PostOptions를 생성한다")
void mapCategories() {
// given
final Post post = Post.builder().build();
final Category category1 = Category.builder().build();
final Category category2 = Category.builder().build();
Post post = Post.builder().build();
Category category1 = Category.builder().build();
Category category2 = Category.builder().build();

final List<Category> categories = List.of(category1, category2);
List<Category> categories = List.of(category1, category2);

// when
post.mapCategories(categories);

// then
final PostCategories actualPostCategories = post.getPostCategories();
PostCategories actualPostCategories = post.getPostCategories();
assertThat(actualPostCategories.getPostCategories()).hasSize(2);
}

Expand Down Expand Up @@ -92,4 +92,21 @@ void isClosed() {
);
}

@Test
@DisplayName("해당 게시글을 조기 마감 합니다.")
void closedEarly() {
// given
LocalDateTime deadline = LocalDateTime.of(2100, 1, 1, 0, 0);
Post post = Post.builder()
.deadline(deadline)
.build();


// when
post.closedEarly();

// then
assertThat(post.getDeadline()).isBefore(deadline);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,30 @@ void getVoteOptionStatistics() {
}
}

@Test
@DisplayName("해당 게시글을 조기 마감 합니다")
void postClosedEarlyById() {
// given
Member writer = memberRepository.save(MemberFixtures.MALE_30);
LocalDateTime oldDeadline = LocalDateTime.of(2100, 7, 12, 0, 0);
Post post = postRepository.save(
Post.builder()
.member(writer)
.postBody(PostBody.builder().title("title").content("content").build())
.deadline(oldDeadline)
.build()
);

Post findedPost = postRepository.findById(post.getId()).get();

// when
postService.postClosedEarlyById(post.getId());

// then
assertAll(
() -> assertThat(findedPost.getId()).isEqualTo(post.getId()),
() -> assertThat(findedPost.getDeadline()).isBefore(oldDeadline)
);
}

}

0 comments on commit e65bfab

Please sign in to comment.