Conversation
|
6주차 내용을 완료했습니다. 제 나름대로 MVC를 분류했는데 수정할 부분 있으면 알려주시면 감사하겠습니다. |
|
7주차 과제를 완성했습니다. 개인적으로 @JsonProperty을 사용하는 건 코드를 확인할 때 조금 혼란이 있을 것 같지만 그래도 이번에 알게 된 어노테이션을 사용해보고 싶어 추가하게 되었습니다. 부족한 부분이 있으면 피드백 부탁드립니다. |
| public Article findById(Long id) { | ||
| return articles.get(id); | ||
| String sql = "SELECT * FROM article WHERE id = ?"; | ||
| List<Article> result = jdbcTemplate.query(sql, articleRowMapper(), id); | ||
| return result.isEmpty() ? null : result.get(0); |
There was a problem hiding this comment.
만약에 전달되는 id가 null이면 jdbc에서 예외를 던지므로 null 체크가 있으면 좋을 듯 합니다.
| @Repository | ||
| public class BoardRepository { | ||
|
|
||
| private final Map<Long, Board> boards = new HashMap<>(); | ||
| private final AtomicLong idCount = new AtomicLong(); | ||
| private final JdbcTemplate jdbcTemplate; | ||
|
|
||
| public Board save(Board board) { | ||
| if (board.getId() == null) { | ||
| long newId = idCount.incrementAndGet(); | ||
| board.setId(newId); | ||
| } | ||
| boards.put(board.getId(), board); | ||
| return board; | ||
| public BoardRepository(JdbcTemplate jdbcTemplate) { | ||
| this.jdbcTemplate = jdbcTemplate; | ||
| } | ||
|
|
||
| public Board findById(Long id) { | ||
| return boards.get(id); | ||
| String sql = "SELECT * FROM board WHERE id = ?"; | ||
| List<Board> result = jdbcTemplate.query(sql, boardRowMapper(), id); | ||
| return result.isEmpty() ? null : result.get(0); | ||
| } | ||
|
|
||
| @PostConstruct | ||
| public void initData() { | ||
| Board board1 = new Board("자유게시판"); | ||
| save(board1); | ||
|
|
||
| Board board2 = new Board("유자게시판"); | ||
| save(board2); | ||
| private RowMapper<Board> boardRowMapper() { | ||
| return (rs, rowNum) -> { | ||
| Board board = new Board(); | ||
| board.setId(rs.getLong("id")); | ||
| board.setName(rs.getString("name")); | ||
| return board; | ||
| }; | ||
| } | ||
| } No newline at end of file |
| this.jdbcTemplate = jdbcTemplate; | ||
| } | ||
|
|
||
| public Article insert(Article article) { |
There was a problem hiding this comment.
다른 코드들에서도 같은 기능인 메서드들 이름을 통일시키면 좋겠습니다.
| public String getPostsView(Model model) { | ||
| List<ArticleResponseDTO> articles = articleService.findAllArticles(); | ||
| public String getPostsView(@RequestParam(required = false) Long boardId, Model model) { | ||
| List<Article> articles = articleService.findAllArticles(boardId); |
There was a problem hiding this comment.
findAllArticles 네이밍도 좋지만, getAllArticles도 좋은 것 같습니다.
| </li> | ||
| </ul> | ||
| </body> | ||
| </html> No newline at end of file |
There was a problem hiding this comment.
모든 파일에서 EOF가 발생하고 있습니다.
아래 주소에서 확인 후 수정하시면 될 것 같습니다.
|
8주차 과제를 진행했습니다. 예외처리 부분을 이런식으로 하는게 맞는지 아직 확신이 들지 않아서 부족한 부분이 있으면 피드백 부탁드립니다. |
| @@ -67,9 +107,11 @@ public Article updateArticle(Long id, ArticleRequestDTO request) { | |||
| @Transactional | |||
| public Article deleteArticle(Long id) { | |||
| Article existArticle = articleRepository.findById(id); | |||
There was a problem hiding this comment.
게시글이 없으면 null을 반환하여 처리하는 것 보다, 게시글이 없더라도 빈 배열을 반환시켜 배열의 길이로 판단하면 좋을 것 같습니다.
|
|
||
| @Transactional | ||
| public Board createBoard(Board board) { | ||
| if (board.getName() == null) { |
There was a problem hiding this comment.
게시판 이름이 띄어쓰기로 시작하거나 " " 이렇게 null은 아니지만 공백으로만 이루어져 있다면 빈 문자열로 처리해야할지 고민해보면 좋을 것 같습니다.
| if (request.title() == null || request.content() == null || | ||
| request.authorId() == null || request.boardId() == null) { | ||
| throw new AllException(HttpStatus.BAD_REQUEST, "필수 값이 누락되었습니다."); | ||
| } | ||
|
|
||
| if (memberRepository.findById(request.authorId()) == null) { | ||
| throw new AllException(HttpStatus.BAD_REQUEST, "존재하지 않는 사용자입니다."); | ||
| } | ||
|
|
||
| if (boardRepository.findById(request.boardId()) == null) { | ||
| throw new AllException(HttpStatus.BAD_REQUEST, "존재하지 않는 게시판입니다."); | ||
| } | ||
|
|
There was a problem hiding this comment.
null을 직접 검사하는 것 보다 isEmpty를 사용해보는게 좋을 것 같다고 생각합니다. 어떻게 생각하시나요?
|
9주차입니다. 엔티티와 레포지터리 부분을 주로 변경했습니다. |
| if (member.getName() == null || member.getName().trim().isEmpty() || | ||
| member.getEmail() == null || member.getEmail().trim().isEmpty() || | ||
| member.getPassword() == null || member.getPassword().trim().isEmpty()) { | ||
| throw new AllException(HttpStatus.BAD_REQUEST, "필수 값이 누락되었거나 공백입니다."); | ||
| } |
There was a problem hiding this comment.
지금도 좋으나, 조금 더 보기 좋게 if 문을 나누거나 다른 방식으로 진행하면 더 좋을 듯 합니다!
|
10주차 부분 코딩 완료했습니다. |
|
11주차 부분입니다. JWT 방식으로 로그인을 구현하였습니다. |
실습에 있던 내용을 구현했습니다. 혹시나 구현이 안된 부분이 있거나 조금 더 효율적으로 할 수 있는 방안이 있으면 알려주시면 감사하겠습니다.