Create travel
This commit is contained in:
@@ -3,20 +3,21 @@ package eu.fosil.okupamicoche.dto
|
||||
import eu.fosil.okupamicoche.entities.Travel
|
||||
import eu.fosil.okupamicoche.entities.TravelId
|
||||
import eu.fosil.okupamicoche.entities.UserIdNotFoundException
|
||||
import eu.fosil.okupamicoche.entities.UserNotSpecifiedException
|
||||
import eu.fosil.okupamicoche.repositories.UserRepository
|
||||
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 driverInfo: UserInfoDto,
|
||||
val travelersInfo: List<UserInfoDto>,
|
||||
val departureDate: String,
|
||||
val origin: String,
|
||||
val destination: String,
|
||||
val places: Int,
|
||||
var driverInfo: UserInfoDto? = null,
|
||||
val travelersInfo: List<UserInfoDto> = emptyList(),
|
||||
val departureDate: String = "",
|
||||
val origin: String = "",
|
||||
val destination: String = "",
|
||||
val places: Int = 0,
|
||||
val description: String? = null,
|
||||
val matrixRoomId: String
|
||||
val matrixRoomId: String = ""
|
||||
) {
|
||||
constructor(travel: Travel) : this(
|
||||
travel.id,
|
||||
@@ -31,19 +32,22 @@ class TravelDto(
|
||||
)
|
||||
|
||||
fun toTravel(userRepository: UserRepository): Travel {
|
||||
val driver = userRepository.findByIdOrNull(driverInfo.id) ?: throw UserIdNotFoundException()
|
||||
val travelers = travelersInfo.mapNotNull { t -> userRepository.findByIdOrNull(t.id) }
|
||||
.toMutableList()
|
||||
return Travel(
|
||||
id,
|
||||
driver,
|
||||
travelers,
|
||||
departureDate,
|
||||
origin,
|
||||
destination,
|
||||
places,
|
||||
description,
|
||||
matrixRoomId
|
||||
)
|
||||
driverInfo?.let { driverInfo ->
|
||||
val driver = userRepository.findByIdOrNull(driverInfo.id) ?: throw UserIdNotFoundException()
|
||||
val travelers = travelersInfo.mapNotNull { t -> userRepository.findByIdOrNull(t.id) }
|
||||
.toMutableList()
|
||||
return Travel(
|
||||
id,
|
||||
driver,
|
||||
travelers,
|
||||
departureDate,
|
||||
origin,
|
||||
destination,
|
||||
places,
|
||||
description,
|
||||
matrixRoomId
|
||||
)
|
||||
}
|
||||
throw UserNotSpecifiedException()
|
||||
}
|
||||
}
|
||||
@@ -3,9 +3,11 @@ package eu.fosil.okupamicoche.entities
|
||||
import com.fasterxml.jackson.annotation.JsonInclude
|
||||
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
class ApiResponse<T>(val success: Boolean, val data: T?, val error: ApiError?) {
|
||||
open class ApiResponse<T>(val success: Boolean, val data: T?, val error: ApiError?) {
|
||||
constructor(success: Boolean, data: T) : this(success, data, null)
|
||||
constructor(success: Boolean, error: ApiError) : this(success, null, error)
|
||||
}
|
||||
|
||||
class ApiErrorResponse<T>(code: String, msg: String?) : ApiResponse<T>(false, ApiError(code, msg))
|
||||
|
||||
class ApiError(val code: String, val msg: String?)
|
||||
@@ -1,3 +1,5 @@
|
||||
package eu.fosil.okupamicoche.entities
|
||||
|
||||
class UserIdNotFoundException : Exception()
|
||||
class UserIdNotFoundException : Exception()
|
||||
|
||||
class UserNotSpecifiedException : Exception()
|
||||
@@ -1,6 +1,6 @@
|
||||
package eu.fosil.okupamicoche.spring.controller
|
||||
|
||||
import eu.fosil.okupamicoche.entities.ApiError
|
||||
import eu.fosil.okupamicoche.entities.ApiErrorResponse
|
||||
import eu.fosil.okupamicoche.entities.ApiResponse
|
||||
import eu.fosil.okupamicoche.entities.UserIdNotFoundException
|
||||
import org.springframework.security.core.context.SecurityContextHolder
|
||||
@@ -13,7 +13,7 @@ interface ApiRestController {
|
||||
ApiResponse(true, data)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
ApiResponse(false, ApiError(e.javaClass.canonicalName, e.message))
|
||||
ApiErrorResponse(e.javaClass.canonicalName, e.message)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
package eu.fosil.okupamicoche.spring.controller
|
||||
|
||||
import eu.fosil.okupamicoche.dto.ListDto
|
||||
import eu.fosil.okupamicoche.dto.TravelDto
|
||||
import eu.fosil.okupamicoche.dto.UserInfoDto
|
||||
import eu.fosil.okupamicoche.entities.ApiResponse
|
||||
import eu.fosil.okupamicoche.entities.TravelId
|
||||
import eu.fosil.okupamicoche.entities.UserId
|
||||
import eu.fosil.okupamicoche.entities.UserIdNotFoundException
|
||||
import eu.fosil.okupamicoche.repositories.TravelRepository
|
||||
import eu.fosil.okupamicoche.repositories.UserRepository
|
||||
import eu.fosil.okupamicoche.dto.TravelDto
|
||||
import eu.fosil.okupamicoche.dto.ListDto
|
||||
import eu.fosil.okupamicoche.usecases.travel.*
|
||||
import org.springframework.data.repository.findByIdOrNull
|
||||
import org.springframework.validation.annotation.Validated
|
||||
@@ -23,6 +25,8 @@ class PrivateTravelRestController(
|
||||
@RequestMapping("/create")
|
||||
fun createTravel(@ModelAttribute @Validated travel: TravelDto): ApiResponse<Unit> {
|
||||
return response {
|
||||
val driver = userRepository.findByIdOrNull(getCurrentUserId()) ?: throw UserIdNotFoundException()
|
||||
travel.driverInfo = UserInfoDto(driver)
|
||||
CreateTravel(travelRepository).createTravel(travel.toTravel(userRepository))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user