Skip to content

Commit

Permalink
[BE] 체크리스트 목록 조회 시 지하철 json 형태를 변경한다. (#896)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsulocalize authored Oct 24, 2024
1 parent 5cece98 commit c507458
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.bang_ggood.checklist.dto.request.ChecklistRequest;
import com.bang_ggood.checklist.dto.request.ChecklistRequestV1;
import com.bang_ggood.checklist.dto.response.ChecklistsPreviewResponse;
import com.bang_ggood.checklist.dto.response.ChecklistsPreviewResponseV1;
import com.bang_ggood.checklist.dto.response.SelectedChecklistResponse;
import com.bang_ggood.checklist.dto.response.SelectedChecklistResponseV1;
import com.bang_ggood.checklist.service.ChecklistManageService;
Expand Down Expand Up @@ -58,6 +59,11 @@ public ResponseEntity<ChecklistsPreviewResponse> readChecklistsPreview(@UserPrin
return ResponseEntity.ok(checklistManageService.readAllChecklistsPreview(user));
}

@GetMapping("/v1/checklists")
public ResponseEntity<ChecklistsPreviewResponseV1> readChecklistsPreviewV1(@UserPrincipal User user) {
return ResponseEntity.ok(checklistManageService.readAllChecklistsPreviewV1(user));
}

@GetMapping("/checklists/like")
public ResponseEntity<ChecklistsPreviewResponse> readLikedChecklistsPreview(@AuthRequiredPrincipal User user) {
return ResponseEntity.ok(checklistManageService.readLikedChecklistsPreview(user));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.bang_ggood.checklist.dto.response;

import com.bang_ggood.checklist.domain.Checklist;
import com.bang_ggood.station.dto.response.SubwayStationResponse;
import java.time.LocalDateTime;

public record ChecklistPreviewResponseV1(
Long checklistId, String roomName, String address, String buildingName,
SubwayStationResponse station, Integer walkingTime,
Integer deposit, Integer rent, LocalDateTime createdAt,
String summary, boolean isLiked) {

public static ChecklistPreviewResponseV1 of(Checklist checklist, SubwayStationResponse station, boolean isLiked) {
return new ChecklistPreviewResponseV1(
checklist.getId(),
checklist.getRoomName(),
checklist.getRoomAddress(),
checklist.getRoomBuildingName(),
station,
checklist.getRoomWalkingTime(),
checklist.getDeposit(),
checklist.getRent(),
checklist.getCreatedAt(),
checklist.getSummary(),
isLiked);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.bang_ggood.checklist.dto.response;

import java.util.List;

public record ChecklistsPreviewResponseV1(List<ChecklistPreviewResponseV1> checklists) {

public static ChecklistsPreviewResponseV1 from(List<ChecklistPreviewResponseV1> checklists) {
return new ChecklistsPreviewResponseV1(checklists);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import com.bang_ggood.checklist.dto.request.ChecklistRequest;
import com.bang_ggood.checklist.dto.request.ChecklistRequestV1;
import com.bang_ggood.checklist.dto.response.ChecklistPreviewResponse;
import com.bang_ggood.checklist.dto.response.ChecklistPreviewResponseV1;
import com.bang_ggood.checklist.dto.response.ChecklistsPreviewResponse;
import com.bang_ggood.checklist.dto.response.ChecklistsPreviewResponseV1;
import com.bang_ggood.checklist.dto.response.SelectedChecklistResponse;
import com.bang_ggood.checklist.dto.response.SelectedChecklistResponseV1;
import com.bang_ggood.like.service.ChecklistLikeService;
Expand Down Expand Up @@ -210,6 +212,32 @@ private ChecklistPreviewResponse mapToChecklistPreview(Checklist checklist) {
return ChecklistPreviewResponse.of(checklist, isLiked);
}

@Transactional(readOnly = true)
public ChecklistsPreviewResponseV1 readAllChecklistsPreviewV1(User user) {
List<Checklist> checklists = checklistService.readAllChecklistsOrderByLatest(user);
List<ChecklistPreviewResponseV1> responses = checklists.stream()
.map(this::mapToChecklistPreviewV1)
.toList();

return ChecklistsPreviewResponseV1.from(responses);
}

private ChecklistPreviewResponseV1 mapToChecklistPreviewV1(Checklist checklist) {
boolean isLiked = checklistLikeService.isLikedChecklist(checklist);
SubwayStationResponse stationResponse = readNearestStation(checklist);
return ChecklistPreviewResponseV1.of(checklist, stationResponse, isLiked);
}

private SubwayStationResponse readNearestStation(Checklist checklist) {
List<ChecklistStation> checklistStations = checklistStationService.readChecklistStationsByChecklist(checklist);
List<SubwayStationResponse> stationResponses = checklistStations.stream()
.map(SubwayStationResponse::from)
.toList();
SubwayStationResponses subwayStationResponses = SubwayStationResponses.from(stationResponses);

return subwayStationResponses.getNearestStation();
}

@Transactional
public void updateChecklistById(User user, Long checklistId, ChecklistRequest checklistRequest) {
Checklist checklist = checklistService.readChecklist(user, checklistId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,11 @@ private static List<SubwayStationResponse> mergeTransferStations(List<SubwayStat
.limit(REQUESTED_STATION_NUMBER)
.toList();
}

public SubwayStationResponse getNearestStation() {
if (stations.isEmpty()) {
return null;
}
return stations.get(0);
}
}

0 comments on commit c507458

Please sign in to comment.