Working basic CRUD

This commit is contained in:
Florian RICHER 2024-01-02 21:20:15 +01:00
parent 75a73ad16a
commit 30713ca8d5
7 changed files with 65 additions and 8 deletions

View file

@ -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)
}

View file

@ -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)
}
}

View 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)

View file

@ -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>

View file

@ -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