Skip to content

Commit

Permalink
Merge pull request #22 from branch smaller-features-creation-and-opti…
Browse files Browse the repository at this point in the history
…mizations
  • Loading branch information
stevensolleder authored Dec 16, 2024
2 parents 17b0882 + ba49c57 commit 8aca0f1
Show file tree
Hide file tree
Showing 40 changed files with 803 additions and 166 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-validation")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-websocket")
runtimeOnly("org.postgresql:postgresql")
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.boot:spring-boot-starter-oauth2-resource-server")
Expand All @@ -24,6 +25,7 @@ dependencies {
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
implementation("org.gavaghan:geodesy:1.1.3")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")
}

plugins {
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/de/uniflitzer/backend/BackendApplication.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package de.uniflitzer.backend

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.scheduling.annotation.EnableScheduling

@SpringBootApplication
@EnableScheduling
class BackendApplication
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,47 @@ private class DriveOffersCommunicator(
@Operation(description = "Get all drive offers.")
@CommonApiResponses @OkApiResponse
@GetMapping("")
fun getDriveOffers(@RequestParam @Min(1) pageNumber: Int, @RequestParam @Min(1) @Max(50) perPage: Int, @RequestParam latitude:Double, @RequestParam longitude: Double, @RequestParam sortingDirection: SortingDirection = SortingDirection.Ascending, userToken: UserToken): ResponseEntity<PageDP<PartialDriveOfferDP>> {
fun getDriveOffers(
@RequestParam @Min(1) pageNumber: Int,
@RequestParam @Min(1) @Max(50) perPage: Int,
@RequestParam startLatitude:Double,
@RequestParam startLongitude: Double,
@RequestParam destinationLatitude:Double,
@RequestParam destinationLongitude: Double,
@RequestParam allowedAnimals: List<AnimalDP>? = null,
@RequestParam isSmoking: Boolean? = null,
@RequestParam allowedDrivingStyles: List<DrivingStyleDP>? = null,
@RequestParam allowedGenders: List<GenderDP>? = null,
@RequestParam sortingDirection: SortingDirection = SortingDirection.Ascending,
userToken: UserToken
): ResponseEntity<PageDP<PartialDriveOfferDP>> {
val actingUser: User = usersRepository.findById(UUIDType.fromString(userToken.id)).getOrNull() ?: throw ForbiddenError(ErrorDP("User with id ${userToken.id} does not exist in resource server."))

val destinationCoordinate: Coordinate = Coordinate(latitude, longitude)
val searchedDriveOffers: List<DriveOffer> = driveOffersRepository.findAll(
Sort.by(
when (sortingDirection) {
SortingDirection.Ascending -> Sort.Direction.ASC
SortingDirection.Descending -> Sort.Direction.DESC
},
DriveOffer::plannedDeparture.name
val allowedAnimals: List<Animal>? = allowedAnimals?.map { it.toAnimal() }
val allowedDrivingStyles: List<DrivingStyle>? = allowedDrivingStyles?.map { it.toDrivingStyle() }
val allowedGenders: List<Gender>? = allowedGenders?.map { it.toGender() }

val startCoordinate: Coordinate = Coordinate(startLatitude, startLongitude)
val destinationCoordinate: Coordinate = Coordinate(destinationLatitude, destinationLongitude)
val tolerance: Meters = Meters(1000.0)
val searchedDriveOffers: List<DriveOffer> =
driveOffersRepository.findAll(
allowedAnimals,
isSmoking,
allowedDrivingStyles,
allowedGenders,
actingUser.blockedUsers,
Sort.by(
when (sortingDirection) {
SortingDirection.Ascending -> Sort.Direction.ASC
SortingDirection.Descending -> Sort.Direction.DESC
},
DriveOffer::plannedDeparture.name
)
)
)
.filter {(it.route.destination.coordinate distanceTo destinationCoordinate).value <= 1000.0 }
//TODO: CarpoolDriveOffers
.filter { it.route.isCoordinateOnRoute(startCoordinate, tolerance) && it.route.isCoordinateOnRoute(destinationCoordinate, tolerance) }
.filter { it.route.areCoordinatesInCorrectDirection(startCoordinate, destinationCoordinate) }

return ResponseEntity.ok(
PartialDriveOfferPageDP.fromList(
Expand Down Expand Up @@ -138,6 +165,20 @@ private class DriveOffersCommunicator(
return ResponseEntity.status(HttpStatus.CREATED).body(IdDP(newDriveOffer.id.toString()))
}

@Operation(description = "Delete a drive offer.")
@CommonApiResponses @CreatedApiResponse
@DeleteMapping("{id}/")
fun deleteDriveOffer(@PathVariable @UUID id: String, @RequestBody @Valid userToken: UserToken): ResponseEntity<IdDP> {
if(!usersRepository.existsById(UUIDType.fromString(userToken.id))) throw ForbiddenError(ErrorDP("User with id ${userToken.id} does not exist in resource server."))
val driveOfferInEditing: DriveOffer = driveOffersRepository.findById(UUIDType.fromString(id)).getOrNull() ?: throw NotFoundError(ErrorDP("The drive offer with the id $id could not be found."))
if (driveOfferInEditing.driver.id.toString() != userToken.id) throw ForbiddenError(ErrorDP("The user with the id $id is not the driver of the drive offer with the id $id."))

driveOffersRepository.delete(driveOfferInEditing)
driveOffersRepository.flush()

return ResponseEntity.noContent().build()
}

@Operation(description = "Update a specific drive offer. Only the planned departure time can be updated.")
@CommonApiResponses @NoContentApiResponse @NotFoundApiResponse
@PatchMapping("{id}")
Expand Down
Loading

0 comments on commit 8aca0f1

Please sign in to comment.