Skip to content

Commit

Permalink
Add REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil-adam committed Apr 5, 2024
1 parent 36ffad3 commit 4bddd12
Show file tree
Hide file tree
Showing 11 changed files with 1,233 additions and 8 deletions.
12 changes: 8 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
id("com.netflix.dgs.codegen") version "6.0.3"
id("org.hibernate.orm") version "6.4.4.Final"
id("org.graalvm.buildtools.native") version "0.9.28"
id("com.vaadin") version "24.3.9"
// id("com.vaadin") version "24.3.9"
kotlin("jvm") version "1.9.23"
kotlin("plugin.spring") version "1.9.23"
kotlin("plugin.jpa") version "1.9.23"
Expand All @@ -29,7 +29,7 @@ repositories {
mavenCentral()
}

extra["vaadinVersion"] = "24.3.9"
//extra["vaadinVersion"] = "24.3.9"

dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
Expand All @@ -38,10 +38,14 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("com.vaadin:vaadin-spring-boot-starter")
// implementation("com.vaadin:vaadin-spring-boot-starter")
implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")

implementation("org.springdoc:springdoc-openapi-starter-webmvc-api:2.5.0")
implementation("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.5.0")

compileOnly("org.projectlombok:lombok")
developmentOnly("org.springframework.boot:spring-boot-devtools")
runtimeOnly("com.h2database:h2")
Expand All @@ -54,7 +58,7 @@ dependencies {

dependencyManagement {
imports {
mavenBom("com.vaadin:vaadin-bom:${property("vaadinVersion")}")
// mavenBom("com.vaadin:vaadin-bom:${property("vaadinVersion")}")
}
}

Expand Down
32 changes: 32 additions & 0 deletions src/main/kotlin/pl/writeonly/omnibus/GenerateTestData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package pl.writeonly.omnibus

import org.springframework.boot.runApplication
import java.io.File
import java.sql.Timestamp
import java.time.LocalDate
import java.time.LocalDateTime

class GenerateTestData

fun main(args: Array<String>) {

val dataSql = File("src/main/resources/data.sql")
dataSql.writeText("")

repeat(100) { i ->
val timestamp = Timestamp.valueOf(LocalDateTime.now().minusDays(100 - i.toLong()))
dataSql.appendText(
"INSERT INTO post(id, title, content, created)"
+ " VALUES(${i}, 'Test Post ${i}', 'Content ${i}', TIMESTAMP '${timestamp}');\n"
)
}

repeat(1000) { i ->
val post_id = i/10
val timestamp = Timestamp.valueOf(LocalDateTime.now().minusDays(100 - i.toLong()))
dataSql.appendText(
"INSERT INTO comment(id, post_id, content, created)"
+ " VALUES(${i}, ${post_id}, 'Content ${i}', TIMESTAMP '${timestamp}');\n"
)
}
}
1 change: 0 additions & 1 deletion src/main/kotlin/pl/writeonly/omnibus/config/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pl.writeonly.omnibus.config

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Configuration

@Configuration
Expand Down
18 changes: 18 additions & 0 deletions src/main/kotlin/pl/writeonly/omnibus/controller/PostController.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package pl.writeonly.omnibus.controller

import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
import pl.writeonly.omnibus.model.Post
import pl.writeonly.omnibus.service.PostService

@Tag(name = "Post", description = "the Post Api")
@RestController
class PostController(val service: PostService) {

@GetMapping("/posts")
fun getPosts(): List<Post> = service.getPosts()

@GetMapping("/posts/{id}")
fun getPost(id: Long): Post = service.getPost(id)
}
14 changes: 14 additions & 0 deletions src/main/kotlin/pl/writeonly/omnibus/model/Comment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package pl.writeonly.omnibus.model

import jakarta.persistence.Entity
import jakarta.persistence.Id
import java.time.LocalDateTime

@Entity
open class Comment(
@Id
open var id: Long?,
open var content: String?,
open var created: LocalDateTime?

)
20 changes: 20 additions & 0 deletions src/main/kotlin/pl/writeonly/omnibus/model/Post.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package pl.writeonly.omnibus.model

import jakarta.persistence.Entity
import jakarta.persistence.Id
import jakarta.persistence.JoinColumn
import jakarta.persistence.OneToMany
import java.time.LocalDateTime

@Entity
open class Post(
@Id
open var id: Long?,
open var title: String?,
open var content: String?,
open var created: LocalDateTime?,

@OneToMany(targetEntity = Comment::class)
@JoinColumn(name = "post_id")
open var comments: List<Comment>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package pl.writeonly.omnibus.repository

import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.stereotype.Repository
import pl.writeonly.omnibus.model.Post

@Repository
interface PostRepository : JpaRepository<Post, Long> {
}
13 changes: 13 additions & 0 deletions src/main/kotlin/pl/writeonly/omnibus/service/PostService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package pl.writeonly.omnibus.service

import org.springframework.stereotype.Service
import pl.writeonly.omnibus.model.Post
import pl.writeonly.omnibus.repository.PostRepository

@Service
class PostService(val repository: PostRepository) {

fun getPosts(): List<Post> = repository.findAll()

fun getPost(id: Long): Post = repository.getById(id)
}
5 changes: 5 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
spring.application.name=omnibus
spring.jpa.hibernate.ddl.auto=none
spring.jpa.show-sql=true
#springdoc.swagger-ui.path=/swagger
#springdoc.api-docs.path=/api-docs

Loading

0 comments on commit 4bddd12

Please sign in to comment.