diff --git a/backend/src/main/java/com/project/capstone/book/domain/BookRepository.java b/backend/src/main/java/com/project/capstone/book/domain/BookRepository.java index 311c458d53..163fc57b0a 100644 --- a/backend/src/main/java/com/project/capstone/book/domain/BookRepository.java +++ b/backend/src/main/java/com/project/capstone/book/domain/BookRepository.java @@ -2,9 +2,12 @@ import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; import java.util.Optional; public interface BookRepository extends JpaRepository { Optional findBookById(Long id); Optional findBookByIsbn(String isbn); + + List findBooksByTitleContaining(String title); } diff --git a/backend/src/main/java/com/project/capstone/club/controller/ClubController.java b/backend/src/main/java/com/project/capstone/club/controller/ClubController.java index 99082527a7..2411cdbad6 100644 --- a/backend/src/main/java/com/project/capstone/club/controller/ClubController.java +++ b/backend/src/main/java/com/project/capstone/club/controller/ClubController.java @@ -4,6 +4,7 @@ import com.project.capstone.book.controller.dto.AddBookRequest; import com.project.capstone.club.controller.dto.ClubCreateRequest; import com.project.capstone.club.controller.dto.ClubResponse; +import com.project.capstone.club.controller.dto.SimpleClubResponse; import com.project.capstone.club.service.ClubService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -79,4 +80,11 @@ public ResponseEntity setBook(@AuthenticationPrincipal PrincipalDetails detai clubService.setBook(details.getUserId(), request, clubId); return ResponseEntity.ok().body("선정 완료"); } + + // 대표책으로 모임 조회 + @GetMapping("/search/book") + public ResponseEntity> getClubByBookName(@RequestParam String title) { + List clubResponseList = clubService.getClubByBookTitle(title); + return ResponseEntity.ok().body(clubResponseList); + } } diff --git a/backend/src/main/java/com/project/capstone/club/controller/dto/SimpleClubResponse.java b/backend/src/main/java/com/project/capstone/club/controller/dto/SimpleClubResponse.java new file mode 100644 index 0000000000..bc0d8304e6 --- /dev/null +++ b/backend/src/main/java/com/project/capstone/club/controller/dto/SimpleClubResponse.java @@ -0,0 +1,18 @@ +package com.project.capstone.club.controller.dto; + +import com.project.capstone.club.domain.Club; +import com.project.capstone.club.domain.PublicStatus; + +public record SimpleClubResponse( + Long id, + String topic, + String name, + int memberCnt, + int maximum, + PublicStatus publicStatus + // String imageUrl +) { + public SimpleClubResponse(Club club) { + this(club.getId(), club.getTopic(), club.getName(), club.getMembers().size(), club.getMaximum(), club.getPublicStatus()); + } +} diff --git a/backend/src/main/java/com/project/capstone/club/domain/ClubRepository.java b/backend/src/main/java/com/project/capstone/club/domain/ClubRepository.java index 1a65fcb65c..b15488c1c6 100644 --- a/backend/src/main/java/com/project/capstone/club/domain/ClubRepository.java +++ b/backend/src/main/java/com/project/capstone/club/domain/ClubRepository.java @@ -20,4 +20,6 @@ public interface ClubRepository extends JpaRepository { @Modifying(clearAutomatically = true) @Query("update Club c set c.book = :book where c.id = :id") void updateBook(Book book, Long id); + + List findClubsByBook(Book book); } diff --git a/backend/src/main/java/com/project/capstone/club/service/ClubService.java b/backend/src/main/java/com/project/capstone/club/service/ClubService.java index 5ae6fa1575..38ebeef3a7 100644 --- a/backend/src/main/java/com/project/capstone/club/service/ClubService.java +++ b/backend/src/main/java/com/project/capstone/club/service/ClubService.java @@ -5,6 +5,7 @@ import com.project.capstone.book.domain.BookRepository; import com.project.capstone.club.controller.dto.ClubCreateRequest; import com.project.capstone.club.controller.dto.ClubResponse; +import com.project.capstone.club.controller.dto.SimpleClubResponse; import com.project.capstone.club.domain.Club; import com.project.capstone.club.domain.ClubRepository; import com.project.capstone.club.exception.ClubException; @@ -142,4 +143,16 @@ public void setBook(String managerId, AddBookRequest request, Long clubId) { club.setBook(book); clubRepository.updateBook(book, clubId); } + + public List getClubByBookTitle(String title) { + List booksContainingTitle = bookRepository.findBooksByTitleContaining(title); + List clubResponseList = new ArrayList<>(); + for (Book book : booksContainingTitle) { + List clubList = clubRepository.findClubsByBook(book); + for (Club club : clubList) { + clubResponseList.add(new SimpleClubResponse(club)); + } + } + return clubResponseList; + } }