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

Feature 11/커뮤니티 api 수정 #12

Merged
merged 4 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
[fix] 댓글 조회 API -> 게시글 정보 포함
  • Loading branch information
Ryeolee committed Nov 12, 2023
commit 2d6fd5cb6dd78d741b3b2290bb868f10220ae3dc
2 changes: 1 addition & 1 deletion .github/workflows/actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: ModernFarmer CI/CD

on:
push:
branches: ["feature_9/11/11개발-회의-수정"]
branches: ["feature_11/커뮤니티-API-수정"]

pull_request:
branches: ["dev"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ public BaseResponseDto deleteComment(HttpServletRequest request, @Validated @Req
}

@GetMapping("/posting-comments")
public BaseResponseDto postingComment(@RequestParam("posterId") Long posterId){
public BaseResponseDto postingComment(@RequestParam("posterId") Long posterId,
@RequestParam("userId") Long userId
){

log.info("댓글 조회 완료");

return commentService.postingComment(posterId);
return commentService.postingComment(posterId, userId);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.ArrayList;

import java.util.List;


@NoArgsConstructor
Expand All @@ -14,5 +15,7 @@
@Builder
public class PostingCommentResponseDto {

ArrayList<PostingCommentDto> postingCommentList;
WholePostingDto wholePostingDto;

List<PostingCommentDto> postingCommentList;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package modernfarmer.server.farmuscommunity.community.dto.response;


import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import modernfarmer.server.farmuscommunity.community.entity.PostingImage;

import java.util.List;
import java.util.Set;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Builder
public class SpecificPostingDto {

private Integer userId;

private String title;

private String contents;

private Set<PostingImage> postingImage;

private Long postingId;

private String tag;

private String created_at;

private int commentCount;

private String nickName;

private String userImageUrl;

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public class Posting extends BaseEntity{
private String tag;


@OneToMany(mappedBy = "posting")
@OneToMany(mappedBy = "posting", fetch = FetchType.LAZY)
private Set<PostingImage> postingImages = new LinkedHashSet<>();

@OneToMany(mappedBy = "posting")
@OneToMany(mappedBy = "posting", fetch = FetchType.LAZY)
private Set<Comment> comments = new LinkedHashSet<>();

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Repository
public interface PostingRepository extends JpaRepository<Posting, Long> {
Expand All @@ -22,6 +23,12 @@ void updatePosting(@Param("userId") Long userId, @Param("title") String title,
@Param("tag") String tag
);

Optional<Posting> findById(Long postingId);

@Query("select p from Posting p join p.postingImages pi where p.id = :postingId")
Optional<Posting> getPostingData(@Param("postingId") Long postingId);



List<Posting> findByUserIdOrderByCreatedAtDesc(Long userId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,19 @@
import modernfarmer.server.farmuscommunity.community.dto.request.DeleteCommentRequest;
import modernfarmer.server.farmuscommunity.community.dto.request.UpdateCommentRequest;
import modernfarmer.server.farmuscommunity.community.dto.request.WriteCommentRequest;
import modernfarmer.server.farmuscommunity.community.dto.response.BaseResponseDto;
import modernfarmer.server.farmuscommunity.community.dto.response.PostingCommentDto;
import modernfarmer.server.farmuscommunity.community.dto.response.WholePostingDto;
import modernfarmer.server.farmuscommunity.community.dto.response.*;
import modernfarmer.server.farmuscommunity.community.entity.Comment;
import modernfarmer.server.farmuscommunity.community.entity.Posting;
import modernfarmer.server.farmuscommunity.community.entity.PostingImage;
import modernfarmer.server.farmuscommunity.community.repository.CommentRepository;
import modernfarmer.server.farmuscommunity.community.repository.PostingRepository;
import modernfarmer.server.farmuscommunity.global.exception.fail.ErrorMessage;
import modernfarmer.server.farmuscommunity.global.exception.success.SuccessMessage;
import modernfarmer.server.farmuscommunity.user.UserServiceFeignClient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;


Expand All @@ -35,6 +32,7 @@ public class CommentService {
private final CommentRepository commentRepository;
private final UserServiceFeignClient userServiceFeignClient;
private final PostingService postingService;
private final PostingRepository postingRepository;

public BaseResponseDto writeComment(Long userId, WriteCommentRequest writeCommentRequest){

Expand Down Expand Up @@ -70,39 +68,59 @@ public BaseResponseDto deleteComment(Long userId, DeleteCommentRequest deleteCom
return BaseResponseDto.of(SuccessMessage.SUCCESS, null);
}

public BaseResponseDto postingComment(Long postingId){
public BaseResponseDto postingComment(Long postingId, Long userId){


modernfarmer.server.farmuscommunity.user.dto.BaseResponseDto userData = userServiceFeignClient.allUser();
modernfarmer.server.farmuscommunity.user.dto.BaseResponseDto speicificUserData = userServiceFeignClient.specificUser(userId);

Map<String, Object> userDataMap = (Map<String, Object>) userData.getData();
LinkedHashMap<String, Object> speicificUserDataMap = (LinkedHashMap<String, Object>) speicificUserData.getData();

List<Map<String, Object>> allUserDtoList = (List<Map<String, Object>>) userDataMap.get("allUserDtoList");


Map<Integer, Map<String, Object>> userDtoMap = new HashMap<>();
for (Map<String, Object> userDto : allUserDtoList) {
Integer userId = (Integer) userDto.get("id");
userDtoMap.put(userId, userDto);
Integer getUserId = (Integer) userDto.get("id");
userDtoMap.put(getUserId, userDto);
}


List<Comment> commentList = commentRepository.findByPostingId(postingId);
Optional<Posting> posting = postingRepository.findById(postingId);

if(commentList.isEmpty()){
return BaseResponseDto.of(ErrorMessage.NOT_EXIST_COMMENT);
if(posting.isEmpty()){
return BaseResponseDto.of(ErrorMessage.NOT_EXIST_POSTING);
}

List<String> list = posting.get().getPostingImages().stream().map(PostingImage::getImageUrl)
.collect(Collectors.toList());

WholePostingDto wholePostingDto = WholePostingDto
.builder()
.userId(Math.toIntExact(userId))
.nickName((String) speicificUserDataMap.get("nickName"))
.userImageUrl((String) speicificUserDataMap.get("userImageUrl"))
.postingId(postingId)
.tag(posting.get().getTag())
.title(posting.get().getTitle())
.contents(posting.get().getContents())
.postingImage(list)
.created_at(postingService.formatCreatedAt(posting.get().getCreatedAt()))
.build();


List<PostingCommentDto> postingCommentList = commentList.stream()
.map(comment -> {

// 시간 형식 업데이트 로직
String formattedDate = postingService.formatCreatedAt(comment.getCreatedAt());

Integer userId = Math.toIntExact(comment.getUserId());
Map<String, Object> userDto = userDtoMap.get(userId);
Integer getUserId = Math.toIntExact(comment.getUserId());
Map<String, Object> userDto = userDtoMap.get(getUserId);

PostingCommentDto.PostingCommentDtoBuilder builder = PostingCommentDto.builder()
.userId(userId)
.userId(getUserId)
.postingId(postingId)
.commentContents(comment.getCommentContents())
.commentCount(commentList.size())
Expand All @@ -117,7 +135,7 @@ public BaseResponseDto postingComment(Long postingId){
})
.collect(Collectors.toList());

return BaseResponseDto.of(SuccessMessage.SUCCESS, postingCommentList);
return BaseResponseDto.of(SuccessMessage.SUCCESS, PostingCommentResponseDto.of(wholePostingDto, postingCommentList));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public enum ErrorMessage {
INTERVAL_SERVER_ERROR(2001, "요청을 처리하는 과정에서 서버가 예상하지 못한 오류가 발생하였습니다."),
REFRESH_NOTIFICATION_ERROR(2002, "Refresh Token 인증 오류"),
OBJECT_URL_INSERT_ERROR (2003,"객체 URL을 처리하는 과정에서 오류가 났습니다."),
NOT_EXIST_COMMENT (2004,"댓글이 존재하지 않습니다.");
NOT_EXIST_COMMENT (2004,"댓글이 존재하지 않습니다."),
NOT_EXIST_POSTING (2005,"게시글이 존재하지 않습니다.");



Expand Down