-
Notifications
You must be signed in to change notification settings - Fork 10
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
[8주차]Cow_mvc_practice(fuirian) #8
Open
fuirian
wants to merge
5
commits into
Hoya324:main
Choose a base branch
from
fuirian:main
base: main
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
Changes from 3 commits
Commits
Show all changes
5 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
48 changes: 48 additions & 0 deletions
48
src/main/java/com/cow/cow_mvc_practice/comment/controller/CommentController.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.cow.cow_mvc_practice.comment.controller; | ||
|
||
import com.cow.cow_mvc_practice.comment.controller.dto.request.CommentRequest; | ||
import com.cow.cow_mvc_practice.comment.controller.dto.request.UpdateCommentRequest; | ||
import com.cow.cow_mvc_practice.comment.entity.Comment; | ||
import com.cow.cow_mvc_practice.comment.service.CommentService; | ||
import com.cow.cow_mvc_practice.member.entity.Member; | ||
import lombok.RequiredArgsConstructor; | ||
import org.hibernate.annotations.Comments; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
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.RestController; | ||
|
||
import java.security.cert.Extension; | ||
import java.util.List; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
public class CommentController { | ||
|
||
private final CommentService commentService; | ||
|
||
@PostMapping("/api/comments") | ||
public ResponseEntity<Comment> Comment(@RequestBody CommentRequest request, Member member) { | ||
Comment savedComment = commentService.save(request, member.getId()); | ||
|
||
return ResponseEntity.status(HttpStatus.CREATED) | ||
.body(savedComment); | ||
} | ||
|
||
@GetMapping("/api/comments/{commentId}") | ||
public void findComment(@PathVariable long commentId) { | ||
Comment comment = commentService.findComment(commentId); | ||
} | ||
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. db에서 정보를 가져오는 부분인데 response가 존재하지 않습니다 |
||
|
||
@PutMapping("/api/comments/{commentId}") | ||
public ResponseEntity<Comment> updateComment(@PathVariable long id, @RequestBody UpdateCommentRequest request) { | ||
Comment updatedComment = commentService.update(id, request); | ||
|
||
return ResponseEntity.ok() | ||
.body(updatedComment); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/cow/cow_mvc_practice/comment/controller/dto/request/CommentRequest.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.cow.cow_mvc_practice.comment.controller.dto.request; | ||
|
||
import java.sql.ConnectionBuilder; | ||
import java.util.List; | ||
import com.cow.cow_mvc_practice.comment.entity.Comment; | ||
import com.cow.cow_mvc_practice.member.controller.dto.response.MemberResponse; | ||
import com.cow.cow_mvc_practice.member.entity.Member; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class CommentRequest { | ||
|
||
private Member member; | ||
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. Member로 선언하면 json파싱이 되나요? |
||
private String title; | ||
private String comment; | ||
|
||
|
||
public Comment toEntity(Member member) { | ||
return Comment.builder() | ||
.member(member) | ||
.title(title) | ||
.comment(comment) | ||
.build(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...in/java/com/cow/cow_mvc_practice/comment/controller/dto/request/UpdateCommentRequest.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.cow.cow_mvc_practice.comment.controller.dto.request; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
public class UpdateCommentRequest { | ||
private String title; | ||
private String comment; | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/cow/cow_mvc_practice/comment/controller/dto/response/CommentDto.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.cow.cow_mvc_practice.comment.controller.dto.response; | ||
|
||
import com.cow.cow_mvc_practice.comment.entity.Comment; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class CommentDto { | ||
private final Long commentId; | ||
private final String title; | ||
private final String comment; | ||
|
||
@Builder | ||
private CommentDto(Long commentId, String title, String comment) { | ||
this.commentId = commentId; | ||
this.title = title; | ||
this.comment = comment; | ||
} | ||
|
||
public static CommentDto of(Comment comment) { | ||
return CommentDto.builder() | ||
.commentId(comment.getId()) | ||
.title(comment.getTitle()) | ||
.comment(comment.getComment()) | ||
.build(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/cow/cow_mvc_practice/comment/controller/dto/response/CommentResponse.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.cow.cow_mvc_practice.comment.controller.dto.response; | ||
|
||
import com.cow.cow_mvc_practice.comment.entity.Comment; | ||
import lombok.Builder; | ||
|
||
import java.util.List; | ||
|
||
@Builder | ||
public class CommentResponse { | ||
private final String title; | ||
private final String comment; | ||
|
||
@Builder | ||
public CommentResponse(String title, String comment) { | ||
this.title = title; | ||
this.comment = comment; | ||
} | ||
|
||
public static CommentResponse from(Comment comment) { | ||
return CommentResponse.builder() | ||
.title(comment.getTitle()) | ||
.comment(comment.getComment()) | ||
.build(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/cow/cow_mvc_practice/comment/entity/Comment.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.cow.cow_mvc_practice.comment.entity; | ||
|
||
import com.cow.cow_mvc_practice.member.entity.Member; | ||
import jakarta.persistence.CascadeType; | ||
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.ManyToOne; | ||
import jakarta.persistence.OneToMany; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
@Entity | ||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Comment { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "comment_id") | ||
private Long id; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "member_id") | ||
private Member member; | ||
|
||
private String title; | ||
private String comment; | ||
|
||
@Builder | ||
private Comment(Member member, String title, String comment) { | ||
this.member = member; | ||
this.title = title; | ||
this.comment = comment; | ||
} | ||
|
||
public void update(String title, String comment) { | ||
this.title = title; | ||
this.comment = comment; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/cow/cow_mvc_practice/comment/repository/CommentRepository.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.cow.cow_mvc_practice.comment.repository; | ||
|
||
import com.cow.cow_mvc_practice.comment.entity.Comment; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.Optional; | ||
|
||
public interface CommentRepository extends JpaRepository<Comment, Long> { | ||
|
||
@Override | ||
Optional<Comment> findById(Long commentId); | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/com/cow/cow_mvc_practice/comment/service/CommentService.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.cow.cow_mvc_practice.comment.service; | ||
|
||
import com.cow.cow_mvc_practice.comment.controller.dto.request.CommentRequest; | ||
import com.cow.cow_mvc_practice.comment.controller.dto.request.UpdateCommentRequest; | ||
import com.cow.cow_mvc_practice.comment.entity.Comment; | ||
import com.cow.cow_mvc_practice.comment.repository.CommentRepository; | ||
import com.cow.cow_mvc_practice.member.service.MemberService; | ||
import jakarta.persistence.EntityNotFoundException; | ||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
@Transactional | ||
public class CommentService { | ||
|
||
private final CommentRepository commentRepository; | ||
private final MemberService memberService; | ||
|
||
public Comment save(CommentRequest commentRequest, Long memberId) { | ||
Comment comment = CommentRequest.toEntity(memberService.findOne(memberId)); | ||
return commentRepository.save(comment); | ||
} | ||
|
||
public Comment findComment(long id) { | ||
return commentRepository.findById(id) | ||
.orElseThrow(() -> new EntityNotFoundException("not found : " + id)); | ||
} | ||
|
||
@Transactional | ||
public Comment update(long id, UpdateCommentRequest request) { | ||
Comment comment = commentRepository.findById(id) | ||
.orElseThrow(() -> new IllegalArgumentException("not found : " + id)); | ||
|
||
comment.update(request.getTitle(), request.getComment()); | ||
|
||
return comment; | ||
} | ||
} |
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.
/api/comments 경로는 클래스위에 @RequestMapping으로 전체 경로 지정해주세요.