Skip to content

Commit b2c2354

Browse files
authored
Merge pull request #9 from Cotatus/feat/8-emotional-index-updown
Feat: 감성 지수 변화 방향 조회 API 구현
2 parents d9e8132 + 0dce732 commit b2c2354

3 files changed

Lines changed: 26 additions & 0 deletions

File tree

src/main/java/com/server/cotatus/controller/EmotionalIndexController.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import org.springframework.http.ResponseEntity;
99
import org.springframework.web.bind.annotation.*;
1010

11+
import java.util.Map;
12+
1113
@RestController
1214
@RequestMapping("/api/emotional-index")
1315
@RequiredArgsConstructor
@@ -28,4 +30,11 @@ public ResponseEntity<Object> getEmotionalIndices(
2830

2931
return ResponseEntity.ok(response);
3032
}
33+
34+
@GetMapping("/change-directions")
35+
@Operation(summary = "감성 지수 변화 방향 조회", description = "각 코인별 최근 감성 지수의 변화 방향(상승/하락/유지)을 조회합니다.")
36+
public ResponseEntity<Map<String, String>> getChangeDirections() {
37+
Map<String, String> response = emotionalIndexService.getChangeDirections();
38+
return ResponseEntity.ok(response);
39+
}
3140
}

src/main/java/com/server/cotatus/repository/EmotionalIndexRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
import org.springframework.stereotype.Repository;
77

88
import java.util.List;
9+
import java.util.Optional;
910

1011
@Repository
1112
public interface EmotionalIndexRepository extends JpaRepository<EmotionalIndex, Long> {
1213
// 특정 코인의 최근 9개
1314
List<EmotionalIndex> findTop9ByCoinOrderByCreatedAtDesc(Coin coin);
15+
16+
// 특정 코인의 최근 1개
17+
Optional<EmotionalIndex> findTop1ByCoinOrderByCreatedAtDesc(Coin coin);
1418
}

src/main/java/com/server/cotatus/service/EmotionalIndexService.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,17 @@ public List<EmotionalIndexDataPoint> getEmotionalIndices(String type) {
7474
.build())
7575
.collect(Collectors.toList());
7676
}
77+
78+
public Map<String, String> getChangeDirections() {
79+
List<Coin> coins = coinRepository.findAll();
80+
Map<String, String> result = new LinkedHashMap<>();
81+
82+
for (Coin coin : coins) {
83+
String coinName = COIN_NAME_MAP.getOrDefault(coin.getId(), coin.getName());
84+
emotionalIndexRepository.findTop1ByCoinOrderByCreatedAtDesc(coin)
85+
.ifPresent(index -> result.put(coinName, index.getChangeDirection().name()));
86+
}
87+
88+
return result;
89+
}
7790
}

0 commit comments

Comments
 (0)