Order and pagination in travel list
This commit is contained in:
@@ -3,21 +3,20 @@ package eu.fosil.okupamicoche.repositories
|
||||
import eu.fosil.okupamicoche.entities.Travel
|
||||
import eu.fosil.okupamicoche.entities.TravelId
|
||||
import eu.fosil.okupamicoche.entities.UserId
|
||||
import org.springframework.data.domain.Page
|
||||
import org.springframework.data.domain.Pageable
|
||||
import org.springframework.data.jpa.repository.Query
|
||||
import org.springframework.data.repository.CrudRepository
|
||||
import org.springframework.data.repository.PagingAndSortingRepository
|
||||
import org.springframework.data.repository.query.Param
|
||||
|
||||
interface TravelRepository : CrudRepository<Travel, TravelId> {
|
||||
interface TravelRepository : PagingAndSortingRepository<Travel, TravelId> {
|
||||
@Query("SELECT t FROM Travel t WHERE t.driver.id = :userId")
|
||||
fun findUserTravelsAsDriver(@Param("userId") userId: UserId): List<Travel>
|
||||
|
||||
@Query("SELECT t FROM Travel t JOIN t.travelers u WHERE u.id = :userId")
|
||||
fun findUserTravelsAsTraveler(@Param("userId") userId: UserId): List<Travel>
|
||||
|
||||
@Query("SELECT t FROM Travel t WHERE t.origin LIKE :filter")
|
||||
// @Query("SELECT t FROM Travel t WHERE t.origin LIKE :filter LIMIT :pageSize")
|
||||
fun findFilteredTravels(
|
||||
@Param("filter") filter: String,
|
||||
// @Param("pageSize") pageSize: Int
|
||||
): List<Travel>
|
||||
fun findByOriginContainingOrDestinationContainingAllIgnoreCase(
|
||||
filter: String, filter2: String, pageable: Pageable
|
||||
): Page<Travel>
|
||||
}
|
||||
@@ -11,10 +11,12 @@ class JWTSecurityConfig : WebSecurityConfigurerAdapter() {
|
||||
http.cors()
|
||||
.and()
|
||||
.authorizeRequests()
|
||||
.antMatchers("/api/private/**").authenticated()
|
||||
.anyRequest().permitAll()
|
||||
// .and()
|
||||
// .oauth2ResourceServer()
|
||||
// .jwt()
|
||||
.antMatchers("/api/public/**").permitAll()
|
||||
.antMatchers("/api/user/**").authenticated()
|
||||
.antMatchers("/api/travel/**").authenticated()
|
||||
.anyRequest().denyAll()
|
||||
.and()
|
||||
.oauth2ResourceServer()
|
||||
.jwt()
|
||||
}//@formatter:on
|
||||
}
|
||||
@@ -13,7 +13,7 @@ import org.springframework.validation.annotation.Validated
|
||||
import org.springframework.web.bind.annotation.*
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/private/travel")
|
||||
@RequestMapping("/api/travel")
|
||||
@CrossOrigin(origins = ["http://localhost:4200"])
|
||||
class PrivateTravelRestController(
|
||||
private val userRepository: UserRepository,
|
||||
|
||||
@@ -13,7 +13,7 @@ import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/private/user")
|
||||
@RequestMapping("/api/user")
|
||||
@CrossOrigin(origins = ["http://localhost:4200"])
|
||||
class PrivateUserRestController(private val userRepository: UserRepository) : ApiRestController {
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ import eu.fosil.okupamicoche.dto.TravelListDto
|
||||
import eu.fosil.okupamicoche.entities.ApiResponse
|
||||
import eu.fosil.okupamicoche.repositories.TravelRepository
|
||||
import eu.fosil.okupamicoche.usecases.travel.ListTravels
|
||||
import eu.fosil.okupamicoche.usecases.travel.SORT_ASCENDING
|
||||
import org.springframework.validation.annotation.Validated
|
||||
import org.springframework.web.bind.annotation.CrossOrigin
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
@@ -19,14 +18,16 @@ class PublicRestController(private val travelRepository: TravelRepository) : Api
|
||||
|
||||
@RequestMapping("/list")
|
||||
fun listTravels(
|
||||
@RequestParam @Validated filter: String?
|
||||
@RequestParam @Validated filter: String?,
|
||||
@RequestParam @Validated sortColumn: String?,
|
||||
@RequestParam @Validated sortAscending: Boolean?,
|
||||
@RequestParam @Validated pageIndex: Int?,
|
||||
@RequestParam @Validated pageSize: Int?,
|
||||
): ApiResponse<TravelListDto> {
|
||||
return response {
|
||||
val travels = if ((filter == null) || (filter.isEmpty()))
|
||||
ListTravels(travelRepository).listTravels().map { t -> TravelDto(t) }
|
||||
else
|
||||
val travels =
|
||||
ListTravels(travelRepository).listTravels(
|
||||
filter, SORT_ASCENDING, 0, 20
|
||||
filter, sortColumn, sortAscending, pageIndex, pageSize
|
||||
).map { t -> TravelDto(t) }
|
||||
TravelListDto(travelRepository.count(), travels)
|
||||
}
|
||||
|
||||
@@ -2,15 +2,37 @@ package eu.fosil.okupamicoche.usecases.travel
|
||||
|
||||
import eu.fosil.okupamicoche.entities.Travel
|
||||
import eu.fosil.okupamicoche.repositories.TravelRepository
|
||||
|
||||
const val SORT_ASCENDING = true
|
||||
const val SORT_DESCENDING = false
|
||||
import org.springframework.data.domain.PageRequest
|
||||
import org.springframework.data.domain.Sort
|
||||
|
||||
class ListTravels(private val travelRepository: TravelRepository) {
|
||||
fun listTravels():List<Travel> {
|
||||
fun listTravels(): List<Travel> {
|
||||
return travelRepository.findAll().toList()
|
||||
}
|
||||
fun listTravels(filter: String = "", sortDirection: Boolean, pageIndex: Int, pageSize: Int):List<Travel> {
|
||||
return travelRepository.findFilteredTravels(filter).toList()
|
||||
|
||||
fun listTravels(
|
||||
filter: String?,
|
||||
sortColumn: String?,
|
||||
sortAscending: Boolean?,
|
||||
pageIndex: Int?,
|
||||
pageSize: Int?
|
||||
): List<Travel> {
|
||||
val sort = if (sortColumn == null) {
|
||||
Sort.unsorted()
|
||||
} else {
|
||||
Sort.by(
|
||||
if (sortAscending != false) Sort.Direction.ASC else Sort.Direction.DESC,
|
||||
sortColumn
|
||||
)
|
||||
}
|
||||
return travelRepository.findByOriginContainingOrDestinationContainingAllIgnoreCase(
|
||||
filter ?: "",
|
||||
filter ?: "",
|
||||
PageRequest.of(
|
||||
pageIndex ?: 0,
|
||||
pageSize ?: 10,
|
||||
sort
|
||||
)
|
||||
).toList()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user