forked from CSID-DGU/2024-2-OSSProj-Running-Machines-04
-
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.
- Loading branch information
1 parent
5d67c1a
commit e16f3c1
Showing
3 changed files
with
73 additions
and
0 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
51 changes: 51 additions & 0 deletions
51
...ain/java/RunningMachines/R2R/domain/crew/post/gallery/service/GalleryPostLikeService.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,51 @@ | ||
package RunningMachines.R2R.domain.crew.post.gallery.service; | ||
|
||
import RunningMachines.R2R.domain.community.post.entity.Post; | ||
import RunningMachines.R2R.domain.community.post.repository.PostRepository; | ||
import RunningMachines.R2R.domain.crew.common.repository.CrewRepository; | ||
import RunningMachines.R2R.domain.crew.post.entity.CrewPost; | ||
import RunningMachines.R2R.domain.crew.post.entity.CrewPostLike; | ||
import RunningMachines.R2R.domain.crew.post.repository.CrewPostLikeRepository; | ||
import RunningMachines.R2R.domain.crew.post.repository.CrewPostRepository; | ||
import RunningMachines.R2R.domain.user.service.AuthCommandService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import RunningMachines.R2R.domain.user.entity.User; | ||
import RunningMachines.R2R.domain.crew.common.entity.Crew; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class GalleryPostLikeService { | ||
private final CrewPostRepository crewPostRepository; | ||
private final CrewPostLikeRepository crewPostLikeRepository; | ||
private final CrewRepository crewRepository; | ||
private final AuthCommandService authCommandService; | ||
|
||
@Transactional | ||
public void likeGalleryPost(Long crewId, Long crewPostId) { | ||
User currentUser = authCommandService.getCurrentUser(); | ||
|
||
CrewPost crewPost = crewPostRepository.findById(crewPostId) | ||
.orElseThrow(() -> new IllegalArgumentException("게시글을 찾을 수 없습니다.")); | ||
Crew crew = crewRepository.findById(crewId) | ||
.orElseThrow(() -> new IllegalArgumentException("크루를 찾을 수 없습니다.")); | ||
|
||
boolean isMemberOrLeader = crew.getCrewUsers().stream() | ||
.anyMatch(crewUser -> crewUser.getUser().equals(currentUser)); | ||
if (!isMemberOrLeader) { | ||
throw new IllegalArgumentException("좋아요는 크루 멤버만 누를 수 있습니다."); | ||
} | ||
|
||
boolean alreadyLiked = crewPostLikeRepository.existsByCrewPostAndUser(crewPost, currentUser); | ||
if (alreadyLiked) { | ||
throw new IllegalArgumentException("이미 좋아요를 눌렀습니다."); | ||
} | ||
|
||
CrewPostLike like = CrewPostLike.builder() | ||
.crewPost(crewPost) | ||
.user(currentUser) | ||
.build(); | ||
crewPostLikeRepository.save(like); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...src/main/java/RunningMachines/R2R/domain/crew/post/repository/CrewPostLikeRepository.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 RunningMachines.R2R.domain.crew.post.repository; | ||
|
||
import RunningMachines.R2R.domain.crew.post.entity.CrewPost; | ||
import RunningMachines.R2R.domain.user.entity.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import RunningMachines.R2R.domain.crew.post.entity.CrewPostLike; | ||
|
||
import java.util.Optional; | ||
|
||
public interface CrewPostLikeRepository extends JpaRepository<CrewPostLike, Integer> { | ||
boolean existsByCrewPostAndUser(CrewPost crewPost, User user); | ||
|
||
Optional<CrewPostLike> findByCrewPostAndUser(CrewPost crewPost, User user); | ||
} |