spring-web-modules/api/src/main/kotlin/com/cubetiqs/web/modules/user/UserController.kt
Sambo Chea 048c11ee62
Some checks failed
continuous-integration/drone/push Build is failing
Add uploader and file storage provider with openapi docs and demo for modules
2022-04-21 11:57:44 +07:00

84 lines
2.6 KiB
Kotlin

package com.cubetiqs.web.modules.user
import com.cubetiqs.web.util.RouteConstants
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.Parameter
import io.swagger.v3.oas.annotations.tags.Tag
import org.springdoc.core.converters.models.PageableAsQueryParam
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.web.bind.annotation.*
import java.util.*
@UserModule
@Tag(name = "User Controller")
@RestController
@RequestMapping(RouteConstants.INDEX + "user")
class UserController @Autowired constructor(
private val repository: UserRepository,
) {
@GetMapping
@PageableAsQueryParam
@Operation(summary = "Get all users")
fun getAll(
@Parameter(hidden = true)
pageable: Pageable?,
): Page<UserEntity> {
return repository.findAll(pageable ?: Pageable.unpaged())
}
@ResponseStatus(value = org.springframework.http.HttpStatus.OK)
@GetMapping("/{id}")
@Operation(summary = "Get a user by id")
fun get(
@PathVariable id: String,
): UserEntity {
val entity = repository.findById(UUID.fromString(id)).orElseThrow {
throw IllegalArgumentException("User not found")
}
return repository.save(entity)
}
@ResponseStatus(value = org.springframework.http.HttpStatus.CREATED)
@PostMapping
@Operation(summary = "Create a user")
fun create(
@RequestBody body: UserEntity
): UserEntity {
return repository.save(body)
}
@ResponseStatus(value = org.springframework.http.HttpStatus.OK)
@PutMapping("/{id}")
@Operation(summary = "Update a user by id")
fun update(
@PathVariable id: String,
@RequestBody body: UserEntity
): UserEntity {
val entity = repository.findById(UUID.fromString(id)).orElseThrow {
throw IllegalArgumentException("User not found")
}
body.id = entity.id
return repository.save(body)
}
@ResponseStatus(value = org.springframework.http.HttpStatus.NO_CONTENT)
@DeleteMapping("/{id}")
@Operation(summary = "Delete user by id")
fun delete(
@PathVariable id: String,
) {
val entity = repository.findById(UUID.fromString(id)).orElseThrow {
throw IllegalArgumentException("User not found")
}
repository.delete(entity)
}
@ResponseStatus(value = org.springframework.http.HttpStatus.NO_CONTENT)
@DeleteMapping
@Operation(summary = "Delete all users")
fun deleteAll() {
repository.deleteAll()
}
}