Skip to content

Commit

Permalink
refactor: BE 코드리뷰 반영
Browse files Browse the repository at this point in the history
reviewer : somsom13
  • Loading branch information
splitCoding committed Jul 29, 2023
1 parent 6a1de24 commit 311e9b0
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package shook.shook.part.application;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import shook.shook.part.application.dto.PartCommentRegisterRequest;
import shook.shook.part.application.dto.PartCommentsResponse;
import shook.shook.part.application.dto.PartCommentResponse;
import shook.shook.part.domain.Part;
import shook.shook.part.domain.PartComment;
import shook.shook.part.domain.repository.PartCommentRepository;
import shook.shook.part.domain.repository.PartRepository;
import shook.shook.part.exception.PartException;
import shook.shook.part.exception.PartException.PartNotExistException;

@RequiredArgsConstructor
Expand All @@ -29,10 +31,12 @@ public void register(final Long partId, final PartCommentRegisterRequest request
partCommentRepository.save(partComment);
}

public PartCommentsResponse findPartReplies(final Long partId) {
public List<PartCommentResponse> findPartReplies(final Long partId) {
final Part part = partRepository.findById(partId)
.orElseThrow(PartNotExistException::new);
.orElseThrow(PartException.PartNotExistException::new);

return PartCommentsResponse.of(part.getCommentsInRecentOrder());
return part.getCommentsInRecentOrder().stream()
.map(PartCommentResponse::from)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public List<PartComment> getComments() {

public List<PartComment> getCommentsInRecentOrder() {
return comments.stream()
.sorted(Comparator.comparing((PartComment::getCreatedAt)))
.sorted(Comparator.comparing(PartComment::getCreatedAt).reversed())
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package shook.shook.part.ui;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -11,10 +12,10 @@
import org.springframework.web.bind.annotation.RestController;
import shook.shook.part.application.PartCommentService;
import shook.shook.part.application.dto.PartCommentRegisterRequest;
import shook.shook.part.application.dto.PartCommentsResponse;
import shook.shook.part.application.dto.PartCommentResponse;

@RequiredArgsConstructor
@RequestMapping("/songs/{song_id}/parts/{part_id}/comment")
@RequestMapping("/songs/{song_id}/parts/{part_id}/comments")
@RestController
public class PartCommentController {

Expand All @@ -31,11 +32,9 @@ public ResponseEntity<Void> registerPartComment(
}

@GetMapping
public ResponseEntity<PartCommentsResponse> findPartReplies(
public ResponseEntity<List<PartCommentResponse>> findPartReplies(
@PathVariable(name = "part_id") final Long partId
) {
final PartCommentsResponse partReplies = partCommentService.findPartReplies(partId);

return ResponseEntity.ok().body(partReplies);
return ResponseEntity.ok().body(partCommentService.findPartReplies(partId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import shook.shook.part.application.dto.PartCommentRegisterRequest;
import shook.shook.part.application.dto.PartCommentsResponse;
import shook.shook.part.application.dto.PartCommentResponse;
import shook.shook.part.domain.Part;
import shook.shook.part.domain.PartComment;
import shook.shook.part.domain.PartLength;
Expand All @@ -35,8 +35,8 @@ class PartCommentServiceTest extends UsingJpaTest {

@BeforeEach
void setUp() {
Song SAVED_SONG = songRepository.save(new Song("제목", "비디오URL", "가수", 30));
SAVED_PART = partRepository.save(Part.forSave(3, PartLength.SHORT, SAVED_SONG));
final Song savedSong = songRepository.save(new Song("제목", "비디오URL", "가수", 30));
SAVED_PART = partRepository.save(Part.forSave(3, PartLength.SHORT, savedSong));
partCommentService = new PartCommentService(partRepository, partCommentRepository);
}

Expand Down Expand Up @@ -65,11 +65,10 @@ void findPartReplies() {

//when
saveAndClearEntityManager();
final PartCommentsResponse partReplies = partCommentService.findPartReplies(
final List<PartCommentResponse> partReplies = partCommentService.findPartReplies(
SAVED_PART.getId());

//then
assertThat(partReplies.getPartReplies()).usingRecursiveComparison()
.isEqualTo(List.of(early, late));
assertThat(partReplies).usingRecursiveComparison().isEqualTo(List.of(early, late));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,6 @@ void getRepliesInRecentOrder() {
final List<PartComment> repliesInRecentOrder = comments.getCommentsInRecentOrder();

//then
assertThat(repliesInRecentOrder).usingRecursiveComparison().isEqualTo(List.of(early, late));
assertThat(repliesInRecentOrder).usingRecursiveComparison().isEqualTo(List.of(late, early));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void registerPartReply() {
.contentType(ContentType.JSON)
.body(request)
.when().log().all()
.post("/songs/" + song.getId() + "/parts/" + part.getId() + "/comment")
.post("/songs/" + song.getId() + "/parts/" + part.getId() + "/comments")
.then().log().all()
.statusCode(HttpStatus.CREATED.value());
}
Expand All @@ -72,7 +72,7 @@ void findPartReplies() {
//when
final PartCommentsResponse response = RestAssured.given().log().all()
.when().log().all()
.get("/songs/" + song.getId() + "/parts/" + part.getId() + "/comment")
.get("/songs/" + song.getId() + "/parts/" + part.getId() + "/comments")
.then().log().all()
.statusCode(HttpStatus.OK.value())
.extract().body().as(PartCommentsResponse.class);
Expand Down

0 comments on commit 311e9b0

Please sign in to comment.