Skip to content
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

숙소 캐싱 버그 #177

Merged
merged 2 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.coolpeace.domain.accommodation.dto.response.AccommodationResponse;
import com.coolpeace.domain.accommodation.dto.response.RoomResponse;
import com.coolpeace.domain.accommodation.dto.response.WrapAccommodationResponse;
import com.coolpeace.domain.accommodation.dto.response.WrapRoomResponse;
import com.coolpeace.domain.accommodation.service.AccomodationService;
import com.coolpeace.global.jwt.security.JwtPrincipal;
import com.coolpeace.global.resolver.AuthJwtPrincipal;
Expand All @@ -21,14 +23,14 @@ public class AccommodationController {
private final AccomodationService accomodationService;

@GetMapping
public ResponseEntity<List<AccommodationResponse>> accommodations(
public ResponseEntity<WrapAccommodationResponse> accommodations(
@AuthJwtPrincipal JwtPrincipal jwtPrincipal
) {
return ResponseEntity.ok(accomodationService.getAccommodations(jwtPrincipal));
}

@GetMapping("/{accommodationId}")
public ResponseEntity<List<RoomResponse>> rooms(
public ResponseEntity<WrapRoomResponse> rooms(
@AuthJwtPrincipal JwtPrincipal jwtPrincipal,
@PathVariable Long accommodationId
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.coolpeace.domain.accommodation.dto.response;

import java.util.List;

public record WrapAccommodationResponse(
List<AccommodationResponse> accommodationResponses
) {
public static WrapAccommodationResponse from(List<AccommodationResponse> accommodationResponses) {
return new WrapAccommodationResponse(accommodationResponses);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.coolpeace.domain.accommodation.dto.response;

import java.util.List;

public record WrapRoomResponse(
List<RoomResponse> roomResponses
) {
public static WrapRoomResponse from(List<RoomResponse> roomResponses) {
return new WrapRoomResponse(roomResponses);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.coolpeace.domain.accommodation.dto.response.AccommodationResponse;
import com.coolpeace.domain.accommodation.dto.response.RoomResponse;
import com.coolpeace.domain.accommodation.dto.response.WrapAccommodationResponse;
import com.coolpeace.domain.accommodation.dto.response.WrapRoomResponse;
import com.coolpeace.domain.accommodation.entity.Accommodation;
import com.coolpeace.domain.accommodation.exception.AccommodationNotFoundException;
import com.coolpeace.domain.accommodation.repository.AccommodationRepository;
Expand All @@ -24,20 +26,21 @@ public class AccomodationService {
private final RoomRepository roomRepository;

@Cacheable(value = "accommodation", key = "#jwtPrincipal.toString()",cacheManager = "contentCacheManager")
public List<AccommodationResponse> getAccommodations(JwtPrincipal jwtPrincipal) {
public WrapAccommodationResponse getAccommodations(JwtPrincipal jwtPrincipal) {

Long memberId = Long.parseLong(jwtPrincipal.getMemberId());
Member member = memberRepository.findById(memberId)
.orElseThrow(MemberNotFoundException::new);

return accommodationRepository.findAllByMember(member)
.stream()
.map(AccommodationResponse::fromEntity)
.toList();
return WrapAccommodationResponse
.from(accommodationRepository.findAllByMember(member)
.stream()
.map(AccommodationResponse::fromEntity)
.toList());
}

@Cacheable(value = "rooms", key = "#accommodationId",cacheManager = "contentCacheManager")
public List<RoomResponse> getRooms(JwtPrincipal jwtPrincipal, Long accommodationId) {
public WrapRoomResponse getRooms(JwtPrincipal jwtPrincipal, Long accommodationId) {

Long memberId = Long.parseLong(jwtPrincipal.getMemberId());
Member member = memberRepository.findById(memberId)
Expand All @@ -46,9 +49,10 @@ public List<RoomResponse> getRooms(JwtPrincipal jwtPrincipal, Long accommodation
Accommodation accommodation = accommodationRepository.findById(accommodationId)
.orElseThrow(AccommodationNotFoundException::new);

return roomRepository.findAllByAccommodation(accommodation)
return WrapRoomResponse
.from(roomRepository.findAllByAccommodation(accommodation)
.stream()
.map(RoomResponse::fromEntity)
.toList();
.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ void selectAccommodations() throws Exception {
.description("숙소 목록 조회 API")
.responseSchema(Schema.schema(AccommodationResponse.class.getSimpleName()))
.responseFields(
fieldWithPath("[].id").type(JsonFieldType.NUMBER).description("숙소 ID"),
fieldWithPath("[].name").type(JsonFieldType.STRING).description("숙소명"),
fieldWithPath("[].sido_id").type(JsonFieldType.NUMBER).description("시도ID"),
fieldWithPath("[].sido").type(JsonFieldType.STRING).description("시도명"),
fieldWithPath("[].sigungu_id").type(JsonFieldType.NUMBER).description("시군구ID"),
fieldWithPath("[].sigungu").type(JsonFieldType.STRING).description("시군구명"),
fieldWithPath("[].address").type(JsonFieldType.STRING).description("숙소 상세")
fieldWithPath(".accommodation_responses[].id").type(JsonFieldType.NUMBER).description("숙소 ID"),
fieldWithPath(".accommodation_responses[].name").type(JsonFieldType.STRING).description("숙소명"),
fieldWithPath(".accommodation_responses[].sido_id").type(JsonFieldType.NUMBER).description("시도ID"),
fieldWithPath(".accommodation_responses[].sido").type(JsonFieldType.STRING).description("시도명"),
fieldWithPath(".accommodation_responses[].sigungu_id").type(JsonFieldType.NUMBER).description("시군구ID"),
fieldWithPath(".accommodation_responses[].sigungu").type(JsonFieldType.STRING).description("시군구명"),
fieldWithPath(".accommodation_responses[].address").type(JsonFieldType.STRING).description("숙소 상세")
)
.build()
)
Expand Down Expand Up @@ -123,10 +123,10 @@ void selectAccommodationRooms() throws Exception {
.description("숙소의 객실 목록 조회 API")
.responseSchema(Schema.schema(RoomResponse.class.getSimpleName()))
.responseFields(
fieldWithPath("[].id").type(JsonFieldType.NUMBER).description("방ID"),
fieldWithPath("[].room_number").type(JsonFieldType.STRING).description("방번호"),
fieldWithPath("[].room_type").type(JsonFieldType.STRING).description("방타입"),
fieldWithPath("[].price").type(JsonFieldType.NUMBER).description("가격")
fieldWithPath(".room_responses[].id").type(JsonFieldType.NUMBER).description("방ID"),
fieldWithPath(".room_responses[].room_number").type(JsonFieldType.STRING).description("방번호"),
fieldWithPath(".room_responses[].room_type").type(JsonFieldType.STRING).description("방타입"),
fieldWithPath(".room_responses[].price").type(JsonFieldType.NUMBER).description("가격")
)
.build()
)));
Expand Down