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 1 commit
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);
}

}
2 changes: 1 addition & 1 deletion src/main/java/HookKiller/server/common/util/UserUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static Long getCurrentUserId() {
}

public User getUser() {
return userRepository.findById(getCurrentUserId()).orElseThrow(()-> UserNotFoundException.EXCEPTION);
return userRepository.findById(1L).orElseThrow(()-> UserNotFoundException.EXCEPTION);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 코드는 롤백해주셔야할 것 같습니다.

테스트로 만들어 드렸던건데 같이 들어간거 같아요

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
.authorizeHttpRequests(authorization -> authorization
.requestMatchers(
"/login",
"/register", "/health").permitAll()
"/register", "/health", "/file/**").permitAll()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/file/**의 경우 사용자만 사용가능한 영역이어야 하기 떄문에 permitAll에 두면 안될 것 같습니다.

제가 테스트로 보여드리느라 추가했던 코드라서 롤백해주셔야 할 것 같아요 :)

.requestMatchers("/user/**").authenticated() // 인증이 되면 들어갈 수 있음
.requestMatchers("/admin/**").hasAuthority("ADMIN") // 관리자 권한만 들어갈 수 있음
)
Expand Down
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