-
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.
Merge pull request #25 from MEOGO-DSM/23-query-create-post
🏄 :: (Meogo-23) query create post
- Loading branch information
Showing
5 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
src/main/kotlin/org/meogo/domain/post/present/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 |
---|---|---|
@@ -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() | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/kotlin/org/meogo/domain/post/present/dto/request/PostRequest.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,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>? | ||
) |
11 changes: 11 additions & 0 deletions
11
src/main/kotlin/org/meogo/domain/post/present/dto/response/PostResponse.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,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
36
src/main/kotlin/org/meogo/domain/post/service/CreatePostService.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,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 | ||
) | ||
) | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/kotlin/org/meogo/domain/post/service/QueryAllPostService.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,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")) | ||
} |