diff --git a/place/src/main/java/com/umc/place/exhibition/dto/SearchExhibitionsByNameResDto.java b/place/src/main/java/com/umc/place/exhibition/dto/SearchExhibitionsByNameResDto.java new file mode 100644 index 0000000..9891824 --- /dev/null +++ b/place/src/main/java/com/umc/place/exhibition/dto/SearchExhibitionsByNameResDto.java @@ -0,0 +1,39 @@ +package com.umc.place.exhibition.dto; + +import com.umc.place.exhibition.entity.Exhibition; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.springframework.data.domain.Page; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +@Data +@NoArgsConstructor +public class SearchExhibitionsByNameResDto { + + private List searchedExhibitions = new ArrayList(); + + @Builder + public SearchExhibitionsByNameResDto(Page exhibitions) { + this.searchedExhibitions + = exhibitions.stream() + .map(exhibition -> new SearchedExhibitionByName(exhibition)) + .collect(Collectors.toList()); + } + + @Data + @NoArgsConstructor + public static class SearchedExhibitionByName { + private Long exhibitionIdx; + private String exhibitionName; + + @Builder + public SearchedExhibitionByName(Exhibition exhibition) { + this.exhibitionIdx = exhibition.getExhibitionIdx(); + this.exhibitionName = exhibition.getExhibitionName(); + } + } +} diff --git a/place/src/main/java/com/umc/place/exhibition/repository/ExhibitionRepository.java b/place/src/main/java/com/umc/place/exhibition/repository/ExhibitionRepository.java index ebc4428..f87bd22 100644 --- a/place/src/main/java/com/umc/place/exhibition/repository/ExhibitionRepository.java +++ b/place/src/main/java/com/umc/place/exhibition/repository/ExhibitionRepository.java @@ -13,7 +13,9 @@ @Repository public interface ExhibitionRepository extends JpaRepository { Page findByCategory(Category category, Pageable pageable); // 카테고리 기반 전체 조회(페이징) + Page findAll(Pageable pageable); // 전체 조회(페이징) + boolean existsByCategory(Category category); @Query("select case when count(e) > 0 then true else false end from Exhibition e where Function('replace', e.location, ' ', '') like %:location%") @@ -25,4 +27,6 @@ public interface ExhibitionRepository extends JpaRepository { @Query("select e from Exhibition e where Function('replace', e.location, ' ', '') like %:location%") Page findByLocationLike(@Param("location") String location, Pageable pageable); + + Page findByExhibitionNameContainingOrderByExhibitionName(String searchKeyword, Pageable pageable); } diff --git a/place/src/main/java/com/umc/place/story/controller/StoryController.java b/place/src/main/java/com/umc/place/story/controller/StoryController.java index c01a137..bc6cd8c 100644 --- a/place/src/main/java/com/umc/place/story/controller/StoryController.java +++ b/place/src/main/java/com/umc/place/story/controller/StoryController.java @@ -5,11 +5,14 @@ import com.umc.place.comment.service.CommentService; import com.umc.place.common.BaseException; import com.umc.place.common.BaseResponse; +import com.umc.place.exhibition.dto.SearchExhibitionsByNameResDto; import com.umc.place.story.dto.StoryDetailResponseDto; import com.umc.place.story.dto.StoryUploadRequestDto; import com.umc.place.story.dto.StoryUploadResponseDto; import com.umc.place.story.service.StoryService; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Pageable; +import org.springframework.data.web.PageableDefault; import org.springframework.web.bind.annotation.*; import static com.umc.place.common.BaseResponseStatus.NULL_STORY; @@ -41,6 +44,17 @@ public BaseResponse getStoryDetail(@PathVariable Long st } } + @GetMapping("/search") + public BaseResponse getExhibitionWhenUploadStory( + @PageableDefault(size = 5) Pageable pageable, + @RequestParam(required = false) String searchWord) { + try { + return new BaseResponse<>(storyService.searchExhibitionByName(searchWord, pageable)); + } catch (BaseException e) { + return new BaseResponse<>(e.getStatus()); + } + } + @PostMapping("/{storyIdx}/comment") public BaseResponse uploadStoryComment(@PathVariable Long storyIdx, @RequestBody CommentUploadReqDto reqDto, diff --git a/place/src/main/java/com/umc/place/story/service/StoryService.java b/place/src/main/java/com/umc/place/story/service/StoryService.java index 112d9eb..99495e8 100644 --- a/place/src/main/java/com/umc/place/story/service/StoryService.java +++ b/place/src/main/java/com/umc/place/story/service/StoryService.java @@ -3,6 +3,7 @@ import com.umc.place.comment.dto.CommentResDto; import com.umc.place.comment.repository.CommentRepository; import com.umc.place.common.BaseException; +import com.umc.place.exhibition.dto.SearchExhibitionsByNameResDto; import com.umc.place.exhibition.entity.Exhibition; import com.umc.place.exhibition.repository.ExhibitionRepository; import com.umc.place.story.dto.StoryDetailResponseDto; @@ -16,6 +17,8 @@ import com.umc.place.user.entity.User; import com.umc.place.user.repository.UserRepository; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -79,6 +82,19 @@ public StoryDetailResponseDto getStoryDetail(Long storyIdx, Long userId) throws } } + public SearchExhibitionsByNameResDto searchExhibitionByName(String searchWord, Pageable page) throws BaseException { + try { + searchWord = searchWord.trim(); + Page searchedExhibitions + = exhibitionRepository.findByExhibitionNameContainingOrderByExhibitionName(searchWord, page); + return SearchExhibitionsByNameResDto.builder() + .exhibitions(searchedExhibitions) + .build(); + } catch (Exception e) { + throw new BaseException(DATABASE_ERROR); + } + } + @Transactional public StoryUploadResponseDto uploadStory(StoryUploadRequestDto storyUploadRequestDto, Long userId) throws BaseException { try {