-
Notifications
You must be signed in to change notification settings - Fork 25
[이하원_BackEnd] 10주차 과제 제출합니다. #5
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
base: main
Are you sure you want to change the base?
Changes from all commits
649e8e5
ecbd0e5
2daa92f
03c41c7
4212b5f
3317b28
bec4afd
6faf3dc
ece05fd
00978ba
06d4b90
d8a7748
be8ff92
d29cc8f
aeecbac
c6d1c96
88826c7
36d01a7
6c91648
7fe8b9b
8d8ca66
86a3c3e
ece0f32
b1f6868
04136e7
0b95708
cd40e82
a31fa9a
8dfd075
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package com.example.bcsd; | ||
|
|
||
| import jakarta.persistence.*; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| @Entity | ||
| public class Article { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| @Column(name = "author_id") | ||
| private Long memberId; | ||
|
|
||
| private String title; | ||
| private String content; | ||
|
|
||
| @Column(name = "created_date") | ||
| private LocalDateTime createdAt; | ||
|
|
||
| @Column(name = "modified_date") | ||
| private LocalDateTime modifiedAt; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "board_id") | ||
| private Board board; | ||
|
|
||
| protected Article() { | ||
| } | ||
|
|
||
| public Article(Long memberId, String title, String content) { | ||
| this.memberId = memberId; | ||
| this.title = title; | ||
| this.content = content; | ||
| this.createdAt = LocalDateTime.now(); | ||
| this.modifiedAt = this.createdAt; | ||
| } | ||
|
|
||
| public Long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public Long getMemberId() { | ||
| return memberId; | ||
| } | ||
|
|
||
| public String getTitle() { | ||
| return title; | ||
| } | ||
|
|
||
| public String getContent() { | ||
| return content; | ||
| } | ||
|
|
||
| public LocalDateTime getCreatedAt() { | ||
| return createdAt; | ||
| } | ||
|
|
||
| public LocalDateTime getModifiedAt() { | ||
| return modifiedAt; | ||
| } | ||
|
|
||
| public void updateTitle(String newTitle) { | ||
| this.title = newTitle; | ||
| this.modifiedAt = LocalDateTime.now(); | ||
| } | ||
|
|
||
| public void updateContent(String newContent) { | ||
| this.content = newContent; | ||
| this.modifiedAt = LocalDateTime.now(); | ||
| } | ||
|
|
||
| public void setBoard(Board board){ | ||
| this.board = board; | ||
| if(!board.getArticles().contains(this)){ | ||
| board.getArticles().add(this); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| package com.example.bcsd; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.ui.Model; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.net.URI; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/articles") | ||
| public class ArticleController { | ||
| private final ArticleService articleService; | ||
| private final MemberService memberService; | ||
| private final BoardService boardService; | ||
|
|
||
| public ArticleController(ArticleService articleService, | ||
| MemberService memberService, | ||
| BoardService boardService) { | ||
| this.articleService = articleService; | ||
| this.memberService = memberService; | ||
| this.boardService = boardService; | ||
| } | ||
|
|
||
| @PostMapping | ||
| public ResponseEntity<Article> post(@RequestBody Map<String, String> article) { | ||
| long memberId = Long.parseLong(article.get("memberId")); | ||
| long boardId = Long.parseLong(article.get("boardId")); | ||
|
|
||
| try { | ||
| memberService.validId(memberId); | ||
| boardService.validId(boardId); | ||
| } catch (RuntimeException e) { | ||
| throw new ReferencedEntityNotFoundException("유효하지 않은 게시판 또는 사용자 입니다."); | ||
| } | ||
|
Comment on lines
+37
to
39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. service에서
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 예외처리의 경우에는 의도적으로 컨트롤러에서 처리했었습니다 아마 이것 관련해서 옛날 과제에서 말씀하셨던것같은데 관련해서 다음과제때 조사해보겠습니다
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 그리고 제가 저번주 수요일부터 미국에와있고 7월 4일까지 해외에 있어서 이번 11주차과제를 수행 못하여 미리 말씀드립니다 |
||
|
|
||
| String title = article.get("title"); | ||
| String content = article.get("content"); | ||
|
|
||
| if (title == null || content == null) { | ||
| throw new InvalidRequestBodyException("유효하지 않은 요청입니다."); | ||
| } | ||
|
|
||
| Article created = articleService.save(memberId, boardId, title, content); | ||
| return ResponseEntity.created(URI.create("/articles/" + created.getId())).build(); | ||
| } | ||
|
|
||
| @GetMapping("/{id}") | ||
| public ResponseEntity<Article> get(@PathVariable long id) { | ||
| try { | ||
| Article article = articleService.findById(id); | ||
| return ResponseEntity.ok(article); | ||
| } catch (RuntimeException e) { | ||
| throw new EntityNotFoundException("게시물을 찾을 수 없습니다"); | ||
| } | ||
| } | ||
|
|
||
| @PutMapping("/{id}") | ||
| public ResponseEntity<Article> put(@PathVariable long id, | ||
| @RequestBody Map<String, String> article) { | ||
| long memberId = Long.parseLong(article.get("memberId")); | ||
| long boardId = Long.parseLong(article.get("boardId")); | ||
|
|
||
| try { | ||
| memberService.validId(memberId); | ||
| boardService.validId(boardId); | ||
| } catch (RuntimeException e) { | ||
| throw new ReferencedEntityNotFoundException("유효하지 않은 게시판 또는 사용자 입니다."); | ||
| } | ||
|
|
||
| String password = article.get("password"); | ||
| if (!memberService.findById(articleService.findById(id).getMemberId()).getPassword().equals(password)) { | ||
| throw new InvalidRequestBodyException("비밀번호가 일치하지 않습니다."); | ||
| } | ||
|
|
||
| String title = article.get("title"); | ||
| String content = article.get("content"); | ||
| Article updated = articleService.update(id, title, content); | ||
| return ResponseEntity.ok(updated); | ||
| } | ||
|
|
||
| @DeleteMapping("/{id}") | ||
| public ResponseEntity<Void> delete(@PathVariable long id, | ||
| @RequestBody Map<String, String> article) { | ||
| try { | ||
| articleService.validArticle(id); | ||
| } catch (RuntimeException e) { | ||
| throw new InvalidRequestBodyException("유효한 게시물 ID가 아닙니다."); | ||
| } | ||
|
|
||
| String password = article.get("password"); | ||
| if (!memberService.findById(articleService.findById(id).getMemberId()).getPassword().equals(password)) { | ||
| throw new InvalidRequestBodyException("비밀번호가 일치하지 않습니다."); | ||
| } | ||
|
|
||
| articleService.delete(id); | ||
| return ResponseEntity.noContent().build(); | ||
| } | ||
|
|
||
| @GetMapping | ||
| public ResponseEntity<List<Article>> getArticlesByBoardId(@RequestParam(name = "boardId", required = true) | ||
| Long id) { | ||
| List<Article> articles = articleService.findByBoardId(id); | ||
|
|
||
| return ResponseEntity.ok(articles); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.example.bcsd; | ||
|
|
||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface ArticleRepository extends JpaRepository<Article, Long> { | ||
|
|
||
| List<Article> findByBoardId(Long boardId); | ||
|
|
||
| List<Article> findByMemberId(Long memberId); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package com.example.bcsd; | ||
|
|
||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Service | ||
| public class ArticleService { | ||
| private final ArticleRepository articleRepository; | ||
| private final BoardRepository boardRepository; | ||
|
|
||
| public ArticleService(ArticleRepository articleRepository, | ||
| BoardRepository boardRepository) { | ||
| this.articleRepository = articleRepository; | ||
| this.boardRepository = boardRepository; | ||
| } | ||
|
|
||
| @Transactional | ||
| public Article save(long memberId, long boardId, String title, String content) { | ||
| Board board = boardRepository.findById(boardId).get(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 맞습니다 과제할때 미처 생각하지 못한 문제였던것 같습니다 |
||
|
|
||
| Article article = new Article(memberId, title, content); | ||
| article.setBoard(board); | ||
|
|
||
| return articleRepository.save(article); | ||
| } | ||
|
|
||
| @Transactional(readOnly = true) | ||
| public Article findById(long id) { | ||
| return articleRepository.findById(id).get(); | ||
| } | ||
|
|
||
| @Transactional(readOnly = true) | ||
| public List<Article> findByBoardId(long boardId) { | ||
| return articleRepository.findByBoardId(boardId); | ||
| } | ||
|
|
||
| @Transactional(readOnly = true) | ||
| public List<Article> findByMemberId(long memberId) { | ||
| return articleRepository.findByMemberId(memberId); | ||
| } | ||
|
|
||
| @Transactional | ||
| public Article update(long id, String title, String content) { | ||
| Article article = articleRepository.findById(id).get(); | ||
| article.updateTitle(title); | ||
| article.updateContent(content); | ||
| return articleRepository.save(article); | ||
| } | ||
|
|
||
| @Transactional | ||
| public void delete(long id) { | ||
| articleRepository.deleteById(id); | ||
| } | ||
|
|
||
| @Transactional(readOnly = true) | ||
| public List<Article> findAll() { | ||
| return articleRepository.findAll(); | ||
| } | ||
|
|
||
| public boolean validArticle(long id) { | ||
| return articleRepository.existsById(id); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package com.example.bcsd; | ||
|
|
||
| import jakarta.persistence.*; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| @Entity | ||
| public class Board { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| private String name; | ||
|
|
||
| @OneToMany(mappedBy = "board", cascade = CascadeType.ALL, orphanRemoval = true) | ||
| private List<Article> articles = new ArrayList<>(); | ||
|
Comment on lines
+17
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
|
||
| protected Board() { | ||
| } | ||
|
|
||
| public Board(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public Long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void updateName(String newName) { | ||
| this.name = newName; | ||
| } | ||
|
|
||
| public List<Article> getArticles(){ | ||
| return articles; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package com.example.bcsd; | ||
|
|
||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.http.ResponseEntity; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| import java.net.URI; | ||
| import java.util.Map; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/board") | ||
| public class BoardController { | ||
| private final BoardService boardService; | ||
| private final ArticleService articleService; | ||
|
|
||
| public BoardController(BoardService boardService, ArticleService articleService1) { | ||
| this.boardService = boardService; | ||
| this.articleService = articleService1; | ||
| } | ||
|
|
||
| @PostMapping | ||
| public ResponseEntity<Board> post(@RequestBody Map<String, String> article) { | ||
| String name = article.get("name"); | ||
|
|
||
| if (name == null) { | ||
| throw new InvalidRequestBodyException("유효하지 않은 요청입니다."); | ||
| } | ||
|
|
||
| Board created = boardService.save(name); | ||
| return ResponseEntity.created(URI.create("/board/" + created.getId())).build(); | ||
| } | ||
|
|
||
| @GetMapping("/{id}") | ||
| public ResponseEntity<Board> get(@PathVariable long id) { | ||
| try { | ||
| Board board = boardService.findById(id); | ||
| return ResponseEntity.ok(board); | ||
| } catch (RuntimeException e) { | ||
| throw new EntityNotFoundException("게시판을 찾을 수 없습니다."); | ||
| } | ||
| } | ||
|
|
||
| @DeleteMapping("/{id}") | ||
| public ResponseEntity<Void> delete(@PathVariable long id) { | ||
|
|
||
| boardService.delete(id); | ||
| return ResponseEntity.noContent().build(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.example.bcsd; | ||
|
|
||
| import org.springframework.data.jpa.repository.EntityGraph; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| public interface BoardRepository extends JpaRepository<Board, Long> { | ||
|
|
||
| @Override | ||
| @EntityGraph(attributePaths = "articles") | ||
| Optional<Board> findById(Long id); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@CreatedDate어노테이션에 대해 알아보는 것도 좋을 것 같아요과제 수준에서는 변경하지 않아도 괜찮을 것 같습니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
네 더불어서
@LastModifiedDate 등도 과제하면서 찾아봤는데 해당 과제에서는 기존에 쓰던 기능으로 해도 무관할것같아 사용하지 않았습니다