22
33import com .likelion .ai_teacher_a .domain .logsolve .dto .TotalLogCountDto ;
44import com .likelion .ai_teacher_a .domain .logsolve .service .LogSolveService ;
5- import com .likelion .ai_teacher_a .domain .user .entity .User ;
65import com .likelion .ai_teacher_a .domain .user .repository .UserRepository ;
76import com .likelion .ai_teacher_a .domain .userJr .entity .UserJr ;
87import com .likelion .ai_teacher_a .domain .userJr .repository .UserJrRepository ;
1716import org .springframework .http .HttpStatus ;
1817import org .springframework .http .MediaType ;
1918import org .springframework .http .ResponseEntity ;
20- import org .springframework .security .core .userdetails .UsernameNotFoundException ;
2119import org .springframework .web .bind .annotation .*;
2220import org .springframework .web .multipart .MultipartFile ;
2321
@@ -33,37 +31,36 @@ public class LogSolveController {
3331 private final UserRepository userRepository ;
3432 private final UserJrRepository userJrRepository ;
3533
34+ private void validateUserJrOwner (Long userId , UserJr userJr ) {
35+ if (!userJr .getUser ().getId ().equals (userId )) {
36+ throw new RuntimeException ("해당 자녀에 접근할 권한이 없습니다." );
37+ }
38+ }
39+
3640 @ Operation (summary = "이미지 문제 업로드 및 AI 해설 시작" )
3741 @ PostMapping (value = "/solve" , consumes = MediaType .MULTIPART_FORM_DATA_VALUE )
3842 public ResponseEntity <?> solveImage (
3943 @ RequestParam ("mathProblemImage" ) MultipartFile image ,
4044 @ RequestParam ("userJrId" ) Long userJrId , // 쿼리 파라미터
4145 @ LoginUserId Long userId ) {
4246
43- User user = userRepository .findById (userId )
44- .orElseThrow (() -> new UsernameNotFoundException ("사용자를 찾을 수 없습니다" ));
45-
46- UserJr userJr = userJrRepository .findById (userJrId )
47+ UserJr userJr = userJrRepository .findByIdWithUser (userJrId )
4748 .orElseThrow (() -> new RuntimeException ("자녀 정보를 찾을 수 없습니다." ));
48-
49- if (!userJr .getUser ().getId ().equals (user .getId ())) {
50- throw new RuntimeException ("해당 자녀에 접근할 권한이 없습니다." );
51- }
49+ validateUserJrOwner (userId , userJr );
5250
5351 int grade = userJr .getSchoolGrade ();
5452
55- return logSolveService .handleSolveImage (image , user , userJr , grade );
53+ return logSolveService .handleSolveImage (image , userId , userJr , grade );
54+
5655 }
5756
5857
5958 @ Operation (summary = "단일 문제해설 상세 조회" )
6059 @ GetMapping ("/{logSolveId}" )
6160 public ResponseEntity <?> getLogDetail (@ Parameter (description = "해당 문제해설 로그 ID" ) @ PathVariable ("logSolveId" ) Long logSolveId ,
6261 @ LoginUserId Long userId ) {
63- User user = userRepository .findById (userId )
64- .orElseThrow (() -> new UsernameNotFoundException ("사용자를 찾을 수 없습니다" ));
6562 try {
66- Map <String , Object > result = logSolveService .getLogDetail (logSolveId , user );
63+ Map <String , Object > result = logSolveService .getLogDetail (logSolveId , userId );
6764 return ResponseEntity .ok (result );
6865 } catch (IllegalArgumentException e ) {
6966 return ResponseEntity .status (HttpStatus .NOT_FOUND )
@@ -75,10 +72,8 @@ public ResponseEntity<?> getLogDetail(@Parameter(description = "해당 문제해
7572 @ DeleteMapping ("/{logSolveId}" )
7673 public ResponseEntity <?> deleteLog (@ Parameter (description = "삭제할 로그 ID" ) @ PathVariable ("logSolveId" ) Long logSolveId ,
7774 @ LoginUserId Long userId ) {
78- User user = userRepository .findById (userId )
79- .orElseThrow (() -> new UsernameNotFoundException ("사용자를 찾을 수 없습니다" ));
8075 try {
81- logSolveService .deleteLogById (logSolveId , user );
76+ logSolveService .deleteLogById (logSolveId , userId );
8277 return ResponseEntity .ok (Map .of ("message" , "삭제가 완료되었습니다." ));
8378 } catch (IllegalArgumentException e ) {
8479 return ResponseEntity .status (HttpStatus .NOT_FOUND )
@@ -93,9 +88,8 @@ public ResponseEntity<?> askAgain(
9388 @ Parameter (description = "사용자의 추가 질문" ) @ RequestParam ("question" ) String question ,
9489 @ LoginUserId Long userId
9590 ) {
96- User user = userRepository .findById (userId )
97- .orElseThrow (() -> new UsernameNotFoundException ("사용자를 찾을 수 없습니다" ));
98- return logSolveService .executeFollowUp (logSolveId , question , user );
91+
92+ return logSolveService .executeFollowUp (logSolveId , question , userId );
9993 }
10094
10195
@@ -118,18 +112,13 @@ public ResponseEntity<?> getSimpleLogs(
118112 @ RequestParam (name = "userJrId" ) Long userJrId ,
119113 @ LoginUserId Long userId
120114 ) {
121- User user = userRepository .findById (userId )
122- .orElseThrow (() -> new UsernameNotFoundException ("사용자를 찾을 수 없습니다" ));
123-
124- UserJr userJr = userJrRepository .findById (userJrId )
115+ UserJr userJr = userJrRepository .findByIdWithUser (userJrId )
125116 .orElseThrow (() -> new RuntimeException ("자녀 정보를 찾을 수 없습니다." ));
117+ validateUserJrOwner (userId , userJr );
126118
127119
128- if (!userJr .getUser ().getId ().equals (user .getId ())) {
129- throw new RuntimeException ("해당 자녀에 접근할 권한이 없습니다." );
130- }
120+ Pageable pageable = PageRequest .of (page , size , Sort .by (Sort .Direction .DESC , "createdAt" ));
131121
132- Pageable pageable = PageRequest .of (page , size , Sort .by (Sort .Direction .DESC , "image.uploadedAt" ));
133122
134123 return ResponseEntity .ok (logSolveService .getAllSimpleLogs (pageable , userJr ));
135124 }
0 commit comments