Skip to content
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

User 서버 request 변경에 따른 수정사항 반영, 댓글 달기 및 조회 API #4

Merged
merged 5 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## 🔥 Summary
<!-- 작업 내용 요약 -->
- line1
- line2



## ✏️ Describe your changes
<!-- 변경 사항과 해당 되는 커밋을 링크로 걸기 -->



## 📖 Review Point
<!-- 리뷰가 필요한 포인트 설명 -->
2 changes: 0 additions & 2 deletions .github/workflows/github-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ name: Java CI with Gradle
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

permissions:
contents: read
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import porori.backend.community.config.BaseResponse;
import porori.backend.community.dto.PostResDto;
import porori.backend.community.dto.PostResDto.*;
import porori.backend.community.service.BookmarkService;

@RequiredArgsConstructor
Expand All @@ -18,7 +18,7 @@ public class BookmarkController {
//북마크 생성하기
@Operation(summary = "북마크 생성", description = "해당 postId에 북마크를 생성합니다.")
@PostMapping("/bookmark/{postId}")
public BaseResponse<PostResDto.PostContentRes> createBookmark(@RequestHeader("Authorization") String token, @PathVariable("postId") Long postId){
public BaseResponse<PostContentRes> createBookmark(@RequestHeader("Authorization") String token, @PathVariable("postId") Long postId){
return new BaseResponse<>(bookmarkService.createBookmark(token, postId));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package porori.backend.community.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import porori.backend.community.config.BaseResponse;
import porori.backend.community.dto.CommentReqDto.*;
import porori.backend.community.dto.CommentResDto.*;
import porori.backend.community.service.CommentService;

import java.util.List;

@RequiredArgsConstructor
@RequestMapping("/api/communities")
@RestController
@Tag(name = "Comment API", description = "댓글 API")
public class CommentController {
private final CommentService commentService;

@Operation(summary = "게시글에 댓글 작성", description = "postId로 게시글의 댓글을 작성합니다.")
@PostMapping("/comments/{postId}")
public BaseResponse<String> createComments(@RequestHeader("Authorization") String token, @PathVariable Long postId,
@RequestBody CommentContentReq comment){
commentService.createComment(token, postId, comment.getContent());
return new BaseResponse<>("postId가 "+postId+"인 게시글에 댓글이 작성되었습니다.");
}

@Operation(summary = "게시글의 댓글 조회", description = "postId로 게시글의 댓글을 조회합니다.")
@GetMapping("/comments/{postId}")
public BaseResponse<List<CommentDetailRes>> getComments(@RequestHeader("Authorization") String token, @PathVariable Long postId){
return new BaseResponse<>(commentService.getComments(token, postId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,10 @@ public BaseResponse<List<PostResDto.PostSwipeRes>> getPostsOnSwipe(@RequestHeade
return new BaseResponse<>(postService.getPostsOnSwipe(token, postIdList.getPostIdList()));
}

@Operation(summary = "게시글 상세 보기", description = "postId로 게시글을 조회합니다.")
@GetMapping("/post/{postId}")
public BaseResponse<PostResDto.PostDetailRes> getPostDetail(@RequestHeader("Authorization") String token, @PathVariable Long postId){
return new BaseResponse<>(postService.getPostDetail(token, postId));
}

}
37 changes: 37 additions & 0 deletions src/main/java/porori/backend/community/domain/Comment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package porori.backend.community.domain;

import lombok.*;
import org.hibernate.annotations.DynamicInsert;
import porori.backend.community.domain.core.BaseTimeEntity;

import javax.persistence.*;

@Entity
@Getter
@Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "comment")
@DynamicInsert
public class Comment extends BaseTimeEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "comment_id")
private Long commentId;

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "post_id")
private Post postId;

@Column(nullable = false, name = "user_id")
private Long userId;

@Column(nullable = false, columnDefinition = "TEXT")
private String content;

@Builder
public Comment(Post postId, Long userId, String content) {
this.postId = postId;
this.userId = userId;
this.content = content;
}
}
12 changes: 12 additions & 0 deletions src/main/java/porori/backend/community/dto/CommentReqDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package porori.backend.community.dto;

import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor
public class CommentReqDto {
@Getter
public static class CommentContentReq{
String content;
}
}
33 changes: 33 additions & 0 deletions src/main/java/porori/backend/community/dto/CommentResDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package porori.backend.community.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import porori.backend.community.domain.Comment;

import java.time.LocalDateTime;

@NoArgsConstructor
public class CommentResDto {

@Getter
@AllArgsConstructor
@NoArgsConstructor
public static class CommentDetailRes{
private String content;
private LocalDateTime createdAt;
private String nickname;
private String image;
private String backgroundColor;

@Builder
public CommentDetailRes(Comment comment, UserResDto.CommunityUserInfo userInfo){
this.content = comment.getContent();
this.createdAt = comment.getCreatedAt();
this.nickname = userInfo.getNickname();
this.image = userInfo.getImage();
this.backgroundColor = userInfo.getBackgroundColor();
}
}
}
29 changes: 29 additions & 0 deletions src/main/java/porori/backend/community/dto/PostResDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,33 @@ public PostSwipeRes(Post post, CommunityUserInfo user) {
}
}

@Getter
public static class PostDetailRes {
private String title;
private String content;
private Double latitude;
private Double longitude;
private List<String> tagList;
private List<String> imageList;
private LocalDateTime createdAt;
private String nickName;
private String imageUrl;
private String backgroundColor;


@Builder
public PostDetailRes(Post post, CommunityUserInfo user) {
this.title = post.getTitle();
this.content = post.getContent();
this.latitude = post.getLatitude();
this.longitude = post.getLongitude();
this.tagList = post.getTagList().stream().map((postTag) -> postTag.getTagId().getName()).collect(Collectors.toList());
this.imageList = post.getImageList().stream().map(PostAttach::getImageName).collect(Collectors.toList());
this.createdAt = post.getCreatedAt();
this.nickName = user.getNickname();
this.imageUrl = user.getImage();
this.backgroundColor = user.getBackgroundColor();
}
}

}
23 changes: 0 additions & 23 deletions src/main/java/porori/backend/community/dto/UserReqDto.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package porori.backend.community.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import porori.backend.community.domain.Comment;
import porori.backend.community.domain.Post;

import java.util.List;

public interface CommentRepository extends JpaRepository<Comment, Long> {
List<Comment> findAllByPostIdOrderByCreatedAt(Post post);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import porori.backend.community.config.exception.post.NotFoundPostIdException;
import porori.backend.community.domain.Bookmark;
import porori.backend.community.domain.Post;
import porori.backend.community.dto.PostResDto;
import porori.backend.community.dto.PostResDto.*;
import porori.backend.community.repository.BookmarkRepository;
import porori.backend.community.repository.PostRepository;

Expand All @@ -26,7 +26,7 @@ public class BookmarkService {

private final UserService userService;

public PostResDto.PostContentRes createBookmark(String token, Long postId){
public PostContentRes createBookmark(String token, Long postId){
//토큰 유효 확인
userService.sendTestJwtRequest(token);

Expand All @@ -48,7 +48,7 @@ public PostResDto.PostContentRes createBookmark(String token, Long postId){
.postId(post)
.build());

return new PostResDto.PostContentRes(post);
return new PostContentRes(post);

}
}
58 changes: 58 additions & 0 deletions src/main/java/porori/backend/community/service/CommentService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package porori.backend.community.service;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import porori.backend.community.config.exception.post.NotFoundPostIdException;
import porori.backend.community.domain.Comment;
import porori.backend.community.domain.Post;
import porori.backend.community.dto.CommentResDto;
import porori.backend.community.dto.CommentResDto.*;
import porori.backend.community.dto.UserResDto;
import porori.backend.community.repository.CommentRepository;
import porori.backend.community.repository.PostRepository;

import javax.transaction.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
@Transactional
public class CommentService {
private final PostRepository postRepository;
private final CommentRepository commentRepository;

private final UserService userService;

//게시글에 댓글 달기
public void createComment(String token, Long postId, String content) {
Long userId = userService.getUserId(token);

Post post = postRepository.findByPostIdAndStatus(postId, "ACTIVE").orElseThrow(NotFoundPostIdException::new);

Comment comment = Comment.builder().userId(userId).postId(post).content(content).build();

commentRepository.save(comment);
}

//게시글 댓글 조회 (분리)
public List<CommentDetailRes> getComments(String token, Long postId) {
Post post = postRepository.findByPostIdAndStatus(postId, "ACTIVE").orElseThrow(NotFoundPostIdException::new);

List<Comment> commentList = commentRepository.findAllByPostIdOrderByCreatedAt(post);
List<Long> userIdList = commentList.stream().map(Comment::getUserId).collect(Collectors.toList());

List<UserResDto.CommunityUserInfo> commentUserInfoBlocks = userService.sendCommunitiesInfoRequest(token, userIdList);

HashMap<Long, UserResDto.CommunityUserInfo> commentUserInfoHashMap = new HashMap<>();
commentUserInfoBlocks.forEach(communityUserInfo -> {
commentUserInfoHashMap.put(communityUserInfo.getUserId(), communityUserInfo);
});

return commentList.stream().map(comment -> CommentResDto.CommentDetailRes.builder()
.comment(comment)
.userInfo(commentUserInfoHashMap.get(comment.getUserId())).build()).collect(Collectors.toList());
}

}
Loading