Skip to content

Commit

Permalink
Merge pull request #25 from MEOGO-DSM/23-query-create-post
Browse files Browse the repository at this point in the history
🏄 :: (Meogo-23) query create post
  • Loading branch information
meltapplee authored Sep 9, 2024
2 parents 8dc6d8a + f57e99e commit 2999b61
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/main/kotlin/org/meogo/domain/post/present/PostController.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.meogo.domain.post.present

import lombok.RequiredArgsConstructor
import org.meogo.domain.post.present.dto.request.PostRequest
import org.meogo.domain.post.service.CreatePostService
import org.meogo.domain.post.service.QueryAllPostService
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestController
import javax.validation.Valid

@RequiredArgsConstructor
@RestController
@RequestMapping("/post")
class PostController(
private val createPostService: CreatePostService,
private val queryAllPostService: QueryAllPostService
) {

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
fun create(
@Valid @RequestBody
request: PostRequest
) =
createPostService.execute(request)

@GetMapping("/query/all")
fun queryAll() = queryAllPostService.execute()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.meogo.domain.post.present.dto.request

import javax.validation.constraints.Size

data class PostRequest(
@field: Size(min = 1, max = 20)
val title: String,
@field: Size(min = 1, max = 300)
val content: String,
val isOk: Boolean,
val image: List<String>?,
val keyWord: List<String>?
)
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?
)
36 changes: 36 additions & 0 deletions src/main/kotlin/org/meogo/domain/post/service/CreatePostService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.meogo.domain.post.service

import org.meogo.domain.post.domain.Post
import org.meogo.domain.post.domain.PostRepository
import org.meogo.domain.post.present.dto.request.PostRequest
import org.meogo.domain.user.exception.UserNotFoundException
import org.meogo.domain.user.facade.UserFacade
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.time.LocalDateTime

@Service
class CreatePostService(
private val userFacade: UserFacade,
private val postRepository: PostRepository
) {
@Transactional
fun execute(request: PostRequest) {
val user = userFacade.currentUser() ?: throw UserNotFoundException

val schoolId = if (!request.isOk || user.enrolledSchool == null) null else user.enrolledSchool

val keyWord = request.keyWord?.joinToString(separator = ",")

postRepository.save(
Post(
title = request.title,
content = request.content,
userId = user.id!!,
date = LocalDateTime.now(),
schoolId = schoolId,
keyWord = keyWord
)
)
}
}
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 2999b61

Please sign in to comment.