-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 유저 조회 기능 추가 * fix: 잘못 편집한 로직 복구 * test: 잘못된 패키지 구조 변경 * test: 잘못된 Repository DI 변경 * test: 유저 조회 test 추가 * test: 테스트 돌아가도록 수정
- Loading branch information
Showing
9 changed files
with
161 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/main/kotlin/com/example/daitssuapi/domain/main/controller/UserController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.example.daitssuapi.domain.main.controller | ||
|
||
import com.example.daitssuapi.common.dto.Response | ||
import com.example.daitssuapi.domain.main.dto.response.UserResponse | ||
import com.example.daitssuapi.domain.main.service.UserService | ||
import io.swagger.v3.oas.annotations.Operation | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse | ||
import io.swagger.v3.oas.annotations.tags.Tag | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.PathVariable | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@RestController | ||
@RequestMapping("/user") | ||
@Tag(name = "user", description = "유저 API") | ||
class UserController( | ||
private val userService: UserService | ||
) { | ||
@Operation( | ||
summary = "유저 조회", | ||
responses = [ | ||
ApiResponse( | ||
responseCode = "200", | ||
description = "OK" | ||
), | ||
ApiResponse( | ||
responseCode = "404", | ||
description = "유저 없음" | ||
) | ||
] | ||
) | ||
@GetMapping("/{userId}") | ||
fun getUser( | ||
@PathVariable userId: Long | ||
): Response<UserResponse> = Response(data = userService.getUser(userId = userId)) | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/com/example/daitssuapi/domain/main/dto/response/UserResponse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.example.daitssuapi.domain.main.dto.response | ||
|
||
data class UserResponse( | ||
val studentId: Int, | ||
val name: String, | ||
val nickname: String, | ||
val departmentName: String, | ||
val term: Int | ||
) |
27 changes: 27 additions & 0 deletions
27
src/main/kotlin/com/example/daitssuapi/domain/main/service/UserService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.example.daitssuapi.domain.main.service | ||
|
||
import com.example.daitssuapi.common.enums.ErrorCode | ||
import com.example.daitssuapi.common.exception.DefaultException | ||
import com.example.daitssuapi.domain.main.dto.response.UserResponse | ||
import com.example.daitssuapi.domain.main.model.repository.UserRepository | ||
import org.springframework.data.repository.findByIdOrNull | ||
import org.springframework.http.HttpStatus | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class UserService( | ||
private val userRepository: UserRepository | ||
) { | ||
fun getUser(userId: Long): UserResponse { | ||
val user = (userRepository.findByIdOrNull(userId) | ||
?: throw DefaultException(errorCode = ErrorCode.USER_NOT_FOUND, httpStatus = HttpStatus.NOT_FOUND)) | ||
|
||
return UserResponse( | ||
studentId = user.studentId, | ||
name = user.name, | ||
nickname = user.nickname ?: throw DefaultException(errorCode = ErrorCode.USER_NICKNAME_MISSING), | ||
departmentName = user.department.name, | ||
term = user.term | ||
) | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...n/main/controller/CourseControllerTest.kt → ...course/controller/CourseControllerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/test/kotlin/com/example/daitssuapi/domain/main/controller/UserControllerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.example.daitssuapi.domain.main.controller | ||
|
||
import com.example.daitssuapi.domain.main.model.repository.UserRepository | ||
import com.example.daitssuapi.utils.ControllerTest | ||
import org.junit.jupiter.api.Assertions.* | ||
import org.junit.jupiter.api.DisplayName | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.test.web.servlet.MockMvc | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.* | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.* | ||
|
||
@ControllerTest | ||
class UserControllerTest( | ||
private val userRepository: UserRepository | ||
) { | ||
@Autowired | ||
private lateinit var mockMvc: MockMvc | ||
|
||
private val url = "/user" | ||
|
||
@Test | ||
@DisplayName("성공_올바른 userId를 이용하여 유저 조회 시_유저가 조회된다") | ||
fun success_get_course_with_user_id() { | ||
userRepository.findAll().filter { null != it.nickname }.forEach { user -> | ||
mockMvc.perform(get("$url/${user.id}")) | ||
.andExpect(status().isOk) | ||
} | ||
} | ||
|
||
@Test | ||
@DisplayName("실패_잘못된 userId를 이용하여 유저 조회 시_404를 받는다") | ||
fun success_get_empty_course_with_wrong_user_id() { | ||
val wrongUserId = 0 | ||
|
||
mockMvc.perform(get("$url/$wrongUserId")) | ||
.andExpect(status().isNotFound) | ||
.andExpect(jsonPath("$.data").isEmpty) | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/test/kotlin/com/example/daitssuapi/domain/main/service/UserServiceTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.example.daitssuapi.domain.main.service | ||
|
||
import com.example.daitssuapi.common.exception.DefaultException | ||
import com.example.daitssuapi.domain.main.model.repository.UserRepository | ||
import com.example.daitssuapi.utils.IntegrationTest | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Assertions.* | ||
import org.junit.jupiter.api.DisplayName | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertThrows | ||
|
||
@IntegrationTest | ||
class UserServiceTest( | ||
private val userRepository: UserRepository, | ||
private val userService: UserService | ||
) { | ||
@Test | ||
@DisplayName("성공_올바른 userId를 이용하여 유저 조회 시_유저가 조회된다") | ||
fun success_get_course_with_user_id() { | ||
userRepository.findAll().filter { null != it.nickname }.forEach { | ||
val findUser = userService.getUser(userId = it.id) | ||
|
||
assertAll( | ||
{ assertThat(findUser.studentId).isEqualTo(it.studentId) }, | ||
{ assertThat(findUser.name).isEqualTo(it.name) }, | ||
{ assertThat(findUser.nickname).isEqualTo(it.nickname) }, | ||
{ assertThat(findUser.departmentName).isEqualTo(it.department.name) }, | ||
{ assertThat(findUser.term).isEqualTo(it.term) } | ||
) | ||
} | ||
} | ||
|
||
@Test | ||
@DisplayName("실패_잘못된 userId를 이용하여 유저 조회 시_404를 받는다") | ||
fun success_get_empty_course_with_wrong_user_id() { | ||
val wrongUserId = 0L | ||
|
||
assertThrows<DefaultException> { userService.getUser(userId = wrongUserId) } | ||
} | ||
} |