Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
rudeh2926 committed Aug 29, 2024
2 parents ef9db42 + e12252a commit 0995774
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestController
import java.util.UUID
import javax.validation.Valid

@Tag(name = "application API")
@RestController
Expand All @@ -41,7 +42,8 @@ class ApplicationController(
@ResponseStatus(value = HttpStatus.CREATED)
@PostMapping
fun application(
@RequestBody applicationRequest: ApplicationRequest
@Valid @RequestBody
applicationRequest: ApplicationRequest
) = applicationUseCase.application(applicationRequest)

@Operation(summary = "외출신청 수락또는 거절 API")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package dsm.pick2024.domain.application.presentation.dto.request

import dsm.pick2024.domain.application.enums.ApplicationType
import dsm.pick2024.global.annotation.ValidFormat

@ValidFormat
data class ApplicationRequest(
val reason: String,
val start: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestController
import javax.validation.Valid

@Tag(name = "early API")
@RestController
Expand All @@ -37,7 +38,8 @@ class EarlyReturnController(
@ResponseStatus(value = HttpStatus.CREATED)
@PostMapping("/create")
fun createEarlyReturn(
@RequestBody createEarlyReturnRequest: CreateEarlyReturnRequest
@Valid @RequestBody
createEarlyReturnRequest: CreateEarlyReturnRequest
) = createEarlyReturnUseCase.createEarlyReturn(createEarlyReturnRequest)

@Operation(summary = "조기귀가 신청 수락또는 거절 API")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package dsm.pick2024.domain.earlyreturn.presentation.dto.request

import dsm.pick2024.global.annotation.ValidFormat

@ValidFormat
data class CreateEarlyReturnRequest(
val reason: String,
val start: String
Expand Down
15 changes: 15 additions & 0 deletions src/main/kotlin/dsm/pick2024/global/annotation/ValidFormat.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package dsm.pick2024.global.annotation

import dsm.pick2024.global.annotation.common.FormatValidator
import javax.validation.Constraint
import javax.validation.Payload
import kotlin.reflect.KClass

@Constraint(validatedBy = [FormatValidator::class])
@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.RUNTIME)
annotation class ValidFormat(
val message: String = "형식이 올바르지 않습니다.",
val groups: Array<KClass<*>> = [], // 어떤 타입의 데이터든 포함할 수 있다
val payload: Array<KClass<out Payload>> = [] //Payload 타입을 상속받는 어떤 클래스든 수용할 수 있음을 의미
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package dsm.pick2024.global.annotation.common

import dsm.pick2024.domain.application.enums.ApplicationType
import dsm.pick2024.domain.application.presentation.dto.request.ApplicationRequest
import dsm.pick2024.domain.earlyreturn.presentation.dto.request.CreateEarlyReturnRequest
import dsm.pick2024.global.annotation.ValidFormat
import javax.validation.ConstraintValidator
import javax.validation.ConstraintValidatorContext
import java.time.LocalTime
import java.time.format.DateTimeParseException

class FormatValidator : ConstraintValidator<ValidFormat, Any> {

override fun isValid(request: Any, context: ConstraintValidatorContext): Boolean {
return when (request) {
is ApplicationRequest -> validateApplicationRequest(request)
is CreateEarlyReturnRequest -> validateCreateEarlyReturnRequest(request)
else -> false
}
}

private fun validateApplicationRequest(request: ApplicationRequest): Boolean {
return if (request.applicationType == ApplicationType.PERIOD) {
isValidPeriodFormat(request.start) && isValidPeriodFormat(request.end)
} else {
isValidTimeFormat(request.start) && isValidTimeFormat(request.end)
}
}

private fun validateCreateEarlyReturnRequest(request: CreateEarlyReturnRequest): Boolean {
return isValidTimeFormat(request.start)
}

private fun isValidPeriodFormat(period: String): Boolean {
return Regex("""\d+교시""").matches(period)
}

private fun isValidTimeFormat(time: String): Boolean {
return try {
LocalTime.parse(time)
true
} catch (e: DateTimeParseException) {
false
}
}
}

0 comments on commit 0995774

Please sign in to comment.