Skip to content

Commit

Permalink
feat: article topic별로 가져오기 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
JooHui-void committed Mar 7, 2024
1 parent fd15864 commit 1c9b4f0
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.example.daitssuapi.domain.article.dto.request.CommentWriteRequest
import com.example.daitssuapi.domain.article.dto.response.ArticleResponse
import com.example.daitssuapi.domain.article.dto.response.CommentResponse
import com.example.daitssuapi.domain.article.dto.response.PageArticlesResponse
import com.example.daitssuapi.domain.article.enums.Topic
import com.example.daitssuapi.domain.article.service.ArticleService
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.Parameter
Expand Down Expand Up @@ -75,6 +76,42 @@ sort: [\"createdAt\"]
)
}

@Operation(
summary = "게시글 토픽으로 조회",
responses = [
ApiResponse(
responseCode = "200",
description = "OK"
)
]
)
@GetMapping("/topic")
fun pageArticleListWithTopic(
@Parameter(
description = """
<b>[필수]</b> 조회할 Page, Page 당 개수, 정렬 기준입니다. <br />
`page`는 zero-indexed 입니다. <br />
<b>[기본 값]</b><br />
page: 0 <br />
size: 5 <br />
sort: [\"createdAt\"]
""",
)
@PageableDefault(page = 0, size = 10, sort = ["createdAt"]) pageable: Pageable,
@RequestParam topic: Topic,
@RequestParam inquiry: String?,
): Response<PageArticlesResponse> { // TODO : 유저의 nickname이 null이면 예외 파악이 매우 어려움
val articles = articleService.pageArticleListWithTopic(
inquiry = inquiry,
pageable = pageable,
topic = topic,
)

return Response(
data = articles
)
}

@Operation(
summary = "인기 게시글 조회(24시간)",
responses = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.example.daitssuapi.domain.article.model.repository

import com.example.daitssuapi.domain.article.enums.Topic
import com.example.daitssuapi.domain.article.model.entity.Article
import com.example.daitssuapi.domain.user.model.entity.User
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query
import java.time.LocalDateTime

interface ArticleRepository : JpaRepository<Article, Long> {
Expand All @@ -14,6 +16,19 @@ interface ArticleRepository : JpaRepository<Article, Long> {
pageable: Pageable,
): Page<Article>

fun findByTopic(
topic :Topic,
pageable: Pageable,
):Page<Article>

@Query("SELECT a FROM Article a WHERE (a.topic = :topic) AND (a.title LIKE %:title% OR a.content LIKE %:content%)")
fun findByTitleContainingOrContentContainingAndTopic(
title: String,
content: String,
pageable: Pageable,
topic: Topic,
): Page<Article>

fun findAllByCreatedAtIsGreaterThanEqual(
createdAt: LocalDateTime
): List<Article>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.example.daitssuapi.domain.article.dto.request.CommentWriteRequest
import com.example.daitssuapi.domain.article.dto.response.ArticleResponse
import com.example.daitssuapi.domain.article.dto.response.CommentResponse
import com.example.daitssuapi.domain.article.dto.response.PageArticlesResponse
import com.example.daitssuapi.domain.article.enums.Topic
import com.example.daitssuapi.domain.article.model.entity.Article
import com.example.daitssuapi.domain.article.model.entity.ArticleLike
import com.example.daitssuapi.domain.article.model.entity.Comment
Expand Down Expand Up @@ -91,6 +92,45 @@ class ArticleService(
)
}

// topic으로 article 가져오기
fun pageArticleListWithTopic(
pageable: Pageable,
inquiry: String?,
topic: Topic,
): PageArticlesResponse {
val articles: Page<Article> =
if (inquiry == null)
articleRepository.findByTopic(
pageable = pageable,
topic = topic)
else
articleRepository.findByTitleContainingOrContentContainingAndTopic(
title = inquiry,
content = inquiry,
pageable = pageable,
topic = topic,
)

val articleResponses = articles.map {
ArticleResponse(
id = it.id,
topic = it.topic.value,
title = it.title,
content = it.content,
writerNickName = it.writer.nickname!!,
updatedAt = it.updatedAt,
imageUrls = it.imageUrl,
likes = it.likes.size,
comments = it.comments.size
)
}

return PageArticlesResponse(
articles = articleResponses.content,
totalPages = articleResponses.totalPages
)
}

fun getPopularArticles(): List<ArticleResponse> {
val articles: List<Article> = articleRepository.findAllByCreatedAtIsGreaterThanEqual(
createdAt = LocalDateTime.now().minusDays(7)
Expand Down

0 comments on commit 1c9b4f0

Please sign in to comment.