Skip to content

Commit

Permalink
Rename plannedDepartureTime and add UnprocessableContentApiResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
isabellwaas committed Dec 18, 2024
1 parent 386594d commit d4dc8f8
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import de.uniflitzer.backend.applicationservices.authentication.UserToken
import de.uniflitzer.backend.applicationservices.communicators.version1.datapackages.*
import de.uniflitzer.backend.applicationservices.communicators.version1.documentationinformationadder.apiresponses.*
import de.uniflitzer.backend.applicationservices.communicators.version1.errors.ForbiddenError
import de.uniflitzer.backend.applicationservices.communicators.version1.errors.InternalServerError
import de.uniflitzer.backend.applicationservices.communicators.version1.errors.NotFoundError
import de.uniflitzer.backend.applicationservices.communicators.version1.errors.UnprocessableContentError
import de.uniflitzer.backend.applicationservices.communicators.version1.valuechecker.UUID
Expand Down Expand Up @@ -158,7 +159,7 @@ private class DriveOffersCommunicator(
geographyService.createPosition(driveOfferCreation.route.start.toCoordinate()),
geographyService.createPosition(driveOfferCreation.route.destination.toCoordinate())
),
driveOfferCreation.plannedDepartureTime?.let { ZonedDateTime.parse(it) }
driveOfferCreation.plannedDeparture?.let { ZonedDateTime.parse(it) }
)
}
is CarpoolDriveOfferCreationDP -> {
Expand All @@ -172,7 +173,7 @@ private class DriveOffersCommunicator(
geographyService.createPosition(driveOfferCreation.route.start.toCoordinate()),
geographyService.createPosition(driveOfferCreation.route.destination.toCoordinate())
),
driveOfferCreation.plannedDepartureTime?.let { ZonedDateTime.parse(it) },
driveOfferCreation.plannedDeparture?.let { ZonedDateTime.parse(it) },
targetedCarpool
)
}
Expand Down Expand Up @@ -205,7 +206,7 @@ private class DriveOffersCommunicator(
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."))

driveOfferInEditing.plannedDeparture = ZonedDateTime.parse(driveOfferUpdate.plannedDepartureTime)
driveOfferInEditing.plannedDeparture = ZonedDateTime.parse(driveOfferUpdate.plannedDeparture)
driveOffersRepository.save(driveOfferInEditing)

return ResponseEntity.noContent().build()
Expand Down Expand Up @@ -252,7 +253,7 @@ private class DriveOffersCommunicator(
}

@Operation(description = "Accept a requesting user for a specific drive offer.")
@CommonApiResponses @NoContentApiResponse @NotFoundApiResponse
@CommonApiResponses @UnprocessableContentApiResponse @NoContentApiResponse @NotFoundApiResponse
@PostMapping("{driveOfferId}/requesting-users/{requestingUserId}/acceptances")
fun acceptRequestingUser(@PathVariable @UUID driveOfferId: String, @PathVariable @UUID requestingUserId: String, userToken: UserToken):ResponseEntity<Void> {
val actingUser: User = usersRepository.findById(UUIDType.fromString(userToken.id)).getOrNull() ?: throw ForbiddenError(ErrorDP("User with id ${userToken.id} does not exist in resource server."))
Expand All @@ -278,7 +279,7 @@ private class DriveOffersCommunicator(
}

@Operation(description = "Reject a requesting user for a specific drive offer")
@CommonApiResponses @NoContentApiResponse @NotFoundApiResponse
@CommonApiResponses @UnprocessableContentApiResponse @NoContentApiResponse @NotFoundApiResponse
@PostMapping("{driveOfferId}/requesting-users/{requestingUserId}/rejections")
fun rejectRequestingUser(@PathVariable @UUID driveOfferId: String, @PathVariable @UUID requestingUserId: String, userToken: UserToken):ResponseEntity<Void> {
val actingUser: User = usersRepository.findById(UUIDType.fromString(userToken.id)).getOrNull() ?: throw ForbiddenError(ErrorDP("User with id ${userToken.id} does not exist in resource server."))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package de.uniflitzer.backend.applicationservices.communicators.version1
import de.uniflitzer.backend.applicationservices.authentication.UserToken
import de.uniflitzer.backend.applicationservices.communicators.version1.datapackages.*
import de.uniflitzer.backend.applicationservices.communicators.version1.documentationinformationadder.apiresponses.*
import de.uniflitzer.backend.applicationservices.communicators.version1.errors.ForbiddenError
import de.uniflitzer.backend.applicationservices.communicators.version1.errors.InternalServerError
import de.uniflitzer.backend.applicationservices.communicators.version1.errors.NotFoundError
import de.uniflitzer.backend.applicationservices.communicators.version1.errors.*
import de.uniflitzer.backend.applicationservices.communicators.version1.valuechecker.UUID
import de.uniflitzer.backend.applicationservices.geography.GeographyService
import de.uniflitzer.backend.model.*
Expand Down Expand Up @@ -181,7 +179,7 @@ private class DriveRequestsCommunicator(
}

@Operation(description = "Create a new drive offer for a specific drive request. The drive request is either deleted if it's a CarpoolDriveRequest or its drive offers list is updated if it's a PublicDriveRequest.")
@CommonApiResponses @CreatedApiResponse @NotFoundApiResponse
@CommonApiResponses @UnprocessableContentApiResponse @CreatedApiResponse @NotFoundApiResponse
@PostMapping("{id}/drive-offers")
fun createDriveOfferForDriveRequest(@PathVariable @UUID id:String, @RequestBody @Valid driveOfferCreation: DriveOfferCreationDP, userToken: UserToken): ResponseEntity<IdDP>
{
Expand All @@ -204,29 +202,29 @@ private class DriveRequestsCommunicator(
car,
Seats(driveOfferCreation.freeSeats.toUInt()),
geographyService.createRoute(geographyService.createPosition(driveOfferCreation.route.start.toCoordinate()), geographyService.createPosition(driveOfferCreation.route.destination.toCoordinate())),
driveOfferCreation.plannedDepartureTime?.let { ZonedDateTime.parse(it) },
driveOfferCreation.plannedDeparture?.let { ZonedDateTime.parse(it) },
carpoolsRepository.findById(UUIDType.fromString(driveOfferCreation.carpoolId)).getOrNull() ?: throw NotFoundError(ErrorDP("Carpool with id ${driveOfferCreation.carpoolId} not found."))
)
car.image?.let { driveOffer.car.image = imagesRepository.copy(it) }

driveOffersRepository.saveAndFlush(driveOffer)
driveRequestsRepository.delete(driveRequest)
}
is PublicDriveOfferCreationDP -> { throw ForbiddenError(ErrorDP("PublicDriveOffer creation is not allowed for CarpoolDriveRequests.")) }
is PublicDriveOfferCreationDP -> { throw UnprocessableContentError(ErrorDP("PublicDriveOffer creation is not allowed for CarpoolDriveRequests.")) }
}
}
is PublicDriveRequest -> {
when (driveOfferCreation)
{
is CarpoolDriveOfferCreationDP -> { throw ForbiddenError(ErrorDP("CarpoolDriveOffer creation is not allowed for PublicDriveRequests.")) }
is CarpoolDriveOfferCreationDP -> { throw UnprocessableContentError(ErrorDP("CarpoolDriveOffer creation is not allowed for PublicDriveRequests.")) }
is PublicDriveOfferCreationDP -> {
val car:Car = try{ user.getCarByIndex(driveOfferCreation.carIndex) } catch(error:NotAvailableError){ throw NotFoundError(ErrorDP(error.message!!)) }
driveOffer = PublicDriveOffer(
user,
car,
Seats(driveOfferCreation.freeSeats.toUInt()),
geographyService.createRoute(geographyService.createPosition(driveOfferCreation.route.start.toCoordinate()), geographyService.createPosition(driveOfferCreation.route.destination.toCoordinate())),
driveOfferCreation.plannedDepartureTime?.let { ZonedDateTime.parse(it) }
driveOfferCreation.plannedDeparture?.let { ZonedDateTime.parse(it) }
)
car.image?.let { driveOffer.car.image = imagesRepository.copy(it) }

Expand All @@ -243,7 +241,7 @@ private class DriveRequestsCommunicator(
}

@Operation(description = "This endpoint is only allowed to use on a PublicRequestRequest. Reject a specific drive offer for a specific drive request. Neither the drive request nor the drive offer is deleted so other users can still see them.")
@CommonApiResponses @NoContentApiResponse @NotFoundApiResponse
@CommonApiResponses @UnprocessableContentApiResponse @NoContentApiResponse @NotFoundApiResponse
@PostMapping("{driveRequestId}/drive-offers/{driveOfferId}/rejections")
fun rejectDriveOffer(@PathVariable @UUID driveRequestId:String, @PathVariable @UUID driveOfferId:String, userToken: UserToken): ResponseEntity<Void>
{
Expand All @@ -254,7 +252,7 @@ private class DriveRequestsCommunicator(

when(driveRequest)
{
is CarpoolDriveRequest -> { throw ForbiddenError(ErrorDP("DriveOffers for CarpoolDriveRequests are automatically accepted.")) }
is CarpoolDriveRequest -> { throw UnprocessableContentError(ErrorDP("DriveOffers for CarpoolDriveRequests are automatically accepted.")) }
is PublicDriveRequest ->
{
try { driveRequest.rejectDriveOffer(UUIDType.fromString(driveOfferId)) }
Expand All @@ -268,7 +266,7 @@ private class DriveRequestsCommunicator(
}

@Operation(description = "This endpoint is only allowed to use on a PublicRequestRequest. Accept a specific drive offer for a specific drive request. The requesting user of the drive request is automatically accepted as a passenger and the drive request is deleted.")
@CommonApiResponses @NoContentApiResponse @NotFoundApiResponse
@CommonApiResponses @UnprocessableContentApiResponse @NoContentApiResponse @NotFoundApiResponse
@PostMapping("{driveRequestId}/drive-offers/{driveOfferId}/acceptances")
fun acceptDriveOffer(@PathVariable @UUID driveRequestId:String, @PathVariable @UUID driveOfferId:String, userToken: UserToken): ResponseEntity<Void>
{
Expand All @@ -278,7 +276,7 @@ private class DriveRequestsCommunicator(
if(driveRequest.requestingUser.id != UUIDType.fromString(userToken.id)) throw ForbiddenError(ErrorDP("UserToken id does not match the requesting user id of the drive request."))

when(driveRequest) {
is CarpoolDriveRequest -> { throw ForbiddenError(ErrorDP("DriveOffers for CarpoolDriveRequests are automatically accepted.")) }
is CarpoolDriveRequest -> { throw UnprocessableContentError(ErrorDP("DriveOffers for CarpoolDriveRequests are automatically accepted.")) }
is PublicDriveRequest -> {
try { driveRequest.acceptDriveOffer(UUIDType.fromString(driveOfferId)) }
catch (entityNotFoundError: EntityNotFoundError) { throw NotFoundError(ErrorDP(entityNotFoundError.message!!)) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ class CarpoolDriveOfferCreationDP private constructor(
carIndex: Int,
freeSeats: Int,
route: RouteCreationDP,
plannedDepartureTime: String?,
plannedDeparture: String?,
@field:UUID val carpoolId: String
) : DriveOfferCreationDP(carIndex, freeSeats, route, plannedDepartureTime)
) : DriveOfferCreationDP(carIndex, freeSeats, route, plannedDeparture)
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ class DetailedCarpoolDriveOfferDP constructor(
freeSeats: Int,
route: RouteDP,
passengers: List<UserStopDP>,
plannedDepartureTime: String?,
plannedDeparture: String?,
@field:Valid val carpool: PartialCarpoolDP,
): DetailedDriveOfferDP(containsFavoriteDriver, id, driver, car, freeSeats, route, passengers, plannedDepartureTime)
): DetailedDriveOfferDP(containsFavoriteDriver, id, driver, car, freeSeats, route, passengers, plannedDeparture)
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ sealed class DetailedDriveOfferDP(
@field:Min(1) @field:Max(8) val freeSeats: Int,
@field:Valid val route: RouteDP,
@field:Valid val passengers: List<UserStopDP>,
@field:Pattern(regexp = DateTimeFormat) @field:Schema(example = DateTimeFormatExample) open val plannedDepartureTime: String?
@field:Pattern(regexp = DateTimeFormat) @field:Schema(example = DateTimeFormatExample) open val plannedDeparture: String?
) {
companion object {
fun fromDriveOffer(driveOffer: DriveOffer, containsFavoriteDriver: Boolean): DetailedDriveOfferDP {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ class DetailedPublicDriveOfferDP constructor(
freeSeats: Int,
route: RouteDP,
passengers: List<UserStopDP>,
plannedDepartureTime: String?,
plannedDeparture: String?,
@field:Valid val requestingUsers: List<UserStopDP>?
): DetailedDriveOfferDP(containsFavoriteDriver, id, driver, car, freeSeats, route, passengers, plannedDepartureTime)
): DetailedDriveOfferDP(containsFavoriteDriver, id, driver, car, freeSeats, route, passengers, plannedDeparture)
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ sealed class DriveOfferCreationDP(
@field:Min(0) val carIndex: Int,
@field:Min(1) @field:Max(8) val freeSeats: Int,
@field:Valid val route: RouteCreationDP,
@field:Pattern(regexp = DateTimeFormat) @field:Schema(example = DateTimeFormatExample) val plannedDepartureTime: String?,
@field:Pattern(regexp = DateTimeFormat) @field:Schema(example = DateTimeFormatExample) val plannedDeparture: String?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import io.swagger.v3.oas.annotations.media.Schema
import jakarta.validation.constraints.Pattern

data class DriverOfferUpdateDP private constructor(
@field:Pattern(regexp = DateTimeFormat) @field:Schema(example = DateTimeFormatExample) val plannedDepartureTime: String,
@field:Pattern(regexp = DateTimeFormat) @field:Schema(example = DateTimeFormatExample) val plannedDeparture: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class PartialCarpoolDriveOfferDP (
freeSeats: Int,
route: RouteDP,
passengersCount: Int,
plannedDepartureTime: String?,
plannedDeparture: String?,
@field:UUID val carpoolId: String,
): PartialDriveOfferDP(containsFavoriteDriver, id, driver, freeSeats, route, passengersCount, plannedDepartureTime) {
): PartialDriveOfferDP(containsFavoriteDriver, id, driver, freeSeats, route, passengersCount, plannedDeparture) {
companion object {
fun fromCarpoolDriveOffer(carpoolDriveOffer: CarpoolDriveOffer, containsFavoriteDriver: Boolean): PartialCarpoolDriveOfferDP =
PartialCarpoolDriveOfferDP(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ sealed class PartialDriveOfferDP(
@field:Min(1) @field:Max(8) val freeSeats: Int,
@field:Valid val route: RouteDP,
@field:Min(1) @field:Max(8) val passengersCount: Int,
@field:Pattern(regexp = DateTimeFormat) @field:Schema(example = DateTimeFormatExample) val plannedDepartureTime: String?
@field:Pattern(regexp = DateTimeFormat) @field:Schema(example = DateTimeFormatExample) val plannedDeparture: String?
) {
companion object {
fun fromDriveOffer(driveOffer: DriveOffer, containsFavoriteDriver: Boolean): PartialDriveOfferDP {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class PartialPublicDriveOfferDP constructor(
freeSeats: Int,
route: RouteDP,
passengersCount: Int,
plannedDepartureTime: String?,
plannedDeparture: String?,
@field:Size(min = 0) val requestingUserIds: List<String>?
): PartialDriveOfferDP(containsFavoriteDriver, id, driver, freeSeats, route, passengersCount, plannedDepartureTime) {
): PartialDriveOfferDP(containsFavoriteDriver, id, driver, freeSeats, route, passengersCount, plannedDeparture) {
companion object {
fun fromPublicDriveOffer(publicDriveOffer: PublicDriveOffer, containsFavoriteDriver: Boolean): PartialPublicDriveOfferDP {
return PartialPublicDriveOfferDP(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ class PublicDriveOfferCreationDP private constructor(
carIndex: Int,
freeSeats: Int,
route: RouteCreationDP,
plannedDepartureTime: String?,
) : DriveOfferCreationDP(carIndex, freeSeats, route, plannedDepartureTime)
plannedDeparture: String?,
) : DriveOfferCreationDP(carIndex, freeSeats, route, plannedDeparture)
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package de.uniflitzer.backend.applicationservices.communicators.version1.documentationinformationadder.apiresponses

import de.uniflitzer.backend.applicationservices.communicators.version1.datapackages.ErrorDP
import io.swagger.v3.oas.annotations.media.Content
import io.swagger.v3.oas.annotations.media.Schema
import io.swagger.v3.oas.annotations.responses.ApiResponse

@Target(AnnotationTarget.FUNCTION)
@ApiResponse(
responseCode = "422",
content = [Content(schema = Schema(implementation = ErrorDP::class))]
)
annotation class UnprocessableContentApiResponse

0 comments on commit d4dc8f8

Please sign in to comment.