Skip to content

Commit

Permalink
[#33] 유저 조회 API Conflict 해결 (#40)
Browse files Browse the repository at this point in the history
* feat: 유저 조회 기능 추가

* fix: 잘못 편집한 로직 복구

* test: 잘못된 패키지 구조 변경

* test: 잘못된 Repository DI 변경

* test: 유저 조회 test 추가

* test: 테스트 돌아가도록 수정
  • Loading branch information
kkanggu authored Aug 28, 2023
1 parent cf76b73 commit 6839ee3
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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, "유저가 수강중인 강의를 찾을 수 없습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,18 +161,15 @@ class CourseService(
return CourseResponse(name = course.name, term = course.term)
}

fun getUserCourses(userId: Long): List<UserCourseResponse> {
val findByUserIdOrderByCreatedAtDesc = userCourseRelationRepository.findByUserIdOrderByCreatedAtDesc(userId = userId)
val filter = findByUserIdOrderByCreatedAtDesc.filter {
fun getUserCourses(userId: Long): List<UserCourseResponse> =
userCourseRelationRepository.findByUserIdOrderByCreatedAtDesc(userId = userId).filter {
RegisterStatus.ACTIVE == it.registerStatus
}
return filter.map {
}.map {
UserCourseResponse(
courseId = it.course.id,
name = it.course.name,
term = it.course.term,
updatedAt = it.course.updatedAt
)
}
}
}
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))
}
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
)
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
)
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.*
Expand All @@ -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
Expand Down
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)
}
}
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) }
}
}

0 comments on commit 6839ee3

Please sign in to comment.