Required changes for edit travel
This commit is contained in:
@@ -2,7 +2,6 @@ package eu.fosil.okupamicoche.dto
|
||||
|
||||
import eu.fosil.okupamicoche.entities.Travel
|
||||
import eu.fosil.okupamicoche.entities.TravelId
|
||||
import eu.fosil.okupamicoche.entities.UserId
|
||||
import eu.fosil.okupamicoche.entities.UserIdNotFoundException
|
||||
import eu.fosil.okupamicoche.repositories.UserRepository
|
||||
import org.springframework.data.repository.findByIdOrNull
|
||||
@@ -10,30 +9,30 @@ import org.springframework.data.repository.findByIdOrNull
|
||||
class TravelDto(
|
||||
// Los campos deben ser públicos para que aparezcan en el JSON
|
||||
val id: TravelId? = null,
|
||||
val driverId: UserId,
|
||||
val travelersId: List<UserId>,
|
||||
val driverInfo: UserInfoDto,
|
||||
val travelersInfo: List<UserInfoDto>,
|
||||
val departureDate: String,
|
||||
val origin: String,
|
||||
val destination: String,
|
||||
val availablePlaces: Int,
|
||||
val places: Int,
|
||||
val description: String? = null,
|
||||
val matrixRoomId: String
|
||||
) {
|
||||
constructor(travel: Travel) : this(
|
||||
travel.id,
|
||||
travel.driver.id,
|
||||
travel.travelers.map { traveler -> traveler.id },
|
||||
UserInfoDto(travel.driver),
|
||||
travel.travelers.map { traveler -> UserInfoDto(traveler) },
|
||||
travel.departureDate,
|
||||
travel.origin,
|
||||
travel.destination,
|
||||
travel.availablePlaces,
|
||||
travel.places,
|
||||
travel.description,
|
||||
travel.matrixRoomId
|
||||
)
|
||||
|
||||
fun toTravel(userRepository: UserRepository): Travel {
|
||||
val driver = userRepository.findByIdOrNull(driverId) ?: throw UserIdNotFoundException()
|
||||
val travelers = travelersId.mapNotNull { id -> userRepository.findByIdOrNull(id) }
|
||||
val driver = userRepository.findByIdOrNull(driverInfo.id) ?: throw UserIdNotFoundException()
|
||||
val travelers = travelersInfo.mapNotNull { t -> userRepository.findByIdOrNull(t.id) }
|
||||
.toMutableList()
|
||||
return Travel(
|
||||
id,
|
||||
@@ -42,7 +41,7 @@ class TravelDto(
|
||||
departureDate,
|
||||
origin,
|
||||
destination,
|
||||
availablePlaces,
|
||||
places,
|
||||
description,
|
||||
matrixRoomId
|
||||
)
|
||||
|
||||
@@ -10,8 +10,8 @@ class UserDto(
|
||||
val matrixId: String?,
|
||||
val name: String,
|
||||
val email: String?,
|
||||
val travelsAsDriver: List<TravelDto>,
|
||||
val travelsAsTraveler: List<TravelDto>
|
||||
val travelsAsDriver: List<TravelDto> = emptyList(),
|
||||
val travelsAsTraveler: List<TravelDto> = emptyList()
|
||||
) {
|
||||
constructor(user: User) : this(
|
||||
user.id,
|
||||
|
||||
26
src/main/kotlin/eu/fosil/okupamicoche/dto/UserInfoDto.kt
Normal file
26
src/main/kotlin/eu/fosil/okupamicoche/dto/UserInfoDto.kt
Normal file
@@ -0,0 +1,26 @@
|
||||
package eu.fosil.okupamicoche.dto
|
||||
|
||||
import eu.fosil.okupamicoche.entities.User
|
||||
import eu.fosil.okupamicoche.entities.UserId
|
||||
import eu.fosil.okupamicoche.repositories.UserRepository
|
||||
|
||||
class UserInfoDto(
|
||||
// Los campos deben ser públicos para que aparezcan en el JSON
|
||||
val id: UserId,
|
||||
val matrixId: String?,
|
||||
val name: String
|
||||
) {
|
||||
constructor(user: User) : this(
|
||||
user.id,
|
||||
user.matrixId,
|
||||
user.name
|
||||
)
|
||||
|
||||
fun toUser(): User {
|
||||
return User(
|
||||
id,
|
||||
matrixId,
|
||||
name
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -14,7 +14,7 @@ class Travel(
|
||||
var departureDate: String,
|
||||
var origin: String,
|
||||
var destination: String,
|
||||
var availablePlaces: Int,
|
||||
var places: Int,
|
||||
var description: String? = null,
|
||||
var matrixRoomId: String
|
||||
)
|
||||
@@ -27,13 +27,6 @@ class PrivateTravelRestController(
|
||||
}
|
||||
}
|
||||
|
||||
@RequestMapping("/get")
|
||||
fun getTravel(@RequestParam @Validated travelId: TravelId): ApiResponse<TravelDto?> {
|
||||
return response {
|
||||
travelRepository.findByIdOrNull(travelId)?.let { TravelDto(it) }
|
||||
}
|
||||
}
|
||||
|
||||
@RequestMapping("/cancel")
|
||||
fun cancelTravel(@RequestParam @Validated travelId: TravelId): ApiResponse<Unit> {
|
||||
return response {
|
||||
|
||||
@@ -3,8 +3,10 @@ package eu.fosil.okupamicoche.spring.controller
|
||||
import eu.fosil.okupamicoche.dto.TravelDto
|
||||
import eu.fosil.okupamicoche.dto.ListDto
|
||||
import eu.fosil.okupamicoche.entities.ApiResponse
|
||||
import eu.fosil.okupamicoche.entities.TravelId
|
||||
import eu.fosil.okupamicoche.repositories.TravelRepository
|
||||
import eu.fosil.okupamicoche.usecases.travel.ListTravels
|
||||
import org.springframework.data.repository.findByIdOrNull
|
||||
import org.springframework.validation.annotation.Validated
|
||||
import org.springframework.web.bind.annotation.CrossOrigin
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
@@ -32,4 +34,11 @@ class PublicRestController(private val travelRepository: TravelRepository) : Api
|
||||
ListDto(travelRepository.count(), travels)
|
||||
}
|
||||
}
|
||||
|
||||
@RequestMapping("/travel")
|
||||
fun getTravel(@RequestParam @Validated travelId: TravelId): ApiResponse<TravelDto?> {
|
||||
return response {
|
||||
travelRepository.findByIdOrNull(travelId)?.let { TravelDto(it) }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user