Skip to content

Commit

Permalink
Merge pull request #134 from CSID-DGU/backend/feature/crew
Browse files Browse the repository at this point in the history
BE: [feat] 갤러리 좋아요 #83
  • Loading branch information
Seoyoung2222 authored Dec 1, 2024
2 parents 98ffd29 + e16f3c1 commit 60df3e7
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import RunningMachines.R2R.domain.crew.post.gallery.dto.GalleryPostCreateRequestDto;
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 @@ -18,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 @@ -30,4 +33,16 @@ public ResponseEntity<GalleryPostDetailResponseDto> getGalleryPost(@PathVariable
GalleryPostDetailResponseDto responseDto = galleryPostQueryService.getGalleryPostDetail(crewId, postId);
return ResponseEntity.ok(responseDto);
}

@GetMapping
public ResponseEntity<GalleryPreviewResponseDto> getGalleryPreview(@PathVariable Long crewId) {
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,17 @@
package RunningMachines.R2R.domain.crew.post.gallery.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

import java.util.List;

@Getter
@Builder
@AllArgsConstructor
public class GalleryPreviewResponseDto {
private String crewTitle;
private int postCount;
private int memberCount;
private List<GallerySimpleResponseDto> posts;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package RunningMachines.R2R.domain.crew.post.gallery.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Builder
@AllArgsConstructor
public class GallerySimpleResponseDto {
private Long postId;
private String imageUrl;
}
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
@@ -1,19 +1,26 @@
package RunningMachines.R2R.domain.crew.post.gallery.service;

import RunningMachines.R2R.domain.crew.board.entity.CrewBoard;
import RunningMachines.R2R.domain.crew.common.entity.Crew;
import RunningMachines.R2R.domain.crew.common.repository.CrewRepository;
import RunningMachines.R2R.domain.crew.post.entity.CrewPost;
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.dto.GallerySimpleResponseDto;
import RunningMachines.R2R.domain.crew.post.repository.CrewPostRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.stream.Collectors;
import java.util.List;

@Service
@RequiredArgsConstructor
public class GalleryPostQueryService {
private final CrewPostRepository crewPostRepository;
private final CrewRepository crewRepository;

@Transactional(readOnly = true)
public GalleryPostDetailResponseDto getGalleryPostDetail(Long crewId, Long crewPostId) {
Expand All @@ -40,4 +47,28 @@ public GalleryPostDetailResponseDto getGalleryPostDetail(Long crewId, Long crewP
.content(crewPost.getContent())
.build();
}

@Transactional(readOnly = true)
public GalleryPreviewResponseDto getGalleryPreview(Long crewId) {
Crew crew = crewRepository.findById(crewId)
.orElseThrow(() -> new IllegalArgumentException("해당 크루를 찾을 수 없습니다."));

int postCount = crewPostRepository.countByCrewId(crewId);
int memberCount = crew.getCrewUsers().size();

List<GallerySimpleResponseDto> posts = crewPostRepository.findAllByCrewIdAndBoard(crewId, CrewBoard.GALLERY).stream()
.filter(post -> !post.getImages().isEmpty())
.map(post -> GallerySimpleResponseDto.builder()
.postId(post.getId())
.imageUrl(post.getImages().get(0).getImageUrl())
.build())
.collect(Collectors.toList());

return GalleryPreviewResponseDto.builder()
.crewTitle(crew.getTitle())
.postCount(postCount)
.memberCount(memberCount)
.posts(posts)
.build();
}
}
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import java.util.Optional;

public interface CrewPostRepository extends JpaRepository<CrewPost, Long> {
List<CrewPost> findAllByUserIdAndBoard(Long userId, CrewBoard board);
int countByCrewId(Long crewId);

List<CrewPost> findAllByCrewIdAndBoard(Long crewId, CrewBoard board);

Optional<CrewPost> findByIdAndCrewIdAndBoard(Long id, Long crewId, CrewBoard board);
Expand Down

0 comments on commit 60df3e7

Please sign in to comment.