Skip to content

Commit

Permalink
BE: [feat] 갤러리글 좋아요 누르기 CSID-DGU#83
Browse files Browse the repository at this point in the history
  • Loading branch information
Seoyoung2222 committed Dec 1, 2024
1 parent 5d67c1a commit e16f3c1
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import RunningMachines.R2R.domain.crew.post.gallery.dto.GalleryPostDetailResponseDto;
import RunningMachines.R2R.domain.crew.post.gallery.dto.GalleryPreviewResponseDto;
import RunningMachines.R2R.domain.crew.post.gallery.service.GalleryPostCommandService;
import RunningMachines.R2R.domain.crew.post.gallery.service.GalleryPostLikeService;
import RunningMachines.R2R.domain.crew.post.gallery.service.GalleryPostQueryService;
import RunningMachines.R2R.domain.crew.post.repository.CrewPostRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -19,6 +20,7 @@
public class GalleryPostController {
private final GalleryPostCommandService galleryPostCommandService;
private final GalleryPostQueryService galleryPostQueryService;
private final GalleryPostLikeService galleryPostLikeService;

@PostMapping
public ResponseEntity<Long> createGalleryPost(@PathVariable Long crewId, @RequestPart("content") GalleryPostCreateRequestDto contentDto, @RequestPart("images") List<MultipartFile> images) {
Expand All @@ -37,4 +39,10 @@ public ResponseEntity<GalleryPreviewResponseDto> getGalleryPreview(@PathVariable
GalleryPreviewResponseDto responseDto = galleryPostQueryService.getGalleryPreview(crewId);
return ResponseEntity.ok(responseDto);
}

@PostMapping("/{crewPostId}/like")
public ResponseEntity<Void> likeGalleryPost(@PathVariable Long crewId, @PathVariable Long crewPostId) {
galleryPostLikeService.likeGalleryPost(crewId, crewPostId);
return ResponseEntity.ok().build();
}
}
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);
}
}
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);
}

0 comments on commit e16f3c1

Please sign in to comment.