Skip to content

Commit

Permalink
Add Pageable
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil-adam committed Apr 5, 2024
1 parent 4bddd12 commit 799fb0f
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 40 deletions.
34 changes: 18 additions & 16 deletions src/main/kotlin/pl/writeonly/omnibus/GenerateTestData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,24 @@ class GenerateTestData
fun main(args: Array<String>) {

val dataSql = File("src/main/resources/data.sql")
dataSql.writeText("")
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(100) { i ->
val localDataTime = LocalDateTime.now().minusDays(100 - i.toLong())
val timestamp = Timestamp.valueOf(localDataTime)
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"
)
}
repeat(1000) { i ->
val post_id = i/10
val localDataTime = LocalDateTime.now().minusDays(100 - i.toLong())
val timestamp = Timestamp.valueOf(localDataTime)
dataSql.appendText(
"INSERT INTO comment(id, post_id, content, created)"
+ " VALUES(${i}, ${post_id}, 'Content ${i}', TIMESTAMP '${timestamp}');\n"
)
}
}
6 changes: 3 additions & 3 deletions src/main/kotlin/pl/writeonly/omnibus/config/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.springframework.context.annotation.Configuration
@Configuration
class Config(val objectMapper: ObjectMapper) {

fun customizeObjectMapper() {
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
}
fun customizeObjectMapper() {
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import pl.writeonly.omnibus.service.HelloService
@RestController
class HelloController(val helloService: HelloService) {

@GetMapping("/")
fun hello() = helloService.hello()
@GetMapping("/")
fun hello() = helloService.hello()
}
16 changes: 12 additions & 4 deletions src/main/kotlin/pl/writeonly/omnibus/controller/PostController.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
package pl.writeonly.omnibus.controller

import io.swagger.v3.oas.annotations.tags.Tag
import jakarta.validation.constraints.Min
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController
import pl.writeonly.omnibus.dto.PostDto
import pl.writeonly.omnibus.mapper.mapToPostDtos
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")
fun getPosts(@Min(0) page: Int = 0): List<PostDto> =
mapToPostDtos(service.getPosts(page))

@GetMapping("/posts/{id}")
fun getPost(id: Long): Post = service.getPost(id)
@GetMapping("/posts/{id}")
fun getPost(@Min(0) id: Long): Post = service.getPost(id)

@GetMapping("/posts/title/{title}")
fun findAllByTitle(title: String): List<PostDto> =
mapToPostDtos(service.findAllByTitle(title))
}
10 changes: 10 additions & 0 deletions src/main/kotlin/pl/writeonly/omnibus/dto/PostDto.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package pl.writeonly.omnibus.dto

import java.time.LocalDateTime

data class PostDto(
val id: Long?,
val title: String?,
val content: String?,
val created: LocalDateTime?,
)
17 changes: 17 additions & 0 deletions src/main/kotlin/pl/writeonly/omnibus/mapper/PostDtoMapper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package pl.writeonly.omnibus.mapper

import pl.writeonly.omnibus.dto.PostDto
import pl.writeonly.omnibus.model.Post

class PostDtoMapper

fun mapToPostDtos(posts: List<Post>) = posts.map {
mapToPostDto(it)
}

fun mapToPostDto(post: Post) = PostDto(
id = post.id,
title = post.title,
content = post.content,
created = post.created
)
8 changes: 4 additions & 4 deletions src/main/kotlin/pl/writeonly/omnibus/model/Comment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import java.time.LocalDateTime

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

)
16 changes: 8 additions & 8 deletions src/main/kotlin/pl/writeonly/omnibus/model/Post.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ 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?,
@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>
@OneToMany(targetEntity = Comment::class)
@JoinColumn(name = "post_id")
open var comments: List<Comment>
)
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
package pl.writeonly.omnibus.repository

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

@Repository
interface PostRepository : JpaRepository<Post, Long> {

// @Query("SELECT p FROM Post p left join fetch p.comments")
@Query("SELECT p FROM Post p")
fun findAllPost(pageable: Pageable): List<Post>

fun findAllByTitle(title: String): List<Post>

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import org.springframework.stereotype.Service

@Service
class HelloService {
fun hello() = "Hello World!"
fun hello() = "Hello World!"
}
11 changes: 9 additions & 2 deletions src/main/kotlin/pl/writeonly/omnibus/service/PostService.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package pl.writeonly.omnibus.service

import org.springframework.data.domain.PageRequest
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 getPosts(pageNumber0: Int): List<Post> = run {
val pageNumber = if (0 <= pageNumber0) pageNumber0 else 0
repository.findAllPost(PageRequest.of(pageNumber, 10))
}

fun getPost(id: Long): Post = repository.getById(id)
fun getPost(id: Long): Post = repository.getById(id)

fun findAllByTitle(title: String): List<Post> =
repository.findAllByTitle(title)
}

0 comments on commit 799fb0f

Please sign in to comment.