-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat : 게시글 좋아요, 좋아요 해제 구현 및 테스트코드 작성 (#31)
* refactor : Post 좋아요 삽입 기능 구현, 동시성 이슈 해결을 위한 낙관적 Lock도입. * refactor : Post 좋아요 삽입 기능에 대한 테스트 코드 작성 * fix : 엔드포인트 및 메소드명 수정 * feat : Post 좋아요 해제 기능 구현 * fix : 게시물 좋아요 기능 관련 api수정
- Loading branch information
1 parent
db85927
commit 579dac9
Showing
11 changed files
with
443 additions
and
33 deletions.
There are no files selected for viewing
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
29 changes: 29 additions & 0 deletions
29
BE/school/src/main/java/thisisus/school/post/controller/PostLikeController.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,29 @@ | ||
package thisisus.school.post.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.*; | ||
import thisisus.school.auth.config.AuthenticatedMemberId; | ||
import thisisus.school.common.response.SuccessResonse; | ||
import thisisus.school.post.service.PostLikeService; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/post") | ||
public class PostLikeController { | ||
|
||
private final PostLikeService postLikeService; | ||
|
||
@PostMapping("/{postId}/like") | ||
public SuccessResonse likePost(@PathVariable("postId") final Long postId, | ||
@AuthenticatedMemberId final Long memberId) { | ||
postLikeService.likePost(postId, memberId); | ||
return SuccessResonse.of(); | ||
} | ||
|
||
@DeleteMapping("{postId}/like") | ||
public SuccessResonse disLikePost(@PathVariable("postId") final Long postId, | ||
@AuthenticatedMemberId final Long memberId) { | ||
postLikeService.disLikePost(postId, memberId); | ||
return SuccessResonse.of(); | ||
} | ||
} |
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
10 changes: 10 additions & 0 deletions
10
BE/school/src/main/java/thisisus/school/post/exception/AlreadyExistPostLikeException.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,10 @@ | ||
package thisisus.school.post.exception; | ||
|
||
import thisisus.school.common.exception.CustomException; | ||
import thisisus.school.common.exception.ExceptionCode; | ||
|
||
public class AlreadyExistPostLikeException extends CustomException { | ||
public AlreadyExistPostLikeException() { | ||
super(ExceptionCode.ALREADY_EXIST_POSTLIKE); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
BE/school/src/main/java/thisisus/school/post/exception/NotExistPostLikeException.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,10 @@ | ||
package thisisus.school.post.exception; | ||
|
||
import thisisus.school.common.exception.CustomException; | ||
import thisisus.school.common.exception.ExceptionCode; | ||
|
||
public class NotExistPostLikeException extends CustomException { | ||
public NotExistPostLikeException() { | ||
super(ExceptionCode.NOT_EXIST_POSTLIKE); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
BE/school/src/main/java/thisisus/school/post/repository/PostLikeRepository.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 thisisus.school.post.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
import thisisus.school.member.domain.Member; | ||
import thisisus.school.post.domain.Post; | ||
import thisisus.school.post.domain.PostLike; | ||
|
||
import java.util.Optional; | ||
|
||
@Repository | ||
public interface PostLikeRepository extends JpaRepository<PostLike, Long> { | ||
boolean existsByMemberAndPost(Member member, Post post); | ||
|
||
PostLike findByMemberAndPost(Member member, Post post); | ||
} |
6 changes: 6 additions & 0 deletions
6
BE/school/src/main/java/thisisus/school/post/repository/PostRepository.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 |
---|---|---|
@@ -1,10 +1,16 @@ | ||
package thisisus.school.post.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Lock; | ||
import thisisus.school.post.domain.Post; | ||
|
||
import javax.persistence.LockModeType; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface PostRepository extends JpaRepository<Post, Long> { | ||
List<Post> findAllByMemberId(Long memberId); | ||
|
||
@Lock(LockModeType.PESSIMISTIC_WRITE) | ||
Optional<Post> findWithLockById(Long id); | ||
} |
6 changes: 6 additions & 0 deletions
6
BE/school/src/main/java/thisisus/school/post/service/PostLikeService.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,6 @@ | ||
package thisisus.school.post.service; | ||
|
||
public interface PostLikeService { | ||
void likePost(Long postId, Long memberId); | ||
void disLikePost(Long postId, Long memberId); | ||
} |
59 changes: 59 additions & 0 deletions
59
BE/school/src/main/java/thisisus/school/post/service/PostLikeServiceImpl.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,59 @@ | ||
package thisisus.school.post.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import thisisus.school.member.domain.Member; | ||
import thisisus.school.member.exception.NotFoundMemberException; | ||
import thisisus.school.member.repository.MemberRepository; | ||
import thisisus.school.post.domain.Post; | ||
import thisisus.school.post.domain.PostLike; | ||
import thisisus.school.post.exception.AlreadyExistPostLikeException; | ||
import thisisus.school.post.exception.NotExistPostLikeException; | ||
import thisisus.school.post.exception.NotFoundPostException; | ||
import thisisus.school.post.repository.PostLikeRepository; | ||
import thisisus.school.post.repository.PostRepository; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class PostLikeServiceImpl implements PostLikeService { | ||
private final PostLikeRepository postLikeRepository; | ||
private final PostRepository postRepository; | ||
private final MemberRepository memberRepository; | ||
|
||
@Override | ||
@Transactional | ||
public void likePost(Long postId, Long memberId) { | ||
Member member = memberRepository.findById(memberId) | ||
.orElseThrow(NotFoundMemberException::new); | ||
Post post = postRepository.findWithLockById(postId) | ||
.orElseThrow(NotFoundPostException::new); | ||
if (postLikeRepository.existsByMemberAndPost(member, post)) { | ||
throw new AlreadyExistPostLikeException(); | ||
} | ||
post.increaseLikeCount(); | ||
|
||
PostLike postLike = PostLike.builder() | ||
.post(post) | ||
.member(member) | ||
.build(); | ||
|
||
postLikeRepository.save(postLike); | ||
} | ||
|
||
@Override | ||
@Transactional | ||
public void disLikePost(Long postId, Long memberId) { | ||
Member member = memberRepository.findById(memberId) | ||
.orElseThrow(NotFoundMemberException::new); | ||
Post post = postRepository.findWithLockById(postId) | ||
.orElseThrow(NotFoundPostException::new); | ||
if (!postLikeRepository.existsByMemberAndPost(member, post)) { | ||
throw new NotExistPostLikeException(); | ||
} | ||
post.decreaseLikeCount(); | ||
|
||
PostLike postLike = postLikeRepository.findByMemberAndPost(member, post); | ||
postLikeRepository.delete(postLike); | ||
} | ||
} |
Oops, something went wrong.