-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
703 additions
and
0 deletions.
There are no files selected for viewing
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
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
49 changes: 49 additions & 0 deletions
49
src/main/java/com/api/TaveShot/domain/Comment/controller/CommentApiController.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,49 @@ | ||
package com.api.TaveShot.domain.Comment.controller; | ||
|
||
import com.api.TaveShot.domain.Comment.dto.CommentDto; | ||
import com.api.TaveShot.domain.Comment.service.CommentService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
@RequiredArgsConstructor | ||
@RequestMapping("/api") | ||
@RestController | ||
public class CommentApiController { | ||
|
||
private final CommentService commentService; | ||
|
||
/* CREATE */ | ||
@PostMapping("/post/{id}/comments") | ||
public ResponseEntity<Long> save(@PathVariable Long id, @RequestBody CommentDto.Request dto) { | ||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
String gitIdAsString = authentication.getName(); | ||
|
||
Long gitId = Long.valueOf(gitIdAsString); | ||
return ResponseEntity.ok(commentService.save(id, gitId, dto)); | ||
} | ||
|
||
/* READ */ | ||
@GetMapping("/post/{id}/comments") | ||
public List<CommentDto.Response> read(@PathVariable Long id) { | ||
return commentService.findAll(id); | ||
} | ||
|
||
/* UPDATE */ | ||
@PutMapping({"/post/{postId}/comments/{id}"}) | ||
public ResponseEntity<Long> update(@PathVariable Long postId, @PathVariable Long id, @RequestBody CommentDto.Request dto) { | ||
commentService.update(postId, id, dto); | ||
return ResponseEntity.ok(id); | ||
} | ||
|
||
/* DELETE */ | ||
@DeleteMapping("/post/{postId}/comments/{id}") | ||
public ResponseEntity<Long> delete(@PathVariable Long postId, @PathVariable Long id) { | ||
commentService.delete(postId, id); | ||
return ResponseEntity.ok(id); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/com/api/TaveShot/domain/Comment/domain/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,59 @@ | ||
package com.api.TaveShot.domain.Comment.domain; | ||
|
||
import com.api.TaveShot.domain.Member.domain.Member; | ||
import com.api.TaveShot.domain.Post.domain.Post; | ||
import jakarta.persistence.*; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.data.annotation.CreatedDate; | ||
import org.springframework.data.annotation.LastModifiedDate; | ||
|
||
import java.util.List; | ||
|
||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
@Table(name = "comments") | ||
@Entity | ||
public class Comment { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@Column(columnDefinition = "TEXT", nullable = false) | ||
private String comment; // 댓글 내용 | ||
|
||
@Column(name = "created_date") | ||
@CreatedDate | ||
private String createdDate; | ||
|
||
@Column(name = "modified_date") | ||
@LastModifiedDate | ||
private String modifiedDate; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "post_id") | ||
private Post post; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "member_id") | ||
private Member member; // 작성자 | ||
|
||
/* | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "parent_comment_id") | ||
private Comment parentComment; // 부모 댓글 | ||
@OneToMany(mappedBy = "parentComment", cascade = CascadeType.ALL) | ||
private List<Comment> childComments; // 자식 댓글들 | ||
*/ | ||
|
||
/* 댓글 수정 */ | ||
public void update(String comment) { | ||
this.comment = comment; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/api/TaveShot/domain/Comment/domain/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,14 @@ | ||
package com.api.TaveShot.domain.Comment.domain; | ||
|
||
import com.api.TaveShot.domain.Post.domain.Post; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface CommentRepository extends JpaRepository<Comment,Long> { | ||
/* 게시글 댓글 목록 가져오기 */ | ||
List<Comment> getCommentByPostOrderById(Post post); | ||
|
||
Optional<Comment> findByPostIdAndId(Long postId, Long id); | ||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/com/api/TaveShot/domain/Comment/dto/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,68 @@ | ||
package com.api.TaveShot.domain.Comment.dto; | ||
|
||
import com.api.TaveShot.domain.Comment.domain.Comment; | ||
import com.api.TaveShot.domain.Member.domain.Member; | ||
import com.api.TaveShot.domain.Post.domain.Post; | ||
import lombok.*; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class CommentDto { | ||
|
||
/** 댓글 Service 요청을 위한 Request 클래스 **/ | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
public static class Request { | ||
private Long id; | ||
private String comment; | ||
private String createdDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm")); | ||
private String modifiedDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm")); | ||
private Member member; | ||
private Post post; | ||
|
||
|
||
/* Dto -> Entity */ | ||
public Comment toEntity() { | ||
|
||
return Comment.builder() | ||
.id(id) | ||
.comment(comment) | ||
.createdDate(createdDate) | ||
.modifiedDate(modifiedDate) | ||
.member(member) | ||
.post(post) | ||
.build(); | ||
} | ||
} | ||
|
||
|
||
/** 댓글 정보를 리턴할 Response 클래스 **/ | ||
@RequiredArgsConstructor | ||
@Getter | ||
public static class Response { | ||
private Long id; | ||
private String comment; | ||
private String createdDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm")); | ||
private String modifiedDate = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm")); | ||
private String gitName; | ||
private Long memberId; | ||
private Long postId; | ||
|
||
|
||
/* Entity -> Dto*/ | ||
public Response(Comment comment) { | ||
this.id = comment.getId(); | ||
this.comment = comment.getComment(); | ||
this.createdDate = comment.getCreatedDate(); | ||
this.modifiedDate = comment.getModifiedDate(); | ||
this.gitName = comment.getMember().getGitName(); | ||
this.memberId = comment.getMember().getId(); | ||
this.postId = comment.getPost().getId(); | ||
} | ||
} | ||
|
||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/com/api/TaveShot/domain/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,68 @@ | ||
package com.api.TaveShot.domain.Comment.service; | ||
|
||
import com.api.TaveShot.domain.Comment.domain.Comment; | ||
import com.api.TaveShot.domain.Comment.domain.CommentRepository; | ||
import com.api.TaveShot.domain.Comment.dto.CommentDto; | ||
import com.api.TaveShot.domain.Member.domain.Member; | ||
import com.api.TaveShot.domain.Member.repository.MemberRepository; | ||
import com.api.TaveShot.domain.Post.domain.Post; | ||
import com.api.TaveShot.domain.Post.domain.PostRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class CommentService { | ||
|
||
private final CommentRepository commentRepository; | ||
private final MemberRepository memberRepository; | ||
private final PostRepository postRepository; | ||
|
||
/* CREATE */ | ||
@Transactional | ||
public Long save(Long id, Long gitId, CommentDto.Request dto) { | ||
Optional<Member> member = memberRepository.findByGitId(gitId); | ||
Post post = postRepository.findById(id).orElseThrow(() -> | ||
new IllegalArgumentException("댓글 쓰기 실패: 해당 게시글이 존재하지 않습니다. " + id)); | ||
|
||
dto.setMember(member.orElse(null)); | ||
dto.setPost(post); | ||
|
||
Comment comment = dto.toEntity(); | ||
commentRepository.save(comment); | ||
|
||
return comment.getId(); | ||
} | ||
|
||
/* READ */ | ||
@Transactional(readOnly = true) | ||
public List<CommentDto.Response> findAll(Long id) { | ||
Post post = postRepository.findById(id).orElseThrow(() -> | ||
new IllegalArgumentException("해당 게시글이 존재하지 않습니다. id: " + id)); | ||
List<Comment> comments = post.getComments(); | ||
return comments.stream().map(CommentDto.Response::new).collect(Collectors.toList()); | ||
} | ||
|
||
/* UPDATE */ | ||
@Transactional | ||
public void update(Long postId, Long id, CommentDto.Request dto) { | ||
Comment comment = commentRepository.findByPostIdAndId(postId, id).orElseThrow(() -> | ||
new IllegalArgumentException("해당 댓글이 존재하지 않습니다. " + id)); | ||
|
||
comment.update(dto.getComment()); | ||
} | ||
|
||
/* DELETE */ | ||
@Transactional | ||
public void delete(Long postId, Long id) { | ||
Comment comment = commentRepository.findByPostIdAndId(postId, id).orElseThrow(() -> | ||
new IllegalArgumentException("해당 댓글이 존재하지 않습니다. id=" + id)); | ||
|
||
commentRepository.delete(comment); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -28,3 +28,4 @@ public class Member extends BaseEntity { | |
private String profileImageUrl; | ||
|
||
} | ||
|
55 changes: 55 additions & 0 deletions
55
src/main/java/com/api/TaveShot/domain/Post/controller/PostApiController.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,55 @@ | ||
package com.api.TaveShot.domain.Post.controller; | ||
|
||
import com.api.TaveShot.domain.Post.dto.PostDto; | ||
import com.api.TaveShot.domain.Post.service.PostService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContext; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
|
||
/** REST API Controller **/ | ||
@RequestMapping("/api") | ||
@RequiredArgsConstructor | ||
@RestController | ||
public class PostApiController { | ||
|
||
private final PostService postService; | ||
|
||
/* CREATE : 게시글을 현재 로그인한 사용자의 gitID를 가져와 관련된 정보로 저장 */ | ||
@PostMapping("/post") | ||
public ResponseEntity<Long> save(@RequestBody PostDto.Request dto) { | ||
SecurityContext securityContext = SecurityContextHolder.getContext(); | ||
|
||
Authentication authentication = securityContext.getAuthentication(); | ||
String gitIdAsString = authentication.getName(); | ||
|
||
Long gitId = Long.valueOf(gitIdAsString); | ||
|
||
return ResponseEntity.ok(postService.save(dto, gitId)); | ||
} | ||
|
||
/* READ */ | ||
@GetMapping("/post/{id}") | ||
public ResponseEntity<PostDto.Response> read(@PathVariable Long id) { | ||
PostDto.Response postResponse = postService.findById(id); | ||
return ResponseEntity.ok(postResponse); | ||
} | ||
|
||
/* UPDATE */ | ||
@PutMapping("/post/{id}") | ||
public ResponseEntity<Long> update(@PathVariable Long id, @RequestBody PostDto.Request dto) { | ||
postService.update(id, dto); | ||
return ResponseEntity.ok(id); | ||
} | ||
|
||
/* DELETE */ | ||
@DeleteMapping("/post/{id}") | ||
public ResponseEntity<Long> delete(@PathVariable Long id) { | ||
postService.delete(id); | ||
return ResponseEntity.ok(id); | ||
} | ||
|
||
} |
Oops, something went wrong.