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 98ffd29 commit 5d67c1a
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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.GalleryPostQueryService;
import RunningMachines.R2R.domain.crew.post.repository.CrewPostRepository;
Expand Down Expand Up @@ -30,4 +31,10 @@ 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);
}
}
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
@@ -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
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 5d67c1a

Please sign in to comment.