Skip to content

Commit

Permalink
feature: 글 내용 조회 api 추가 (#40)
Browse files Browse the repository at this point in the history
* feature: 글 내용 조회 api 추가

* fix: spring transactional로 교체
  • Loading branch information
Sangwook02 authored Jul 27, 2024
1 parent 6820c2a commit 74b8035
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.goldbalance.dive.domain.article.domain.Article;
import com.goldbalance.dive.domain.article.domain.Quiz;
import com.goldbalance.dive.domain.article.dto.ArticleDto;
import com.goldbalance.dive.domain.article.dto.request.ArticleQueryOption;
import com.goldbalance.dive.domain.article.service.ArticleService;
import java.util.List;
Expand All @@ -26,4 +27,10 @@ public ResponseEntity<List<Quiz>> findQuizzes(@PathVariable Long articleId, @Req
List<Quiz> response = articleService.findQuizzes(articleId);
return ResponseEntity.ok(response);
}

@GetMapping("/{articleId}")
public ResponseEntity<ArticleDto> getArticleContents(@PathVariable Long articleId, @RequestBody String nickname) {
ArticleDto response = articleService.getArticleContents(nickname, articleId);
return ResponseEntity.ok(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.goldbalance.dive.domain.article.dto;

import com.goldbalance.dive.domain.article.domain.ArticleContent;
import java.util.List;

public record ArticleDto(String nickname, String title, int totalContents, List<Content> contents) {

public static ArticleDto of(String nickname, List<ArticleContent> contents) {
List<Content> contentList = contents.stream()
.map(articleContent -> new Content(articleContent.getSubTitle(), articleContent.getContent()))
.toList();

return new ArticleDto(nickname, contents.get(0).getArticle().getTitle(), contents.size(), contentList);
}

static class Content {
private String subtitle;
private String content;

public Content(String subtitle, String content) {
this.subtitle = subtitle;
this.content = content;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.goldbalance.dive.domain.article.repository;

import com.goldbalance.dive.domain.article.domain.ArticleContent;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ArticleContentRepository extends JpaRepository<ArticleContent, Long> {

List<ArticleContent> findAllByArticleId(Long articleId);
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
package com.goldbalance.dive.domain.article.service;

import com.goldbalance.dive.domain.article.domain.Article;
import com.goldbalance.dive.domain.article.domain.ArticleContent;
import com.goldbalance.dive.domain.article.domain.Quiz;
import com.goldbalance.dive.domain.article.dto.ArticleDto;
import com.goldbalance.dive.domain.article.dto.request.ArticleQueryOption;
import com.goldbalance.dive.domain.article.repository.ArticleContentRepository;
import com.goldbalance.dive.domain.article.repository.article.ArticleRepository;
import com.goldbalance.dive.domain.article.repository.quiz.QuizRepository;
import jakarta.transaction.Transactional;
import com.goldbalance.dive.global.exception.CustomException;
import com.goldbalance.dive.global.exception.ErrorCode;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
@Transactional
@Transactional(readOnly = true)
public class ArticleService {

private final ArticleRepository articleRepository;
private final ArticleContentRepository articleContentRepository;
private final QuizRepository quizRepository;

public List<Article> findArticles(ArticleQueryOption queryOption) {
Expand All @@ -25,4 +31,12 @@ public List<Article> findArticles(ArticleQueryOption queryOption) {
public List<Quiz> findQuizzes(Long articleId) {
return quizRepository.searchQuiz(articleId);
}

public ArticleDto getArticleContents(String nickname, Long articleId) {
List<ArticleContent> articleContents = articleContentRepository.findAllByArticleId(articleId);
if (articleContents.isEmpty()) {
throw new CustomException(ErrorCode.ARTICLE_NOT_FOUND);
}
return ArticleDto.of(nickname, articleContents);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ public enum ErrorCode {

// Member
MEMBER_NICKNAME_DUPLICATE(HttpStatus.BAD_REQUEST, "이미 존재하는 닉네임입니다."),
MEMBER_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 멤버입니다.");
MEMBER_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 멤버입니다."),

// Article
ARTICLE_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않는 글입니다.");

private final HttpStatus status;
private final String message;
Expand Down

0 comments on commit 74b8035

Please sign in to comment.