Skip to content

Commit

Permalink
Merge pull request #52 from DongnaeFriend/feature/#18
Browse files Browse the repository at this point in the history
Feature/#18
  • Loading branch information
Suanna01 authored Jul 30, 2023
2 parents c46259d + 669ddd2 commit e07b3d2
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.umc.DongnaeFriend.domain.account.sharing.controller;

import com.umc.DongnaeFriend.domain.account.sharing.dto.ReqSharingCommentDto;
import com.umc.DongnaeFriend.domain.account.sharing.dto.ResSharingCommentList;
import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingComment;
import com.umc.DongnaeFriend.domain.account.sharing.service.SharingCommentService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequiredArgsConstructor
@RestController
@RequestMapping("/account-books/sharing/comments")
public class SharingCommentController {
private final SharingCommentService sharingCommentService;

// [가계부 공유] 댓글 등록
@PostMapping("/{accountBookId}")
public String postComment(@PathVariable("accountBookId") Long accountBookId, @RequestBody ReqSharingCommentDto reqSharingCommentDto) {
sharingCommentService.newComment(accountBookId, reqSharingCommentDto);
return "";
}

// [가계부 공유] 댓글 수정
@PutMapping("/{commentId}")
public String putComment(@PathVariable("commentId") Long commentId, @RequestBody ReqSharingCommentDto reqSharingCommentDto) {
sharingCommentService.modifyComment(commentId, reqSharingCommentDto);
return "";
}

// [가계부 공유] 댓글 삭제
@DeleteMapping("/{commentId}")
public String deleteComment(@PathVariable("commentId") Long commentId) {
sharingCommentService.deleteComment(commentId);
return "";
}

// [가계부 공유] 댓글 목록 조회
@GetMapping("")
public ResSharingCommentList getList(@RequestParam Long accountBookId) {
ResSharingCommentList resSharingCommentList = sharingCommentService.getCommentList(accountBookId);
return resSharingCommentList;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.umc.DongnaeFriend.domain.account.sharing.controller;

import com.umc.DongnaeFriend.domain.account.sharing.dto.ReqSharingCommentDto;
import com.umc.DongnaeFriend.domain.account.sharing.service.SharingCommentLikeService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RequiredArgsConstructor
@RestController
@RequestMapping("/account-books/sharing/likes")
public class SharingCommentLikeController {
private final SharingCommentLikeService sharingCommentLikeService;

// [가계부 공유] 댓글 좋아요
@PostMapping("/{commentId}")
public String postCommentLike(@PathVariable("commentId") Long commentId) {
sharingCommentLikeService.newLike(commentId);
return "";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.umc.DongnaeFriend.domain.account.sharing.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class ReqSharingCommentDto {
Long parentCommentId;
String content;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.umc.DongnaeFriend.domain.account.sharing.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingComment;
import lombok.*;

import java.util.List;
import java.util.Objects;

@Getter
public class ResSharingCommentList {
int totalCount;
List<SharingComment> commentList;

@Builder
public ResSharingCommentList(int totalCount, List<SharingComment> commentList) {
this.totalCount = totalCount;
this.commentList = commentList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static lombok.AccessLevel.PROTECTED;

import com.umc.DongnaeFriend.domain.BaseTimeEntity;
import com.umc.DongnaeFriend.domain.account.sharing.dto.ReqSharingCommentDto;
import com.umc.DongnaeFriend.domain.user.entity.User;
import java.util.List;
import javax.persistence.*;
Expand Down Expand Up @@ -43,4 +44,8 @@ public class SharingComment extends BaseTimeEntity {

@Column(nullable = false)
private String content;

public void modifyComment(ReqSharingCommentDto reqSharingCommentDto) {
this.content = reqSharingCommentDto.getContent();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.umc.DongnaeFriend.domain.account.sharing.repository;

import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingComment;
import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingCommentLike;
import com.umc.DongnaeFriend.domain.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface SharingCommentLikeRepository extends JpaRepository<SharingCommentLike, Long> {
@Query("SELECT u FROM User u WHERE u.id = :user_id")
User findByUserId(@Param("user_id") Long user_id);

@Query("SELECT sc FROM SharingComment sc WHERE sc.id = :sharing_comment_id")
SharingComment findByCommentId(@Param("sharing_comment_id") Long sharing_comment_id);
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
package com.umc.DongnaeFriend.domain.account.sharing.repository;

import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingBoard;
import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingComment;
import com.umc.DongnaeFriend.domain.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Objects;

@Repository
public interface SharingCommentRepository extends JpaRepository<SharingComment, Long> {
@Query("SELECT u FROM User u WHERE u.id = :user_id")
User findByUserId(@Param("user_id") Long user_id);
@Query("SELECT sb FROM SharingBoard sb WHERE sb.id = :sharing_board_id")
SharingBoard findBySharingBoardId(@Param("sharing_board_id") Long sharing_board_id);
@Query("SELECT sc FROM SharingComment sc WHERE sc.sharingBoard = :sharingBoard")
List<SharingComment> findAllByBoard(@Param("sharingBoard") SharingBoard sharingBoard);


public int countAllBySharingBoardId(Long sharing_board_id);
int countAllByUserId(Long userId);

@Query(value = "select c from SharingComment c join fetch c.sharingBoard sb " +
"where c.user.id = :userId order by c.createdAt desc")
List<SharingComment> getCommentByUserIdAndBoard(@Param("userId") Long userId);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.umc.DongnaeFriend.domain.account.sharing.service;

import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingComment;
import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingCommentLike;
import com.umc.DongnaeFriend.domain.account.sharing.repository.SharingCommentLikeRepository;
import com.umc.DongnaeFriend.domain.user.entity.User;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@RequiredArgsConstructor
@Service
public class SharingCommentLikeService {
private final SharingCommentLikeRepository sharingCommentLikeRepository;

public String newLike(Long commentId) {
// !임시! 유저 가져오기
User user = sharingCommentLikeRepository.findByUserId(1L);

// 댓글 가져오기
SharingComment sharingComment = sharingCommentLikeRepository.findByCommentId(commentId);

SharingCommentLike sharingCommentLike = SharingCommentLike.builder()
.user(user)
.sharingComment(sharingComment)
.build();

sharingCommentLikeRepository.save(sharingCommentLike);

return "가계부 공유 댓글 좋아요 성공";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.umc.DongnaeFriend.domain.account.sharing.service;

import com.umc.DongnaeFriend.domain.account.sharing.dto.ReqSharingCommentDto;
import com.umc.DongnaeFriend.domain.account.sharing.dto.ResSharingCommentList;
import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingBoard;
import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingComment;
import com.umc.DongnaeFriend.domain.account.sharing.repository.SharingCommentRepository;
import com.umc.DongnaeFriend.domain.user.entity.User;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

@RequiredArgsConstructor
@Service
public class SharingCommentService {
private final SharingCommentRepository sharingCommentRepository;

public String newComment(Long accountBookId, ReqSharingCommentDto reqSharingCommentDto) {
// !임시! 유저 가져오기
User user = sharingCommentRepository.findByUserId(1L);

// 게시판 가져오기
SharingBoard sharingBoard = sharingCommentRepository.findBySharingBoardId(accountBookId);

// 대댓글 등록
if (!(reqSharingCommentDto.getParentCommentId() == null)){
// 부모 댓글 가져오기
Optional<SharingComment> parentCommentOptional = sharingCommentRepository.findById(reqSharingCommentDto.getParentCommentId());
SharingComment parentComment = parentCommentOptional.get();

// 댓글 빌드
SharingComment comment = SharingComment.builder()
.parentComment(parentComment)
.content(reqSharingCommentDto.getContent())
.isDeleted(false)
.sharingBoard(sharingBoard)
.user(user)
.build();

sharingCommentRepository.save(comment);

return "대댓글 등록 성공";

}

// 댓글 등록
SharingComment comment = SharingComment.builder()
.content(reqSharingCommentDto.getContent())
.isDeleted(false)
.sharingBoard(sharingBoard)
.user(user)
.build();

sharingCommentRepository.save(comment);

return "댓글 등록 성공";
}

// [가계부 공유] 댓글 수정
public String modifyComment(Long commentId, ReqSharingCommentDto reqSharingCommentDto) {
// 댓글 찾기
Optional<SharingComment> sharingCommentOptional = sharingCommentRepository.findById(commentId);
SharingComment sharingComment = sharingCommentOptional.get();

sharingComment.modifyComment(reqSharingCommentDto);

sharingCommentRepository.save(sharingComment);

return "댓글 수정 성공";
}

// [가계부 공유] 댓글 삭제
public String deleteComment(Long commentId) {
// 댓글 찾기
Optional<SharingComment> sharingCommentOptional = sharingCommentRepository.findById(commentId);
SharingComment sharingComment = sharingCommentOptional.get();

sharingCommentRepository.delete(sharingComment);

return "댓글 삭제 성공";
}

// [가계부 공유] 댓글 목록 조회
public ResSharingCommentList getCommentList(Long accountBookId) {
// 게시판 가져오기
SharingBoard sharingBoard = sharingCommentRepository.findBySharingBoardId(accountBookId);

List<SharingComment> commentList = sharingCommentRepository.findAllByBoard(sharingBoard);

ResSharingCommentList resSharingCommentList = ResSharingCommentList.builder()
.totalCount(commentList.size())
.commentList(sharingCommentRepository.findAllByBoard(sharingBoard))
.build();

return resSharingCommentList;
}

}

0 comments on commit e07b3d2

Please sign in to comment.