Skip to content

Commit

Permalink
fix: modify package location and add response wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
DongGeon0908 committed Jul 2, 2024
1 parent 9248200 commit 6dc7d53
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.hero.alignlab.dto
package com.hero.alignlab.common.model

import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Pageable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.hero.alignlab.dto
package com.hero.alignlab.common.model

import com.hero.alignlab.exception.ErrorCode
import jakarta.validation.ConstraintViolationException
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.hero.alignlab.dto
package com.hero.alignlab.common.model

import org.springframework.data.domain.Page
import org.springframework.data.domain.Sort

data class PageResponseDto<T>(
data class PageResponse<T>(
val data: List<T>,
val page: Int?,
val size: Int?,
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/com/hero/alignlab/common/model/Response.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.hero.alignlab.common.model

data class Response<T>(
val data: T
)
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.hero.alignlab.dto
package com.hero.alignlab.common.model

import org.springframework.data.domain.Slice
import org.springframework.data.domain.Sort

data class SliceResponseDto<T>(
data class SliceResponse<T>(
val data: List<T>,
val page: Int?,
val size: Int?,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.hero.alignlab.exception.advice

import com.hero.alignlab.dto.ErrorResponse
import com.hero.alignlab.common.model.ErrorResponse
import com.hero.alignlab.exception.AlignlabException
import io.github.oshai.kotlinlogging.KotlinLogging
import org.springframework.core.Ordered
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.hero.alignlab.exception.advice

import com.hero.alignlab.dto.ErrorResponse
import com.hero.alignlab.common.model.ErrorResponse
import io.github.oshai.kotlinlogging.KotlinLogging
import jakarta.validation.ConstraintViolationException
import kotlinx.coroutines.CancellationException
Expand Down
11 changes: 7 additions & 4 deletions src/main/kotlin/com/hero/alignlab/extension/ResponseExtension.kt
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
package com.hero.alignlab.extension

import com.hero.alignlab.dto.PageResponseDto
import com.hero.alignlab.common.model.PageResponse
import com.hero.alignlab.common.model.Response
import org.springframework.data.domain.Page
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import java.net.URI

/** Wrap Response Page */
fun <T> Page<T>.wrapPage() = PageResponseDto(this)
fun <T> Page<T>.wrapPage() = PageResponse(this)

/** Wrap Response Ok */
fun <T> T.wrapOk() = ResponseEntity.ok(this)
fun <T> T.wrapOk() = ResponseEntity.ok(Response(this))

/** Wrap Response Created */
fun <T> T.wrapCreated() = ResponseEntity.status(HttpStatus.CREATED).body(this)
fun <T> T.wrapCreated() = ResponseEntity.status(HttpStatus.CREATED)
.body(Response(this))

/** Wrap Response Void */
fun Unit.wrapVoid() = ResponseEntity.noContent().build<Unit>()

/** Wrap Response Redirect */
fun String.wrapRedirected(): ResponseEntity<Unit> {
val headers = HttpHeaders()
headers.location = URI.create(this)
Expand Down

0 comments on commit 6dc7d53

Please sign in to comment.