Skip to content

Commit

Permalink
feat: get TravelInfos
Browse files Browse the repository at this point in the history
  • Loading branch information
jeongho1209 committed Mar 24, 2024
1 parent b96e34c commit eed89d6
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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<TravelInfo, Long>
interface TravelInfoRepository : CoroutineCrudRepository<TravelInfo, Long> {

@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<TravelInfoElement>

@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<MyTravelInfoElement>
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.trip.safe.travel.presentation.dto.response

import java.time.LocalDate

data class MyTravelInfoListResponse(
val myTravelInfoList: List<MyTravelInfoElement>,
)

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,
)
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() }
)
}
}

0 comments on commit eed89d6

Please sign in to comment.