-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from Ewha-thon-Melting-Pot/feat/post
[feat] 투표 생성 및 삭제
- Loading branch information
Showing
10 changed files
with
109 additions
and
117 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
42 changes: 0 additions & 42 deletions
42
src/main/java/melting_pot/ewha_sinmungo/post/domain/PostMemberReaction.java
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
src/main/java/melting_pot/ewha_sinmungo/post/domain/PostMemberReactionId.java
This file was deleted.
Oops, something went wrong.
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
8 changes: 0 additions & 8 deletions
8
src/main/java/melting_pot/ewha_sinmungo/post/repository/PostMemberReactionRepository.java
This file was deleted.
Oops, something went wrong.
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
Empty file.
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
14 changes: 14 additions & 0 deletions
14
src/main/java/melting_pot/ewha_sinmungo/vote/reporitory/VoteRepository.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,14 @@ | ||
package melting_pot.ewha_sinmungo.vote.reporitory; | ||
|
||
import melting_pot.ewha_sinmungo.member.entity.Member; | ||
import melting_pot.ewha_sinmungo.post.entity.Post; | ||
import melting_pot.ewha_sinmungo.vote.entity.Vote; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.Optional; | ||
|
||
public interface VoteRepository extends JpaRepository<Vote, Long> { | ||
boolean existsByMemberAndPost(Member member, Post post); | ||
|
||
Optional<Vote> findByMemberAndPost(Member member, Post post); | ||
} |
67 changes: 67 additions & 0 deletions
67
src/main/java/melting_pot/ewha_sinmungo/vote/service/VoteService.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,67 @@ | ||
package melting_pot.ewha_sinmungo.vote.service; | ||
|
||
|
||
import lombok.RequiredArgsConstructor; | ||
import melting_pot.ewha_sinmungo.member.entity.Member; | ||
import melting_pot.ewha_sinmungo.member.service.MemberService; | ||
import melting_pot.ewha_sinmungo.post.entity.Post; | ||
import melting_pot.ewha_sinmungo.post.repository.PostRepository; | ||
import melting_pot.ewha_sinmungo.post.service.PostService; | ||
import melting_pot.ewha_sinmungo.vote.entity.Vote; | ||
import melting_pot.ewha_sinmungo.vote.reporitory.VoteRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Lazy; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Transactional | ||
@Service | ||
@RequiredArgsConstructor | ||
public class VoteService { | ||
|
||
private final MemberService memberService; | ||
private final PostService postService; | ||
private final VoteRepository voteRepository; | ||
|
||
//투표 생성 | ||
public void createVote(Long postId){ | ||
Member member = memberService.getCurrentMember(); | ||
Post post = postService.findById(postId); | ||
|
||
if(isExistsByMemberAndPost(member,post)){ | ||
throw new RuntimeException("이미 투표한 게시물입니다."); | ||
} | ||
|
||
Vote vote = Vote.builder() | ||
.post(post) | ||
.member(member) | ||
.build(); | ||
|
||
voteRepository.save(vote); | ||
increaseVoteCount(post); | ||
} | ||
|
||
//투표 삭제 | ||
public void deleteVote(Long postId){ | ||
Post post = postService.findById(postId); | ||
Member member = memberService.getCurrentMember(); | ||
|
||
Vote vote = voteRepository.findByMemberAndPost(member, post) | ||
.orElseThrow(()->new RuntimeException("투표가 존재하지 않습니다.")); | ||
voteRepository.delete(vote); | ||
decreaseVoteCount(post); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public boolean isExistsByMemberAndPost(Member member, Post post){ | ||
return voteRepository.existsByMemberAndPost(member,post); | ||
} | ||
|
||
private void increaseVoteCount(Post post){ | ||
post.setVoteCount(post.getVoteCount()+1); | ||
} | ||
|
||
private void decreaseVoteCount(Post post){ | ||
post.setVoteCount(post.getVoteCount()-1); | ||
} | ||
} |