-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* hotfix: Swagger 경로 설정 * Chore: Swagger 환경변수 설정 * Chore: Swagger 환경변수 설정 * Feat: gitignore yml파일추가 * Feat: Swagger 주소 설정 * Hotfix: 스웨거 경로 설정 * Update README.md * Update README.md * Feat: 댓글 조회/작성 구현 * Feat: 댓글 작성, 조회 구현 * Fix: 디렉토리 에러 해결 * Fix: 게시글 조회 방식 수정 * Fix: dto 수정 * Fix: 날짜 표기 변경 * Refactor: 가방 API 멤버 관련 수정 * Refactor: #48 - memberId 삭제 및 주석 추가 * Refactor: #48 - 스웨거 어노테이션 추가 * Refactor: #48 - 스웨거 어노테이션 추가 --------- Co-authored-by: tjdgns8439 <[email protected]> Co-authored-by: Minhyeok Song <[email protected]>
- Loading branch information
1 parent
241df3e
commit bbd0bd2
Showing
14 changed files
with
283 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
## 트리피 | ||
## 클밋 | ||
|
||
|
||
## MVP 핵심기능소개 | ||
|
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
45 changes: 45 additions & 0 deletions
45
src/main/java/com/example/tripy/domain/comment/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,45 @@ | ||
package com.example.tripy.domain.comment; | ||
|
||
import com.example.tripy.domain.member.Member; | ||
import com.example.tripy.domain.post.Post; | ||
import com.example.tripy.global.utils.BaseTimeEntity; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Entity | ||
@Builder | ||
public class Comment extends BaseTimeEntity { | ||
|
||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@NotNull | ||
private String content; | ||
|
||
@ManyToOne | ||
private Member member; | ||
|
||
@ManyToOne | ||
private Post post; | ||
|
||
public static Comment toEntity(Member member, Post post, String content) { | ||
return Comment.builder() | ||
.member(member) | ||
.post(post) | ||
.content(content) | ||
.build(); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/com/example/tripy/domain/comment/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,44 @@ | ||
package com.example.tripy.domain.comment; | ||
|
||
import com.example.tripy.domain.comment.dto.CommentRequestDto.CreateCommentRequest; | ||
import com.example.tripy.domain.comment.dto.CommentResponseDto.GetCommentInfo; | ||
import com.example.tripy.global.common.dto.PageResponseDto; | ||
import com.example.tripy.global.common.response.ApiResponse; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
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.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
public class CommentController { | ||
|
||
private final CommentService commentService; | ||
|
||
/** | ||
* [POST] 댓글 작성 | ||
*/ | ||
@PostMapping("api/{posts-id}/comments") | ||
public ApiResponse<String> createComment( | ||
@PathVariable(value = "posts-id") Long postId, | ||
@RequestBody CreateCommentRequest createCommentRequest | ||
) { | ||
commentService.addComment(createCommentRequest.getContent(), postId); | ||
return ApiResponse.onSuccess("댓글 작성에 성공했습니다."); | ||
} | ||
|
||
/** | ||
* [GET] 댓글 조회 | ||
*/ | ||
@GetMapping("/api/{posts-id}/comments") | ||
public ApiResponse<PageResponseDto<List<GetCommentInfo>>> findComments( | ||
@RequestParam int page, @RequestParam int size, @PathVariable(value="posts-id") Long postsId | ||
|
||
) { | ||
return ApiResponse.onSuccess(commentService.findComments(page, size, postsId)); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/example/tripy/domain/comment/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.example.tripy.domain.comment; | ||
|
||
import com.example.tripy.domain.comment.Comment; | ||
import com.example.tripy.domain.post.Post; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface CommentRepository extends JpaRepository<Comment, Long> { | ||
|
||
Page<Comment> findByPost(Post post, Pageable pageable); | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/com/example/tripy/domain/comment/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,56 @@ | ||
package com.example.tripy.domain.comment; | ||
|
||
import com.example.tripy.domain.comment.dto.CommentResponseDto.GetCommentInfo; | ||
import com.example.tripy.domain.member.Member; | ||
import com.example.tripy.domain.member.MemberRepository; | ||
import com.example.tripy.domain.post.Post; | ||
import com.example.tripy.domain.post.PostRepository; | ||
import com.example.tripy.global.common.dto.PageResponseDto; | ||
import com.example.tripy.global.common.response.code.status.ErrorStatus; | ||
import com.example.tripy.global.common.response.exception.GeneralException; | ||
import com.example.tripy.global.utils.DateTimeConverter; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class CommentService { | ||
|
||
private final CommentRepository commentRepository; | ||
private final MemberRepository memberRepository; | ||
private final PostRepository postRepository; | ||
|
||
//댓글 추가 | ||
public void addComment(String content, Long postId) { | ||
|
||
Member member = memberRepository.findById(1L) | ||
.orElseThrow(() -> new GeneralException(ErrorStatus._EMPTY_MEMBER)); | ||
|
||
Post post = postRepository.findById(postId) | ||
.orElseThrow(() -> new GeneralException(ErrorStatus._EMPTY_POST)); | ||
|
||
Comment comment = Comment.toEntity(member, post, content); | ||
commentRepository.save(comment); | ||
} | ||
|
||
//댓글 조회 | ||
PageResponseDto<List<GetCommentInfo>> findComments(int page, int size, Long postsId) { | ||
|
||
Pageable pageable = PageRequest.of(page, size); | ||
Post post = postRepository.findById(postsId) | ||
.orElseThrow(() -> new GeneralException(ErrorStatus._EMPTY_POST)); | ||
|
||
Page<Comment> commentList = commentRepository.findByPost(post, pageable); | ||
|
||
List<GetCommentInfo> dtoList = commentList.stream() | ||
.map(comment -> GetCommentInfo.toDto(comment, comment.getMember(), | ||
DateTimeConverter.convertToDisplayTime(comment.getCreatedAt()))) | ||
.toList(); | ||
|
||
return new PageResponseDto<>(commentList.getNumber(), commentList.getTotalPages(), dtoList); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/example/tripy/domain/comment/dto/CommentRequestDto.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,15 @@ | ||
package com.example.tripy.domain.comment.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
public class CommentRequestDto { | ||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
public static class CreateCommentRequest { | ||
private String content; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/example/tripy/domain/comment/dto/CommentResponseDto.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,29 @@ | ||
package com.example.tripy.domain.comment.dto; | ||
|
||
import com.example.tripy.domain.comment.Comment; | ||
import com.example.tripy.domain.member.Member; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
public class CommentResponseDto { | ||
|
||
@Getter | ||
@Builder | ||
public static class GetCommentInfo { | ||
|
||
private String nickname; | ||
private String profileImageUrl; | ||
private String content; | ||
private String createdAt; | ||
|
||
public static GetCommentInfo toDto(Comment comment, Member member, String createdAt) { | ||
return GetCommentInfo.builder() | ||
.nickname(member.getNickName()) | ||
.profileImageUrl(member.getProfileImgUrl()) | ||
.content(comment.getContent()) | ||
.createdAt(createdAt) | ||
.build(); | ||
|
||
} | ||
} | ||
} |
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
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
Oops, something went wrong.