diff --git a/src/main/kotlin/com/trip/safe/travel/domain/TravelInfoRepository.kt b/src/main/kotlin/com/trip/safe/travel/domain/TravelInfoRepository.kt index abd9000..b7fda22 100644 --- a/src/main/kotlin/com/trip/safe/travel/domain/TravelInfoRepository.kt +++ b/src/main/kotlin/com/trip/safe/travel/domain/TravelInfoRepository.kt @@ -1,5 +1,55 @@ package com.trip.safe.travel.domain +import com.trip.safe.travel.presentation.dto.response.MyTravelInfoElement +import com.trip.safe.travel.presentation.dto.response.TravelInfoElement +import org.springframework.data.r2dbc.repository.Query import org.springframework.data.repository.kotlin.CoroutineCrudRepository +import reactor.core.publisher.Flux -interface TravelInfoRepository : CoroutineCrudRepository \ No newline at end of file +interface TravelInfoRepository : CoroutineCrudRepository { + + @Query( + """ + SELECT + ti.id, + ti.title, + ti.content, + ti.created_date, + u.account_id + FROM travel_info AS ti + INNER JOIN user AS u + ON ti.user_id = u.id + WHERE ti.travel_destination_id = :travelDestinationId AND ti.is_deleted = false + ORDER BY ti.created_date DESC + LIMIT :limit + OFFSET :offset + """ + ) + suspend fun findAllByTravelDestinationId( + travelDestinationId: Long, + limit: Int, + offset: Long, + ): Flux + + @Query( + """ + SELECT + ti.id, + ti.title, + ti.content, + ti.created_date + FROM travel_info AS ti + INNER JOIN user AS u + ON ti.user_id = u.id + WHERE ti.user_id = :userId AND ti.is_deleted = false + ORDER BY ti.created_date DESC + LIMIT :limit + OFFSET :offset + """ + ) + suspend fun findAllByUserId( + userId: Long, + limit: Int, + offset: Long, + ): Flux +} diff --git a/src/main/kotlin/com/trip/safe/travel/presentation/TravelInfoController.kt b/src/main/kotlin/com/trip/safe/travel/presentation/TravelInfoController.kt index b1ccd65..7a9a968 100644 --- a/src/main/kotlin/com/trip/safe/travel/presentation/TravelInfoController.kt +++ b/src/main/kotlin/com/trip/safe/travel/presentation/TravelInfoController.kt @@ -2,10 +2,14 @@ package com.trip.safe.travel.presentation import com.trip.safe.travel.presentation.dto.request.CreateTravelInfoRequest import com.trip.safe.travel.presentation.dto.request.UpdateTravelInfoRequest +import com.trip.safe.travel.presentation.dto.response.MyTravelInfoListResponse +import com.trip.safe.travel.presentation.dto.response.TravelInfoListResponse import com.trip.safe.travel.service.TravelInfoService import jakarta.validation.Valid +import org.springframework.data.domain.Pageable import org.springframework.http.HttpStatus import org.springframework.web.bind.annotation.DeleteMapping +import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.PatchMapping import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.PostMapping @@ -43,4 +47,17 @@ class TravelInfoController( ) { travelInfoService.deleteTravelInfo(travelInfoId) } + + @GetMapping("/{travel-destination-id}") + suspend fun getTravelInfosByTravelDestinationId( + @PathVariable("travel-destination-id") travelDestinationId: Long, + pageable: Pageable, + ): TravelInfoListResponse { + return travelInfoService.getTravelInfosByTravelDestinationId(travelDestinationId, pageable) + } + + @GetMapping("/my") + suspend fun getMyTravelInfos(pageable: Pageable): MyTravelInfoListResponse { + return travelInfoService.getMyTravelInfos(pageable) + } } diff --git a/src/main/kotlin/com/trip/safe/travel/presentation/dto/response/MyTravelInfoListResponse.kt b/src/main/kotlin/com/trip/safe/travel/presentation/dto/response/MyTravelInfoListResponse.kt new file mode 100644 index 0000000..2822baf --- /dev/null +++ b/src/main/kotlin/com/trip/safe/travel/presentation/dto/response/MyTravelInfoListResponse.kt @@ -0,0 +1,21 @@ +package com.trip.safe.travel.presentation.dto.response + +import java.time.LocalDate + +data class MyTravelInfoListResponse( + val myTravelInfoList: List, +) + +data class MyTravelInfoElement( + val id: Long, + val title: String, + val content: String, + val createdDate: LocalDate, +) + +fun MyTravelInfoElement.toMyTravelInfoElement() = MyTravelInfoElement( + id = this.id, + title = this.title, + content = this.content, + createdDate = this.createdDate, +) diff --git a/src/main/kotlin/com/trip/safe/travel/presentation/dto/response/TravelInfoListResponse.kt b/src/main/kotlin/com/trip/safe/travel/presentation/dto/response/TravelInfoListResponse.kt index e7a9b26..ad05c4d 100644 --- a/src/main/kotlin/com/trip/safe/travel/presentation/dto/response/TravelInfoListResponse.kt +++ b/src/main/kotlin/com/trip/safe/travel/presentation/dto/response/TravelInfoListResponse.kt @@ -12,16 +12,16 @@ data class TravelInfoElement( val id: Long, val title: String, val content: String, - val createDate: LocalDate, + val createdDate: LocalDate, val accountId: String, val isMine: Boolean, ) -fun TravelInfoElement.toTravelInfoElement() = TravelInfoElement( +fun TravelInfoElement.toTravelInfoElement(isMine: Boolean) = TravelInfoElement( id = this.id, title = this.title, content = this.content, - createDate = this.createDate, + createdDate = this.createdDate, accountId = this.accountId, - isMine = this.isMine, + isMine = isMine, ) diff --git a/src/main/kotlin/com/trip/safe/travel/service/TravelInfoService.kt b/src/main/kotlin/com/trip/safe/travel/service/TravelInfoService.kt index e319431..8a0e769 100644 --- a/src/main/kotlin/com/trip/safe/travel/service/TravelInfoService.kt +++ b/src/main/kotlin/com/trip/safe/travel/service/TravelInfoService.kt @@ -9,6 +9,12 @@ import com.trip.safe.travel.exception.TravelDestinationNotFoundException import com.trip.safe.travel.exception.TravelInfoNotFoundException import com.trip.safe.travel.presentation.dto.request.CreateTravelInfoRequest import com.trip.safe.travel.presentation.dto.request.UpdateTravelInfoRequest +import com.trip.safe.travel.presentation.dto.response.MyTravelInfoListResponse +import com.trip.safe.travel.presentation.dto.response.TravelInfoListResponse +import com.trip.safe.travel.presentation.dto.response.toMyTravelInfoElement +import com.trip.safe.travel.presentation.dto.response.toTravelInfoElement +import kotlinx.coroutines.reactor.awaitSingle +import org.springframework.data.domain.Pageable import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional import java.time.LocalDate @@ -71,7 +77,44 @@ class TravelInfoService( travelInfo.deleteTravelInfo() } - suspend fun getTravelInfosByTravelDestinationId(travelDestinationId: Long) { + suspend fun getTravelInfosByTravelDestinationId( + travelDestinationId: Long, + pageable: Pageable + ): TravelInfoListResponse { + val user = securityFacade.getCurrentUser() + + val travelInfoList = travelInfoRepository.findAllByTravelDestinationId( + travelDestinationId = travelDestinationId, + limit = pageable.pageSize, + offset = pageable.offset, + ).collectList().awaitSingle() + + val response = travelInfoList.map { + val isMine = it.accountId == user.accountId + it.toTravelInfoElement(isMine) + } + + val travelDestination = travelDestinationRepository.findById(travelDestinationId) + ?: throw TravelDestinationNotFoundException(TravelDestinationNotFoundException.TRAVEL_DESTINATION_NOT_FOUND) + + return TravelInfoListResponse( + travelInfoList = response, + travelDestination = travelDestination, + ) + } + + suspend fun getMyTravelInfos(pageable: Pageable): MyTravelInfoListResponse { + val user = securityFacade.getCurrentUser() + + val myTravelInfoList = travelInfoRepository.findAllByUserId( + userId = user.id, + limit = pageable.pageSize, + offset = pageable.offset, + ).collectList().awaitSingle() + + return MyTravelInfoListResponse( + myTravelInfoList = myTravelInfoList.map { it.toMyTravelInfoElement() } + ) } }