From 6839ee3c1f9313b76bd831e56e3fb6b1fb1f04d5 Mon Sep 17 00:00:00 2001 From: kkanggu Date: Mon, 28 Aug 2023 21:58:18 +0900 Subject: [PATCH] =?UTF-8?q?[#33]=20=EC=9C=A0=EC=A0=80=20=EC=A1=B0=ED=9A=8C?= =?UTF-8?q?=20API=20Conflict=20=ED=95=B4=EA=B2=B0=20(#40)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: 유저 조회 기능 추가 * fix: 잘못 편집한 로직 복구 * test: 잘못된 패키지 구조 변경 * test: 잘못된 Repository DI 변경 * test: 유저 조회 test 추가 * test: 테스트 돌아가도록 수정 --- .../daitssuapi/common/enums/ErrorCode.kt | 1 + .../domain/course/service/CourseService.kt | 9 ++--- .../domain/main/controller/UserController.kt | 37 +++++++++++++++++ .../domain/main/dto/response/UserResponse.kt | 9 +++++ .../domain/main/service/UserService.kt | 27 +++++++++++++ .../controller/CourseControllerTest.kt | 2 +- .../service/CourseServiceTest.kt | 6 +-- .../main/controller/UserControllerTest.kt | 40 +++++++++++++++++++ .../domain/main/service/UserServiceTest.kt | 40 +++++++++++++++++++ 9 files changed, 161 insertions(+), 10 deletions(-) create mode 100644 src/main/kotlin/com/example/daitssuapi/domain/main/controller/UserController.kt create mode 100644 src/main/kotlin/com/example/daitssuapi/domain/main/dto/response/UserResponse.kt create mode 100644 src/main/kotlin/com/example/daitssuapi/domain/main/service/UserService.kt rename src/test/kotlin/com/example/daitssuapi/domain/{main => course}/controller/CourseControllerTest.kt (96%) rename src/test/kotlin/com/example/daitssuapi/domain/{main => course}/service/CourseServiceTest.kt (89%) create mode 100644 src/test/kotlin/com/example/daitssuapi/domain/main/controller/UserControllerTest.kt create mode 100644 src/test/kotlin/com/example/daitssuapi/domain/main/service/UserServiceTest.kt diff --git a/src/main/kotlin/com/example/daitssuapi/common/enums/ErrorCode.kt b/src/main/kotlin/com/example/daitssuapi/common/enums/ErrorCode.kt index 8193037b..cd3e7133 100644 --- a/src/main/kotlin/com/example/daitssuapi/common/enums/ErrorCode.kt +++ b/src/main/kotlin/com/example/daitssuapi/common/enums/ErrorCode.kt @@ -10,6 +10,7 @@ enum class ErrorCode(val code: Int, val message: String) { DEPARTMENT_NOT_FOUND(MAIN_NUMBERING + 2, "학과를 찾을 수 없습니다"), ARTICLE_NOT_FOUND(MAIN_NUMBERING + 3, "게시글을 찾을 수 없습니다."), NICKNAME_REQUIRED(MAIN_NUMBERING + 4, "닉네임이 필요한 작업입니다."), + USER_NICKNAME_MISSING(MAIN_NUMBERING + 5, "유저의 닉네임이 없습니다"), COURSE_NOT_FOUND(COURSE_NUMBERING + 1, "과목을 찾을 수 없습니다."), USER_COURSE_RELATION_NOT_FOUND(COURSE_NUMBERING + 2, "유저가 수강중인 강의를 찾을 수 없습니다."), diff --git a/src/main/kotlin/com/example/daitssuapi/domain/course/service/CourseService.kt b/src/main/kotlin/com/example/daitssuapi/domain/course/service/CourseService.kt index 348aa0c5..54bb599f 100644 --- a/src/main/kotlin/com/example/daitssuapi/domain/course/service/CourseService.kt +++ b/src/main/kotlin/com/example/daitssuapi/domain/course/service/CourseService.kt @@ -161,12 +161,10 @@ class CourseService( return CourseResponse(name = course.name, term = course.term) } - fun getUserCourses(userId: Long): List { - val findByUserIdOrderByCreatedAtDesc = userCourseRelationRepository.findByUserIdOrderByCreatedAtDesc(userId = userId) - val filter = findByUserIdOrderByCreatedAtDesc.filter { + fun getUserCourses(userId: Long): List = + userCourseRelationRepository.findByUserIdOrderByCreatedAtDesc(userId = userId).filter { RegisterStatus.ACTIVE == it.registerStatus - } - return filter.map { + }.map { UserCourseResponse( courseId = it.course.id, name = it.course.name, @@ -174,5 +172,4 @@ class CourseService( updatedAt = it.course.updatedAt ) } - } } diff --git a/src/main/kotlin/com/example/daitssuapi/domain/main/controller/UserController.kt b/src/main/kotlin/com/example/daitssuapi/domain/main/controller/UserController.kt new file mode 100644 index 00000000..9bcd3dbd --- /dev/null +++ b/src/main/kotlin/com/example/daitssuapi/domain/main/controller/UserController.kt @@ -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 = Response(data = userService.getUser(userId = userId)) +} diff --git a/src/main/kotlin/com/example/daitssuapi/domain/main/dto/response/UserResponse.kt b/src/main/kotlin/com/example/daitssuapi/domain/main/dto/response/UserResponse.kt new file mode 100644 index 00000000..8152a3a8 --- /dev/null +++ b/src/main/kotlin/com/example/daitssuapi/domain/main/dto/response/UserResponse.kt @@ -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 +) diff --git a/src/main/kotlin/com/example/daitssuapi/domain/main/service/UserService.kt b/src/main/kotlin/com/example/daitssuapi/domain/main/service/UserService.kt new file mode 100644 index 00000000..1a4dc153 --- /dev/null +++ b/src/main/kotlin/com/example/daitssuapi/domain/main/service/UserService.kt @@ -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 + ) + } +} diff --git a/src/test/kotlin/com/example/daitssuapi/domain/main/controller/CourseControllerTest.kt b/src/test/kotlin/com/example/daitssuapi/domain/course/controller/CourseControllerTest.kt similarity index 96% rename from src/test/kotlin/com/example/daitssuapi/domain/main/controller/CourseControllerTest.kt rename to src/test/kotlin/com/example/daitssuapi/domain/course/controller/CourseControllerTest.kt index 9d76770b..b6a02dfb 100644 --- a/src/test/kotlin/com/example/daitssuapi/domain/main/controller/CourseControllerTest.kt +++ b/src/test/kotlin/com/example/daitssuapi/domain/course/controller/CourseControllerTest.kt @@ -1,4 +1,4 @@ -package com.example.daitssuapi.domain.main.controller +package com.example.daitssuapi.domain.course.controller import com.example.daitssuapi.domain.course.model.repository.UserCourseRelationRepository import com.example.daitssuapi.utils.ControllerTest diff --git a/src/test/kotlin/com/example/daitssuapi/domain/main/service/CourseServiceTest.kt b/src/test/kotlin/com/example/daitssuapi/domain/course/service/CourseServiceTest.kt similarity index 89% rename from src/test/kotlin/com/example/daitssuapi/domain/main/service/CourseServiceTest.kt rename to src/test/kotlin/com/example/daitssuapi/domain/course/service/CourseServiceTest.kt index 49bb8404..f0cf8361 100644 --- a/src/test/kotlin/com/example/daitssuapi/domain/main/service/CourseServiceTest.kt +++ b/src/test/kotlin/com/example/daitssuapi/domain/course/service/CourseServiceTest.kt @@ -1,8 +1,8 @@ -package com.example.daitssuapi.domain.main.service +package com.example.daitssuapi.domain.course.service import com.example.daitssuapi.common.enums.RegisterStatus import com.example.daitssuapi.domain.course.model.repository.UserCourseRelationRepository -import com.example.daitssuapi.domain.course.service.CourseService +import com.example.daitssuapi.domain.main.model.repository.UserRepository import com.example.daitssuapi.utils.IntegrationTest import org.assertj.core.api.Assertions.* import org.junit.jupiter.api.Assertions.* @@ -12,7 +12,7 @@ import org.junit.jupiter.api.Test @IntegrationTest class CourseServiceTest( private val courseService: CourseService, - private val userRepository: UserCourseRelationRepository, + private val userRepository: UserRepository, private val userCourseRelationRepository: UserCourseRelationRepository ) { @Test diff --git a/src/test/kotlin/com/example/daitssuapi/domain/main/controller/UserControllerTest.kt b/src/test/kotlin/com/example/daitssuapi/domain/main/controller/UserControllerTest.kt new file mode 100644 index 00000000..54e4f278 --- /dev/null +++ b/src/test/kotlin/com/example/daitssuapi/domain/main/controller/UserControllerTest.kt @@ -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) + } +} diff --git a/src/test/kotlin/com/example/daitssuapi/domain/main/service/UserServiceTest.kt b/src/test/kotlin/com/example/daitssuapi/domain/main/service/UserServiceTest.kt new file mode 100644 index 00000000..f44d6655 --- /dev/null +++ b/src/test/kotlin/com/example/daitssuapi/domain/main/service/UserServiceTest.kt @@ -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 { userService.getUser(userId = wrongUserId) } + } +}