Skip to content

Commit

Permalink
GET /users/my/groups 구현
Browse files Browse the repository at this point in the history
내가 속한 그룹 목록 조회
  • Loading branch information
gidskql6671 committed May 28, 2024
1 parent 3ada0f8 commit 90da6af
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ interface ContainGroupRepository: JpaRepository<ContainGroup, Long> {

@EntityGraph(attributePaths = ["user"])
fun findAllByGroup(group: Group): List<ContainGroup>

@EntityGraph(attributePaths = ["group"])
fun findAllByUser(user: User): List<ContainGroup>
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ import java.util.*
interface GroupRepository: JpaRepository<Group, Long> {

fun findByInviteCode(inviteCode: String): Optional<Group>

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.time.LocalDate
import java.time.YearMonth
import java.util.stream.Collectors

private val myLogger = KotlinLogging.logger {}

Expand All @@ -32,11 +31,11 @@ class GroupService (
private val problemRepository: ProblemRepository,
private val passwordEncoder: PasswordEncoder
){
fun getGroups(): List<GroupOfListDto> =
groupRepository.findAll()
.stream()
.map { it.toGroupOfListDto() }
.collect(Collectors.toList())
fun getGroups(): List<GroupOfListDto> = groupRepository.findAll().map { it.toGroupOfListDto() }


fun getGroupsOfUser(user: User): List<GroupOfListDto> =
containGroupRepository.findAllByUser(user).map { it.group.toGroupOfListDto() }

fun getGroupDetail(user: User, groupId: Long): GroupDetailDto {
val group = groupRepository.findById(groupId).orElseThrow{ NotFoundException(message = "해당 그룹이 없습니다.") }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse
import io.swagger.v3.oas.annotations.responses.ApiResponses
import io.swagger.v3.oas.annotations.tags.Tag
import knu.dong.onedayonebaek.common.exception.response.UnauthorizedResponse
import knu.dong.onedayonebaek.group.dto.GroupOfListDto
import knu.dong.onedayonebaek.group.service.GroupService
import knu.dong.onedayonebaek.user.domain.User
import knu.dong.onedayonebaek.user.dto.toUserDto
import org.springframework.security.core.Authentication
Expand All @@ -18,7 +20,9 @@ import org.springframework.web.bind.annotation.RestController
@RestController
@RequestMapping("/users")
@Tag(name = "User APIs", description = "유저 정보와 관련된 APIs")
class UserController {
class UserController(
private val groupService: GroupService
) {

@Operation(
summary = "로그인된 유저의 정보 조회",
Expand All @@ -38,4 +42,12 @@ class UserController {
)
@GetMapping
fun loginUserInfo(authentication: Authentication) = (authentication.principal as User).toUserDto()


@GetMapping("/my/groups")
fun getMyGroups(authentication: Authentication): List<GroupOfListDto> {
val user = authentication.principal as User

return groupService.getGroupsOfUser(user)
}
}

0 comments on commit 90da6af

Please sign in to comment.