This commit is contained in:
@@ -13,7 +13,7 @@ data class TestEntity (
|
|||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
val id: Long = 0,
|
val id: Long? = null,
|
||||||
|
|
||||||
@Column(name = "key")
|
@Column(name = "key")
|
||||||
var key: String? = null,
|
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
|
package com.test.rest
|
||||||
|
|
||||||
|
import com.test.model.TestModel
|
||||||
|
import com.test.service.TestService
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import org.springframework.beans.factory.annotation.Value
|
import org.springframework.web.bind.annotation.DeleteMapping
|
||||||
import org.springframework.security.access.prepost.PreAuthorize
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping
|
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
|
import org.springframework.web.bind.annotation.RestController
|
||||||
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
open class TestController {
|
@RequestMapping("/test")
|
||||||
|
open class TestController(private val testService: TestService) {
|
||||||
|
|
||||||
private val logger = LoggerFactory.getLogger(TestController::class.java)
|
private val logger = LoggerFactory.getLogger(TestController::class.java)
|
||||||
|
|
||||||
@Value("\${SPRING_DATASOURCE_PASSWORD}")
|
//@PreAuthorize("hasRole('ADMIN')")
|
||||||
private val password: String? = null
|
@PostMapping("/add")
|
||||||
|
fun add(@RequestBody testModel: TestModel): TestModel {
|
||||||
@PreAuthorize("hasRole('ADMIN')")
|
return testService.add(testModel)
|
||||||
@GetMapping("/test")
|
|
||||||
fun test(): String {
|
|
||||||
logger.info("test!")
|
|
||||||
return "Test page"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@PreAuthorize("hasRole('USER')")
|
//@PreAuthorize("hasRole('USER')")
|
||||||
@GetMapping("/users")
|
@PutMapping("/update")
|
||||||
fun users(): ArrayList<String> {
|
fun users(@RequestBody testModel: TestModel): TestModel {
|
||||||
return ArrayList()
|
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("/")
|
@GetMapping("/")
|
||||||
fun home(): String {
|
fun getAll(): List<TestModel> {
|
||||||
return "Home page"
|
return testService.getAll()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,36 @@
|
|||||||
package com.test.service
|
package com.test.service
|
||||||
|
|
||||||
|
import com.test.entities.TestEntity
|
||||||
|
import com.test.model.TestModel
|
||||||
import com.test.repository.TestEntityRepository
|
import com.test.repository.TestEntityRepository
|
||||||
|
import org.springframework.data.repository.findByIdOrNull
|
||||||
import org.springframework.stereotype.Service
|
import org.springframework.stereotype.Service
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
class TestService(private val testEntityRepository: TestEntityRepository) {
|
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