22
33import com .backend .domain .analysis .dto .request .AnalysisRequest ;
44import com .backend .domain .analysis .dto .response .AnalysisResultResponseDto ;
5+ import com .backend .domain .analysis .dto .response .AnalysisStartResponse ;
56import com .backend .domain .analysis .dto .response .HistoryResponseDto ;
67import com .backend .domain .analysis .entity .AnalysisResult ;
8+ import com .backend .domain .analysis .service .AnalysisProgressService ;
79import com .backend .domain .analysis .service .AnalysisService ;
810import com .backend .domain .repository .dto .response .RepositoryResponse ;
911import com .backend .domain .repository .entity .Repositories ;
1012import com .backend .domain .repository .service .RepositoryService ;
13+ import com .backend .domain .user .util .JwtUtil ;
1114import com .backend .global .exception .BusinessException ;
1215import com .backend .global .exception .ErrorCode ;
1316import com .backend .global .response .ApiResponse ;
17+ import jakarta .servlet .http .HttpServletRequest ;
1418import lombok .RequiredArgsConstructor ;
1519import org .springframework .http .ResponseEntity ;
1620import org .springframework .transaction .annotation .Transactional ;
1721import org .springframework .web .bind .annotation .*;
22+ import org .springframework .web .servlet .mvc .method .annotation .SseEmitter ;
1823
1924import java .util .ArrayList ;
2025import java .util .List ;
2530public class AnalysisController {
2631 private final AnalysisService analysisService ;
2732 private final RepositoryService repositoryService ;
33+ private final AnalysisProgressService analysisProgressService ;
34+ private final JwtUtil jwtUtil ;
2835
2936 // POST: 분석 요청
3037 @ PostMapping
31- public ResponseEntity <ApiResponse <Void >> analyzeRepository (@ RequestBody AnalysisRequest request ) {
38+ public ResponseEntity <ApiResponse <AnalysisStartResponse >> analyzeRepository (
39+ @ RequestBody AnalysisRequest request ,
40+ HttpServletRequest httpRequest
41+ ) {
3242 if (request == null ) {
3343 throw new BusinessException (ErrorCode .INVALID_INPUT_VALUE );
3444 }
45+ Long jwtUserId = jwtUtil .getUserId (httpRequest );
3546
36- analysisService .analyze (request .githubUrl ());
37- return ResponseEntity .ok (ApiResponse .success ());
47+ Long repositoryId = analysisService .analyze (request .githubUrl (), jwtUserId );
48+ AnalysisStartResponse response = new AnalysisStartResponse (repositoryId );
49+
50+ return ResponseEntity .ok (ApiResponse .success (response ));
3851 }
3952
4053 // GET: 사용자의 모든 Repository 목록 조회
41- @ GetMapping ("/{memberId }/repositories" )
54+ @ GetMapping ("/{userId }/repositories" )
4255 @ Transactional (readOnly = true )
43- public ResponseEntity <List <RepositoryResponse >> getMemberHistory (
44- @ PathVariable Long memberId
56+ public ResponseEntity <ApiResponse <List <RepositoryResponse >>> getMemberHistory (
57+ @ PathVariable Long userId ,
58+ HttpServletRequest httpRequest
4559 ){
46- if (memberId == null ) {
47- throw new BusinessException (ErrorCode .INVALID_INPUT_VALUE );
60+ Long jwtUserId = jwtUtil .getUserId (httpRequest );
61+ if (!jwtUserId .equals (userId )) {
62+ throw new BusinessException (ErrorCode .FORBIDDEN );
4863 }
4964
5065 List <RepositoryResponse > repositories = repositoryService
51- .findRepositoryByMember ( memberId )
66+ .findRepositoryByUser ( userId )
5267 .stream ()
5368 .map (RepositoryResponse ::new )
5469 .toList ();
5570
56- return ResponseEntity .ok (repositories );
71+ return ResponseEntity .ok (ApiResponse . success ( repositories ) );
5772 }
5873
5974 // GET: 특정 Repository의 분석 히스토리 조회, 모든 분석 결과 조회
60- @ GetMapping ("/{memberId }/repositories/{repositoriesId}" )
75+ @ GetMapping ("/{userId }/repositories/{repositoriesId}" )
6176 @ Transactional (readOnly = true )
62- public ResponseEntity <HistoryResponseDto > getAnalysisByRepositoriesId (
63- @ PathVariable ("memberId" ) Long memberId ,
64- @ PathVariable ("repositoriesId" ) Long repoId
77+ public ResponseEntity <ApiResponse <HistoryResponseDto >> getAnalysisByRepositoriesId (
78+ @ PathVariable ("userId" ) Long userId ,
79+ @ PathVariable ("repositoriesId" ) Long repoId ,
80+ HttpServletRequest httpRequest
6581 ){
66- // TODO: 추후 인증/인가 기능 완성 후 소유권 검증 로직 추가
82+ Long jwtUserId = jwtUtil .getUserId (httpRequest );
83+ if (!jwtUserId .equals (userId )) {
84+ throw new BusinessException (ErrorCode .FORBIDDEN );
85+ }
6786
6887 // 1. Repository 정보 조회
6988 Repositories repository = repositoryService .findById (repoId )
@@ -87,18 +106,22 @@ public ResponseEntity<HistoryResponseDto> getAnalysisByRepositoriesId(
87106 // 4. 응답 조합
88107 HistoryResponseDto response = HistoryResponseDto .of (repositoryResponse , versions );
89108
90- return ResponseEntity .ok (response );
109+ return ResponseEntity .ok (ApiResponse . success ( response ) );
91110 }
92111
93112 // GET: 특정 분석 결과 상세 조회
94- @ GetMapping ("/{memberId }/repositories/{repositoryId}/results/{analysisId}" )
113+ @ GetMapping ("/{userId }/repositories/{repositoryId}/results/{analysisId}" )
95114 @ Transactional (readOnly = true )
96- public ResponseEntity <AnalysisResultResponseDto > getAnalysisDetail (
97- @ PathVariable Long memberId ,
115+ public ResponseEntity <ApiResponse < AnalysisResultResponseDto > > getAnalysisDetail (
116+ @ PathVariable Long userId ,
98117 @ PathVariable Long repositoryId ,
99- @ PathVariable Long analysisId
118+ @ PathVariable Long analysisId ,
119+ HttpServletRequest httpRequest
100120 ) {
101- // TODO: 추후 인증/인가 검증
121+ Long jwtUserId = jwtUtil .getUserId (httpRequest );
122+ if (!jwtUserId .equals (userId )) {
123+ throw new BusinessException (ErrorCode .FORBIDDEN );
124+ }
102125
103126 // 분석 결과 조회
104127 AnalysisResult analysisResult = analysisService .getAnalysisById (analysisId );
@@ -110,33 +133,64 @@ public ResponseEntity<AnalysisResultResponseDto> getAnalysisDetail(
110133 AnalysisResultResponseDto response =
111134 new AnalysisResultResponseDto (analysisResult , analysisResult .getScore ());
112135
113- return ResponseEntity .ok (response );
136+ return ResponseEntity .ok (ApiResponse . success ( response ) );
114137 }
115138
116139 // Repository 삭제
117- @ DeleteMapping ("/{memberId}/repositories/{repositoriesId}" )
118- public void deleteRepository (@ PathVariable ("repositoriesId" ) Long repositoriesId ){
119- analysisService .delete (repositoriesId );
140+ @ DeleteMapping ("/{userId}/repositories/{repositoriesId}" )
141+ public ResponseEntity <ApiResponse <Void >> deleteRepository (
142+ @ PathVariable ("repositoriesId" ) Long repositoriesId ,
143+ @ PathVariable Long userId ,
144+ HttpServletRequest httpRequest ){
145+ Long jwtUserId = jwtUtil .getUserId (httpRequest );
146+ if (!jwtUserId .equals (userId )) {
147+ throw new BusinessException (ErrorCode .FORBIDDEN );
148+ }
149+
150+ analysisService .delete (repositoriesId , userId );
151+ return ResponseEntity .ok (ApiResponse .success ());
120152 }
121153
122154 // 특정 AnalysisResult 삭제
123- @ DeleteMapping ("/{memberId }/repositories/{repositoryId}/results/{analysisId}" )
155+ @ DeleteMapping ("/{userId }/repositories/{repositoryId}/results/{analysisId}" )
124156 public ResponseEntity <ApiResponse <Void >> deleteAnalysisResult (
125- @ PathVariable Long memberId ,
157+ @ PathVariable Long userId ,
126158 @ PathVariable Long repositoryId ,
127- @ PathVariable Long analysisId
128- ) {
129- analysisService .deleteAnalysisResult (analysisId , memberId );
159+ @ PathVariable Long analysisId ,
160+ HttpServletRequest httpRequest ){
161+ Long jwtUserId = jwtUtil .getUserId (httpRequest );
162+ if (!jwtUserId .equals (userId )) {
163+ throw new BusinessException (ErrorCode .FORBIDDEN );
164+ }
165+
166+ analysisService .deleteAnalysisResult (analysisId , userId );
130167 return ResponseEntity .ok (ApiResponse .success ());
131168 }
132169
133170 // 분석 결과 공개 여부 변경
134- @ PutMapping ("/{memberId}/repositories/{repositoryId}/public" )
135- public ResponseEntity updatePublicStatus (
136- @ PathVariable Long memberId ,
137- @ PathVariable Long repositoryId
138- ) {
139- analysisService .updatePublicStatus (repositoryId , memberId );
171+ @ PutMapping ("/{userId}/repositories/{repositoryId}/public" )
172+ public ResponseEntity <ApiResponse <Void >> updatePublicStatus (
173+ @ PathVariable Long userId ,
174+ @ PathVariable Long repositoryId ,
175+ HttpServletRequest httpRequest ){
176+ Long jwtUserId = jwtUtil .getUserId (httpRequest );
177+ if (!jwtUserId .equals (userId )) {
178+ throw new BusinessException (ErrorCode .FORBIDDEN );
179+ }
180+
181+ analysisService .updatePublicStatus (repositoryId , userId );
140182 return ResponseEntity .ok (ApiResponse .success ());
141183 }
184+
185+ // 분석 현황 Sse
186+ @ GetMapping ("/stream/{userId}" )
187+ public SseEmitter stream (@ PathVariable Long userId ,
188+ HttpServletRequest httpRequest ){
189+ Long jwtUserId = jwtUtil .getUserId (httpRequest );
190+ if (!jwtUserId .equals (userId )) {
191+ throw new BusinessException (ErrorCode .FORBIDDEN );
192+ }
193+
194+ return analysisProgressService .connect (userId );
195+ }
142196}
0 commit comments