Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat : 게시판 단건조회 기능 구현 #33

Merged
merged 2 commits into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ public List<ArticleRequestDto> getArticleList(@PathVariable Long boardId, HttpSe
/**
* 단건 조회
*/
// @GetMapping("/{boardId}/{articleId}")
// public ArticleRequestDto getArticle(@PathVariable Long articleId, HttpServletRequest request) {
// BoardType language = BoardType.valueOf(request.getHeader("language"));
// return articleService.getArticle(boardId, articleId, language);
// }
@GetMapping("/{articleId}")
public ArticleRequestDto getArticle(@PathVariable Long articleId, HttpServletRequest request) {
return articleService.getArticleByArticleId(articleId, LanguageType.findTypeByRequest(request));
}

/**
* 게시글 등록
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package HookKiller.server.board.repository;

import HookKiller.server.board.entity.Article;
import HookKiller.server.board.entity.ArticleContent;
import HookKiller.server.board.entity.Board;
import HookKiller.server.board.type.ArticleStatus;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;


public interface ArticleRepository extends JpaRepository<Article, Long> {

List<Article> findAllByBoardAndArticleStatus(Board board, ArticleStatus status);

Optional<Article> findByIdAndArticleStatus(Long id, ArticleStatus status);


}
17 changes: 15 additions & 2 deletions src/main/java/HookKiller/server/board/service/ArticleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import HookKiller.server.board.dto.ArticleRequestDto;
import HookKiller.server.board.dto.PostArticleRequestDto;
import HookKiller.server.board.entity.Article;
import HookKiller.server.board.entity.ArticleContent;
import HookKiller.server.board.entity.Board;
import HookKiller.server.board.exception.ArticleContentNotFoundException;
import HookKiller.server.board.exception.BoardNotFoundException;
Expand All @@ -21,6 +22,8 @@
import java.util.ArrayList;
import java.util.List;

import static HookKiller.server.board.type.ArticleStatus.PUBLIC;

@Slf4j
@Service
@RequiredArgsConstructor
Expand All @@ -35,7 +38,7 @@ public class ArticleService {
public List<ArticleRequestDto> getArticleList(Long boardId, LanguageType language) {
// boardId로 board에 해당하는 Article들을 모두 뽑아온다
Board board = boardRepository.findById(boardId).orElseThrow(()-> BoardNotFoundException.EXCEPTION);
List<Article> articleList = articleRepository.findAllByBoardAndArticleStatus(board, ArticleStatus.PUBLIC);
List<Article> articleList = articleRepository.findAllByBoardAndArticleStatus(board, PUBLIC);

return articleList.stream()
.map(article ->
Expand All @@ -45,14 +48,23 @@ public List<ArticleRequestDto> getArticleList(Long boardId, LanguageType languag
.toList();
}

@Transactional(readOnly = true)
public ArticleRequestDto getArticleByArticleId(Long articleId, LanguageType language) {
Article article = articleRepository.findByIdAndArticleStatus(articleId, PUBLIC)
.orElseThrow(()-> ArticleContentNotFoundException.EXCEPTION);
ArticleContent content = articleContentRepository.findByArticleAndLanguage(article, language)
.orElseThrow(()-> ArticleContentNotFoundException.EXCEPTION);
return ArticleRequestDto.of(article, content);
}

public Article createArticle(PostArticleRequestDto postArticleRequestDto) {
Board board = boardRepository.findById(postArticleRequestDto.getBoardId())
.orElseThrow(() -> BoardNotFoundException.EXCEPTION);

User requestUser = userUtils.getUser();
return articleRepository.save(Article.builder()
.board(board)
.articleStatus(ArticleStatus.PUBLIC)
.articleStatus(PUBLIC)
.orgArticleLanguage(postArticleRequestDto.getOrgArticleLanguage())
.createdUser(requestUser)
.updatedUser(requestUser)
Expand Down Expand Up @@ -86,4 +98,5 @@ public void deleteArticle(Long articleId) {
.orElseThrow(() -> ArticleContentNotFoundException.EXCEPTION)
.updateStatus(ArticleStatus.DELETE);
}

}
4 changes: 2 additions & 2 deletions src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# TODO : 각자 개발용 URL, 개인 Database명 입력 필요, 해당 2개의 속성은 커밋 제외 필수!!!!!
hook:
db:
url:
database:
url: db-it7f7-kr.vpc-pub-cdb.ntruss.com
database: jh
username: hooklocal
password: hooklocal1234!
port: 3306
Expand Down
Loading