-
Notifications
You must be signed in to change notification settings - Fork 7
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
[14주차] 임현우 학습 PR 제출합니다. #2
base: main
Are you sure you want to change the base?
Changes from all commits
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,59 @@ | ||
package com.example.jpa.comment.controller; | ||
|
||
import com.example.jpa.comment.dto.request.CommentCreateRequest; | ||
import com.example.jpa.comment.dto.request.CommentUpdateRequest; | ||
import com.example.jpa.comment.dto.response.CommentResponse; | ||
import com.example.jpa.comment.service.CommentService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("api/comments") | ||
@RequiredArgsConstructor | ||
public class CommentController { | ||
|
||
private final CommentService commentService; | ||
|
||
@PostMapping | ||
public ResponseEntity<Void> create(@RequestBody CommentCreateRequest commentCreateRequest) { | ||
|
||
commentService.create(commentCreateRequest); | ||
|
||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public ResponseEntity<CommentResponse> findOne(@PathVariable("id") Long id) { | ||
|
||
CommentResponse commentResponse = commentService.findOne(id); | ||
|
||
return ResponseEntity.ok(commentResponse); | ||
} | ||
|
||
@PatchMapping("/{id}") | ||
public ResponseEntity<Void> update(@PathVariable("id") Long id, @RequestBody CommentUpdateRequest commentUpdateRequest) { | ||
|
||
commentService.update(id, commentUpdateRequest); | ||
|
||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public ResponseEntity<Void> delete(@PathVariable("id") Long id) { | ||
|
||
commentService.delete(id); | ||
|
||
return ResponseEntity.ok().build(); | ||
} | ||
|
||
@GetMapping | ||
public ResponseEntity<List> findAll() { | ||
|
||
List<CommentResponse> commentResponses = commentService.findAll(); | ||
|
||
return ResponseEntity.ok(commentResponses); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.example.jpa.comment.domain; | ||
|
||
import com.example.jpa.member.domain.Member; | ||
import com.example.jpa.post.domain.Post; | ||
import jakarta.persistence.*; | ||
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) | ||
private Long id; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "member") | ||
private Member member; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "post") | ||
private Post post; | ||
|
||
private String content; | ||
|
||
@Builder | ||
public Comment(Member member, Post post, String content) { | ||
this.member = member; | ||
this.post = post; | ||
this.content = content; | ||
} | ||
|
||
public Comment(Member member, String content) { | ||
this.member = member; | ||
this.content = content; | ||
} | ||
|
||
public void update(String content) { | ||
this.content = content; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.example.jpa.comment.dto.request; | ||
|
||
import com.example.jpa.comment.domain.Comment; | ||
import com.example.jpa.member.domain.Member; | ||
import com.example.jpa.post.domain.Post; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class CommentCreateRequest { | ||
|
||
private Long member; | ||
private Long post; | ||
private String content; | ||
|
||
public Comment toEntity(Member member, Post post) { | ||
return Comment.builder() | ||
.member(member) | ||
.post(post) | ||
.content(this.content) | ||
.build(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.example.jpa.comment.dto.request; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class CommentUpdateRequest { | ||
|
||
private String content; | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.example.jpa.comment.dto.response; | ||
|
||
import com.example.jpa.comment.domain.Comment; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class CommentResponse { | ||
|
||
private Long id; | ||
private String content; | ||
|
||
public CommentResponse(Long id, String content) { | ||
this.id = id; | ||
this.content = content; | ||
} | ||
|
||
public static CommentResponse from(Comment comment) { | ||
return new CommentResponse(comment.getId(), comment.getContent()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.example.jpa.comment.service; | ||
|
||
import com.example.jpa.comment.domain.Comment; | ||
import com.example.jpa.comment.dto.request.CommentCreateRequest; | ||
import com.example.jpa.comment.dto.request.CommentUpdateRequest; | ||
import com.example.jpa.comment.dto.response.CommentResponse; | ||
import com.example.jpa.member.domain.Member; | ||
import com.example.jpa.post.domain.Post; | ||
import com.example.jpa.repository.CommentRepository; | ||
import com.example.jpa.repository.MemberRepository; | ||
import com.example.jpa.repository.PostRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CommentService { | ||
|
||
private final CommentRepository commentRepository; | ||
private final MemberRepository memberRepository; | ||
private final PostRepository postRepository; | ||
|
||
@Transactional | ||
public void create(CommentCreateRequest commentCreateRequest) { | ||
Member member = memberRepository.findById(commentCreateRequest.getMember()).get(); | ||
Post post = postRepository.findById(commentCreateRequest.getPost()).get(); | ||
commentRepository.save(commentCreateRequest.toEntity(member, post)); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public CommentResponse findOne(Long id) { | ||
Comment comment = commentRepository.findById(id) | ||
.orElseThrow(() -> new NoSuchElementException("댓글이 존재하지 않습니다.")); | ||
|
||
return CommentResponse.from(comment); | ||
} | ||
|
||
@Transactional | ||
public void update(Long id, CommentUpdateRequest commentUpdateRequest) { | ||
Comment comment = commentRepository.findById(id) | ||
.orElseThrow(() -> new NoSuchElementException("댓글이 존재하지 않습니다.")); | ||
comment.update(commentUpdateRequest.getContent()); | ||
} | ||
|
||
@Transactional | ||
public void delete(Long id) { | ||
commentRepository.deleteById(id); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public List<CommentResponse> findAll() { | ||
return commentRepository.findAll().stream() | ||
.map(CommentResponse::from) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
package com.example.jpa.member.domain; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import com.example.jpa.comment.domain.Comment; | ||
import com.example.jpa.post.domain.Post; | ||
import jakarta.persistence.*; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Entity | ||
@Getter | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
|
@@ -20,12 +22,18 @@ public class Member { | |
|
||
private String name; | ||
|
||
@OneToMany(mappedBy = "member", cascade = CascadeType.ALL, orphanRemoval = true) | ||
private List<Post> posts = new ArrayList<>(); | ||
|
||
@OneToMany(mappedBy = "member", cascade = CascadeType.ALL, orphanRemoval = true) | ||
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. CascadeType.DELETE와 orphanRemoval 차이에 대해서도 학습하시면 좋을 것 같아요! |
||
private List<Comment> comments = new ArrayList<>(); | ||
|
||
Comment on lines
+25
to
+30
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. 간단한 토이프로젝트라서 큰 상관은 없지만 양방향 매핑은 지양하는게 좋습니다! |
||
@Builder | ||
public Member(String name) { | ||
this.name = name; | ||
} | ||
|
||
void update(String name) { | ||
public void update(String name) { | ||
this.name = name; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.example.jpa.member.dto.request; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class MemberUpdateRequest { | ||
|
||
private String name; | ||
|
||
} |
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.
제약조건 걸어주기! 당장 member필드 post필드 content필드에 null이라 빈 값이 들어올 경우도 생각해주세요.
NotNull, NotEmpty, NotBlank의 차이점에 대해 공부해보면 좋습니다!