This commit is contained in:
@@ -13,7 +13,7 @@ data class TestEntity (
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
val id: Long = 0,
|
||||
val id: Long? = null,
|
||||
|
||||
@Column(name = "key")
|
||||
var key: String? = null,
|
||||
|
||||
14
src/main/kotlin/com/test/model/TestModel.kt
Normal file
14
src/main/kotlin/com/test/model/TestModel.kt
Normal file
@@ -0,0 +1,14 @@
|
||||
package com.test.model
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty
|
||||
|
||||
data class TestModel (
|
||||
@JsonProperty(value = "id")
|
||||
val id: Long? = null,
|
||||
|
||||
@JsonProperty(value = "key")
|
||||
var key: String? = null,
|
||||
|
||||
@JsonProperty(value = "value")
|
||||
var value: String? = null
|
||||
)
|
||||
@@ -1,36 +1,48 @@
|
||||
package com.test.rest
|
||||
|
||||
import com.test.model.TestModel
|
||||
import com.test.service.TestService
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.beans.factory.annotation.Value
|
||||
import org.springframework.security.access.prepost.PreAuthorize
|
||||
import org.springframework.web.bind.annotation.DeleteMapping
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.PathVariable
|
||||
import org.springframework.web.bind.annotation.PostMapping
|
||||
import org.springframework.web.bind.annotation.PutMapping
|
||||
import org.springframework.web.bind.annotation.RequestBody
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
|
||||
|
||||
@RestController
|
||||
open class TestController {
|
||||
@RequestMapping("/test")
|
||||
open class TestController(private val testService: TestService) {
|
||||
|
||||
private val logger = LoggerFactory.getLogger(TestController::class.java)
|
||||
|
||||
@Value("\${SPRING_DATASOURCE_PASSWORD}")
|
||||
private val password: String? = null
|
||||
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
@GetMapping("/test")
|
||||
fun test(): String {
|
||||
logger.info("test!")
|
||||
return "Test page"
|
||||
//@PreAuthorize("hasRole('ADMIN')")
|
||||
@PostMapping("/add")
|
||||
fun add(@RequestBody testModel: TestModel): TestModel {
|
||||
return testService.add(testModel)
|
||||
}
|
||||
|
||||
@PreAuthorize("hasRole('USER')")
|
||||
@GetMapping("/users")
|
||||
fun users(): ArrayList<String> {
|
||||
return ArrayList()
|
||||
//@PreAuthorize("hasRole('USER')")
|
||||
@PutMapping("/update")
|
||||
fun users(@RequestBody testModel: TestModel): TestModel {
|
||||
return testService.update(testModel)
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
fun get(@PathVariable(value = "id") id: Long): TestModel {
|
||||
return testService.get(id)!!
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
fun delete(@RequestBody id: Long) {
|
||||
testService.remove(id)
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
fun home(): String {
|
||||
return "Home page"
|
||||
fun getAll(): List<TestModel> {
|
||||
return testService.getAll()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,8 +1,36 @@
|
||||
package com.test.service
|
||||
|
||||
import com.test.entities.TestEntity
|
||||
import com.test.model.TestModel
|
||||
import com.test.repository.TestEntityRepository
|
||||
import org.springframework.data.repository.findByIdOrNull
|
||||
import org.springframework.stereotype.Service
|
||||
|
||||
@Service
|
||||
class TestService(private val testEntityRepository: TestEntityRepository) {
|
||||
|
||||
fun add(model: TestModel): TestModel {
|
||||
var entity = testEntityRepository.save(TestEntity(null, model.key, model.value))
|
||||
return TestModel(entity.id, entity.key, entity.value)
|
||||
}
|
||||
|
||||
fun remove(id: Long) {
|
||||
testEntityRepository.deleteById(id)
|
||||
}
|
||||
|
||||
fun update(model: TestModel): TestModel {
|
||||
var entity = testEntityRepository.save(TestEntity(model.id, model.key, model.value))
|
||||
return TestModel(null, entity.key, entity.value)
|
||||
}
|
||||
|
||||
fun get(id: Long): TestModel? {
|
||||
return testEntityRepository.findByIdOrNull(id)?.let {
|
||||
TestModel(it.id, it.key, it.value)
|
||||
}
|
||||
}
|
||||
|
||||
fun getAll(): List<TestModel> {
|
||||
return testEntityRepository.findAll()
|
||||
.map { entity -> TestModel(entity.id, entity.key, entity.value)}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user