Skip to content

Commit

Permalink
Merge pull request #29 from MEOGO-DSM/28-query-school-post
Browse files Browse the repository at this point in the history
🏄 :: (Meogo-28) query school post
  • Loading branch information
meltapplee authored Sep 10, 2024
2 parents d1df65c + 09619b3 commit 28bc36c
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/main/kotlin/org/meogo/domain/post/domain/Post.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.meogo.domain.post.domain

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import java.util.UUID
import javax.persistence.Column
import javax.persistence.Entity
Expand Down Expand Up @@ -39,4 +40,6 @@ class Post(
fun addGood() {
this.good += 1
}

fun format(date: LocalDateTime) = date.format(DateTimeFormatter.ofPattern("yy.MM.dd HH:mm"))!!
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ interface PostRepository : Repository<Post, Long> {
fun save(post: Post)

fun findAll(): List<Post>

fun findBySchoolId(schoolId: Int): List<Post>
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ 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.meogo.domain.post.service.QuerySchoolPostService
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.RequestParam
import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestController
import javax.validation.Valid
Expand All @@ -18,7 +20,8 @@ import javax.validation.Valid
@RequestMapping("/post")
class PostController(
private val createPostService: CreatePostService,
private val queryAllPostService: QueryAllPostService
private val queryAllPostService: QueryAllPostService,
private val querySchoolPostService: QuerySchoolPostService
) {

@PostMapping
Expand All @@ -31,4 +34,7 @@ class PostController(

@GetMapping("/query/all")
fun queryAll() = queryAllPostService.execute()

@GetMapping("/query/school")
fun querySchool(@RequestParam(name = "school_id") schoolId: Int) = querySchoolPostService.execute(schoolId)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ 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(
Expand All @@ -22,13 +20,10 @@ class QueryAllPostService(
name = "익명",
title = post.title,
content = post.content,
date = format(post.date),
date = post.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("yy.MM.dd HH:mm"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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

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

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

return posts.map { post ->
PostResponse(
id = post.id,
name = "익명",
title = post.title,
content = post.content,
date = post.format(post.date),
keyWord = post.keyWord?.split(",")?.map { it.trim() },
schoolId = post.schoolId
)
}
}
}
3 changes: 3 additions & 0 deletions src/main/kotlin/org/meogo/domain/review/domain/Review.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class Review(
@Column(nullable = false)
var content: String,

@Column(name = "key_word")
val keyWord: String?,

var picture: String?

) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ data class ReviewRequest(
val content: String,
val schoolId: Int,
val star: Float,
val keyWord: List<String>?,
val image: String?
)
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ class CreateReviewService(
fun execute(request: ReviewRequest) {
val user = userFacade.currentUser() ?: throw UserNotFoundException

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

reviewRepository.save(
Review(
date = LocalDateTime.now(),
userId = user.id!!,
schoolId = request.schoolId,
star = request.star,
content = request.content,
keyWord = keyWord,
picture = request.image
)
)
Expand Down

0 comments on commit 28bc36c

Please sign in to comment.