Skip to content

Commit

Permalink
Merge pull request #23 from lotte-team6-onthefarm/feedDetail
Browse files Browse the repository at this point in the history
feat : 피드 상세페이지 태그 정보 추가 및 댓글 기능 구현
  • Loading branch information
jikimomo authored Oct 7, 2022
2 parents 85daa99 + b783fce commit 8972229
Show file tree
Hide file tree
Showing 18 changed files with 319 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.team6.onandthefarmsnsservice.controller;

import com.team6.onandthefarmsnsservice.dto.comment.CommentInfoDto;
import com.team6.onandthefarmsnsservice.service.CommentService;
import com.team6.onandthefarmsnsservice.utils.BaseResponse;
import com.team6.onandthefarmsnsservice.vo.comment.CommentDetailResponse;
import com.team6.onandthefarmsnsservice.vo.comment.CommentRequest;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.security.Principal;
import java.util.List;

@RestController
@RequestMapping("/api/user/sns/comment")
public class CommentController {

private final CommentService commentService;

@Autowired
public CommentController(CommentService commentService){
this.commentService = commentService;
}

@PostMapping
@ApiOperation("피드 댓글 등록")
public ResponseEntity<BaseResponse> addComment(@ApiIgnore Principal principal, @RequestBody CommentRequest commentUploadRequest){

Long memberId = null;
String memberRole = null;
CommentInfoDto commentInfoDto = CommentInfoDto.builder()
.memberId(memberId)
.memberRole(memberRole)
.feedId(commentUploadRequest.getFeedId())
.feedCommentContent(commentUploadRequest.getFeedCommentContent()).build();

Long commentId = commentService.addComment(commentInfoDto);

BaseResponse baseResponse = BaseResponse.builder()
.httpStatus(HttpStatus.OK)
.message("add comment success")
.data(commentId)
.build();
if(commentId == null){
baseResponse = BaseResponse.builder()
.httpStatus(HttpStatus.BAD_REQUEST)
.message("add comment fail")
.build();
return new ResponseEntity(baseResponse, HttpStatus.BAD_REQUEST);
}

return new ResponseEntity(baseResponse, HttpStatus.OK);
}

@PutMapping
@ApiOperation("피드 댓글 수정")
public ResponseEntity<BaseResponse> modifyComment(@ApiIgnore Principal principal, @RequestBody CommentRequest commentModityRequest){

Long memberId = null;
String memberRole = null;
CommentInfoDto commentInfoDto = CommentInfoDto.builder()
.memberId(memberId)
.memberRole(memberRole)
.feedCommentId(commentModityRequest.getFeedCommentId())
.feedCommentContent(commentModityRequest.getFeedCommentContent()).build();

Long commentId = commentService.modifyComment(commentInfoDto);

BaseResponse baseResponse = BaseResponse.builder()
.httpStatus(HttpStatus.OK)
.message("add comment success")
.data(commentId)
.build();
if(commentId == null){
baseResponse = BaseResponse.builder()
.httpStatus(HttpStatus.BAD_REQUEST)
.message("add comment fail")
.build();
return new ResponseEntity(baseResponse, HttpStatus.BAD_REQUEST);
}

return new ResponseEntity(baseResponse, HttpStatus.OK);
}

@GetMapping
@ApiOperation("피드 댓글 조회")
public ResponseEntity<BaseResponse<List<CommentDetailResponse>>> findComment(@RequestParam Long feedId){

List<CommentDetailResponse> commentList = commentService.findCommentDetail(feedId);

BaseResponse baseResponse = BaseResponse.builder()
.httpStatus(HttpStatus.OK)
.message("find comment success")
.data(commentList)
.build();
if(commentList == null){
baseResponse = BaseResponse.builder()
.httpStatus(HttpStatus.BAD_REQUEST)
.message("find comment fail")
.build();
return new ResponseEntity(baseResponse, HttpStatus.BAD_REQUEST);
}

return new ResponseEntity(baseResponse, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import com.team6.onandthefarmsnsservice.dto.FeedInfoDto;
import com.team6.onandthefarmsnsservice.service.FeedService;
import com.team6.onandthefarmsnsservice.utils.BaseResponse;
import com.team6.onandthefarmsnsservice.vo.FeedDetailResponse;
import com.team6.onandthefarmsnsservice.vo.FeedUploadProductRequest;
import com.team6.onandthefarmsnsservice.vo.FeedUploadRequest;
import com.team6.onandthefarmsnsservice.vo.feed.FeedDetailResponse;
import com.team6.onandthefarmsnsservice.vo.feed.FeedUploadProductRequest;
import com.team6.onandthefarmsnsservice.vo.feed.FeedUploadRequest;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -70,7 +70,6 @@ public ResponseEntity<BaseResponse<FeedDetailResponse>> findFeedDetail(@RequestP
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
}

//태그도 보내줘야함
FeedDetailResponse feedDetailResponse = feedService.findFeedDetail(feedId);

BaseResponse baseResponse = BaseResponse.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.team6.onandthefarmsnsservice.dto;

import com.team6.onandthefarmsnsservice.vo.imageProduct.ImageProductInfo;
import com.team6.onandthefarmsnsservice.vo.feed.imageProduct.ImageProductInfo;
import lombok.Data;
import org.springframework.web.multipart.MultipartFile;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.team6.onandthefarmsnsservice.dto.comment;

import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class CommentInfoDto {

private Long memberId;
private String memberRole;
private Long feedId;
private Long feedCommentId;
private String feedCommentContent;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public class FeedComment {
@JoinColumn(name = "feedId")
private Feed feed;

private Long memberId;

private String memberRole;

private String feedCommentContent;

private String feedCommentCreateAt;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package com.team6.onandthefarmsnsservice.repository;

import com.team6.onandthefarmsnsservice.entity.FeedComment;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface FeedCommentRepository extends CrudRepository<FeedComment,Long> {

@Query("select c from FeedComment c where c.feed.feedId=:feedId")
List<FeedComment> findByFeedId(@Param("feedId") Long feedId);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.team6.onandthefarmsnsservice.repository;

import com.team6.onandthefarmsnsservice.entity.Feed;
import com.team6.onandthefarmsnsservice.entity.FeedTag;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface FeedTagRepository extends JpaRepository<FeedTag, Long> {

List<FeedTag> findByFeed(Feed feed);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.team6.onandthefarmsnsservice.service;

import com.team6.onandthefarmsnsservice.dto.comment.CommentInfoDto;
import com.team6.onandthefarmsnsservice.vo.comment.CommentDetailResponse;

import java.util.List;

public interface CommentService {

List<CommentDetailResponse> findCommentDetail(Long feedId);

Long addComment(CommentInfoDto commentInfoDto);

Long modifyComment(CommentInfoDto commentInfoDto);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.team6.onandthefarmsnsservice.service;

import com.team6.onandthefarmsnsservice.dto.comment.CommentInfoDto;
import com.team6.onandthefarmsnsservice.entity.Feed;
import com.team6.onandthefarmsnsservice.entity.FeedComment;
import com.team6.onandthefarmsnsservice.feignclient.MemberServiceClient;
import com.team6.onandthefarmsnsservice.repository.FeedCommentRepository;
import com.team6.onandthefarmsnsservice.repository.FeedRepository;
import com.team6.onandthefarmsnsservice.utils.DateUtils;
import com.team6.onandthefarmsnsservice.vo.comment.CommentDetailResponse;
import com.team6.onandthefarmsnsservice.vo.user.Seller;
import com.team6.onandthefarmsnsservice.vo.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
@Transactional
public class CommentServiceImpl implements CommentService {

private final FeedRepository feedRepository;
private final FeedCommentRepository feedCommentRepository;
private final MemberServiceClient memberServiceClient;
private DateUtils dateUtils;
Environment env;

@Autowired
public CommentServiceImpl(FeedRepository feedRepository,
FeedCommentRepository feedCommentRepository,
MemberServiceClient memberServiceClient,
DateUtils dateUtils,
Environment env){
this.feedRepository = feedRepository;
this.feedCommentRepository = feedCommentRepository;
this.memberServiceClient = memberServiceClient;
this.dateUtils = dateUtils;
this.env = env;
}

@Override
public List<CommentDetailResponse> findCommentDetail(Long feedId) {

List<CommentDetailResponse> commentDetailList = new ArrayList<>();

List<FeedComment> feedCommentList = feedCommentRepository.findByFeedId(feedId);
for(FeedComment feedComment : feedCommentList){
CommentDetailResponse commentDetail = CommentDetailResponse.builder()
.memberId(feedComment.getMemberId())
.memberRole(feedComment.getMemberRole())
.feedCommnetId(feedComment.getFeedCommnetId())
.feedCommentContent(feedComment.getFeedCommentContent())
.feedCommentCreateAt(feedComment.getFeedCommentCreateAt())
.feedCommentModifiedAt(feedComment.getFeedCommentModifiedAt())
.build();

if(feedComment.getMemberRole().equals("user")){
User user = memberServiceClient.findByUserId(feedComment.getMemberId());
commentDetail.setMemberName(user.getUserName());
}
else if(feedComment.getMemberRole().equals("seller")){
Seller seller = memberServiceClient.findBySellerId(feedComment.getMemberId());
commentDetail.setMemberName(seller.getSellerName());
}

commentDetailList.add(commentDetail);
}

return commentDetailList;
}

@Override
public Long addComment(CommentInfoDto commentInfoDto) {

Optional<Feed> feed = feedRepository.findById(commentInfoDto.getFeedId());

FeedComment feedComment = new FeedComment();
feedComment.setMemberId(commentInfoDto.getMemberId());
feedComment.setMemberRole(commentInfoDto.getMemberRole());
feedComment.setFeed(feed.get());
feedComment.setFeedCommentContent(commentInfoDto.getFeedCommentContent());
feedComment.setFeedCommentCreateAt(dateUtils.transDate(env.getProperty("dateutils.format")));

FeedComment savedFeedComment = feedCommentRepository.save(feedComment);
return savedFeedComment.getFeedCommnetId();
}

@Override
public Long modifyComment(CommentInfoDto commentInfoDto) {

Optional<FeedComment> feedComment = feedCommentRepository.findById(commentInfoDto.getFeedCommentId());

if(feedComment.isPresent()){
feedComment.get().setFeedCommentContent(commentInfoDto.getFeedCommentContent());
feedComment.get().setFeedCommentModifiedAt(dateUtils.transDate(env.getProperty("dateutils.format")));
}

return feedComment.get().getFeedCommnetId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.team6.onandthefarmsnsservice.dto.profile.ProfileMainFeedDto;
import com.team6.onandthefarmsnsservice.dto.profile.ProfileMainScrapDto;
import com.team6.onandthefarmsnsservice.dto.profile.ProfileMainWishDto;
import com.team6.onandthefarmsnsservice.vo.FeedDetailResponse;
import com.team6.onandthefarmsnsservice.vo.feed.FeedDetailResponse;
import com.team6.onandthefarmsnsservice.vo.FeedResponse;
import com.team6.onandthefarmsnsservice.dto.FeedInfoDto;
import com.team6.onandthefarmsnsservice.vo.profile.ProfileMainFeedResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import com.team6.onandthefarmsnsservice.feignclient.MemberServiceClient;
import com.team6.onandthefarmsnsservice.feignclient.ProductServiceClient;
import com.team6.onandthefarmsnsservice.repository.*;
import com.team6.onandthefarmsnsservice.vo.FeedDetailResponse;
import com.team6.onandthefarmsnsservice.vo.feed.FeedDetailResponse;
import com.team6.onandthefarmsnsservice.vo.FeedResponse;
import com.team6.onandthefarmsnsservice.vo.imageProduct.ImageInfo;
import com.team6.onandthefarmsnsservice.vo.feed.imageProduct.ImageInfo;
import com.team6.onandthefarmsnsservice.vo.profile.ProfileMainFeedResponse;
import com.team6.onandthefarmsnsservice.vo.profile.ProfileMainScrapResponse;
import com.team6.onandthefarmsnsservice.vo.profile.ProfileMainWishResponse;
Expand All @@ -25,7 +25,7 @@
import org.springframework.data.domain.Sort;

import com.team6.onandthefarmsnsservice.utils.DateUtils;
import com.team6.onandthefarmsnsservice.vo.imageProduct.ImageProductInfo;
import com.team6.onandthefarmsnsservice.vo.feed.imageProduct.ImageProductInfo;

import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -362,6 +362,8 @@ public FeedDetailResponse findFeedDetail(Long feedId) {
}
}

List<FeedTag> feedTagList = feedTagRepository.findByFeed(feedEntity);

feedDetailResponse = FeedDetailResponse.builder()
.feedId(feedEntity.getFeedId())
.feedTitle(feedEntity.getFeedTitle())
Expand All @@ -375,6 +377,7 @@ public FeedDetailResponse findFeedDetail(Long feedId) {
.feedUpdateAt(feedEntity.getFeedUpdateAt())
.feedImageList(imageInfoList)
.feedImageProductList(imageProductInfoList)
.feedTag(feedTagList)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.team6.onandthefarmsnsservice.vo.comment;

import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class CommentDetailResponse {

private Long memberId;

private String memberRole;

private String memberName;

private Long feedCommnetId;

private String feedCommentContent;

private String feedCommentCreateAt;

private String feedCommentModifiedAt;
}
Loading

0 comments on commit 8972229

Please sign in to comment.