diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeCommentController.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeCommentController.java new file mode 100644 index 0000000..af6d763 --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeCommentController.java @@ -0,0 +1,23 @@ +package com.umc.DongnaeFriend.domain.dongnae.controller; + +import com.umc.DongnaeFriend.domain.account.sharing.dto.ReqSharingCommentDto; +import com.umc.DongnaeFriend.domain.dongnae.dto.DongnaeCommentDto; +import com.umc.DongnaeFriend.domain.dongnae.service.DongnaeCommentService; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; + +@RequiredArgsConstructor +@RestController +@RequestMapping("/town-information/comments") +public class DongnaeCommentController { + private final DongnaeCommentService dongnaeCommentService; + + // [동네정보] 댓글 등록 + @PostMapping("/{townInformationId}") + public String postComment(@PathVariable("townInformationId") Long townInformationId, @RequestBody DongnaeCommentDto dongnaeCommentDto) { + dongnaeCommentService.newComment(townInformationId, dongnaeCommentDto); + return ""; + } + + +} diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/dto/DongnaeCommentDto.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/dto/DongnaeCommentDto.java new file mode 100644 index 0000000..8887994 --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/dto/DongnaeCommentDto.java @@ -0,0 +1,13 @@ +package com.umc.DongnaeFriend.domain.dongnae.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class DongnaeCommentDto { + Long parentCommentId; + String content; +} diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeCommentRepository.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeCommentRepository.java index c5368cf..c1401ef 100644 --- a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeCommentRepository.java +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeCommentRepository.java @@ -1,6 +1,8 @@ package com.umc.DongnaeFriend.domain.dongnae.respository; +import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeBoard; import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeComment; +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; @@ -16,4 +18,9 @@ public interface DongnaeCommentRepository extends JpaRepository getCommentByUserIdAndBoard(@Param("userId") Long userId); + @Query("SELECT u FROM User u WHERE u.id = :user_id") + User findByUserId(@Param("user_id") Long user_id); + + @Query("SELECT db FROM DongnaeBoard db WHERE db.id = :dongnae_board_id") + DongnaeBoard findByDongnaeBoardId(@Param("dongnae_board_id") Long dongnae_board_id); } diff --git a/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeCommentService.java b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeCommentService.java new file mode 100644 index 0000000..ccd3e3a --- /dev/null +++ b/src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeCommentService.java @@ -0,0 +1,61 @@ +package com.umc.DongnaeFriend.domain.dongnae.service; + +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingBoard; +import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingComment; +import com.umc.DongnaeFriend.domain.dongnae.dto.DongnaeCommentDto; +import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeBoard; +import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeComment; +import com.umc.DongnaeFriend.domain.dongnae.respository.DongnaeCommentRepository; +import com.umc.DongnaeFriend.domain.user.entity.User; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.Optional; + +@RequiredArgsConstructor +@Service +public class DongnaeCommentService { + private final DongnaeCommentRepository dongnaeCommentRepository; + + + public String newComment(Long townInformationId, DongnaeCommentDto dongnaeCommentDto) { + // !임시! 유저 가져오기 + User user = dongnaeCommentRepository.findByUserId(1L); + + // 게시판 가져오기 + DongnaeBoard dongnaeBoard = dongnaeCommentRepository.findByDongnaeBoardId(townInformationId); + + // 대댓글 등록 + if (!(dongnaeCommentDto.getParentCommentId() == null)){ + // 부모 댓글 가져오기 + Optional parentCommentOptional = dongnaeCommentRepository.findById(dongnaeCommentDto.getParentCommentId()); + DongnaeComment parentComment = parentCommentOptional.get(); + + // 댓글 빌드 + DongnaeComment comment = DongnaeComment.builder() + .parentComment(parentComment) + .content(dongnaeCommentDto.getContent()) + .isDeleted(false) + .dongnaeBoard(dongnaeBoard) + .user(user) + .build(); + + dongnaeCommentRepository.save(comment); + + return "대댓글 등록 성공"; + + } + + // 댓글 빌드 + DongnaeComment comment = DongnaeComment.builder() + .content(dongnaeCommentDto.getContent()) + .isDeleted(false) + .dongnaeBoard(dongnaeBoard) + .user(user) + .build(); + + dongnaeCommentRepository.save(comment); + + return "댓글 등록 성공"; + } +}