Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge: (#301) Request 개선 #311

Merged
merged 13 commits into from
Jan 25, 2023
Merged

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package team.comit.simtong.domain.auth.usecase

import team.comit.simtong.domain.auth.dto.request.CheckAuthCodeData
import team.comit.simtong.domain.auth.exception.AuthExceptions
import team.comit.simtong.domain.auth.model.AuthCodeLimit
import team.comit.simtong.domain.auth.spi.CommandAuthCodeLimitPort
Expand All @@ -22,16 +21,16 @@ class CheckAuthCodeUseCase(
private val queryAuthCodePort: QueryAuthCodePort
) {

fun execute(request: CheckAuthCodeData) {
val authCode = queryAuthCodePort.queryAuthCodeByEmail(request.email)
fun execute(email: String, code: String) {
val authCode = queryAuthCodePort.queryAuthCodeByEmail(email)
?: throw AuthExceptions.RequiredNewEmailAuthentication()

if (!authCode.code.match(request.code)) {
if (!authCode.code.match(code)) {
throw AuthExceptions.DifferentAuthCode()
}

commandAuthCodeLimitPort.save(
AuthCodeLimit.certified(request.email)
AuthCodeLimit.certified(email)
)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package team.comit.simtong.domain.menu.usecase

import team.comit.simtong.domain.menu.dto.request.SaveMenuData
import team.comit.simtong.domain.menu.exception.MenuExceptions
import team.comit.simtong.domain.menu.spi.CommandMenuPort
import team.comit.simtong.domain.menu.spi.ParseMenuFilePort
import team.comit.simtong.domain.menu.spi.QueryMenuPort
import team.comit.simtong.global.annotation.UseCase
import java.io.File
import java.time.LocalDate
import java.util.UUID

Expand All @@ -25,11 +25,16 @@ class SaveMenuUseCase(
private val commandMenuPort: CommandMenuPort
) {

fun execute(request: SaveMenuData, spotId: UUID) {
val menu = parseMenuFilePort.importMenu(request.file, request.year, request.month, spotId)
fun execute(file: File, year: Int, month: Int, spotId: UUID) {
val menu = parseMenuFilePort.importMenu(
file = file,
year = year,
month = month,
spotId = spotId
)

if (queryMenuPort.existsMenuByMonthAndSpotId(LocalDate.of(request.year, request.month, 1), spotId)) {
throw MenuExceptions.AlreadyExistsSameMonth("${request.year}년 ${request.month}월 메뉴가 이미 존재합니다.")
if (queryMenuPort.existsMenuByMonthAndSpotId(LocalDate.of(year, month, 1), spotId)) {
throw MenuExceptions.AlreadyExistsSameMonth("${year}년 ${month}월 메뉴가 이미 존재합니다.")
}

commandMenuPort.saveAll(menu)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package team.comit.simtong.domain.user.usecase

import team.comit.simtong.domain.user.dto.request.FindEmployeeNumberData
import team.comit.simtong.domain.user.dto.response.FindEmployeeNumberResponse
import team.comit.simtong.domain.user.exception.UserExceptions
import team.comit.simtong.domain.user.spi.QueryUserPort
import team.comit.simtong.global.annotation.UseCase
import java.util.UUID

/**
*
Expand All @@ -19,8 +19,8 @@ class FindEmployeeNumberUseCase(
private val queryUserPort: QueryUserPort
) {

fun execute(request: FindEmployeeNumberData): FindEmployeeNumberResponse {
val user = queryUserPort.queryUserByNameAndSpotAndEmail(request.name, request.spotId, request.email)
fun execute(name: String, spotId: UUID, email: String): FindEmployeeNumberResponse {
val user = queryUserPort.queryUserByNameAndSpotAndEmail(name, spotId, email)
?: throw UserExceptions.NotFound()

return user.employeeNumber.value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import org.junit.jupiter.api.assertDoesNotThrow
import org.junit.jupiter.api.assertThrows
import org.mockito.BDDMockito.given
import org.springframework.boot.test.mock.mockito.MockBean
import team.comit.simtong.domain.auth.dto.request.CheckAuthCodeData
import team.comit.simtong.domain.auth.exception.AuthExceptions
import team.comit.simtong.domain.auth.model.AuthCode
import team.comit.simtong.domain.auth.model.Code
Expand Down Expand Up @@ -45,8 +44,6 @@ class CheckAuthCodeUseCaseTests {
)
}

private val requestStub = CheckAuthCodeData(email, code)

@BeforeEach
fun setUp() {
checkAuthCodeUseCase = CheckAuthCodeUseCase(
Expand All @@ -63,7 +60,7 @@ class CheckAuthCodeUseCaseTests {

// when & then
assertDoesNotThrow {
checkAuthCodeUseCase.execute(requestStub)
checkAuthCodeUseCase.execute(email, code)
}
}

Expand All @@ -75,7 +72,7 @@ class CheckAuthCodeUseCaseTests {

// when & then
assertThrows<AuthExceptions.RequiredNewEmailAuthentication> {
checkAuthCodeUseCase.execute(requestStub)
checkAuthCodeUseCase.execute(email, code)
}
}

Expand All @@ -87,7 +84,7 @@ class CheckAuthCodeUseCaseTests {

// when & then
assertThrows<AuthExceptions.DifferentAuthCode> {
checkAuthCodeUseCase.execute(requestStub)
checkAuthCodeUseCase.execute(email, code)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import org.junit.jupiter.api.assertDoesNotThrow
import org.junit.jupiter.api.assertThrows
import org.mockito.kotlin.given
import org.springframework.boot.test.mock.mockito.MockBean
import team.comit.simtong.domain.menu.dto.request.SaveMenuData
import team.comit.simtong.domain.menu.exception.MenuExceptions
import team.comit.simtong.domain.menu.model.Menu
import team.comit.simtong.domain.menu.spi.CommandMenuPort
Expand Down Expand Up @@ -44,14 +43,6 @@ class SaveMenuUseCaseTests {
)
}

private val requestStub: SaveMenuData by lazy {
SaveMenuData(
file = fileStub,
year = year,
month = month
)
}

@BeforeEach
fun setUp() {
saveMenuUseCase = SaveMenuUseCase(
Expand All @@ -72,7 +63,7 @@ class SaveMenuUseCaseTests {

// when & then
assertDoesNotThrow {
saveMenuUseCase.execute(requestStub, spotId)
saveMenuUseCase.execute(fileStub, year, month, spotId)
}
}

Expand All @@ -87,7 +78,7 @@ class SaveMenuUseCaseTests {

// when & then
assertThrows<MenuExceptions.AlreadyExistsSameMonth> {
saveMenuUseCase.execute(requestStub, spotId)
saveMenuUseCase.execute(fileStub, year, month, spotId)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.mockito.BDDMockito.given
import org.springframework.boot.test.mock.mockito.MockBean
import team.comit.simtong.domain.user.dto.request.FindEmployeeNumberData
import team.comit.simtong.domain.user.exception.UserExceptions
import team.comit.simtong.domain.user.model.Authority
import team.comit.simtong.domain.user.model.User
Expand Down Expand Up @@ -44,14 +43,6 @@ class FindEmployeeNumberUseCaseTests {
)
}

private val requestStub: FindEmployeeNumberData by lazy {
FindEmployeeNumberData(
name = name,
spotId = spotId,
email = email
)
}

@BeforeEach
fun setUp() {
findEmployeeNumberUseCase = FindEmployeeNumberUseCase(queryUserPort)
Expand All @@ -64,7 +55,7 @@ class FindEmployeeNumberUseCaseTests {
.willReturn(userStub)

// when
val response = findEmployeeNumberUseCase.execute(requestStub)
val response = findEmployeeNumberUseCase.execute(name, spotId, email)

// when & then
assertEquals(response.employeeNumber, employeeNumber)
Expand All @@ -78,7 +69,7 @@ class FindEmployeeNumberUseCaseTests {

// when & then
assertThrows<UserExceptions.NotFound> {
findEmployeeNumberUseCase.execute(requestStub)
findEmployeeNumberUseCase.execute(name, spotId, email)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ data class AuthCode(

fun issue(email: String) = AuthCode(
key = email,
code = Code.defaultValue(),
code = Code.generate(),
expirationTime = EXPIRED
)
}
Expand All @@ -61,6 +61,6 @@ value class Code private constructor(
companion object {
fun of(value: String) = Code(value)

fun defaultValue() = Code(RandomString(6).nextString())
fun generate() = Code(RandomString(6).nextString())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import javax.validation.ConstraintViolationException
* @author kimbeomjin
* @author Chokyunghyeon
* @date 2022/08/22
* @version 1.2.3
* @version 1.2.5
**/
@RestControllerAdvice
class WebErrorHandler {
Expand Down Expand Up @@ -92,7 +92,7 @@ class WebErrorHandler {
protected fun handleHttpMessageNotReadableException(
exception: HttpMessageNotReadableException
): ErrorResponse? {
return ErrorResponse.of(GlobalExceptions.BadRequest())
return ErrorResponse.of(exception)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import org.springframework.validation.BindingResult
**/
class CustomFieldError(
val field: String,
val value: String,
val value: String?,
val reason: String
) {
companion object {
fun of(field: String, value: String, reason: String): List<CustomFieldError> {
fun of(field: String, value: String?, reason: String): List<CustomFieldError> {
val fieldErrors: MutableList<CustomFieldError> = ArrayList()
fieldErrors.add(CustomFieldError(field, value, reason))
return fieldErrors
Expand All @@ -27,7 +27,7 @@ class CustomFieldError(
return fieldErrors.map { error ->
CustomFieldError(
field = error.field,
value = error.rejectedValue.toString(),
value = error.rejectedValue?.toString(),
reason = error.defaultMessage!!
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package team.comit.simtong.global.error.dto

import org.springframework.dao.DataIntegrityViolationException
import org.springframework.http.converter.HttpMessageNotReadableException
import org.springframework.validation.BindingResult
import org.springframework.web.bind.MissingServletRequestParameterException
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException
Expand All @@ -16,7 +17,7 @@ import javax.validation.ConstraintViolationException
* @author kimbeomjin
* @author Chokyunghyeon
* @date 2022/08/22
* @version 1.2.3
* @version 1.2.5
**/
class ErrorResponse(
val status: Int,
Expand Down Expand Up @@ -44,10 +45,10 @@ class ErrorResponse(

fun of(exception: ConstraintViolationException): ErrorResponse {
val fieldErrors = exception.constraintViolations.flatMap { violation ->
val path = violation.propertyPath
val field = path.last().name
val field = violation.propertyPath.last().name
val value = violation.invalidValue?.toString()
val message = violation.message
CustomFieldError.of(field, "", message)
CustomFieldError.of(field, value, message)
}

return of(
Expand All @@ -57,10 +58,9 @@ class ErrorResponse(
}

fun of(exception: MethodArgumentTypeMismatchException): ErrorResponse {
val value = exception.value
val fieldErrors = CustomFieldError.of(
field = exception.parameter.parameterName ?: "",
value = value.toString(),
value = exception.value?.toString(),
reason = "${exception.requiredType!!.name} 타입으로 변환할 수 없습니다."
)

Expand All @@ -84,10 +84,14 @@ class ErrorResponse(
fieldErrors = CustomFieldError.of("", "", exception.message ?: "")
)

fun of(exception: IllegalArgumentException): ErrorResponse = of(
exception = GlobalExceptions.BadRequest(),
fieldErrors = CustomFieldError.of("", "", exception.message ?: "")
)
fun of(exception: HttpMessageNotReadableException): ErrorResponse {
val message = exception.mostSpecificCause.message?.substringBeforeLast('\n') ?: ""

return of(
exception = GlobalExceptions.BadRequest(),
fieldErrors = CustomFieldError.of("", "", message)
)
}

private fun of(exception: BusinessException, fieldErrors: List<CustomFieldError>) = ErrorResponse(
status = exception.status,
Expand Down
Loading