|
@@ -1,14 +1,20 @@
|
|
package com.swagger.rest.controllers
|
|
package com.swagger.rest.controllers
|
|
|
|
|
|
|
|
+import com.swagger.rest.models.MemberOutput
|
|
import com.swagger.rest.models.User
|
|
import com.swagger.rest.models.User
|
|
import com.swagger.rest.models.UserInput
|
|
import com.swagger.rest.models.UserInput
|
|
import com.swagger.rest.repositories.MemberRepository
|
|
import com.swagger.rest.repositories.MemberRepository
|
|
import com.swagger.rest.repositories.UserRepository
|
|
import com.swagger.rest.repositories.UserRepository
|
|
|
|
+import org.springframework.data.domain.PageRequest
|
|
|
|
+import org.springframework.data.domain.Pageable
|
|
|
|
+import org.springframework.data.domain.Sort
|
|
import org.springframework.http.HttpStatus
|
|
import org.springframework.http.HttpStatus
|
|
import org.springframework.http.ResponseEntity
|
|
import org.springframework.http.ResponseEntity
|
|
import org.springframework.security.core.context.SecurityContextHolder
|
|
import org.springframework.security.core.context.SecurityContextHolder
|
|
import org.springframework.security.crypto.password.PasswordEncoder
|
|
import org.springframework.security.crypto.password.PasswordEncoder
|
|
import org.springframework.web.bind.annotation.*
|
|
import org.springframework.web.bind.annotation.*
|
|
|
|
+import java.util.ArrayList
|
|
|
|
+import java.util.HashMap
|
|
|
|
|
|
@RestController
|
|
@RestController
|
|
@RequestMapping("/api/v1")
|
|
@RequestMapping("/api/v1")
|
|
@@ -18,21 +24,51 @@ class UserController(
|
|
private val memberRepository: MemberRepository
|
|
private val memberRepository: MemberRepository
|
|
) {
|
|
) {
|
|
|
|
|
|
|
|
+ private fun getSortDirection(direction: String): Sort.Direction {
|
|
|
|
+ if (direction == "asc") {
|
|
|
|
+ return Sort.Direction.ASC
|
|
|
|
+ } else if (direction == "desc") {
|
|
|
|
+ return Sort.Direction.DESC
|
|
|
|
+ }
|
|
|
|
+ return Sort.Direction.ASC
|
|
|
|
+ }
|
|
|
|
+
|
|
@GetMapping("/users")
|
|
@GetMapping("/users")
|
|
- fun getUser(@RequestParam(required = false) username: String?): Any {
|
|
|
|
|
|
+ fun getUser(
|
|
|
|
+ @RequestParam(required = false) username: String?,
|
|
|
|
+ @RequestParam(defaultValue = 0.toString()) page: Int,
|
|
|
|
+ @RequestParam(defaultValue = 3.toString()) limit: Int,
|
|
|
|
+ @RequestParam(defaultValue = "id, desc") sort: Array<String>
|
|
|
|
+ ): Any {
|
|
return try {
|
|
return try {
|
|
- val users: List<User> = if (username == null) {
|
|
|
|
- userRepository.findAll()
|
|
|
|
|
|
+ val orders: MutableList<Sort.Order> = ArrayList()
|
|
|
|
+ if (sort[0].contains(",")) {
|
|
|
|
+ for (sortOrder in sort) {
|
|
|
|
+ val sort_ = sortOrder.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
|
|
|
|
+ orders.add(Sort.Order(getSortDirection(sort_[1]), sort_[0]))
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ orders.add(Sort.Order(Sort.Direction.DESC, sort[0]))
|
|
|
|
+ }
|
|
|
|
+ val pagingSort: Pageable = PageRequest.of(page, limit, Sort.by(orders as List<Sort.Order>))
|
|
|
|
+ val users = if (username == null) {
|
|
|
|
+ userRepository.findAll(pagingSort)
|
|
} else {
|
|
} else {
|
|
- userRepository.findByUsernameContaining(username)
|
|
|
|
|
|
+ userRepository.findByUsernameContaining(username, pagingSort)
|
|
}
|
|
}
|
|
- if (users.isNotEmpty()) {
|
|
|
|
- ResponseEntity<List<User>?>(users, HttpStatus.OK)
|
|
|
|
|
|
+ val ret: List<User?> = users.content
|
|
|
|
+ val response: MutableMap<String, Any> = HashMap()
|
|
|
|
+ response["currentPage"] = users.number
|
|
|
|
+ response["totalRecord"] = users.totalElements
|
|
|
|
+ response["totalPage"] = users.totalPages
|
|
|
|
+ response["results"] = ret
|
|
|
|
+ if (ret.isNotEmpty()) {
|
|
|
|
+ ResponseEntity(response, HttpStatus.OK)
|
|
} else {
|
|
} else {
|
|
arrayOf<String>()
|
|
arrayOf<String>()
|
|
}
|
|
}
|
|
} catch (e: Exception) {
|
|
} catch (e: Exception) {
|
|
- ResponseEntity<List<User>?>(null, HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
|
|
|
+ ResponseEntity(null, HttpStatus.INTERNAL_SERVER_ERROR)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -49,7 +85,7 @@ class UserController(
|
|
@PostMapping("/users")
|
|
@PostMapping("/users")
|
|
fun addUser(@RequestBody user: UserInput): ResponseEntity<User> {
|
|
fun addUser(@RequestBody user: UserInput): ResponseEntity<User> {
|
|
return try {
|
|
return try {
|
|
- val found = userRepository.findByUsernameContaining(user.username).size
|
|
|
|
|
|
+ val found = userRepository.findByUsername(user.username!!).size
|
|
if (user.username!!.isNotBlank()) {
|
|
if (user.username!!.isNotBlank()) {
|
|
if (user.username!!.length > 100 || user.password!!.length > 100 || user.name!!.length > 255) {
|
|
if (user.username!!.length > 100 || user.password!!.length > 100 || user.name!!.length > 255) {
|
|
ResponseEntity<User>(HttpStatus.PAYLOAD_TOO_LARGE)
|
|
ResponseEntity<User>(HttpStatus.PAYLOAD_TOO_LARGE)
|
|
@@ -73,7 +109,7 @@ class UserController(
|
|
@PutMapping("/users/{id}")
|
|
@PutMapping("/users/{id}")
|
|
fun updateUserById(@PathVariable("id") id: Long, @RequestBody user: User): ResponseEntity<out Any?> {
|
|
fun updateUserById(@PathVariable("id") id: Long, @RequestBody user: User): ResponseEntity<out Any?> {
|
|
val userData = userRepository.findById(id)
|
|
val userData = userRepository.findById(id)
|
|
- val found = userRepository.findByUsernameContaining(user.username).size
|
|
|
|
|
|
+ val found = userRepository.findByUsername(user.username).size
|
|
return if (user.username.isNotBlank()) {
|
|
return if (user.username.isNotBlank()) {
|
|
if (user.username.length > 100 || user.name.length > 255) {
|
|
if (user.username.length > 100 || user.name.length > 255) {
|
|
ResponseEntity<User>(HttpStatus.PAYLOAD_TOO_LARGE)
|
|
ResponseEntity<User>(HttpStatus.PAYLOAD_TOO_LARGE)
|