diff --git a/api-server-mvc/src/main/kotlin/com/knet/dormitory/web/alarm/AlarmController.kt b/api-server-mvc/src/main/kotlin/com/knet/dormitory/web/alarm/AlarmController.kt deleted file mode 100644 index 716fbd1..0000000 --- a/api-server-mvc/src/main/kotlin/com/knet/dormitory/web/alarm/AlarmController.kt +++ /dev/null @@ -1,32 +0,0 @@ -package com.knet.dormitory.web.alarm - -import com.knet.dormitory.domain.alarm.AlarmService -import com.knet.dormitory.domain.alarm.AlarmTopic -import com.knet.dormitory.web.alarm.dto.SendMessageRequest -import com.knet.dormitory.web.shared.CommonResponse -import org.springframework.http.HttpStatus -import org.springframework.web.bind.annotation.PostMapping -import org.springframework.web.bind.annotation.RequestBody -import org.springframework.web.bind.annotation.RequestMapping -import org.springframework.web.bind.annotation.RestController - -@RestController -@RequestMapping("/alarm") -class AlarmController( - private val alarmService: AlarmService -) { - @PostMapping("/test") - fun test() { - alarmService.sendMessage("test title", "test body", AlarmTopic.KW_DORM_COMMON) - } - - @PostMapping("/send") - fun sendMessage(@RequestBody dto: SendMessageRequest): CommonResponse { - if (isValid(dto)) throw IllegalStateException("입력값에 null이 있으면 안됩니다.") - alarmService.sendMessage(dto.title!!, dto.body!!, dto.topic!!) - return CommonResponse(status = HttpStatus.OK, message = "알림이 성공적으로 보내졌습니다.") - } - - fun isValid(dto: SendMessageRequest): Boolean = dto.body == null || dto.title == null || dto.topic == null -} - diff --git a/api-server-mvc/src/main/kotlin/com/knet/dormitory/web/alarm/dto/SendMessageRequest.kt b/api-server-mvc/src/main/kotlin/com/knet/dormitory/web/alarm/dto/SendMessageRequest.kt index 35e87f1..13843fe 100644 --- a/api-server-mvc/src/main/kotlin/com/knet/dormitory/web/alarm/dto/SendMessageRequest.kt +++ b/api-server-mvc/src/main/kotlin/com/knet/dormitory/web/alarm/dto/SendMessageRequest.kt @@ -1,9 +1,13 @@ package com.knet.dormitory.web.alarm.dto import com.knet.dormitory.domain.alarm.AlarmTopic +import jakarta.validation.constraints.NotNull class SendMessageRequest( + @field:NotNull(message = "title is null") val title: String? = null, + @field:NotNull(message = "body is null") val body: String? = null, + @field:NotNull(message = "topic is null") val topic: AlarmTopic? = null ) \ No newline at end of file diff --git a/api-server-mvc/src/main/kotlin/com/knet/dormitory/web/shared/exception/GlobalExceptionHandler.kt b/api-server-mvc/src/main/kotlin/com/knet/dormitory/web/shared/exception/GlobalExceptionHandler.kt new file mode 100644 index 0000000..696b950 --- /dev/null +++ b/api-server-mvc/src/main/kotlin/com/knet/dormitory/web/shared/exception/GlobalExceptionHandler.kt @@ -0,0 +1,36 @@ +package com.knet.dormitory.web.shared.exception + +import com.knet.dormitory.web.shared.dto.ErrorResponse +import org.springframework.http.HttpStatus +import org.springframework.http.ResponseEntity +import org.springframework.web.bind.MethodArgumentNotValidException +import org.springframework.web.bind.annotation.ExceptionHandler +import org.springframework.web.bind.annotation.RestControllerAdvice + +@RestControllerAdvice +class GlobalExceptionHandler { + + @ExceptionHandler(MethodArgumentNotValidException::class) + fun methodArgumentNotValidException( + exception: MethodArgumentNotValidException + ): ResponseEntity = ResponseEntity + .status(HttpStatus.BAD_REQUEST) + .body( + ErrorResponse( + status = HttpStatus.BAD_REQUEST, + message = exception.bindingResult.allErrors[0].defaultMessage ?: "" + ) + ) + + @ExceptionHandler(IllegalArgumentException::class) + fun illegalArgumentException( + exception: IllegalArgumentException + ): ResponseEntity = ResponseEntity + .status(HttpStatus.BAD_REQUEST) + .body( + ErrorResponse( + status = HttpStatus.BAD_REQUEST, + message = exception.message ?: "" + ) + ) +} \ No newline at end of file