-
Notifications
You must be signed in to change notification settings - Fork 2
3주차 숙제 #2
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
Open
SANGHEEJEONG
wants to merge
3
commits into
Alom-Spring-Study:sanghee
Choose a base branch
from
SANGHEEJEONG:sanghee
base: sanghee
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
3주차 숙제 #2
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ bin/ | |
| *.iws | ||
| *.iml | ||
| *.ipr | ||
| *.yml | ||
| out/ | ||
| !**/src/main/**/out/ | ||
| !**/src/test/**/out/ | ||
|
|
||
29 changes: 29 additions & 0 deletions
29
src/main/java/com/example/springhw32/controller/CommentController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,34 @@ | ||
| package com.example.springhw32.controller; | ||
|
|
||
| import com.example.springhw32.dto.CommentDto; | ||
| import com.example.springhw32.service.CommentService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.ModelAttribute; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RequestParam; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @RequestMapping("/posts/comments") | ||
| @RestController | ||
| @RequiredArgsConstructor | ||
| public class CommentController { | ||
|
|
||
| private final CommentService commentService; | ||
|
|
||
| // 댓글 작성 | ||
| @PostMapping | ||
| public CommentDto createComment(@ModelAttribute CommentDto commentDto, @RequestParam Long postId) { | ||
| return commentService.createComment(commentDto, postId); | ||
| } | ||
|
|
||
| // 특정 게시물에 달린 모든 댓글 조회 | ||
| @GetMapping("/{postId}") | ||
| public List<CommentDto> findAllByPostId(@PathVariable Long postId) { | ||
| return commentService.findAllByPostId(postId); | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/example/springhw32/controller/PostController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,44 @@ | ||
| package com.example.springhw32.controller; | ||
|
|
||
| import com.example.springhw32.dto.PostDto; | ||
| import com.example.springhw32.service.PostService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.ModelAttribute; | ||
| import org.springframework.web.bind.annotation.PathVariable; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @RequestMapping("/posts") | ||
| @RestController | ||
| @RequiredArgsConstructor | ||
| public class PostController { | ||
| private final PostService postService; | ||
|
|
||
| // 글 작성 | ||
| @PostMapping | ||
| public PostDto createPost(@ModelAttribute PostDto postDto, Long userId) { | ||
| return postService.createPost(postDto, userId); | ||
| } | ||
|
|
||
| // 최신순 글 조회 | ||
| @GetMapping | ||
| public List<PostDto> finaAllByCreatedAtDesc() { | ||
| return postService.finaAllByCreatedAtDesc(); | ||
| } | ||
|
|
||
| // 작성자 글 조회 | ||
| @GetMapping("/{writer}") | ||
| public List<PostDto> findAllByUsername(@PathVariable("writer") String username) { | ||
| return postService.findAllByUsername(username); | ||
| } | ||
|
|
||
| // 댓글 많은 순 글 조회 | ||
| @GetMapping("/comments") | ||
| public List<PostDto> findAllByOrderByCommentCountDesc() { | ||
| return postService.findAllByOrderByCommentCountDesc(); | ||
| } | ||
| } |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/example/springhw32/controller/UserController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,24 @@ | ||
| package com.example.springhw32.controller; | ||
|
|
||
| import com.example.springhw32.dto.UserDto; | ||
| import com.example.springhw32.service.UserService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.web.bind.annotation.ModelAttribute; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RequestMapping("/users") | ||
| @RestController | ||
| @RequiredArgsConstructor | ||
| public class UserController { | ||
|
|
||
| private final UserService userService; | ||
|
|
||
| // 회원가입 | ||
| @PostMapping | ||
| public UserDto join(@ModelAttribute UserDto userDto) { | ||
| return userService.join(userDto); | ||
| } | ||
| } | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,11 @@ | ||
| package com.example.springhw32.dto; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| public class CommentDto { | ||
|
|
||
| private String content; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,20 @@ | ||
| package com.example.springhw32.dto; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| public class PostDto { | ||
|
|
||
| private String title; | ||
|
|
||
| private String content; | ||
|
|
||
| private Long commentCount; | ||
|
|
||
| private LocalDateTime createdAt; | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,14 @@ | ||
| package com.example.springhw32.dto; | ||
|
|
||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| public class UserDto { | ||
|
|
||
| private String username; | ||
|
|
||
| private String password; | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,28 @@ | ||
| package com.example.springhw32.entity; | ||
|
|
||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.ManyToOne; | ||
| import lombok.Getter; | ||
| import lombok.NonNull; | ||
| import lombok.Setter; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| @Entity | ||
| public class Comment { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.AUTO) | ||
| private Long CommentId; | ||
|
|
||
| @ManyToOne | ||
| private Post post; | ||
|
|
||
| @NonNull | ||
| private String content; | ||
|
|
||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,38 @@ | ||
| package com.example.springhw32.entity; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.ManyToOne; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| @Entity | ||
| public class Post { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.AUTO) | ||
| private Long postId; | ||
|
|
||
| @ManyToOne | ||
| private User user; | ||
|
|
||
| @Column(nullable = false) | ||
| private String title; | ||
|
|
||
| @Column(nullable = false) | ||
| private String content; | ||
|
|
||
| @Column(nullable = false) | ||
| private Long commentCount = 0L; | ||
|
|
||
| @Column(nullable = false) | ||
| private LocalDateTime createdAt; | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,27 @@ | ||
| package com.example.springhw32.entity; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| import lombok.Getter; | ||
| import lombok.NonNull; | ||
| import lombok.Setter; | ||
|
|
||
| @Getter | ||
| @Setter | ||
| @Entity | ||
| public class User { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.AUTO) | ||
| private Long id; | ||
|
|
||
| @Column(unique = true, nullable = false) | ||
| private String username; | ||
|
|
||
| @Column(nullable = false) | ||
| private String password; | ||
|
|
||
| } |
10 changes: 9 additions & 1 deletion
10
src/main/java/com/example/springhw32/repository/CommentRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,13 @@ | ||
| package com.example.springhw32.repository; | ||
|
|
||
| public interface CommentRepository { | ||
| import com.example.springhw32.entity.Comment; | ||
| import com.example.springhw32.entity.Post; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Repository | ||
| public interface CommentRepository extends JpaRepository<Comment,Long> { | ||
| List<Comment> findAllByPost_PostId(Long postId); | ||
| } |
14 changes: 13 additions & 1 deletion
14
src/main/java/com/example/springhw32/repository/PostRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,17 @@ | ||
| package com.example.springhw32.repository; | ||
|
|
||
| public interface PostRepository { | ||
| import com.example.springhw32.dto.PostDto; | ||
| import com.example.springhw32.entity.Post; | ||
| import org.hibernate.boot.archive.internal.JarProtocolArchiveDescriptor; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Repository | ||
| public interface PostRepository extends JpaRepository<Post,Long> { | ||
|
|
||
| List<Post> findAllByOrderByCreatedAtDesc(); | ||
| List<Post> findAllByUser_Username(String username); | ||
| List<Post> findAllByOrderByCommentCountDesc(); | ||
| } |
10 changes: 9 additions & 1 deletion
10
src/main/java/com/example/springhw32/repository/UserRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,13 @@ | ||
| package com.example.springhw32.repository; | ||
|
|
||
| public interface UserRepository { | ||
| import com.example.springhw32.entity.User; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
|
|
||
| @Repository | ||
| public interface UserRepository extends JpaRepository<User, Long> { | ||
|
|
||
| } |
47 changes: 47 additions & 0 deletions
47
src/main/java/com/example/springhw32/service/CommentService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,52 @@ | ||
| package com.example.springhw32.service; | ||
|
|
||
| import com.example.springhw32.dto.CommentDto; | ||
| import com.example.springhw32.entity.Comment; | ||
| import com.example.springhw32.entity.Post; | ||
| import com.example.springhw32.repository.CommentRepository; | ||
| import com.example.springhw32.repository.PostRepository; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @Service | ||
| @Transactional | ||
| @RequiredArgsConstructor | ||
| public class CommentService { | ||
|
|
||
| private final PostRepository postRepository; | ||
| private final CommentRepository commentRepository; | ||
|
|
||
| public CommentDto createComment(CommentDto commentDto, Long postId){ | ||
| Post post = postRepository.findById(postId).orElseThrow(); | ||
|
|
||
| Comment comment = new Comment(); | ||
| comment.setContent(commentDto.getContent()); | ||
| comment.setPost(post); | ||
| commentRepository.save(comment); | ||
|
|
||
| // 댓글 수 증가 | ||
| post.setCommentCount(post.getCommentCount() + 1L); | ||
| postRepository.save(post); | ||
|
|
||
| return commentDto; | ||
| } | ||
|
|
||
| public List<CommentDto> findAllByPostId(Long postId){ | ||
| return commentRepository.findAllByPost_PostId(postId).stream() | ||
| .map(this::convertToCommentDto) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
|
|
||
| public CommentDto convertToCommentDto(Comment comment) { | ||
| CommentDto commentDto = new CommentDto(); | ||
|
|
||
| commentDto.setContent(comment.getContent()); | ||
|
|
||
| return commentDto; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@nonnull과 nullable=false의 차이점이 궁금합니다..!
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.
아마
@nonnnull은 프로그램 코드 내에서 null이 들어가지 않게 하는 거고nullable=false이건 데이터베이스 column에 null이 들어가는 걸 막는 걸로 알고 잇어