- 
                Notifications
    You must be signed in to change notification settings 
- Fork 200
[4기 강병곤, 최준혁] Spring Boot JPA 게시판 구현 미션 제출합니다. #223
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: hyukgon
Are you sure you want to change the base?
Changes from 1 commit
2dec599
              ecd61ec
              a821939
              75773a1
              eff50c3
              6d92014
              a157d6f
              767a062
              e0ffc36
              e684a26
              0db39ee
              f74b8d9
              c7107c4
              a9824bc
              aac52b4
              3f95c3c
              54c57af
              abdc8c8
              e279941
              5171145
              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,19 @@ | ||
| package com.programmers.heheboard.domain.post; | ||
|  | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
|  | ||
| @Getter | ||
| @AllArgsConstructor | ||
| public class CreatePostRequestDto { | ||
| private String title; | ||
| private String content; | ||
| private Long userId; | ||
|          | ||
|  | ||
| public Post toEntity() { | ||
| return Post.builder() | ||
| .title(title) | ||
| .content(content) | ||
| .build(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package com.programmers.heheboard.domain.post; | ||
|  | ||
| import java.util.Objects; | ||
|  | ||
| import com.programmers.heheboard.domain.user.User; | ||
| import com.programmers.heheboard.global.BaseEntity; | ||
|  | ||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.FetchType; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.JoinColumn; | ||
| import jakarta.persistence.Lob; | ||
| import jakarta.persistence.ManyToOne; | ||
| import jakarta.persistence.Table; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|  | ||
| @Table(name = "posts") | ||
| @Entity | ||
| @Getter | ||
| @NoArgsConstructor | ||
| public class Post extends BaseEntity { | ||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.AUTO) | ||
| 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. (strategy = GenerationType.AUTO) default라 생략해도 될 것 같아요 의도하신거라면 패쓰 | ||
| private Long id; | ||
|  | ||
| @Column(nullable = false) | ||
| private String title; | ||
|  | ||
| @Lob | ||
| private String content; | ||
|          | ||
|  | ||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "user_id") | ||
| private User user; | ||
|  | ||
| @Builder | ||
| public Post(String title, String content) { | ||
| this.title = title; | ||
| this.content = content; | ||
| } | ||
| 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. p1 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. 생성자에서 유효성 검사를 하도록 수정하였습니다! 3f95c3c | ||
|  | ||
| public void setUser(User user) { | ||
|          | ||
| if (Objects.nonNull(this.user)) { | ||
| this.user.getPosts().remove(this); | ||
| } | ||
|  | ||
| this.user = user; | ||
| user.getPosts().add(this); | ||
|          | ||
| } | ||
|  | ||
| public void changeTitle(String newTitle) { | ||
| this.title = newTitle; | ||
| } | ||
|  | ||
| public void changeContents(String newContent) { | ||
| this.content = newContent; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package com.programmers.heheboard.domain.post; | ||
|  | ||
| import org.springframework.data.domain.Slice; | ||
| import org.springframework.web.bind.annotation.ExceptionHandler; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.PutMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RequestParam; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|  | ||
| import com.programmers.heheboard.global.ApiResponse; | ||
|  | ||
| import lombok.RequiredArgsConstructor; | ||
|  | ||
| @RestController | ||
| @RequiredArgsConstructor | ||
| @RequestMapping(value = "/posts") | ||
| public class PostController { | ||
|  | ||
| private final PostService postService; | ||
|  | ||
| @ExceptionHandler(Exception.class) | ||
| public ApiResponse<String> internalServerErrorHandler(Exception e) { | ||
| return ApiResponse.fail(500, e.getMessage()); | ||
| } | ||
|  | ||
| @PostMapping | ||
| public ApiResponse<PostResponseDTO> save(@RequestBody CreatePostRequestDto createPostRequestDto) { | ||
| PostResponseDTO postDto = postService.createPost(createPostRequestDto); | ||
| return ApiResponse.ok(postDto); | ||
| } | ||
|  | ||
| @PutMapping("/{post-id}") | ||
| public ApiResponse<PostResponseDTO> updatePost(@RequestBody UpdatePostRequestDto updatePostRequestDto, | ||
| @PathVariable("post-id") Long postId) { | ||
| PostResponseDTO postResponseDTO = postService.updatePost(postId, updatePostRequestDto); | ||
| return ApiResponse.ok(postResponseDTO); | ||
| } | ||
|  | ||
| @GetMapping("/{post-id}") | ||
| public ApiResponse<PostResponseDTO> getOnePost(@PathVariable("post-id") Long postId) { | ||
| PostResponseDTO postDto = postService.findPost(postId); | ||
| return ApiResponse.ok(postDto); | ||
| } | ||
|  | ||
| @GetMapping | ||
| public ApiResponse<Slice<PostResponseDTO>> getAllPost(@RequestParam int page, @RequestParam int size) { | ||
| Slice<PostResponseDTO> posts = postService.getPosts(page, size); | ||
| return ApiResponse.ok(posts); | ||
|          | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.programmers.heheboard.domain.post; | ||
|  | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.domain.Slice; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
|  | ||
| public interface PostRepository extends JpaRepository<Post, Long> { | ||
|  | ||
| Slice<Post> findSliceBy(Pageable pageable); | ||
| } | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package com.programmers.heheboard.domain.post; | ||
|  | ||
| import java.time.LocalDateTime; | ||
|  | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
|  | ||
| @Getter | ||
| public class PostResponseDTO { | ||
| private String title; | ||
| private String content; | ||
| private LocalDateTime createdAt; | ||
| private LocalDateTime modifiedAt; | ||
|  | ||
| @Builder | ||
| public PostResponseDTO(String title, String content, LocalDateTime createdAt, LocalDateTime modifiedAt) { | ||
| this.title = title; | ||
| this.content = content; | ||
| this.createdAt = createdAt; | ||
| this.modifiedAt = modifiedAt; | ||
| } | ||
|  | ||
| public static PostResponseDTO toResponse(Post post) { | ||
| return PostResponseDTO.builder() | ||
| .title(post.getTitle()) | ||
| .content(post.getContent()) | ||
| .createdAt(post.getCreatedAt()) | ||
| .modifiedAt(post.getModifiedAt()) | ||
| .build(); | ||
| } | ||
| } | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package com.programmers.heheboard.domain.post; | ||
|  | ||
| import org.springframework.data.domain.PageRequest; | ||
| import org.springframework.data.domain.Slice; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|  | ||
| import com.programmers.heheboard.domain.user.User; | ||
| import com.programmers.heheboard.domain.user.UserRepository; | ||
|  | ||
| import lombok.RequiredArgsConstructor; | ||
|  | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| public class PostService { | ||
| private final UserRepository userRepository; | ||
| private final PostRepository postRepository; | ||
|  | ||
| @Transactional | ||
| public PostResponseDTO createPost(CreatePostRequestDto createPostRequestDto) { | ||
| Post post = createPostRequestDto.toEntity(); | ||
| User user = userRepository.findById(createPostRequestDto.getUserId()) | ||
| .orElseThrow(() -> new RuntimeException("User Not Found!")); | ||
|  | ||
| post.setUser(user); | ||
|          | ||
|  | ||
| return PostResponseDTO.toResponse(postRepository.save(post)); | ||
| } | ||
|  | ||
| @Transactional | ||
| public PostResponseDTO findPost(Long postId) { | ||
| Post retrievedPost = postRepository.findById(postId) | ||
| .orElseThrow(() -> new RuntimeException("Post Not Found!")); | ||
|  | ||
| return PostResponseDTO.toResponse(retrievedPost); | ||
| } | ||
| 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. p1; 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. 오호 꿀팁 감사합니다~ 수정하였습니다~! abdc8c8 | ||
|  | ||
| @Transactional | ||
| public Slice<PostResponseDTO> getPosts(int page, int size) { | ||
| PageRequest pageRequest = PageRequest.of(page, size); | ||
|  | ||
| return postRepository.findSliceBy(pageRequest) | ||
| .map(PostResponseDTO::toResponse); | ||
| } | ||
|  | ||
| @Transactional | ||
| public PostResponseDTO updatePost(Long postId, UpdatePostRequestDto updatePostRequestDto) { | ||
| Post retrievedPost = postRepository.findById(postId) | ||
| .orElseThrow(() -> new RuntimeException("Post Not Found!")); | ||
|  | ||
| retrievedPost.changeTitle(updatePostRequestDto.getTitle()); | ||
| retrievedPost.changeContents(updatePostRequestDto.getContent()); | ||
|  | ||
| return PostResponseDTO.toResponse(retrievedPost); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.programmers.heheboard.domain.post; | ||
|  | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
|  | ||
| @Getter | ||
| @AllArgsConstructor | ||
| public class UpdatePostRequestDto { | ||
| private String title; | ||
| private String content; | ||
| } | 
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.
저는 최근부터 AllargsConstructor를 지양하는 방향으로 생각해서 사용하고 있는데요, 그 이유에 대해서도 한번 생각해보면 좋을 것 같아요!
Uh oh!
There was an error while loading. Please reload this page.
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.
저도 AllargsConstructor가 일으키는 버그에 대해 검색해 보고 AllargsConstructor를 사용하지 않도록 전체적으로 수정 하였습니다! f74b8d9