From 3f2811b994a96b180b95d0177ff22cbe751afb64 Mon Sep 17 00:00:00 2001 From: "dmitriy.churmeev" Date: Sat, 4 Apr 2026 02:31:47 +0300 Subject: [PATCH] test value --- .../kotlin/com/test/entities/TestEntity.kt | 2 +- src/main/kotlin/com/test/model/TestModel.kt | 14 ++++++ .../kotlin/com/test/rest/TestController.kt | 48 ++++++++++++------- .../kotlin/com/test/service/TestService.kt | 28 +++++++++++ 4 files changed, 73 insertions(+), 19 deletions(-) create mode 100644 src/main/kotlin/com/test/model/TestModel.kt diff --git a/src/main/kotlin/com/test/entities/TestEntity.kt b/src/main/kotlin/com/test/entities/TestEntity.kt index fcfd801..c142514 100644 --- a/src/main/kotlin/com/test/entities/TestEntity.kt +++ b/src/main/kotlin/com/test/entities/TestEntity.kt @@ -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, diff --git a/src/main/kotlin/com/test/model/TestModel.kt b/src/main/kotlin/com/test/model/TestModel.kt new file mode 100644 index 0000000..858f93c --- /dev/null +++ b/src/main/kotlin/com/test/model/TestModel.kt @@ -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 +) \ No newline at end of file diff --git a/src/main/kotlin/com/test/rest/TestController.kt b/src/main/kotlin/com/test/rest/TestController.kt index 2a035f1..fea0249 100644 --- a/src/main/kotlin/com/test/rest/TestController.kt +++ b/src/main/kotlin/com/test/rest/TestController.kt @@ -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 { - 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 { + return testService.getAll() } } \ No newline at end of file diff --git a/src/main/kotlin/com/test/service/TestService.kt b/src/main/kotlin/com/test/service/TestService.kt index 9639c6a..78f3959 100644 --- a/src/main/kotlin/com/test/service/TestService.kt +++ b/src/main/kotlin/com/test/service/TestService.kt @@ -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 { + return testEntityRepository.findAll() + .map { entity -> TestModel(entity.id, entity.key, entity.value)} + } } \ No newline at end of file