From 30713ca8d5f0bcec160ec59a94edd9e4cf58052e Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Tue, 2 Jan 2024 21:20:15 +0100 Subject: [PATCH] Working basic CRUD --- .gitignore | 1 + build.gradle.kts | 4 ++ .../com/example/demo/DemoApplication.kt | 7 ---- .../demo/controllers/UsersController.kt | 38 +++++++++++++++++++ .../kotlin/com/example/demo/entities/User.kt | 10 +++++ .../demo/repositories/UserRepository.kt | 6 +++ src/main/resources/application.properties | 7 +++- 7 files changed, 65 insertions(+), 8 deletions(-) create mode 100644 src/main/kotlin/com/example/demo/controllers/UsersController.kt create mode 100644 src/main/kotlin/com/example/demo/entities/User.kt create mode 100644 src/main/kotlin/com/example/demo/repositories/UserRepository.kt diff --git a/.gitignore b/.gitignore index c2065bc..9ead01d 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ build/ !gradle/wrapper/gradle-wrapper.jar !**/src/main/**/build/ !**/src/test/**/build/ +**/*.db ### STS ### .apt_generated diff --git a/build.gradle.kts b/build.gradle.kts index 4d57060..3bee12f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,6 +5,7 @@ plugins { id("io.spring.dependency-management") version "1.1.4" kotlin("jvm") version "1.9.21" kotlin("plugin.spring") version "1.9.21" + kotlin("plugin.jpa") version "1.9.21" } group = "com.example" @@ -19,10 +20,13 @@ repositories { } dependencies { + implementation("org.springframework.boot:spring-boot-starter-data-jpa") implementation("org.springframework.boot:spring-boot-starter-thymeleaf") implementation("org.springframework.boot:spring-boot-starter-web") implementation("com.fasterxml.jackson.module:jackson-module-kotlin") implementation("org.jetbrains.kotlin:kotlin-reflect") + implementation("org.xerial:sqlite-jdbc") + implementation("org.hibernate.orm:hibernate-community-dialects") developmentOnly("org.springframework.boot:spring-boot-devtools") testImplementation("org.springframework.boot:spring-boot-starter-test") } diff --git a/src/main/kotlin/com/example/demo/DemoApplication.kt b/src/main/kotlin/com/example/demo/DemoApplication.kt index 43bbe18..78ada7d 100644 --- a/src/main/kotlin/com/example/demo/DemoApplication.kt +++ b/src/main/kotlin/com/example/demo/DemoApplication.kt @@ -2,8 +2,6 @@ package com.example.demo import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication -import org.springframework.web.bind.annotation.GetMapping -import org.springframework.web.bind.annotation.RequestParam @SpringBootApplication @@ -11,9 +9,4 @@ class DemoApplication fun main(args: Array) { runApplication(*args) -} - -@GetMapping("/hello") -fun hello(@RequestParam(value = "name", defaultValue = "World") name: String?): String { - return String.format("Hello %s!", name) } \ No newline at end of file diff --git a/src/main/kotlin/com/example/demo/controllers/UsersController.kt b/src/main/kotlin/com/example/demo/controllers/UsersController.kt new file mode 100644 index 0000000..027a255 --- /dev/null +++ b/src/main/kotlin/com/example/demo/controllers/UsersController.kt @@ -0,0 +1,38 @@ +package com.example.demo.controllers + +import com.example.demo.entities.User +import com.example.demo.repositories.UserRepository +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.web.bind.annotation.* + +@RestController +class UsersController { + @Autowired + lateinit var repo: UserRepository + + @GetMapping("/users") + fun index(): MutableIterable { + return repo.findAll() + } + + @GetMapping("/users/{id}") + fun show(@PathVariable id: Long): User? { + return repo.findById(id).get() + } + + @PostMapping("/users") + fun create(@RequestBody newUser: User): User { + return repo.save(newUser) + } + + @PatchMapping("/users/{id}") + fun update(@PathVariable id: Long, @RequestBody user: User): User { + user.id = id + return repo.save(user) + } + + @DeleteMapping("/users/{id}") + fun destroy(@PathVariable id: Long) { + return repo.deleteById(id) + } +} diff --git a/src/main/kotlin/com/example/demo/entities/User.kt b/src/main/kotlin/com/example/demo/entities/User.kt new file mode 100644 index 0000000..b551242 --- /dev/null +++ b/src/main/kotlin/com/example/demo/entities/User.kt @@ -0,0 +1,10 @@ +package com.example.demo.entities + +import jakarta.persistence.Entity +import jakarta.persistence.GeneratedValue +import jakarta.persistence.Id + +@Entity +class User( + @Id @GeneratedValue var id: Long? = null, + var login: String) \ No newline at end of file diff --git a/src/main/kotlin/com/example/demo/repositories/UserRepository.kt b/src/main/kotlin/com/example/demo/repositories/UserRepository.kt new file mode 100644 index 0000000..8cfb644 --- /dev/null +++ b/src/main/kotlin/com/example/demo/repositories/UserRepository.kt @@ -0,0 +1,6 @@ +package com.example.demo.repositories + +import com.example.demo.entities.User +import org.springframework.data.repository.CrudRepository + +interface UserRepository : CrudRepository \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b13789..fe3fff2 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,6 @@ - +spring.datasource.url=jdbc:sqlite:test.db +spring.datasource.driver-class-name=org.sqlite.JDBC +spring.jpa.properties.hibernate.dialect=org.hibernate.community.dialect.SQLiteDialect +spring.datasource.username= +spring.datasource.password= +spring.jpa.hibernate.ddl-auto=update \ No newline at end of file