-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Feature/#66
- Loading branch information
Showing
6 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
src/main/java/com/umc/DongnaeFriend/domain/dongnae/controller/DongnaeCommentController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
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 ""; | ||
} | ||
|
||
// [동네정보] 댓글 수정 | ||
@PutMapping("/{commentId}") | ||
public String putComment(@PathVariable("commentId") Long commentId, @RequestBody DongnaeCommentDto dongnaeCommentDto) { | ||
dongnaeCommentService.modifyComment(commentId, dongnaeCommentDto); | ||
return ""; | ||
} | ||
|
||
// [동네정보] 댓글 삭제 | ||
@DeleteMapping("/{commentId}") | ||
public String deleteComment(@PathVariable("commentId") Long commentId) { | ||
dongnaeCommentService.deleteComment(commentId); | ||
return ""; | ||
} | ||
|
||
// [동네정보] 댓글 좋아요 | ||
@PostMapping("/likes/{commentId}") | ||
public String postCommentLike(@PathVariable("commentId") Long commentId) { | ||
dongnaeCommentService.newLike(commentId); | ||
return ""; | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/umc/DongnaeFriend/domain/dongnae/dto/DongnaeCommentDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...n/java/com/umc/DongnaeFriend/domain/dongnae/respository/DongnaeCommentLikeRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.umc.DongnaeFriend.domain.dongnae.respository; | ||
|
||
import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingBoard; | ||
import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingSympathy; | ||
import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeComment; | ||
import com.umc.DongnaeFriend.domain.dongnae.entity.DongnaeCommentLike; | ||
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; | ||
|
||
@Repository | ||
public interface DongnaeCommentLikeRepository extends JpaRepository<DongnaeCommentLike, Long> { | ||
@Query(value = "SELECT dongnae_comment_like.* FROM dongnae_comment_like WHERE dongnae_comment_like.dongnae_comment_id = :comment_id", nativeQuery = true) | ||
DongnaeCommentLike findByCommentId(@Param("comment_id") DongnaeComment comment_id); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
src/main/java/com/umc/DongnaeFriend/domain/dongnae/service/DongnaeCommentService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package com.umc.DongnaeFriend.domain.dongnae.service; | ||
|
||
import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingSympathy; | ||
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.entity.DongnaeCommentLike; | ||
import com.umc.DongnaeFriend.domain.dongnae.respository.DongnaeCommentLikeRepository; | ||
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; | ||
private final DongnaeCommentLikeRepository dongnaeCommentLikeRepository; | ||
|
||
|
||
public String newComment(Long townInformationId, DongnaeCommentDto dongnaeCommentDto) { | ||
// !임시! 유저 가져오기 | ||
User user = dongnaeCommentRepository.findByUserId(1L); | ||
|
||
// 게시판 가져오기 | ||
DongnaeBoard dongnaeBoard = dongnaeCommentRepository.findByDongnaeBoardId(townInformationId); | ||
|
||
// 대댓글 등록 | ||
if (!(dongnaeCommentDto.getParentCommentId() == null)){ | ||
// 부모 댓글 가져오기 | ||
Optional<DongnaeComment> 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 "댓글 등록 성공"; | ||
} | ||
|
||
// [동네정보] 댓글 수정 | ||
public String modifyComment(Long commentId, DongnaeCommentDto dongnaeCommentDto) { | ||
// 댓글 찾기 | ||
Optional<DongnaeComment> dongnaeCommentOptional = dongnaeCommentRepository.findById(commentId); | ||
DongnaeComment dongnaeComment = dongnaeCommentOptional.get(); | ||
|
||
dongnaeComment.modifyComment(dongnaeCommentDto); | ||
|
||
dongnaeCommentRepository.save(dongnaeComment); | ||
|
||
return "댓글 수정 성공"; | ||
} | ||
|
||
public String deleteComment(Long commentId) { | ||
// 댓글 찾기 | ||
Optional<DongnaeComment> dongnaeCommentOptional = dongnaeCommentRepository.findById(commentId); | ||
DongnaeComment dongnaeComment = dongnaeCommentOptional.get(); | ||
|
||
dongnaeCommentRepository.delete(dongnaeComment); | ||
|
||
return "댓글 삭제 성공"; | ||
} | ||
|
||
public String newLike(Long commentId) { | ||
// !임시! 유저 가져오기 | ||
User user = dongnaeCommentRepository.findByUserId(1L); | ||
|
||
// 댓글 가져오기 | ||
Optional<DongnaeComment> dongnaeCommentOptional = dongnaeCommentRepository.findById(commentId); | ||
DongnaeComment dongnaeComment = dongnaeCommentOptional.get(); | ||
|
||
// 좋아요 유무 확인하기 | ||
DongnaeCommentLike dongnaeCommentLikeExist = dongnaeCommentLikeRepository.findByCommentId(dongnaeComment); | ||
|
||
if (dongnaeCommentLikeExist == null) { | ||
DongnaeCommentLike dongnaeCommentLike = DongnaeCommentLike.builder() | ||
.user(user) | ||
.dongnaeComment(dongnaeComment) | ||
.build(); | ||
|
||
dongnaeCommentLikeRepository.save(dongnaeCommentLike); | ||
|
||
return "동네정보 댓글 좋아요 성공"; | ||
} | ||
|
||
dongnaeCommentLikeRepository.delete(dongnaeCommentLikeExist); | ||
|
||
return "동네정보 댓글 좋아요 삭제 성공"; | ||
} | ||
} |