Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@

public class DecisionResponse {

public record DecisionFlatRow(
Long decisionId,
String meetingName,
Long applicationId,
String applicationName
) {
}

@Getter
@NoArgsConstructor
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.whylog.server.domain.team.repository;

import com.whylog.server.domain.decision.entity.Decision;
import com.whylog.server.domain.decision.dto.DecisionResponse;
import com.whylog.server.domain.team.entity.Team;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -13,12 +13,18 @@ public interface TeamRepository extends JpaRepository<Team, Long> {
Boolean existsByName(String teamName);

@Query("""
SELECT DISTINCT d
SELECT new com.whylog.server.domain.decision.dto.DecisionResponse$DecisionFlatRow(
d.id,
m.name,
a.id,
a.name
)
FROM Decision d
JOIN FETCH d.meeting m
LEFT JOIN FETCH d.applications
JOIN d.meeting m
LEFT JOIN d.applications a
WHERE m.team.id = :teamId
ORDER BY d.id
""")
List<Decision> findDecisions(@Param("teamId") Long teamId);
List<DecisionResponse.DecisionFlatRow> findDecisionRows(@Param("teamId") Long teamId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import com.whylog.server.domain.decision.dto.ApplicationResponse;
import com.whylog.server.domain.decision.dto.DecisionResponse;
import com.whylog.server.domain.decision.entity.Decision;
import com.whylog.server.domain.team.repository.TeamRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
Expand All @@ -19,22 +21,40 @@ public class TeamQueryService {
@Transactional(readOnly = true)
public List<DecisionResponse.DecisionListDTO> decisions( Long teamId ){

List<Decision> decisions = teamRepository.findDecisions(teamId);

return decisions.stream()
.map(d -> DecisionResponse.DecisionListDTO.builder()
.decisionId(d.getId())
.name(d.getMeeting().getName())
.applications(
d.getApplications().stream()
.map(application -> ApplicationResponse.ApplicationDTO.builder()
.applicationId(application.getId())
.name(application.getName())
.build())
.toList()
)
.build()
).toList();
List<DecisionResponse.DecisionFlatRow> rows = teamRepository.findDecisionRows(teamId);
Map<Long, DecisionAccumulator> decisions = new LinkedHashMap<>();

for (DecisionResponse.DecisionFlatRow row : rows) {
DecisionAccumulator decision = decisions.computeIfAbsent(
row.decisionId(),
decisionId -> new DecisionAccumulator(decisionId, row.meetingName())
);

if (row.applicationId() != null) {
decision.applications().add(ApplicationResponse.ApplicationDTO.builder()
.applicationId(row.applicationId())
.name(row.applicationName())
.build());
}
}

return decisions.values().stream()
.map(decision -> DecisionResponse.DecisionListDTO.builder()
.decisionId(decision.decisionId())
.name(decision.meetingName())
.applications(decision.applications())
.build())
.toList();
}

private record DecisionAccumulator(
Long decisionId,
String meetingName,
List<ApplicationResponse.ApplicationDTO> applications
) {
private DecisionAccumulator(Long decisionId, String meetingName) {
this(decisionId, meetingName, new ArrayList<>());
}
}

}
Loading