Skip to content

Commit

Permalink
add :: queryAll post api
Browse files Browse the repository at this point in the history
  • Loading branch information
meltapplee committed Sep 9, 2024
1 parent 8f00468 commit 0e695bc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.meogo.domain.post.present.dto.response

data class PostResponse(
val id: Long,
val name: String,
val title: String,
val content: String,
val date: String,
val keyWord: List<String>?,
val schoolId: Int?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.meogo.domain.post.service

import org.meogo.domain.post.domain.PostRepository
import org.meogo.domain.post.present.dto.response.PostResponse
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

@Service
class QueryAllPostService(
private val postRepository: PostRepository
) {

@Transactional(readOnly = true)
fun execute(): List<PostResponse> {
val posts = postRepository.findAll()

return posts.map { post ->
PostResponse(
id = post.id,
name = "익명",
title = post.title,
content = post.content,
date = format(post.date),
keyWord = post.keyWord?.split(",")?.map { it.trim() },
schoolId = post.schoolId
)
}.sortedBy { it.id }
}

private fun format(date: LocalDateTime) =
date.format(DateTimeFormatter.ofPattern("MM.dd HH:mm"))
}

0 comments on commit 0e695bc

Please sign in to comment.