diff --git a/src/main/java/com/goormdari/domain/team/application/TeamService.java b/src/main/java/com/goormdari/domain/team/application/TeamService.java index f3c8ad7..ec3c1f5 100644 --- a/src/main/java/com/goormdari/domain/team/application/TeamService.java +++ b/src/main/java/com/goormdari/domain/team/application/TeamService.java @@ -8,6 +8,7 @@ import com.goormdari.domain.team.dto.response.findAllRoutineByUserIdResponse; import com.goormdari.domain.team.exception.TeamAlreadyExistException; import com.goormdari.domain.user.domain.User; +import com.goormdari.domain.user.domain.dto.response.findByTeamIdResponse; import com.goormdari.domain.user.domain.repository.UserRepository; import com.goormdari.global.config.email.EmailClient; import com.goormdari.global.payload.Message; @@ -15,7 +16,9 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.List; import java.util.UUID; +import java.util.stream.Collectors; @Service @RequiredArgsConstructor @@ -42,6 +45,19 @@ public findAllRoutineByUserIdResponse findAllRoutineByUserId(Long userId) { .build(); } + @jakarta.transaction.Transactional + public List findTeamByUserId(Long userId) { + Long teamId = userRepository.findById(userId) + .orElseThrow(()->new NotFoundException("User Not Found")).getTeam().getId(); + List users = userRepository.findByTeamId(teamId); + return users.stream() + .map(user -> findByTeamIdResponse.builder() + .id(user.getId()) + .username(user.getUsername()) + .build()) + .collect(Collectors.toList()); + } + @Transactional public CreateTeamResponse createNewTeam(final String username, final CreateTeamRequest createTeamRequest) { User user = userRepository.findByUsername(username) diff --git a/src/main/java/com/goormdari/domain/team/presentation/TeamController.java b/src/main/java/com/goormdari/domain/team/presentation/TeamController.java index dc0e067..2d042e9 100644 --- a/src/main/java/com/goormdari/domain/team/presentation/TeamController.java +++ b/src/main/java/com/goormdari/domain/team/presentation/TeamController.java @@ -5,6 +5,7 @@ import com.goormdari.domain.team.dto.request.CreateTeamRequest; import com.goormdari.domain.team.dto.response.CreateTeamResponse; import com.goormdari.domain.team.dto.response.findAllRoutineByUserIdResponse; +import com.goormdari.domain.user.domain.dto.response.findByTeamIdResponse; import com.goormdari.global.config.security.jwt.JWTUtil; import com.goormdari.global.payload.ErrorResponse; import com.goormdari.global.payload.Message; @@ -20,6 +21,8 @@ import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; +import java.util.List; + @Tag(name = "Team", description = "Team API") @RestController @RequiredArgsConstructor @@ -105,4 +108,25 @@ public ResponseCustom getRoutineList( return ResponseCustom.OK(teamService.findAllRoutineByUserId(userId)); } + @Operation(summary = "팀 참가 유저 명단 조회", description = "팀에 존재하는 유저 명단") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "조회 성공 ", content = {@Content(mediaType = "application/json", schema = @Schema(implementation = findByTeamIdResponse.class))}), + @ApiResponse(responseCode = "400", description = "조회 실패", content = {@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))}), + }) + @GetMapping("/user-list") + public ResponseCustom> getTeamMember( + @Parameter(description = "Accesstoken을 입력해주세요.", required = true) @RequestHeader("Authorization") String token + ) { + if (token == null) { + throw new InvalidTokenException(); + } + + String jwt = token.startsWith("Bearer ") ? token.substring(7) : token; + if (!jwtUtil.validateToken(jwt)) { + throw new IllegalArgumentException("Invalid token"); + } + Long userId = jwtUtil.extractId(jwt); + return ResponseCustom.OK(teamService.findTeamByUserId(userId)); + } + } diff --git a/src/main/java/com/goormdari/domain/user/domain/service/UserService.java b/src/main/java/com/goormdari/domain/user/domain/service/UserService.java index d3a26e9..d008fbc 100644 --- a/src/main/java/com/goormdari/domain/user/domain/service/UserService.java +++ b/src/main/java/com/goormdari/domain/user/domain/service/UserService.java @@ -44,19 +44,6 @@ public findCurrentStepResponse findCurrentStepById(Long userId) { return findCurrentStepResponse.builder().currentStep(user.getCurrentStep()).build(); } - - @Transactional - public List findTeamByUserId(Long userId) { - Long teamId = userRepository.findById(userId) - .orElseThrow(()->new NotFoundException("User Not Found")).getTeam().getId(); - List users = userRepository.findByTeamId(teamId); - return users.stream() - .map(user -> findByTeamIdResponse.builder() - .id(user.getId()) - .username(user.getUsername()) - .build()) - .collect(Collectors.toList()); - } @Transactional public Long save(AddUserRequest dto) { // 사용자 이름 중복 체크 diff --git a/src/main/java/com/goormdari/domain/user/presentation/UserController.java b/src/main/java/com/goormdari/domain/user/presentation/UserController.java index fba0b53..7776b7f 100644 --- a/src/main/java/com/goormdari/domain/user/presentation/UserController.java +++ b/src/main/java/com/goormdari/domain/user/presentation/UserController.java @@ -21,7 +21,7 @@ import java.util.List; -@Tag(name = "User", description = "Routine API") +@Tag(name = "User", description = "User API") @RestController @RequiredArgsConstructor @RequestMapping("/user") @@ -51,24 +51,4 @@ public ResponseCustom getCurrentStep( return ResponseCustom.OK(userService.findCurrentStepById(userId)); } - @Operation(summary = "팀 참가 유저 명단 조회", description = "같은 팀에 존재하는 유저 명단") - @ApiResponses(value = { - @ApiResponse(responseCode = "200", description = "조회 성공 ", content = {@Content(mediaType = "application/json", schema = @Schema(implementation = findByTeamIdResponse.class))}), - @ApiResponse(responseCode = "400", description = "조회 실패", content = {@Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))}), - }) - @GetMapping("/team-list") - public ResponseCustom> getTeamMember( - @Parameter(description = "Accesstoken을 입력해주세요.", required = true) @RequestHeader("Authorization") String token - ) { - if (token == null) { - throw new InvalidTokenException(); - } - - String jwt = token.startsWith("Bearer ") ? token.substring(7) : token; - if (!jwtUtil.validateToken(jwt)) { - throw new IllegalArgumentException("Invalid token"); - } - Long userId = jwtUtil.extractId(jwt); - return ResponseCustom.OK(userService.findTeamByUserId(userId)); - } }