Fix en consultas JPQL
This commit is contained in:
@@ -8,11 +8,6 @@ class Travel(
|
||||
@ManyToOne
|
||||
var driver: User,
|
||||
@ManyToMany
|
||||
// @JoinTable(
|
||||
// name = "travel_travelers",
|
||||
// joinColumns = [JoinColumn(name = "travel_id")],
|
||||
// inverseJoinColumns = [JoinColumn(name = "user_id")]
|
||||
// )
|
||||
var travelers: MutableList<User>,
|
||||
var departureDate: String,
|
||||
var origin: String,
|
||||
|
||||
@@ -4,12 +4,12 @@ import javax.persistence.*
|
||||
|
||||
@Entity
|
||||
class User(
|
||||
@Id @GeneratedValue var id: Long,
|
||||
var matrixId: String,
|
||||
@Id var id: String,
|
||||
var matrixId: String? = null,
|
||||
var name: String,
|
||||
var email: String,
|
||||
var email: String? = null,
|
||||
@OneToMany(mappedBy = "driver")
|
||||
var travelsAsDriver: List<Travel>,
|
||||
var travelsAsDriver: List<Travel> = emptyList(),
|
||||
@ManyToMany(mappedBy = "travelers")
|
||||
var travelsAsTraveler: List<Travel>
|
||||
var travelsAsTraveler: List<Travel> = emptyList()
|
||||
)
|
||||
@@ -3,10 +3,11 @@ package eu.fosil.okupamicoche.repositories
|
||||
import eu.fosil.okupamicoche.entities.Travel
|
||||
import org.springframework.data.jpa.repository.Query
|
||||
import org.springframework.data.repository.CrudRepository
|
||||
import org.springframework.data.repository.query.Param
|
||||
|
||||
interface TravelRepository : CrudRepository<Travel, Long> {
|
||||
@Query("SELECT t FROM Travel t JOIN t.driver u ON t.id=u.id WHERE t.id = ?1")
|
||||
fun findUserTravelsAsDriver(userId: Long): List<Travel>
|
||||
@Query("SELECT t FROM Travel t JOIN t.travelers u ON t.id=u.id WHERE t.id = ?1")
|
||||
fun findUserTravelsAsTraveler(userId: Long): List<Travel>
|
||||
@Query("SELECT t FROM Travel t WHERE t.driver.id = :userId")
|
||||
fun findUserTravelsAsDriver(@Param("userId") userId: String): List<Travel>
|
||||
@Query("SELECT t FROM Travel t JOIN t.travelers u WHERE u.id = :userId")
|
||||
fun findUserTravelsAsTraveler(@Param("userId") userId: String): List<Travel>
|
||||
}
|
||||
@@ -3,4 +3,4 @@ package eu.fosil.okupamicoche.repositories
|
||||
import eu.fosil.okupamicoche.entities.User
|
||||
import org.springframework.data.repository.CrudRepository
|
||||
|
||||
interface UserRepository : CrudRepository<User, Long>
|
||||
interface UserRepository : CrudRepository<User, String>
|
||||
@@ -5,6 +5,8 @@ import eu.fosil.okupamicoche.repositories.TravelRepository
|
||||
import eu.fosil.okupamicoche.repositories.UserRepository
|
||||
import eu.fosil.okupamicoche.spring.dto.TravelDto
|
||||
import eu.fosil.okupamicoche.usecases.travel.*
|
||||
import org.springframework.security.core.context.SecurityContextHolder
|
||||
import org.springframework.security.oauth2.jwt.Jwt
|
||||
import org.springframework.validation.annotation.Validated
|
||||
import org.springframework.web.bind.annotation.RequestBody
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
@@ -38,6 +40,12 @@ class TravelRestController(
|
||||
return ListTravels(travelRepository).listTravels().map { t -> TravelDto(t) }
|
||||
}
|
||||
|
||||
@RequestMapping("/listusertravels")
|
||||
fun listUserTravels(): List<TravelDto> {
|
||||
val userId = getCurrentUserId() ?: throw Exception()
|
||||
return ListUserTravels(travelRepository).listUserTravels(userId).map { t -> TravelDto(t) }
|
||||
}
|
||||
|
||||
@RequestMapping("/addtraveler")
|
||||
fun addTraveler(travelDto: TravelDto, user: User) {
|
||||
AddTraveler(travelRepository).addTraveler(travelDto.toTravel(userRepository), user)
|
||||
@@ -47,4 +55,17 @@ class TravelRestController(
|
||||
fun removeTraveler(travelDto: TravelDto, user: User) {
|
||||
RemoveTraveler(travelRepository).removeTraveler(travelDto.toTravel(userRepository), user)
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Devuelve el docnumber del usuario actual.
|
||||
*/
|
||||
private fun getCurrentUserId(): String? {
|
||||
val authentication = SecurityContextHolder.getContext().authentication
|
||||
if (authentication.principal is Jwt) {
|
||||
val jwt = authentication.principal as Jwt
|
||||
return jwt.claims["sub"].toString()
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
package eu.fosil.okupamicoche.spring.controller
|
||||
|
||||
import eu.fosil.okupamicoche.repositories.UserRepository
|
||||
import eu.fosil.okupamicoche.spring.dto.CreateUserDto
|
||||
import eu.fosil.okupamicoche.spring.dto.UserDto
|
||||
import eu.fosil.okupamicoche.usecases.user.CreateUser
|
||||
import eu.fosil.okupamicoche.usecases.user.DeleteUser
|
||||
import eu.fosil.okupamicoche.usecases.user.EditUser
|
||||
import eu.fosil.okupamicoche.usecases.user.ListUsers
|
||||
import org.springframework.validation.annotation.Validated
|
||||
import org.springframework.web.bind.annotation.RequestBody
|
||||
import org.springframework.web.bind.annotation.RequestMapping
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
@@ -16,8 +16,8 @@ import org.springframework.web.bind.annotation.RestController
|
||||
class UserRestController(private val userRepository: UserRepository) {
|
||||
|
||||
@RequestMapping("/create")
|
||||
fun createUser(@RequestBody @Validated userDto: UserDto) {
|
||||
CreateUser(userRepository).createUser(userDto.toUser(userRepository))
|
||||
fun createUser(@RequestBody createUserDto: CreateUserDto) {
|
||||
CreateUser(userRepository).createUser(createUserDto.toUser(userRepository))
|
||||
}
|
||||
|
||||
@RequestMapping("/delete")
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package eu.fosil.okupamicoche.spring.dto
|
||||
|
||||
import eu.fosil.okupamicoche.entities.User
|
||||
import eu.fosil.okupamicoche.repositories.UserRepository
|
||||
|
||||
class CreateUserDto(
|
||||
private val id: String,
|
||||
private val name: String,
|
||||
private val email: String?
|
||||
) {
|
||||
constructor(user: User) : this(
|
||||
user.id,
|
||||
user.name,
|
||||
user.email
|
||||
)
|
||||
|
||||
fun toUser(userRepository: UserRepository): User {
|
||||
return User(
|
||||
id = id,
|
||||
name = name,
|
||||
email = email
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -6,9 +6,10 @@ 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: Long,
|
||||
val driverId: Long,
|
||||
val travelersId: List<Long>,
|
||||
val driverId: String,
|
||||
val travelersId: List<String>,
|
||||
val departureDate: String,
|
||||
val origin: String,
|
||||
val destination: String,
|
||||
|
||||
@@ -4,10 +4,11 @@ import eu.fosil.okupamicoche.entities.User
|
||||
import eu.fosil.okupamicoche.repositories.UserRepository
|
||||
|
||||
class UserDto(
|
||||
val id: Long,
|
||||
val matrixId: String,
|
||||
// Los campos deben ser públicos para que aparezcan en el JSON
|
||||
val id: String,
|
||||
val matrixId: String?,
|
||||
val name: String,
|
||||
val email: String,
|
||||
val email: String?,
|
||||
val travelsAsDriver: List<TravelDto>,
|
||||
val travelsAsTraveler: List<TravelDto>
|
||||
) {
|
||||
|
||||
@@ -1,17 +1,13 @@
|
||||
package eu.fosil.okupamicoche.usecases.travel
|
||||
|
||||
import eu.fosil.okupamicoche.entities.Travel
|
||||
import eu.fosil.okupamicoche.entities.User
|
||||
import eu.fosil.okupamicoche.repositories.TravelRepository
|
||||
|
||||
class ListUserTravels(private val travelRepository: TravelRepository) {
|
||||
fun listUserTravels(user: User): List<Travel> {
|
||||
user.id?.let {
|
||||
fun listUserTravels(idUser: String): List<Travel> {
|
||||
return listOf(
|
||||
travelRepository.findUserTravelsAsDriver(it),
|
||||
travelRepository.findUserTravelsAsTraveler(it)
|
||||
travelRepository.findUserTravelsAsDriver(idUser),
|
||||
travelRepository.findUserTravelsAsTraveler(idUser)
|
||||
).flatten()
|
||||
}
|
||||
return listOf()
|
||||
}
|
||||
}
|
||||
@@ -1,2 +1,6 @@
|
||||
spring.security.oauth2.resourceserver.jwt.issuer-uri=https://auth.fosil.eu/auth/realms/fosil
|
||||
spring.security.oauth2.resourceserver.jwt.jwk-set-uri=https://auth.fosil.eu/auth/realms/fosil/protocol/openid-connect/certs
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
spring.datasource.url=jdbc:h2:file:./okupamicoche;DB_CLOSE_ON_EXIT=FALSE;AUTO_RECONNECT=TRUE
|
||||
spring.datasource.driver-class-name=org.h2.Driver
|
||||
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
|
||||
Reference in New Issue
Block a user