Working basic CRUD
This commit is contained in:
parent
75a73ad16a
commit
30713ca8d5
7 changed files with 65 additions and 8 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -4,6 +4,7 @@ build/
|
|||
!gradle/wrapper/gradle-wrapper.jar
|
||||
!**/src/main/**/build/
|
||||
!**/src/test/**/build/
|
||||
**/*.db
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
|
@ -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<String>) {
|
||||
runApplication<DemoApplication>(*args)
|
||||
}
|
||||
|
||||
@GetMapping("/hello")
|
||||
fun hello(@RequestParam(value = "name", defaultValue = "World") name: String?): String {
|
||||
return String.format("Hello %s!", name)
|
||||
}
|
|
@ -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<User> {
|
||||
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)
|
||||
}
|
||||
}
|
10
src/main/kotlin/com/example/demo/entities/User.kt
Normal file
10
src/main/kotlin/com/example/demo/entities/User.kt
Normal file
|
@ -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)
|
|
@ -0,0 +1,6 @@
|
|||
package com.example.demo.repositories
|
||||
|
||||
import com.example.demo.entities.User
|
||||
import org.springframework.data.repository.CrudRepository
|
||||
|
||||
interface UserRepository : CrudRepository<User, Long>
|
|
@ -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
|
Loading…
Reference in a new issue