Skip to content

Commit

Permalink
[BE] 체크리스트 리스트 조회 API를 리팩토링한다. (#618)
Browse files Browse the repository at this point in the history
  • Loading branch information
JINU-CHANG authored Sep 24, 2024
1 parent bad50bc commit dffdf0f
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.bang_ggood.auth.config.AuthPrincipal;
import com.bang_ggood.checklist.dto.request.ChecklistRequest;
import com.bang_ggood.checklist.dto.response.SelectedChecklistResponse;
import com.bang_ggood.checklist.dto.response.ChecklistsPreviewResponse;
import com.bang_ggood.checklist.dto.response.SelectedChecklistResponse;
import com.bang_ggood.checklist.service.ChecklistManageService;
import com.bang_ggood.checklist.service.ChecklistService;
import com.bang_ggood.user.domain.User;
Expand Down Expand Up @@ -44,7 +44,7 @@ public ResponseEntity<SelectedChecklistResponse> readChecklistById(@AuthPrincipa

@GetMapping("/checklists")
public ResponseEntity<ChecklistsPreviewResponse> readChecklistsPreview(@AuthPrincipal User user) {
return ResponseEntity.ok(checklistService.readChecklistsPreview(user));
return ResponseEntity.ok(checklistManageService.readAllChecklistsPreview(user));
}

@GetMapping("/checklists/like")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@
import java.util.List;

public record ChecklistsPreviewResponse(List<ChecklistPreviewResponse> checklists) {

public static ChecklistsPreviewResponse from(List<ChecklistPreviewResponse> checklists) {
return new ChecklistsPreviewResponse(checklists);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import com.bang_ggood.checklist.domain.Checklist;
import com.bang_ggood.checklist.dto.request.ChecklistRequest;
import com.bang_ggood.checklist.dto.response.SelectedChecklistResponse;
import com.bang_ggood.checklist.dto.response.ChecklistPreviewResponse;
import com.bang_ggood.checklist.dto.response.ChecklistsPreviewResponse;
import com.bang_ggood.checklist.dto.response.SelectedChecklistResponse;
import com.bang_ggood.like.service.ChecklistLikeService;
import com.bang_ggood.maintenance.domain.ChecklistMaintenance;
import com.bang_ggood.maintenance.domain.MaintenanceItem;
import com.bang_ggood.maintenance.service.ChecklistMaintenanceService;
Expand Down Expand Up @@ -36,16 +37,19 @@ public class ChecklistManageService {
private final ChecklistOptionService checklistOptionService;
private final ChecklistQuestionService checklistQuestionService;
private final ChecklistMaintenanceService checklistMaintenanceService;
private final ChecklistLikeService checklistLikeService;

public ChecklistManageService(RoomService roomService, ChecklistService checklistService,
ChecklistOptionService checklistOptionService,
ChecklistQuestionService checklistQuestionService,
ChecklistMaintenanceService checklistMaintenanceService) {
ChecklistMaintenanceService checklistMaintenanceService,
ChecklistLikeService checklistLikeService) {
this.roomService = roomService;
this.checklistService = checklistService;
this.checklistOptionService = checklistOptionService;
this.checklistQuestionService = checklistQuestionService;
this.checklistMaintenanceService = checklistMaintenanceService;
this.checklistLikeService = checklistLikeService;
}

@Transactional
Expand Down Expand Up @@ -133,13 +137,27 @@ public ChecklistsPreviewResponse readLikedChecklistsPreview(User user) {
List<Checklist> likedChecklists = checklistService.readLikedChecklistsPreview(user);
List<ChecklistPreviewResponse> responses = mapToChecklistPreviewResponses(
likedChecklists);
return new ChecklistsPreviewResponse(responses);
return ChecklistsPreviewResponse.from(responses);
}

private List<ChecklistPreviewResponse> mapToChecklistPreviewResponses(
List<Checklist> likedChecklists) {
private List<ChecklistPreviewResponse> mapToChecklistPreviewResponses(List<Checklist> likedChecklists) {
return likedChecklists.stream()
.map(checklist -> ChecklistPreviewResponse.of(checklist, true))
.toList();
}

@Transactional(readOnly = true)
public ChecklistsPreviewResponse readAllChecklistsPreview(User user) {
List<Checklist> checklists = checklistService.readAllChecklistsOrderByLatest(user);

List<ChecklistPreviewResponse> responses = checklists.stream()
.map(this::mapToChecklistPreview)
.toList();
return ChecklistsPreviewResponse.from(responses);
}

private ChecklistPreviewResponse mapToChecklistPreview(Checklist checklist) {
boolean isLiked = checklistLikeService.isLikedChecklist(checklist);
return ChecklistPreviewResponse.of(checklist, isLiked);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import com.bang_ggood.checklist.domain.Checklist;
import com.bang_ggood.checklist.dto.request.ChecklistRequest;
import com.bang_ggood.checklist.dto.response.ChecklistPreviewResponse;
import com.bang_ggood.checklist.dto.response.ChecklistsPreviewResponse;
import com.bang_ggood.checklist.repository.ChecklistRepository;
import com.bang_ggood.global.exception.BangggoodException;
import com.bang_ggood.global.exception.ExceptionCode;
Expand Down Expand Up @@ -72,19 +70,9 @@ private void validateChecklistOwnership(User user, Checklist checklist) {
}
}

@Transactional
public ChecklistsPreviewResponse readChecklistsPreview(User user) {
List<Checklist> checklists = checklistRepository.findAllByUserOrderByLatest(user);
List<ChecklistPreviewResponse> responses = checklists.stream()
.map(this::getChecklistPreview)
.toList();

return new ChecklistsPreviewResponse(responses);
}

private ChecklistPreviewResponse getChecklistPreview(Checklist checklist) {
boolean isLiked = checklistLikeRepository.existsByChecklist(checklist);
return ChecklistPreviewResponse.of(checklist, isLiked);
@Transactional(readOnly = true)
public List<Checklist> readAllChecklistsOrderByLatest(User user) {
return checklistRepository.findAllByUserOrderByLatest(user);
}

@Transactional(readOnly = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ private void validateChecklistAlreadyLiked(Checklist checklist) {
}
}

@Transactional
public boolean isLikedChecklist(Checklist checklist) {
return checklistLikeRepository.existsByChecklist(checklist);
}

@Transactional
public void deleteLike(User user, Checklist checklist) {
validateChecklistOwnership(user, checklist);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.bang_ggood.IntegrationTestSupport;
import com.bang_ggood.auth.dto.request.OauthLoginRequest;
import com.bang_ggood.checklist.dto.response.ChecklistsPreviewResponse;
import com.bang_ggood.checklist.service.ChecklistService;
import com.bang_ggood.checklist.service.ChecklistManageService;
import com.bang_ggood.global.exception.BangggoodException;
import com.bang_ggood.global.exception.ExceptionCode;
import com.bang_ggood.question.domain.Question;
Expand Down Expand Up @@ -34,7 +34,7 @@ class AuthServiceTest extends IntegrationTestSupport {
@Autowired
private AuthService authService;
@Autowired
private ChecklistService checklistService;
private ChecklistManageService checklistManageService;
@Autowired
private QuestionManageService questionManageService;
@Autowired
Expand Down Expand Up @@ -106,7 +106,7 @@ void login_default_checklist() {

// then
User user = authService.extractUser(token);
ChecklistsPreviewResponse response = checklistService.readChecklistsPreview(user);
ChecklistsPreviewResponse response = checklistManageService.readAllChecklistsPreview(user);
assertThat(response.checklists()).hasSize(1);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.bang_ggood.like.domain.ChecklistLike;
import com.bang_ggood.maintenance.domain.ChecklistMaintenance;
import com.bang_ggood.maintenance.domain.MaintenanceItem;
import com.bang_ggood.option.domain.Option;
import com.bang_ggood.question.domain.Answer;
import com.bang_ggood.question.domain.ChecklistQuestion;
import com.bang_ggood.question.domain.Question;
Expand Down Expand Up @@ -70,96 +71,104 @@ public static Checklist CHECKLIST1_WITH_USER1_ID(Room room) {
}

public static QuestionRequest QUESTION_1_CREATE_REQUEST() {
return new QuestionRequest(1, "GOOD");
return new QuestionRequest(Question.BATHROOM_1.getId(), "GOOD");
}

public static QuestionRequest QUESTION_2_CREATE_REQUEST() {
return new QuestionRequest(2, "GOOD");
return new QuestionRequest(Question.BATHROOM_2.getId(), "GOOD");
}

public static QuestionRequest QUESTION_3_CREATE_REQUEST() {
return new QuestionRequest(3, "BAD");
return new QuestionRequest(Question.BATHROOM_3.getId(), "BAD");
}

public static QuestionRequest QUESTION_4_CREATE_REQUEST() {
return new QuestionRequest(4, "BAD");
return new QuestionRequest(Question.BATHROOM_4.getId(), "BAD");
}

public static QuestionRequest QUESTION_5_CREATE_REQUEST() {
return new QuestionRequest(5, "GOOD");
return new QuestionRequest(Question.BATHROOM_5.getId(), "GOOD");
}

public static QuestionRequest QUESTION_5_UPDATE_REQUEST() {
return new QuestionRequest(5, "GOOD");
return new QuestionRequest(Question.BATHROOM_5.getId(), "GOOD");
}

public static QuestionRequest QUESTION_CREATE_REQUEST_NO_ID() {
return new QuestionRequest(null, "NONE");
}

public static QuestionRequest QUESTION_CREATE_REQUEST_INVALID_ID() {
return new QuestionRequest(9999, "GOOD");
return new QuestionRequest(Integer.MAX_VALUE, "GOOD");
}

public static ChecklistRequest CHECKLIST_CREATE_REQUEST() {
return new ChecklistRequest(
RoomFixture.ROOM_CREATE_REQUEST(), List.of(1, 2, 3, 5),
RoomFixture.ROOM_CREATE_REQUEST(), List.of(Option.REFRIGERATOR.getId(), Option.SINK.getId(), Option.INDUCTION.getId(), Option.SHOE_RACK.getId()),
List.of(QUESTION_1_CREATE_REQUEST(), QUESTION_2_CREATE_REQUEST(),
QUESTION_3_CREATE_REQUEST(), QUESTION_5_CREATE_REQUEST())
);
}

public static ChecklistRequest CHECKLIST_CREATE_REQUEST2() {
return new ChecklistRequest(
RoomFixture.ROOM_CREATE_REQUEST(), List.of(Option.REFRIGERATOR.getId(), Option.SINK.getId(), Option.INDUCTION.getId(), Option.CLOSET.getId()),
List.of(QUESTION_1_CREATE_REQUEST(), QUESTION_2_CREATE_REQUEST(),
QUESTION_4_CREATE_REQUEST())
);
}

public static ChecklistRequest CHECKLIST_CREATE_REQUEST_NO_ROOM_NAME() {
return new ChecklistRequest(
RoomFixture.ROOM_CREATE_REQUEST_NO_ROOM_NAME(), List.of(1, 2, 3, 5),
RoomFixture.ROOM_CREATE_REQUEST_NO_ROOM_NAME(),List.of(Option.REFRIGERATOR.getId(), Option.SINK.getId(), Option.INDUCTION.getId(), Option.SHOE_RACK.getId()),
List.of(QUESTION_1_CREATE_REQUEST(), QUESTION_2_CREATE_REQUEST(),
QUESTION_3_CREATE_REQUEST(), QUESTION_5_CREATE_REQUEST())
);
}

public static ChecklistRequest CHECKLIST_UPDATE_REQUEST_NO_ROOM_NAME() {
return new ChecklistRequest(
RoomFixture.ROOM_CREATE_REQUEST_NO_ROOM_NAME(), List.of(1, 2, 3, 4),
RoomFixture.ROOM_CREATE_REQUEST_NO_ROOM_NAME(), List.of(Option.REFRIGERATOR.getId(), Option.BED.getId(), Option.INDUCTION.getId(), Option.SHOE_RACK.getId()),
List.of(QUESTION_1_CREATE_REQUEST(), QUESTION_2_CREATE_REQUEST(),
QUESTION_3_CREATE_REQUEST(), QUESTION_5_UPDATE_REQUEST())
);
}

public static ChecklistRequest CHECKLIST_CREATE_REQUEST_NO_QUESTION_ID() {
return new ChecklistRequest(
RoomFixture.ROOM_CREATE_REQUEST(), List.of(1, 2, 3, 5),
RoomFixture.ROOM_CREATE_REQUEST(), List.of(Option.REFRIGERATOR.getId(), Option.SINK.getId(), Option.INDUCTION.getId(), Option.SHOE_RACK.getId()),
List.of(QUESTION_1_CREATE_REQUEST(), QUESTION_2_CREATE_REQUEST(),
QUESTION_3_CREATE_REQUEST(), QUESTION_CREATE_REQUEST_NO_ID())
);
}

public static ChecklistRequest CHECKLIST_UPDATE_REQUEST_NO_QUESTION_ID() {
return new ChecklistRequest(
RoomFixture.ROOM_UPDATE_REQUEST(), List.of(1, 2, 3, 4),
RoomFixture.ROOM_UPDATE_REQUEST(), List.of(Option.REFRIGERATOR.getId(), Option.BED.getId(), Option.INDUCTION.getId(), Option.SHOE_RACK.getId()),
List.of(QUESTION_1_CREATE_REQUEST(), QUESTION_2_CREATE_REQUEST(),
QUESTION_3_CREATE_REQUEST(), QUESTION_CREATE_REQUEST_NO_ID())
);
}

public static ChecklistRequest CHECKLIST_UPDATE_REQUEST_INVALID_QUESTION_ID() {
return new ChecklistRequest(
RoomFixture.ROOM_UPDATE_REQUEST(), List.of(1, 2, 3, 4),
RoomFixture.ROOM_UPDATE_REQUEST(), List.of(Option.REFRIGERATOR.getId(), Option.BED.getId(), Option.INDUCTION.getId(), Option.SHOE_RACK.getId()),
List.of(QUESTION_1_CREATE_REQUEST(), QUESTION_2_CREATE_REQUEST(),
QUESTION_3_CREATE_REQUEST(), QUESTION_CREATE_REQUEST_INVALID_ID())
);
}

public static ChecklistRequest CHECKLIST_UPDATE_REQUEST_INVALID_OPTION_ID() {
return new ChecklistRequest(
RoomFixture.ROOM_UPDATE_REQUEST(), List.of(1, 2, 4, 9999),
RoomFixture.ROOM_UPDATE_REQUEST(), List.of(Option.REFRIGERATOR.getId(), Option.SINK.getId(), Option.INDUCTION.getId(), Integer.MAX_VALUE),
List.of(QUESTION_1_CREATE_REQUEST(), QUESTION_2_CREATE_REQUEST(),
QUESTION_3_CREATE_REQUEST(), QUESTION_5_UPDATE_REQUEST())
);
}

public static ChecklistRequest CHECKLIST_CREATE_REQUEST_DUPLICATED_QUESTION_ID() {
return new ChecklistRequest(
RoomFixture.ROOM_CREATE_REQUEST(), List.of(1, 2, 3, 5),
RoomFixture.ROOM_CREATE_REQUEST(), List.of(Option.REFRIGERATOR.getId(), Option.SINK.getId(), Option.INDUCTION.getId(), Option.SHOE_RACK.getId()),
List.of(QUESTION_1_CREATE_REQUEST(), QUESTION_2_CREATE_REQUEST(),
QUESTION_3_CREATE_REQUEST(), QUESTION_3_CREATE_REQUEST())
);
Expand All @@ -168,7 +177,7 @@ public static ChecklistRequest CHECKLIST_CREATE_REQUEST_DUPLICATED_QUESTION_ID()

public static ChecklistRequest CHECKLIST_UPDATE_REQUEST_DUPLICATED_OPTION_ID() {
return new ChecklistRequest(
RoomFixture.ROOM_UPDATE_REQUEST(), List.of(1, 2, 4, 4),
RoomFixture.ROOM_UPDATE_REQUEST(), List.of(Option.REFRIGERATOR.getId(), Option.SINK.getId(), Option.SINK.getId(), Option.SHOE_RACK.getId()),
List.of(QUESTION_1_CREATE_REQUEST(), QUESTION_2_CREATE_REQUEST(),
QUESTION_3_CREATE_REQUEST(), QUESTION_5_UPDATE_REQUEST())
);
Expand All @@ -185,7 +194,7 @@ public static ChecklistRequest CHECKLIST_UPDATE_REQUEST_DIFFERENT_QUESTION_LENGT

public static ChecklistRequest CHECKLIST_UPDATE_REQUEST_DIFFERENT_QUESTION() {
return new ChecklistRequest(
RoomFixture.ROOM_UPDATE_REQUEST(), List.of(1, 2, 3, 4),
RoomFixture.ROOM_UPDATE_REQUEST(), List.of(Option.REFRIGERATOR.getId(), Option.SINK.getId(), Option.INDUCTION.getId(), Option.SHOE_RACK.getId()),
List.of(QUESTION_1_CREATE_REQUEST(), QUESTION_2_CREATE_REQUEST(),
QUESTION_3_CREATE_REQUEST(), QUESTION_4_CREATE_REQUEST())
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import com.bang_ggood.checklist.ChecklistFixture;
import com.bang_ggood.checklist.domain.Checklist;
import com.bang_ggood.checklist.dto.request.ChecklistRequest;
import com.bang_ggood.checklist.dto.response.SelectedChecklistResponse;
import com.bang_ggood.checklist.dto.response.ChecklistPreviewResponse;
import com.bang_ggood.checklist.dto.response.ChecklistsPreviewResponse;
import com.bang_ggood.checklist.dto.response.SelectedChecklistResponse;
import com.bang_ggood.checklist.repository.ChecklistRepository;
import com.bang_ggood.global.exception.BangggoodException;
import com.bang_ggood.global.exception.ExceptionCode;
Expand All @@ -19,7 +20,6 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -82,6 +82,28 @@ void readChecklistById_invalidChecklistId_exception() {
.hasMessage(ExceptionCode.CHECKLIST_NOT_FOUND.getMessage());
}

@DisplayName("체크리스트 리스트 조회 성공")
@Test
void readUserChecklistsPreview() {
// given
User user = userRepository.save(UserFixture.USER1());
ChecklistRequest checklistRequest1 = ChecklistFixture.CHECKLIST_CREATE_REQUEST();
ChecklistRequest checklistRequest2 = ChecklistFixture.CHECKLIST_CREATE_REQUEST2();

Long checklistId1 = checklistManageService.createChecklist(user, checklistRequest1);
Long checklistId2 = checklistManageService.createChecklist(user, checklistRequest2);

// when
ChecklistsPreviewResponse response = checklistManageService.readAllChecklistsPreview(user);

// then
ChecklistPreviewResponse previewResponse1 = response.checklists().get(0);
ChecklistPreviewResponse previewResponse2 = response.checklists().get(1);

assertThat(previewResponse1.checklistId()).isEqualTo(checklistId2); // 최신순으로 조회
assertThat(previewResponse2.checklistId()).isEqualTo(checklistId1);
}

@DisplayName("좋아요된 체크리스트 리스트 조회 성공")
@Test
void readLikedChecklistsPreview() {
Expand Down
Loading

0 comments on commit dffdf0f

Please sign in to comment.