diff --git a/src/main/kotlin/com/example/daitssuapi/domain/article/controller/ArticleController.kt b/src/main/kotlin/com/example/daitssuapi/domain/article/controller/ArticleController.kt index 57e4e207..c0b83f3f 100644 --- a/src/main/kotlin/com/example/daitssuapi/domain/article/controller/ArticleController.kt +++ b/src/main/kotlin/com/example/daitssuapi/domain/article/controller/ArticleController.kt @@ -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 @@ -75,6 +76,42 @@ sort: [\"createdAt\"] ) } + @Operation( + summary = "게시글 토픽으로 조회", + responses = [ + ApiResponse( + responseCode = "200", + description = "OK" + ) + ] + ) + @GetMapping("/topic") + fun pageArticleListWithTopic( + @Parameter( + description = """ +[필수] 조회할 Page, Page 당 개수, 정렬 기준입니다.
+`page`는 zero-indexed 입니다.
+[기본 값]
+page: 0
+size: 5
+sort: [\"createdAt\"] + """, + ) + @PageableDefault(page = 0, size = 10, sort = ["createdAt"]) pageable: Pageable, + @RequestParam topic: Topic, + @RequestParam inquiry: String?, + ): Response { // TODO : 유저의 nickname이 null이면 예외 파악이 매우 어려움 + val articles = articleService.pageArticleListWithTopic( + inquiry = inquiry, + pageable = pageable, + topic = topic, + ) + + return Response( + data = articles + ) + } + @Operation( summary = "인기 게시글 조회(24시간)", responses = [ diff --git a/src/main/kotlin/com/example/daitssuapi/domain/article/model/repository/ArticleRepository.kt b/src/main/kotlin/com/example/daitssuapi/domain/article/model/repository/ArticleRepository.kt index f73f4dda..add13785 100644 --- a/src/main/kotlin/com/example/daitssuapi/domain/article/model/repository/ArticleRepository.kt +++ b/src/main/kotlin/com/example/daitssuapi/domain/article/model/repository/ArticleRepository.kt @@ -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 { @@ -14,6 +16,20 @@ interface ArticleRepository : JpaRepository { pageable: Pageable, ): Page
+ fun findByTopic( + topic :Topic, + pageable: Pageable, + ):Page
+ + // TODO : Query문 교체 필요 + @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
+ fun findAllByCreatedAtIsGreaterThanEqual( createdAt: LocalDateTime ): List
diff --git a/src/main/kotlin/com/example/daitssuapi/domain/article/service/ArticleService.kt b/src/main/kotlin/com/example/daitssuapi/domain/article/service/ArticleService.kt index 77792be3..d9167512 100644 --- a/src/main/kotlin/com/example/daitssuapi/domain/article/service/ArticleService.kt +++ b/src/main/kotlin/com/example/daitssuapi/domain/article/service/ArticleService.kt @@ -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 @@ -91,6 +92,45 @@ class ArticleService( ) } + // topic으로 article 가져오기 + fun pageArticleListWithTopic( + pageable: Pageable, + inquiry: String?, + topic: Topic, + ): PageArticlesResponse { + val articles: Page
= + 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 { val articles: List
= articleRepository.findAllByCreatedAtIsGreaterThanEqual( createdAt = LocalDateTime.now().minusDays(7)