Skip to content

Commit 1ac28b1

Browse files
authored
Merge pull request #106 from kookmin-sw/refactor/104
[Backend] refactor: 코드 스타일 통합 및 리팩터링
2 parents 137b76c + 792922f commit 1ac28b1

16 files changed

+169
-221
lines changed

backend/src/main/java/com/example/backend/analysis/controller/CaseStatsController.java

Lines changed: 26 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import java.util.Collections;
1111
import java.util.List;
1212
import java.util.Map;
13-
import java.util.NoSuchElementException;
1413

1514
@RestController
1615
@RequestMapping("/api/v1/stats")
@@ -22,33 +21,26 @@ public class CaseStatsController {
2221
// 개요 조회
2322
@GetMapping("/overview")
2423
public ResponseEntity<?> getOverview(HttpSession session) {
25-
try {
26-
CaseStatsOverviewResponse response = caseStatsService.getOverview(session);
27-
return ResponseEntity.ok(response);
28-
} catch (IllegalStateException e) {
29-
return ResponseEntity.status(401).body(Collections.singletonMap("message", e.getMessage()));
30-
} catch (NoSuchElementException e) {
31-
return ResponseEntity.status(404).body(Collections.singletonMap("message", e.getMessage()));
32-
} catch (Exception e) {
33-
return ResponseEntity.status(500).body(Collections.singletonMap("message", "내부 서버 오류 발생"));
24+
// 결과가 없으면 404, 있으면 200
25+
CaseStatsOverviewResponse response = caseStatsService.getOverview(session);
26+
if (response == null) {
27+
return ResponseEntity.status(404)
28+
.body(Collections.singletonMap("message", "개요 정보가 없습니다."));
3429
}
30+
return ResponseEntity.ok(response);
3531
}
3632

3733
// 시간대별 사건 수 조회 (0시~23시 모두 반환)
3834
@GetMapping("/hour")
3935
public ResponseEntity<?> getHourlyCaseStats(@RequestParam("date") String date,
4036
@RequestParam(value = "category", required = false) String category,
4137
HttpSession session) {
42-
try {
43-
List<HourlyCaseStatsResponse> stats = caseStatsService.getHourlyCaseStats(date, category, session);
44-
return ResponseEntity.ok(stats);
45-
} catch (IllegalStateException e) {
46-
return ResponseEntity.status(401).body(Collections.singletonMap("message", e.getMessage()));
47-
} catch (NoSuchElementException e) {
48-
return ResponseEntity.status(404).body(Collections.singletonMap("message", e.getMessage()));
49-
} catch (Exception e) {
50-
return ResponseEntity.status(500).body(Collections.singletonMap("message", "내부 서버 오류가 발생했습니다."));
38+
List<HourlyCaseStatsResponse> stats = caseStatsService.getHourlyCaseStats(date, category, session);
39+
if (stats.isEmpty()) {
40+
return ResponseEntity.status(404)
41+
.body(Collections.singletonMap("message", "시간대별 사건 정보가 없습니다."));
5142
}
43+
return ResponseEntity.ok(stats);
5244
}
5345

5446
// 월별/일별 사건 수 조회 (month 파라미터 존재 여부에 따라 분기; 전체 범위를 0으로 채워 반환)
@@ -57,70 +49,36 @@ public ResponseEntity<?> getCaseStats(@RequestParam("year") int year,
5749
@RequestParam(value = "month", required = false) Integer month,
5850
@RequestParam(value = "category", required = false) String category,
5951
HttpSession session) {
60-
try {
61-
if (month == null) {
62-
List<MonthlyCaseStatsResponse> monthlyStats = caseStatsService.getMonthlyCaseStats(year, category, session);
63-
return ResponseEntity.ok(monthlyStats);
64-
}
65-
List<DailyCaseStatsResponse> dailyStats = caseStatsService.getDailyCaseStats(year, month, category, session);
66-
return ResponseEntity.ok(dailyStats);
67-
} catch (IllegalStateException e) {
68-
return ResponseEntity.status(401).body(Collections.singletonMap("message", e.getMessage()));
69-
} catch (NoSuchElementException e) {
70-
return ResponseEntity.status(404).body(Collections.singletonMap("message", e.getMessage()));
71-
} catch (Exception e) {
72-
return ResponseEntity.status(500).body(Collections.singletonMap("message", "내부 서버 오류가 발생했습니다."));
52+
// month가 없으면 월별 데이터
53+
if (month == null) {
54+
List<MonthlyCaseStatsResponse> monthlyStats = caseStatsService.getMonthlyCaseStats(year, category, session);
55+
return ResponseEntity.ok(monthlyStats);
7356
}
57+
58+
// month가 있으면 일별 데이터
59+
List<DailyCaseStatsResponse> dailyStats = caseStatsService.getDailyCaseStats(year, month, category, session);
60+
return ResponseEntity.ok(dailyStats);
7461
}
7562

7663
// 유형별 사건 수 조회 (기본 카테고리 0 포함)
7764
@GetMapping("/category")
7865
public ResponseEntity<?> getCategoryCaseStats(@RequestParam("period") String period, HttpSession session) {
79-
try {
80-
Map<String, Integer> stats = caseStatsService.getCategoryCaseStats(period, session);
81-
return ResponseEntity.ok(stats);
82-
} catch (IllegalStateException e) {
83-
return ResponseEntity.status(401).body(Collections.singletonMap("message", e.getMessage()));
84-
} catch (IllegalArgumentException e) {
85-
return ResponseEntity.status(400).body(Collections.singletonMap("message", e.getMessage()));
86-
} catch (NoSuchElementException e) {
87-
return ResponseEntity.status(404).body(Collections.singletonMap("message", e.getMessage()));
88-
} catch (Exception e) {
89-
return ResponseEntity.status(500).body(Collections.singletonMap("message", "내부 서버 오류가 발생했습니다."));
90-
}
66+
Map<String, Integer> stats = caseStatsService.getCategoryCaseStats(period, session);
67+
return ResponseEntity.ok(stats);
9168
}
9269

9370
// 장소별 사건 수 조회
9471
@GetMapping("/location")
9572
public ResponseEntity<?> getLocationCaseStats(@RequestParam("period") String period, HttpSession session) {
96-
try {
97-
List<LocationCaseStatsResponse> stats = caseStatsService.getLocationCaseStats(period, session);
98-
return ResponseEntity.ok(stats);
99-
} catch (IllegalStateException e) {
100-
return ResponseEntity.status(401).body(Collections.singletonMap("message", e.getMessage()));
101-
} catch (IllegalArgumentException e) {
102-
return ResponseEntity.status(400).body(Collections.singletonMap("message", e.getMessage()));
103-
} catch (NoSuchElementException e) {
104-
return ResponseEntity.status(404).body(Collections.singletonMap("message", e.getMessage()));
105-
} catch (Exception e) {
106-
return ResponseEntity.status(500).body(Collections.singletonMap("message", "내부 서버 오류가 발생했습니다."));
107-
}
73+
List<LocationCaseStatsResponse> stats = caseStatsService.getLocationCaseStats(period, session);
74+
return ResponseEntity.ok(stats);
10875
}
10976

11077
// 지도용 장소별 사건 수 조회
11178
@GetMapping("/map")
11279
public ResponseEntity<?> getMapCaseStats(@RequestParam("period") String period, HttpSession session) {
113-
try {
114-
List<MapCaseStatsResponse> stats = caseStatsService.getMapCaseStats(period, session);
115-
return ResponseEntity.ok(stats);
116-
} catch (IllegalStateException e) {
117-
return ResponseEntity.status(401).body(Collections.singletonMap("message", e.getMessage()));
118-
} catch (IllegalArgumentException e) {
119-
return ResponseEntity.status(400).body(Collections.singletonMap("message", e.getMessage()));
120-
} catch (NoSuchElementException e) {
121-
return ResponseEntity.status(404).body(Collections.singletonMap("message", e.getMessage()));
122-
} catch (Exception e) {
123-
return ResponseEntity.status(500).body(Collections.singletonMap("message", "내부 서버 오류가 발생했습니다."));
124-
}
80+
List<MapCaseStatsResponse> stats = caseStatsService.getMapCaseStats(period, session);
81+
return ResponseEntity.ok(stats);
12582
}
83+
12684
}
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
package com.example.backend.analysis.dto;
22

3+
import com.example.backend.common.domain.CaseEntity;
4+
import com.example.backend.common.domain.CaseStatsOverviewEntity;
35
import lombok.*;
46

57
@Data
8+
@Builder
9+
@NoArgsConstructor
610
@AllArgsConstructor
711
public class CaseStatsOverviewResponse {
812
private int recentCase;
913
private int todayCase;
10-
private String mostCase;
14+
private CaseEntity.CaseCategory mostCase;
1115
private String patrolRegion;
16+
17+
public static CaseStatsOverviewResponse fromEntity(CaseStatsOverviewEntity entity, String patrolRegionAddress) {
18+
return CaseStatsOverviewResponse.builder()
19+
.recentCase(entity.getRecentCaseCount())
20+
.todayCase(entity.getTodayCaseCount())
21+
.mostCase(entity.getMostCase())
22+
.patrolRegion(patrolRegionAddress)
23+
.build();
24+
}
1225
}

backend/src/main/java/com/example/backend/analysis/dto/DailyCaseStatsResponse.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,26 @@
22

33
import lombok.*;
44

5-
@Getter
6-
@Setter
7-
@AllArgsConstructor
5+
@Data
6+
@Builder
87
@NoArgsConstructor
8+
@AllArgsConstructor
99
public class DailyCaseStatsResponse {
1010
private int day;
1111
private int fireCount;
1212
private int assaultCount;
1313
private int crowdCongestionCount;
1414
private int weaponCount;
1515
private int swoonCount;
16+
17+
public static DailyCaseStatsResponse fromRow(Object[] row) {
18+
return DailyCaseStatsResponse.builder()
19+
.day(((Number) row[0]).intValue())
20+
.fireCount(((Number) row[1]).intValue())
21+
.assaultCount(((Number) row[2]).intValue())
22+
.crowdCongestionCount(((Number) row[3]).intValue())
23+
.weaponCount(((Number) row[4]).intValue())
24+
.swoonCount(((Number) row[5]).intValue())
25+
.build();
26+
}
1627
}
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.example.backend.analysis.dto;
22

3-
import lombok.AllArgsConstructor;
4-
import lombok.Data;
3+
import lombok.*;
54

65
@Data
6+
@Builder
7+
@NoArgsConstructor
78
@AllArgsConstructor
89
public class HourlyCaseStatsResponse {
910
private int hour;
@@ -12,4 +13,15 @@ public class HourlyCaseStatsResponse {
1213
private int crowdCongestionCount;
1314
private int weaponCount;
1415
private int swoonCount;
16+
17+
public static HourlyCaseStatsResponse fromRow(Object[] row) {
18+
return HourlyCaseStatsResponse.builder()
19+
.hour(((Number) row[0]).intValue())
20+
.fireCount(((Number) row[1]).intValue())
21+
.assaultCount(((Number) row[2]).intValue())
22+
.crowdCongestionCount(((Number) row[3]).intValue())
23+
.weaponCount(((Number) row[4]).intValue())
24+
.swoonCount(((Number) row[5]).intValue())
25+
.build();
26+
}
1527
}

backend/src/main/java/com/example/backend/analysis/dto/LocationCaseStatsResponse.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,21 @@
33
import lombok.*;
44

55
@Data
6+
@Builder
7+
@NoArgsConstructor
68
@AllArgsConstructor
79
public class LocationCaseStatsResponse {
8-
String address;
9-
double latitude;
10-
double longitude;
11-
int count;
10+
private String address;
11+
private double latitude;
12+
private double longitude;
13+
private int count;
14+
15+
public static LocationCaseStatsResponse fromRow(Object[] row) {
16+
return LocationCaseStatsResponse.builder()
17+
.address((String) row[0])
18+
.latitude((Double) row[1])
19+
.longitude((Double) row[2])
20+
.count(((Number) row[3]).intValue())
21+
.build();
22+
}
1223
}
Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
package com.example.backend.analysis.dto;
22

3-
import lombok.AllArgsConstructor;
4-
import lombok.Data;
3+
import lombok.*;
54

65
@Data
6+
@Builder
7+
@NoArgsConstructor
78
@AllArgsConstructor
89
public class MapCaseStatsResponse {
9-
String address;
10-
double latitude;
11-
double longitude;
12-
int fire_count;
13-
int assault_count;
14-
int crowd_congestion_count;
15-
int weapon_count;
16-
int swoon_count;
10+
private String address;
11+
private double latitude;
12+
private double longitude;
13+
private int fireCount;
14+
private int assaultCount;
15+
private int crowdCongestionCount;
16+
private int weaponCount;
17+
private int swoonCount;
18+
19+
public static MapCaseStatsResponse fromRow(Object[] row) {
20+
return MapCaseStatsResponse.builder()
21+
.address((String) row[0])
22+
.latitude((Double) row[1])
23+
.longitude((Double) row[2])
24+
.fireCount(((Number) row[3]).intValue())
25+
.assaultCount(((Number) row[4]).intValue())
26+
.crowdCongestionCount(((Number) row[5]).intValue())
27+
.weaponCount(((Number) row[6]).intValue())
28+
.swoonCount(((Number) row[7]).intValue())
29+
.build();
30+
}
1731
}

backend/src/main/java/com/example/backend/analysis/dto/MonthlyCaseStatsResponse.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,26 @@
22

33
import lombok.*;
44

5-
@Getter
6-
@Setter
7-
@AllArgsConstructor
5+
@Data
6+
@Builder
87
@NoArgsConstructor
8+
@AllArgsConstructor
99
public class MonthlyCaseStatsResponse {
1010
private int month;
1111
private int fireCount;
1212
private int assaultCount;
1313
private int crowdCongestionCount;
1414
private int weaponCount;
1515
private int swoonCount;
16+
17+
public static MonthlyCaseStatsResponse fromRow(Object[] row) {
18+
return MonthlyCaseStatsResponse.builder()
19+
.month(((Number) row[0]).intValue())
20+
.fireCount(((Number) row[1]).intValue())
21+
.assaultCount(((Number) row[2]).intValue())
22+
.crowdCongestionCount(((Number) row[3]).intValue())
23+
.weaponCount(((Number) row[4]).intValue())
24+
.swoonCount(((Number) row[5]).intValue())
25+
.build();
26+
}
1627
}

0 commit comments

Comments
 (0)