-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4bddd12
commit 799fb0f
Showing
11 changed files
with
93 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 12 additions & 4 deletions
16
src/main/kotlin/pl/writeonly/omnibus/controller/PostController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
src/main/kotlin/pl/writeonly/omnibus/mapper/PostDtoMapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/main/kotlin/pl/writeonly/omnibus/repository/PostRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 9 additions & 2 deletions
11
src/main/kotlin/pl/writeonly/omnibus/service/PostService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |