-
Notifications
You must be signed in to change notification settings - Fork 1
[#20] Feat: 호텔 조회 API 구현 #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9837b84
[#20] Feat: 숙소 엔티티 생성
Yujin1219 d8275a0
[#20] Config: 숙소 API 비인증 접근 허용 추가
Yujin1219 69423b5
[#20] Feat: Accommodation 커서 기반 조회 Repository 추가
Yujin1219 ca83b24
[#20] Feat: 숙소 조회 응답 DTO 생성
Yujin1219 e6796eb
[#20] Feat: 숙소 DTO 변환 Converter 추가
Yujin1219 db9e026
[#20] Feat: 숙소 조회 로직 추가
Yujin1219 080b8f3
[#20] Feat: 숙소 조회 API 엔드포인트 추가
Yujin1219 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/main/java/com/example/triptalk/domain/tripPlan/controller/AccommodationController.java
This file contains hidden or 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,49 @@ | ||
| package com.example.triptalk.domain.tripPlan.controller; | ||
|
|
||
| import com.example.triptalk.domain.tripPlan.dto.AccommodationResponse; | ||
| import com.example.triptalk.domain.tripPlan.service.AccommodationService; | ||
| import com.example.triptalk.global.apiPayload.ApiResponse; | ||
| import io.swagger.v3.oas.annotations.Operation; | ||
| import io.swagger.v3.oas.annotations.Parameter; | ||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.web.bind.annotation.*; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/api/accommodations") | ||
| @RequiredArgsConstructor | ||
| @Tag(name = "숙소 API", description = "추천 숙소 조회 (매주 업데이트)") | ||
| public class AccommodationController { | ||
|
|
||
| private final AccommodationService accommodationService; | ||
|
|
||
| @GetMapping | ||
| @Operation( | ||
| summary = "추천 숙소 조회", | ||
| description = """ | ||
| **추천 숙소를 조회합니다.** | ||
|
|
||
| ### 📊 응답 데이터 | ||
| - `accommodationList`: 숙소 목록 (최대 10개씩 페이징) | ||
| - `hotelName`: 호텔 이름 (예: 서울 신라호텔) | ||
| - `cityName`: 도시 한국어명 (예: 서울) | ||
| - `pricePerNight`: 1박 가격 (원화, 예: 150000) | ||
| - `checkInDate`: 체크인 날짜 | ||
| - `checkOutDate`: 체크아웃 날짜 | ||
| - `imageUrl`: 호텔 이미지 URL | ||
|
|
||
| ### 🔄 무한스크롤 사용법 | ||
| 1. **첫 요청**: `cursorId` 없이 호출 | ||
| 2. **다음 요청**: 응답의 `nextCursorId` 값을 `cursorId`에 전달 | ||
| 3. **마지막**: `hasNext`가 `false`일 때 종료 | ||
| """ | ||
| ) | ||
| public ApiResponse<AccommodationResponse.AccommodationListResultDTO> getAccommodations( | ||
| @Parameter(description = "커서 ID (다음 페이지 ID, 처음 요청 시 null)", example = "null") | ||
| @RequestParam(required = false) Long cursorId | ||
| ) { | ||
| AccommodationResponse.AccommodationListResultDTO response = accommodationService.getAccommodations(cursorId); | ||
| return ApiResponse.onSuccess(response); | ||
| } | ||
| } | ||
|
|
48 changes: 48 additions & 0 deletions
48
src/main/java/com/example/triptalk/domain/tripPlan/converter/AccommodationConverter.java
This file contains hidden or 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.triptalk.domain.tripPlan.converter; | ||
|
|
||
| import com.example.triptalk.domain.tripPlan.dto.AccommodationResponse; | ||
| import com.example.triptalk.domain.tripPlan.entity.Accommodation; | ||
| import org.springframework.data.domain.Slice; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class AccommodationConverter { | ||
|
|
||
| /** | ||
| * Accommodation 엔티티를 AccommodationDTO로 변환 | ||
| */ | ||
| public static AccommodationResponse.AccommodationDTO toAccommodationDTO(Accommodation accommodation) { | ||
| return AccommodationResponse.AccommodationDTO.builder() | ||
| .id(accommodation.getId()) | ||
| .hotelName(accommodation.getHotelName()) | ||
| .cityName(accommodation.getCityName()) | ||
| .pricePerNight(accommodation.getPricePerNight()) | ||
| .checkInDate(accommodation.getCheckInDate()) | ||
| .checkOutDate(accommodation.getCheckOutDate()) | ||
| .imageUrl(accommodation.getImageUrl()) | ||
| .build(); | ||
| } | ||
|
|
||
| /** | ||
| * Slice<Accommodation>를 AccommodationListResultDTO로 변환 | ||
| */ | ||
| public static AccommodationResponse.AccommodationListResultDTO toAccommodationListResultDTO(Slice<Accommodation> slice) { | ||
| List<AccommodationResponse.AccommodationDTO> accommodationList = slice.getContent().stream() | ||
| .map(AccommodationConverter::toAccommodationDTO) | ||
| .toList(); | ||
|
|
||
| // 다음 커서 ID는 마지막 항목의 ID | ||
| Long nextCursorId = accommodationList.isEmpty() ? | ||
| null : | ||
| accommodationList.getLast().getId(); | ||
|
|
||
| return AccommodationResponse.AccommodationListResultDTO.builder() | ||
| .accommodationList(accommodationList) | ||
| .accommodationListSize(accommodationList.size()) | ||
| .isFirst(slice.isFirst()) | ||
| .hasNext(slice.hasNext()) | ||
| .nextCursorId(nextCursorId) | ||
| .build(); | ||
| } | ||
| } | ||
|
|
||
67 changes: 67 additions & 0 deletions
67
src/main/java/com/example/triptalk/domain/tripPlan/dto/AccommodationResponse.java
This file contains hidden or 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,67 @@ | ||
| package com.example.triptalk.domain.tripPlan.dto; | ||
|
|
||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.util.List; | ||
|
|
||
| public class AccommodationResponse { | ||
|
|
||
| @Getter | ||
| @Builder | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| @Schema(description = "숙소 정보") | ||
| public static class AccommodationDTO { | ||
|
|
||
| @Schema(description = "숙소 ID", example = "1") | ||
| private Long id; | ||
|
|
||
| @Schema(description = "호텔 이름", example = "서울 롯데호텔") | ||
| private String hotelName; | ||
|
|
||
| @Schema(description = "도시 한국어명", example = "서울") | ||
| private String cityName; | ||
|
|
||
| @Schema(description = "1박 가격 (원화)", example = "150000") | ||
| private Integer pricePerNight; | ||
|
|
||
| @Schema(description = "체크인 날짜", example = "2025-12-17") | ||
| private LocalDate checkInDate; | ||
|
|
||
| @Schema(description = "체크아웃 날짜", example = "2025-12-19") | ||
| private LocalDate checkOutDate; | ||
|
|
||
| @Schema(description = "도시 대표 이미지 URL", example = "https://images.unsplash.com/...") | ||
| private String imageUrl; | ||
|
|
||
| } | ||
|
|
||
| @Getter | ||
| @Builder | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| @Schema(description = "숙소 목록 응답 (커서 기반)") | ||
| public static class AccommodationListResultDTO { | ||
|
|
||
| @Schema(description = "숙소 목록") | ||
| private List<AccommodationDTO> accommodationList; | ||
|
|
||
| @Schema(description = "현재 페이지의 숙소 개수", example = "10") | ||
| private Integer accommodationListSize; | ||
|
|
||
| @Schema(description = "페이지 처음 여부", example = "true") | ||
| private Boolean isFirst; | ||
|
|
||
| @Schema(description = "다음 페이지가 있는지 여부", example = "true") | ||
| private Boolean hasNext; | ||
|
|
||
| @Schema(description = "다음 커서 ID (무한스크롤용)", example = "150") | ||
| private Long nextCursorId; | ||
| } | ||
| } | ||
|
|
36 changes: 36 additions & 0 deletions
36
src/main/java/com/example/triptalk/domain/tripPlan/entity/Accommodation.java
This file contains hidden or 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,36 @@ | ||
| package com.example.triptalk.domain.tripPlan.entity; | ||
|
|
||
| import com.example.triptalk.global.apiPayload.code.BaseEntity; | ||
| import jakarta.persistence.*; | ||
| import lombok.*; | ||
|
|
||
| import java.time.LocalDate; | ||
|
|
||
| @Builder | ||
| @Getter | ||
| @Setter | ||
| @NoArgsConstructor | ||
| @AllArgsConstructor | ||
| @Entity | ||
| public class Accommodation extends BaseEntity { | ||
|
|
||
| @Column(length = 200, nullable = false) | ||
| private String hotelName; // 호텔 이름 | ||
|
|
||
| @Column(length = 50, nullable = false) | ||
| private String cityName; // 도시 한국어명 (예: 서울, 도쿄) | ||
|
|
||
| @Column(nullable = false) | ||
| private Integer pricePerNight; // 1박 가격 (원화) | ||
|
|
||
| @Column(nullable = false) | ||
| private LocalDate checkInDate; // 체크인 날짜 | ||
|
|
||
| @Column(nullable = false) | ||
| private LocalDate checkOutDate; // 체크아웃 날짜 | ||
|
|
||
| @Column(length = 255, nullable = false) | ||
| private String imageUrl; // 도시 대표 이미지 URL | ||
|
|
||
| } | ||
|
|
26 changes: 26 additions & 0 deletions
26
src/main/java/com/example/triptalk/domain/tripPlan/repository/AccommodationRepository.java
This file contains hidden or 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,26 @@ | ||
| package com.example.triptalk.domain.tripPlan.repository; | ||
|
|
||
| import com.example.triptalk.domain.tripPlan.entity.Accommodation; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.domain.Slice; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.data.jpa.repository.Query; | ||
| import org.springframework.data.repository.query.Param; | ||
|
|
||
| public interface AccommodationRepository extends JpaRepository<Accommodation, Long> { | ||
|
|
||
| /** | ||
| * 커서 기반 숙소 목록 조회 (ID 내림차순) | ||
| * @param cursorId 커서 ID (null이면 처음부터 조회) | ||
| * @param pageable 페이징 정보 | ||
| * @return Slice<Accommodation> | ||
| */ | ||
| @Query("SELECT a FROM Accommodation a " + | ||
| "WHERE (:cursorId IS NULL OR a.id < :cursorId) " + | ||
| "ORDER BY a.id DESC") | ||
| Slice<Accommodation> findAllByCursor( | ||
| @Param("cursorId") Long cursorId, | ||
| Pageable pageable | ||
| ); | ||
| } | ||
|
|
13 changes: 13 additions & 0 deletions
13
src/main/java/com/example/triptalk/domain/tripPlan/service/AccommodationService.java
This file contains hidden or 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,13 @@ | ||
| package com.example.triptalk.domain.tripPlan.service; | ||
|
|
||
| import com.example.triptalk.domain.tripPlan.dto.AccommodationResponse; | ||
|
|
||
| public interface AccommodationService { | ||
| /** | ||
| * DB에 저장된 숙소 조회 (커서 기반 무한스크롤) | ||
| * @param cursorId 커서 ID (null이면 처음부터) | ||
| * @return 숙소 목록 응답 | ||
| */ | ||
| AccommodationResponse.AccommodationListResultDTO getAccommodations(Long cursorId); | ||
| } | ||
|
|
36 changes: 36 additions & 0 deletions
36
src/main/java/com/example/triptalk/domain/tripPlan/service/AccommodationServiceImpl.java
This file contains hidden or 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,36 @@ | ||
| package com.example.triptalk.domain.tripPlan.service; | ||
|
|
||
| import com.example.triptalk.domain.tripPlan.converter.AccommodationConverter; | ||
| import com.example.triptalk.domain.tripPlan.dto.AccommodationResponse; | ||
| import com.example.triptalk.domain.tripPlan.entity.Accommodation; | ||
| import com.example.triptalk.domain.tripPlan.repository.AccommodationRepository; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.data.domain.PageRequest; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.domain.Slice; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| @Slf4j | ||
| @Service | ||
| @RequiredArgsConstructor | ||
| @Transactional(readOnly = true) | ||
| public class AccommodationServiceImpl implements AccommodationService { | ||
|
|
||
| private final AccommodationRepository accommodationRepository; | ||
|
|
||
| private static final int PAGE_SIZE = 10; // 페이지당 숙소 개수 | ||
|
|
||
| @Override | ||
| public AccommodationResponse.AccommodationListResultDTO getAccommodations(Long cursorId) { | ||
| Pageable pageable = PageRequest.of(0, PAGE_SIZE); | ||
|
|
||
| // 커서 기반 조회 | ||
| Slice<Accommodation> slice = accommodationRepository.findAllByCursor(cursorId, pageable); | ||
|
|
||
| // DTO 변환 | ||
| return AccommodationConverter.toAccommodationListResultDTO(slice); | ||
| } | ||
| } | ||
|
|
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hasNext가 false일 때nextCursorId를 null로 설정하세요.현재 구현은 다음 페이지가 없어도 마지막 항목의 ID를
nextCursorId로 반환합니다. 클라이언트는hasNext를 확인해야 하지만, API를 더 방어적으로 만들기 위해 다음 페이지가 없을 때는nextCursorId를 null로 설정하는 것이 좋습니다.다음과 같이 수정하세요:
📝 Committable suggestion
🤖 Prompt for AI Agents