Skip to content

Commit

Permalink
Feat/40 validation (#93)
Browse files Browse the repository at this point in the history
* feat: apply validation

* docs: update open-api.yaml

* chore: apply lint

* feat: add validation

* docs: update open-api.yaml

---------

Co-authored-by: Git Actions <no-reply@github.com>
  • Loading branch information
ParkJeongseop and Git Actions authored Sep 28, 2023
1 parent b0fbfad commit 8eee37c
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 31 deletions.
2 changes: 2 additions & 0 deletions docs/open-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ components:
type: object
properties:
title:
maxLength: 255
minLength: 1
type: string
date:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,7 +23,11 @@ class BookmarkController @Autowired constructor(val bookmarkService: BookmarkSer

// 북마크 등록
@PostMapping
fun addBookmark(@AuthenticationPrincipal userId: Int, @RequestBody boardFormDto: BookmarkFromdto): ResponseEntity<Any> {
fun addBookmark(
@AuthenticationPrincipal userId: Int,
@RequestBody @Valid
boardFormDto: BookmarkFromdto
): ResponseEntity<Any> {
val savedBookmarkId = bookmarkService.create(userId, boardFormDto)
val headers = HttpHeaders()
headers.set("Content-Location", "/bookmark/%d".format(savedBookmarkId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,13 +22,16 @@ class EventController @Autowired constructor(

// TimeE2ETest를 위한 임시 EndPoint입니다.
@PostMapping
fun createEvent(@RequestBody request: EventCreateRequest): ResponseEntity<EventResponse> {
fun createEvent(
@RequestBody @Valid
request: EventCreateRequest
): ResponseEntity<EventResponse> {
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!!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<ReservationResponse> {
fun createReservation(
@AuthenticationPrincipal userId: Int,
@RequestBody @Valid
request: ReservationCreateRequest
): ResponseEntity<ReservationResponse> {
val reservation: Reservation = reservationService.createReservation(
request.eventId,
request.eventId!!,
userId
)
val response = ReservationResponse(
Expand All @@ -49,9 +54,10 @@ class ReservationController(val reservationService: ReservationService) {
@PutMapping("/{id}")
fun updateReservation(
@PathVariable id: Int,
@RequestBody request: ReservationUpdateRequest
@RequestBody @Valid
request: ReservationUpdateRequest
): ResponseEntity<ReservationResponse> {
val reservation = reservationService.updateReservation(id, request.eventId)
val reservation = reservationService.updateReservation(id, request.eventId!!)
val response = ReservationResponse(
id = reservation.id!!,
eventId = reservation.event.id!!,
Expand Down
Original file line number Diff line number Diff line change
@@ -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?
)
30 changes: 24 additions & 6 deletions src/main/kotlin/com/group4/ticketingservice/dto/EventDto.kt
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
14 changes: 11 additions & 3 deletions src/main/kotlin/com/group4/ticketingservice/dto/ReservationDto.kt
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 8eee37c

Please sign in to comment.