Skip to content

Commit

Permalink
Merge pull request #9 from soma-baekgu/feature/BG-128-get-default-wor…
Browse files Browse the repository at this point in the history
…kspace

[BG-128]: 사용자 디폴트 워크스페이스를 조회하는 API를 구현 (실제소요시간1.5 / 스토리포인트2)
  • Loading branch information
GGHDMS authored Jun 27, 2024
2 parents d93b471 + eabc9f2 commit c6f2a62
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.backgu.amaker.workspace.controller

import com.backgu.amaker.security.JwtAuthentication
import com.backgu.amaker.workspace.dto.WorkspaceCreateDto
import com.backgu.amaker.workspace.dto.WorkspaceDto
import com.backgu.amaker.workspace.dto.WorkspacesDto
import com.backgu.amaker.workspace.service.WorkspaceService
import org.springframework.http.ResponseEntity
import org.springframework.security.core.annotation.AuthenticationPrincipal
Expand All @@ -12,11 +14,11 @@ import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/api/v1")
@RequestMapping("/api/v1/workspaces")
class WorkspaceController(
private val workspaceService: WorkspaceService,
) {
@PostMapping("/workspaces")
@PostMapping
fun createWorkspace(
@AuthenticationPrincipal token: JwtAuthentication,
@RequestBody workspaceCreateDto: WorkspaceCreateDto,
Expand All @@ -25,7 +27,10 @@ class WorkspaceController(
@GetMapping
fun findWorkspaces(
@AuthenticationPrincipal token: JwtAuthentication,
) {
workspaceService.findWorkspaces(token.id)
}
): ResponseEntity<WorkspacesDto> = ResponseEntity.ok().body(workspaceService.findWorkspaces(token.id))

@GetMapping("/default")
fun getDefaultWorkspace(
@AuthenticationPrincipal token: JwtAuthentication,
): ResponseEntity<WorkspaceDto> = ResponseEntity.ok().body(workspaceService.getDefaultWorkspace(token.id))
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@ import com.backgu.amaker.workspace.domain.Workspace
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param
import java.util.UUID

interface WorkspaceRepository : JpaRepository<Workspace, Long> {
@Query("select w from Workspace w where w.id in :workspaceIds")
fun findByWorkspaceIds(
@Param("workspaceIds") workspaceIds: List<Long>,
): List<Workspace>

@Query(
"select w " +
"from Workspace w " +
"join WorkspaceUser wu on w.id = wu.workspaceId " +
"where wu.userId = :userId " +
"order by wu.id desc limit 1",
)
fun getDefaultWorkspaceByUserId(userId: UUID): Workspace?
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,23 @@ class WorkspaceService(
workspaces = workspaceDtos,
)
}

fun getDefaultWorkspace(userId: UUID): WorkspaceDto {
val user =
userRepository.findByIdOrNull(userId) ?: run {
logger.error { "User not found : $userId" }
throw EntityNotFoundException("User not found : $userId")
}

return workspaceRepository.getDefaultWorkspaceByUserId(user.id)?.let {
WorkspaceDto(
id = it.id,
name = it.name,
thumbnail = it.thumbnail,
)
} ?: run {
logger.error { "Default workspace not found : $userId" }
throw EntityNotFoundException("Default workspace not found : $userId")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ class WorkspaceFixture(
listOf(
Workspace(
id = 1L,
name = "워크스페이습",
name = "워크스페이스1",
thumbnail = "image/thumbnail1.png",
),
Workspace(
id = 2L,
name = "워크스페이습",
name = "워크스페이스2",
thumbnail = "image/thumbnail2.png",
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,45 @@ class WorkspaceServiceTest {
}

@Test
@DisplayName("유저를 찾을 수 없을 때 워크스페이스 생성 실패")
fun createWorkspace_UserNotFound() {
@DisplayName("유저의 워크스페이스들 조회")
fun findWorkspaces() {
// given
val request = createWorkspaceRequest()
val userId = UserFixture.defaultUserId

// when & then
assertThrows<EntityNotFoundException> {
workspaceService.createWorkspace(UUID.randomUUID(), request)
}
// when
val result = workspaceService.findWorkspaces(userId)

// then
assertThat(result.userId).isEqualTo(userId)
assertThat(result.workspaces.size).isEqualTo(2)
}

@Test
@DisplayName("유저의 워크스페이스 조회")
fun findWorkspaces() {
@DisplayName("유저의 기본 워크스페이스 조회")
fun findDefaultWorkspace() {
// given
val userId = UserFixture.defaultUserId

// when
val result = workspaceService.findWorkspaces(userId)
val result = workspaceService.getDefaultWorkspace(userId)

// then
assertThat(result.userId).isEqualTo(userId)
assertThat(result.workspaces.size).isEqualTo(2)
assertThat(result.id).isEqualTo(2L)
assertThat(result.name).isEqualTo("워크스페이스2")
}

@Test
@DisplayName("기본 워크스페이스를 찾을 수 없을 때 실패")
fun failFindDefaultWorkspace() {
// given
val userId = UUID.fromString("00000000-0000-0000-0000-000000000004")

// when & then
assertThrows<EntityNotFoundException> {
workspaceService.getDefaultWorkspace(userId)
}.message.let {
assertThat(it).isEqualTo("Default workspace not found : $userId")
}
}

companion object {
Expand Down

0 comments on commit c6f2a62

Please sign in to comment.