-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from Tave-13th-Project-Team-4-Fiurinee/feature…
…/dictionary Feature/dictionary 검색 기능 개발
- Loading branch information
Showing
7 changed files
with
162 additions
and
2 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/main/java/com/example/fiurinee/domain/dictionary/controller/DictionaryController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.example.fiurinee.domain.dictionary.controller; | ||
|
||
import com.example.fiurinee.domain.dictionary.controller.api.DictionaryApi; | ||
import com.example.fiurinee.domain.dictionary.dto.DictionaryResponseDTO; | ||
import com.example.fiurinee.domain.dictionary.service.DictionaryService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class DictionaryController implements DictionaryApi { | ||
private final DictionaryService dictionaryService; | ||
|
||
@Override | ||
public ResponseEntity<List<DictionaryResponseDTO>> getAllFlowers(int page) { | ||
int size = 30; // 한 페이지에 보여줄 꽃의 개수 | ||
List<DictionaryResponseDTO> flowers = dictionaryService.getAllFlowers(page, size); | ||
return ResponseEntity.ok(flowers); | ||
} | ||
|
||
@Override | ||
public ResponseEntity<List<DictionaryResponseDTO>> searchFlowers(String name) { | ||
List<DictionaryResponseDTO> flowers = dictionaryService.searchFlowersByName(name); | ||
return ResponseEntity.ok(flowers); | ||
} | ||
|
||
@Override | ||
public ResponseEntity<Integer> getTotalPages(int size) { | ||
int totalPages = dictionaryService.getTotalPages(size); | ||
return ResponseEntity.ok(totalPages); | ||
} | ||
|
||
} | ||
|
48 changes: 48 additions & 0 deletions
48
src/main/java/com/example/fiurinee/domain/dictionary/controller/api/DictionaryApi.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.example.fiurinee.domain.dictionary.controller.api; | ||
|
||
import com.example.fiurinee.domain.dictionary.dto.DictionaryResponseDTO; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.security.SecurityRequirement; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
import java.util.List; | ||
|
||
@Tag(name = "Dictionary", description = "사전 관련 API") | ||
@RequestMapping("/dictionary") | ||
public interface DictionaryApi { | ||
@Operation( | ||
summary = "모든 꽃 조회", | ||
description = "페이지 단위로 모든 꽃을 조회합니다.", | ||
security = @SecurityRequirement(name = "bearerAuth") | ||
) | ||
@ApiResponse(responseCode = "200", description = "꽃 조회 성공") | ||
@ApiResponse(responseCode = "401", description = "인증 실패") | ||
@GetMapping | ||
ResponseEntity<List<DictionaryResponseDTO>> getAllFlowers(@RequestParam int page); | ||
|
||
@Operation( | ||
summary = "꽃 이름으로 검색", | ||
description = "이름에 특정 단어가 포함된 꽃을 검색합니다.", | ||
security = @SecurityRequirement(name = "bearerAuth") | ||
) | ||
@ApiResponse(responseCode = "200", description = "꽃 검색 성공") | ||
@ApiResponse(responseCode = "401", description = "인증 실패") | ||
@GetMapping("/search") | ||
ResponseEntity<List<DictionaryResponseDTO>> searchFlowers(@RequestParam String name); | ||
|
||
@Operation( | ||
summary = "총 페이지 수 조회", | ||
description = "전체 꽃 목록의 총 페이지 수를 조회합니다.", | ||
security = @SecurityRequirement(name = "bearerAuth") | ||
) | ||
@ApiResponse(responseCode = "200", description = "총 페이지 수 조회 성공") | ||
@ApiResponse(responseCode = "401", description = "인증 실패") | ||
@GetMapping("/total-pages") | ||
ResponseEntity<Integer> getTotalPages(@RequestParam int size); | ||
} | ||
|
25 changes: 25 additions & 0 deletions
25
src/main/java/com/example/fiurinee/domain/dictionary/dto/DictionaryResponseDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.example.fiurinee.domain.dictionary.dto; | ||
|
||
import com.example.fiurinee.domain.flower.entity.Flower; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@Builder | ||
public class DictionaryResponseDTO { | ||
private String flower; | ||
private String period; | ||
private String flowerLanguage; | ||
private String explain; | ||
private String image; | ||
|
||
public static DictionaryResponseDTO of(Flower flower) { | ||
return DictionaryResponseDTO.builder() | ||
.flower(flower.getName()) | ||
.period(String.format("%02d", flower.getPeriod() / 100)) | ||
.flowerLanguage(flower.getFlowerLanguage()) | ||
.explain(flower.getExplain()) | ||
.image(flower.getImage().toString()) | ||
.build(); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/example/fiurinee/domain/dictionary/service/DictionaryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.example.fiurinee.domain.dictionary.service; | ||
|
||
|
||
import com.example.fiurinee.domain.dictionary.dto.DictionaryResponseDTO; | ||
import com.example.fiurinee.domain.flower.entity.Flower; | ||
import com.example.fiurinee.domain.flower.repository.FlowerRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class DictionaryService { | ||
private final FlowerRepository flowerRepository; | ||
|
||
public List<DictionaryResponseDTO> getAllFlowers(int page, int size) { | ||
PageRequest pageRequest = PageRequest.of(page - 1, size); | ||
List<Flower> flowers = flowerRepository.findDistinctAll(pageRequest); | ||
return flowers.stream() | ||
.map(DictionaryResponseDTO::of) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public List<DictionaryResponseDTO> searchFlowersByName(String name) { | ||
List<Flower> flowers = flowerRepository.findDistinctByNameContaining(name); | ||
return flowers.stream() | ||
.map(DictionaryResponseDTO::of) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public int getTotalPages(int size) { | ||
long count = flowerRepository.countDistinctFlowers(); | ||
return (int) Math.ceil((double) count / size); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters