From 8eee37c478b976ea34badf23c04f2390a042e194 Mon Sep 17 00:00:00 2001 From: Park Jeongseop Date: Thu, 28 Sep 2023 13:35:39 +0900 Subject: [PATCH] Feat/40 validation (#93) * feat: apply validation * docs: update open-api.yaml * chore: apply lint * feat: add validation * docs: update open-api.yaml --------- Co-authored-by: Git Actions --- docs/open-api.yaml | 2 ++ .../group4/ticketingservice/TimeE2ETest.kt | 12 ++++---- .../controller/BookmarkController.kt | 7 ++++- .../controller/EventController.kt | 16 ++++++---- .../controller/ReservationController.kt | 14 ++++++--- .../ticketingservice/dto/BookmarkFromdto.kt | 7 ++++- .../group4/ticketingservice/dto/EventDto.kt | 30 +++++++++++++++---- .../ticketingservice/dto/ReservationDto.kt | 14 +++++++-- .../service/BookmarkService.kt | 2 +- .../event/EventControllerTest.kt | 6 ++-- 10 files changed, 79 insertions(+), 31 deletions(-) diff --git a/docs/open-api.yaml b/docs/open-api.yaml index 0de08d43..031d75f9 100644 --- a/docs/open-api.yaml +++ b/docs/open-api.yaml @@ -333,6 +333,8 @@ components: type: object properties: title: + maxLength: 255 + minLength: 1 type: string date: type: string diff --git a/src/integrationTest/kotlin/com/group4/ticketingservice/TimeE2ETest.kt b/src/integrationTest/kotlin/com/group4/ticketingservice/TimeE2ETest.kt index 2e206367..c85d2b02 100644 --- a/src/integrationTest/kotlin/com/group4/ticketingservice/TimeE2ETest.kt +++ b/src/integrationTest/kotlin/com/group4/ticketingservice/TimeE2ETest.kt @@ -59,9 +59,9 @@ class TimeE2ETest @Autowired constructor( @Test fun `All API only returns OffsetDateTime in UTC without any offset info`() { val eventCreateRequest = "{\"title\":\"test title\"," + - "\"date\":\"2022-09-01T21:00:00.001+09:00\"," + - "\"reservationStartTime\":\"2022-09-01T22:00:00.001+09:00\"," + - "\"reservationEndTime\":\"2022-09-01T23:00:00.001+09:00\"," + + "\"date\":\"2044-02-04T21:00:00.001+09:00\"," + + "\"reservationStartTime\":\"2044-01-01T22:00:00.001+09:00\"," + + "\"reservationEndTime\":\"2044-01-01T23:00:00.001+09:00\"," + "\"maxAttendees\":10}" mockMvc.perform( @@ -71,8 +71,8 @@ class TimeE2ETest @Autowired constructor( ) .andExpect(status().isOk) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("$.date").value("2022-09-01T12:00:00.001Z")) - .andExpect(jsonPath("$.reservationStartTime").value("2022-09-01T13:00:00.001Z")) - .andExpect(jsonPath("$.reservationEndTime").value("2022-09-01T14:00:00.001Z")) + .andExpect(jsonPath("$.date").value("2044-02-04T12:00:00.001Z")) + .andExpect(jsonPath("$.reservationStartTime").value("2044-01-01T13:00:00.001Z")) + .andExpect(jsonPath("$.reservationEndTime").value("2044-01-01T14:00:00.001Z")) } } diff --git a/src/main/kotlin/com/group4/ticketingservice/controller/BookmarkController.kt b/src/main/kotlin/com/group4/ticketingservice/controller/BookmarkController.kt index 1bf6d9c1..bd66af72 100644 --- a/src/main/kotlin/com/group4/ticketingservice/controller/BookmarkController.kt +++ b/src/main/kotlin/com/group4/ticketingservice/controller/BookmarkController.kt @@ -2,6 +2,7 @@ package com.group4.ticketingservice.controller import com.group4.ticketingservice.dto.BookmarkFromdto import com.group4.ticketingservice.service.BookmarkService +import jakarta.validation.Valid import org.springframework.beans.factory.annotation.Autowired import org.springframework.http.HttpHeaders import org.springframework.http.HttpStatus @@ -22,7 +23,11 @@ class BookmarkController @Autowired constructor(val bookmarkService: BookmarkSer // 북마크 등록 @PostMapping - fun addBookmark(@AuthenticationPrincipal userId: Int, @RequestBody boardFormDto: BookmarkFromdto): ResponseEntity { + fun addBookmark( + @AuthenticationPrincipal userId: Int, + @RequestBody @Valid + boardFormDto: BookmarkFromdto + ): ResponseEntity { val savedBookmarkId = bookmarkService.create(userId, boardFormDto) val headers = HttpHeaders() headers.set("Content-Location", "/bookmark/%d".format(savedBookmarkId)) diff --git a/src/main/kotlin/com/group4/ticketingservice/controller/EventController.kt b/src/main/kotlin/com/group4/ticketingservice/controller/EventController.kt index 7a4c2d4d..dd2fd765 100644 --- a/src/main/kotlin/com/group4/ticketingservice/controller/EventController.kt +++ b/src/main/kotlin/com/group4/ticketingservice/controller/EventController.kt @@ -3,6 +3,7 @@ package com.group4.ticketingservice.controller import com.group4.ticketingservice.dto.EventCreateRequest import com.group4.ticketingservice.dto.EventResponse import com.group4.ticketingservice.service.EventService +import jakarta.validation.Valid import org.springframework.beans.factory.annotation.Autowired import org.springframework.http.HttpStatus import org.springframework.http.ResponseEntity @@ -21,13 +22,16 @@ class EventController @Autowired constructor( // TimeE2ETest를 위한 임시 EndPoint입니다. @PostMapping - fun createEvent(@RequestBody request: EventCreateRequest): ResponseEntity { + fun createEvent( + @RequestBody @Valid + request: EventCreateRequest + ): ResponseEntity { val event = eventService.createEvent( - request.title, - request.date, - request.reservationStartTime, - request.reservationEndTime, - request.maxAttendees + request.title!!, + request.date!!, + request.reservationStartTime!!, + request.reservationEndTime!!, + request.maxAttendees!! ) val response = EventResponse( id = event.id!!, diff --git a/src/main/kotlin/com/group4/ticketingservice/controller/ReservationController.kt b/src/main/kotlin/com/group4/ticketingservice/controller/ReservationController.kt index 87fc8fde..c9982adb 100644 --- a/src/main/kotlin/com/group4/ticketingservice/controller/ReservationController.kt +++ b/src/main/kotlin/com/group4/ticketingservice/controller/ReservationController.kt @@ -5,6 +5,7 @@ import com.group4.ticketingservice.dto.ReservationResponse import com.group4.ticketingservice.dto.ReservationUpdateRequest import com.group4.ticketingservice.entity.Reservation import com.group4.ticketingservice.service.ReservationService +import jakarta.validation.Valid import org.springframework.http.ResponseEntity import org.springframework.security.core.annotation.AuthenticationPrincipal import org.springframework.web.bind.annotation.DeleteMapping @@ -20,9 +21,13 @@ import org.springframework.web.bind.annotation.RestController @RequestMapping("/reservations") class ReservationController(val reservationService: ReservationService) { @PostMapping - fun createReservation(@AuthenticationPrincipal userId: Int, @RequestBody request: ReservationCreateRequest): ResponseEntity { + fun createReservation( + @AuthenticationPrincipal userId: Int, + @RequestBody @Valid + request: ReservationCreateRequest + ): ResponseEntity { val reservation: Reservation = reservationService.createReservation( - request.eventId, + request.eventId!!, userId ) val response = ReservationResponse( @@ -49,9 +54,10 @@ class ReservationController(val reservationService: ReservationService) { @PutMapping("/{id}") fun updateReservation( @PathVariable id: Int, - @RequestBody request: ReservationUpdateRequest + @RequestBody @Valid + request: ReservationUpdateRequest ): ResponseEntity { - val reservation = reservationService.updateReservation(id, request.eventId) + val reservation = reservationService.updateReservation(id, request.eventId!!) val response = ReservationResponse( id = reservation.id!!, eventId = reservation.event.id!!, diff --git a/src/main/kotlin/com/group4/ticketingservice/dto/BookmarkFromdto.kt b/src/main/kotlin/com/group4/ticketingservice/dto/BookmarkFromdto.kt index 87f24297..72dd3ca9 100644 --- a/src/main/kotlin/com/group4/ticketingservice/dto/BookmarkFromdto.kt +++ b/src/main/kotlin/com/group4/ticketingservice/dto/BookmarkFromdto.kt @@ -1,5 +1,10 @@ package com.group4.ticketingservice.dto +import jakarta.validation.constraints.NotNull +import jakarta.validation.constraints.Positive + data class BookmarkFromdto( - var event_id: Int + @field:NotNull + @field:Positive + var event_id: Int? ) diff --git a/src/main/kotlin/com/group4/ticketingservice/dto/EventDto.kt b/src/main/kotlin/com/group4/ticketingservice/dto/EventDto.kt index 258f3ace..98fbf429 100644 --- a/src/main/kotlin/com/group4/ticketingservice/dto/EventDto.kt +++ b/src/main/kotlin/com/group4/ticketingservice/dto/EventDto.kt @@ -1,17 +1,35 @@ package com.group4.ticketingservice.dto +import jakarta.validation.constraints.Future +import jakarta.validation.constraints.FutureOrPresent +import jakarta.validation.constraints.NotNull +import jakarta.validation.constraints.Positive +import jakarta.validation.constraints.PositiveOrZero +import jakarta.validation.constraints.Size import java.time.OffsetDateTime data class EventCreateRequest( - val title: String, - val date: OffsetDateTime, - val reservationStartTime: OffsetDateTime, - val reservationEndTime: OffsetDateTime, - val maxAttendees: Int + @field:NotNull + @field:Size(min = 1, max = 255) + val title: String?, + @field:NotNull + @field:Future + val date: OffsetDateTime?, + @field:NotNull + @field:FutureOrPresent + val reservationStartTime: OffsetDateTime?, + @field:NotNull + @field:Future + val reservationEndTime: OffsetDateTime?, + @field:NotNull + @field:PositiveOrZero + val maxAttendees: Int? ) data class EventDeleteRequest( - val id: Int + @field:NotNull + @field:Positive + val id: Int? ) data class EventResponse( diff --git a/src/main/kotlin/com/group4/ticketingservice/dto/ReservationDto.kt b/src/main/kotlin/com/group4/ticketingservice/dto/ReservationDto.kt index 38d10d46..5a251b94 100644 --- a/src/main/kotlin/com/group4/ticketingservice/dto/ReservationDto.kt +++ b/src/main/kotlin/com/group4/ticketingservice/dto/ReservationDto.kt @@ -1,17 +1,25 @@ package com.group4.ticketingservice.dto +import jakarta.validation.constraints.NotNull +import jakarta.validation.constraints.Positive import java.time.OffsetDateTime data class ReservationCreateRequest( - val eventId: Int + @field:NotNull + @field:Positive + val eventId: Int? ) data class ReservationUpdateRequest( - val eventId: Int + @field:NotNull + @field:Positive + val eventId: Int? ) data class ReservationDeleteRequest( - val id: Int + @field:NotNull + @field:Positive + val id: Int? ) data class ReservationResponse( diff --git a/src/main/kotlin/com/group4/ticketingservice/service/BookmarkService.kt b/src/main/kotlin/com/group4/ticketingservice/service/BookmarkService.kt index 514b5902..003908f1 100644 --- a/src/main/kotlin/com/group4/ticketingservice/service/BookmarkService.kt +++ b/src/main/kotlin/com/group4/ticketingservice/service/BookmarkService.kt @@ -20,7 +20,7 @@ class BookmarkService @Autowired constructor( fun create(userId: Int, bookmarkFormDto: BookmarkFromdto): Int? { val user: User = userRepository.getReferenceById(userId) - val event: Event = eventRepository.getReferenceById(bookmarkFormDto.event_id) + val event: Event = eventRepository.getReferenceById(bookmarkFormDto.event_id!!) val bookmark = Bookmark(user = user, event = event) diff --git a/src/test/kotlin/com/group4/ticketingservice/event/EventControllerTest.kt b/src/test/kotlin/com/group4/ticketingservice/event/EventControllerTest.kt index 13cd2964..854c09fd 100644 --- a/src/test/kotlin/com/group4/ticketingservice/event/EventControllerTest.kt +++ b/src/test/kotlin/com/group4/ticketingservice/event/EventControllerTest.kt @@ -68,9 +68,9 @@ class EventControllerTest( every { eventService.createEvent(any(), any(), any(), any(), any()) } returns sampleEvent val eventCreateRequest = "{\"title\":\"test title\"," + - "\"date\":\"2022-09-01T21:00:00.001+09:00\"," + - "\"reservationStartTime\":\"2022-09-01T22:00:00.001+09:00\"," + - "\"reservationEndTime\":\"2022-09-01T23:00:00.001+09:00\"," + + "\"date\":\"2044-02-04T21:00:00.001+09:00\"," + + "\"reservationStartTime\":\"2044-01-01T22:00:00.001+09:00\"," + + "\"reservationEndTime\":\"2044-01-01T23:00:00.001+09:00\"," + "\"maxAttendees\":10}" mockMvc.perform(