Skip to content

Commit

Permalink
sign-in 응답값에 userId 반환 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
zoangrak committed Apr 23, 2024
1 parent 8691f63 commit 1eb7646
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,31 +90,32 @@ public ResponseEntity<String> createRoom(@RequestBody Map<String, Long> requestB
}

/**
* 특정 방 번호 알아내기
* 부모의 ID 를 이용해 선생님과 연결된 방 번호 알아내기
* @param requestBody
* @return
*/
@GetMapping("/findRoomId")
public ResponseEntity<?> findRoomId(@RequestBody Map<String, Long> requestBody) {
Long parentUserId = requestBody.get("parentId");
try {
List<Long> teacherUserIds = friendService.findTeacherUserIdsAsParent(parentUserId);

if (teacherUserIds.isEmpty()) {
return ResponseEntity.ok("부모와 선생님이 연결되지 않았습니다.");
}

String roomId = friendService.findRoomId(teacherUserIds, parentUserId); // 🧚🏻‍ 이 코드 검토하기

if (roomId != null) {
return ResponseEntity.ok(roomId);
} else {
return ResponseEntity.ok("roomId 가 null 입니다.");
}
} catch (RuntimeException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
}
// @GetMapping("/findRoomId")
// public ResponseEntity<?> findRoomId(@RequestBody Map<String, Long> requestBody) {
// // 부모의 아이디 가져오기
// Long parentUserId = requestBody.get("parentId");
// try {
// List<Long> teacherUserIds = friendService.findTeacherUserIdsAsParent(parentUserId);
//
// if (teacherUserIds.isEmpty()) {
// return ResponseEntity.ok("부모와 선생님이 연결되지 않았습니다.");
// }
//
// String roomId = friendService.findRoomId(teacherUserIds, parentUserId); // 🧚🏻‍ 이 코드 검토하기
//
// if (roomId != null) {
// return ResponseEntity.ok(roomId);
// } else {
// return ResponseEntity.ok("roomId 가 null 입니다.");
// }
// } catch (RuntimeException e) {
// return ResponseEntity.badRequest().body(e.getMessage());
// }
// }

/**
* 특정 채팅방 인원 조회
Expand All @@ -129,6 +130,11 @@ public ResponseEntity<Integer> getUserCount(@PathVariable String roomId) {
* 유저의 채팅방 리스트 반환
*/

/**
* roomId 로 채팅방에 참여 중인 유저 리스트 조회
* @param roomId
* @return
*/
@Operation(summary = "roomId 로 채팅방 참여 유저 리스트 조회")
@GetMapping("/findUsers/{roomId}")
public ResponseEntity<?> getUserListByRoomId(@PathVariable String roomId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ public class UserController {
private final UserService userService;
private final ChatRoomService chatRoomService;



@Operation(summary = "회원가입")
@PostMapping("/auth/sign-up")
public String singUp(@RequestBody UserDto userDto) throws Exception {
Expand All @@ -44,12 +42,14 @@ public String singUp(@RequestBody UserDto userDto) throws Exception {
public ResponseEntity<Map<String, Object>> login(@RequestBody UserDto userDto) {
Map<String, Object> tokens = userService.loginUser(userDto.getEmail(), userDto.getPassword());
Role role = userService.getUserRole(userDto.getEmail());
Long userId = userService.getUserId(userDto.getEmail());

HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.AUTHORIZATION, (String) tokens.get("accessToken"));

Map<String, Object> responseBody = new HashMap<>();
responseBody.put("role", role);
responseBody.put("userId", userId);

return ResponseEntity.ok().headers(headers).body(responseBody);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,30 +47,19 @@ public void acceptFriendRequest(Teacher teacher, Parent parent) {
friendRepository.save(friend);
}

/**
* roomId 로 채팅방 참여 유저 리스트 조회시 사용
* @param roomId
* @return
*/
public boolean roomExists(String roomId) {
return friendRepository.findByRoomId(roomId).isPresent();
}

/**
* parentId로 teacherId 찾기
* @param parentUserId
* @return
*/
public List<Long> findTeacherUserIdsAsParent(Long parentUserId) {
return friendRepository.findTeacherUserIdAsParent(parentUserId);
}

public String findRoomId(List<Long> teacherUserIds, Long parentUserId) {
for (Long teacherUserId : teacherUserIds) {
Optional<String> roomIdOptional = friendRepository.findRoomId(teacherUserId, parentUserId);
if (roomIdOptional.isPresent()) {
return roomIdOptional.get();
}
}
return null;
}

public List<Long> findTeacherUserIds(Parent parent) {
public List<Long> findTeacherUserIdsAsParent(Parent parent) {
List<Long> childTeacherIds = getChildteacherIds(parent);
List<Long> friendTeacherIds = friendRepository.findTeacherUserIdAsParent(parent.getUser().getId());
List<Long> matchingTeacherIds = new ArrayList<>();
Expand All @@ -84,6 +73,32 @@ public List<Long> findTeacherUserIds(Parent parent) {
}
return matchingTeacherIds;
}
// public List<Long> findTeacherUserIds(Parent parent) {
// List<Long> childTeacherIds = getChildteacherIds(parent);
// List<Long> friendTeacherIds = friendRepository.findTeacherUserIdAsParent(parent.getUser().getId());
// List<Long> matchingTeacherIds = new ArrayList<>();
//
// // 자식이 가지고 있는 선생님의 ID와 친구 목록에서 가져온 선생님의 ID를 비교하여 일치하는 ID를 찾습니다.
// for (Long childTeacherId : childTeacherIds) {
// if (friendTeacherIds.contains(childTeacherId)) {
// // 일치하는 선생님의 ID가 발견되면 리스트에 추가합니다.
// matchingTeacherIds.add(childTeacherId);
// }
// }
// return matchingTeacherIds;
// }

public String findRoomId(List<Long> teacherUserIds, Long parentUserId) {
for (Long teacherUserId : teacherUserIds) {
Optional<String> roomIdOptional = friendRepository.findRoomId(teacherUserId, parentUserId);
if (roomIdOptional.isPresent()) {
return roomIdOptional.get();
}
}
return null;
}



private List<Long> getChildteacherIds(Parent parent) {
// 여기에 부모의 자식 목록에서 선생님의 ID를 가져오는 로직을 구현합니다.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,14 @@ public Role getUserRole(String email) {
throw new IllegalArgumentException("이메일에 해당하는 사용자가 없습니다.");
}
}

public Long getUserId(String email) {
Optional<User> userOptional = userRepository.findByEmail(email);
if (userOptional.isPresent()) {
User user = userOptional.get();
return user.getId();
} else {
throw new RuntimeException("해당하는 이메일을 가진 사용자를 찾을 수 없습니다.");
}
}
}

0 comments on commit 1eb7646

Please sign in to comment.