SaveTripRequest.java
996 bytes
| 1 | package com.tasteprint.journey; |
|---|---|
| 2 | |
| 3 | import java.time.LocalDate; |
| 4 | import java.time.temporal.ChronoUnit; |
| 5 | |
| 6 | import jakarta.validation.constraints.NotBlank; |
| 7 | import jakarta.validation.constraints.NotNull; |
| 8 | import jakarta.validation.constraints.Pattern; |
| 9 | import jakarta.validation.constraints.Size; |
| 10 | |
| 11 | public record SaveTripRequest( |
| 12 | @NotBlank @Pattern(regexp = "^[A-Za-z]{2}$", message = "must be a two-letter country code") String destinationCode, |
| 13 | @NotBlank @Size(max = 100) String city, |
| 14 | @NotNull LocalDate startsOn, |
| 15 | @NotNull LocalDate endsOn |
| 16 | ) { |
| 17 | public SaveTripRequest { |
| 18 | if (startsOn != null && endsOn != null) { |
| 19 | if (endsOn.isBefore(startsOn)) { |
| 20 | throw new IllegalArgumentException("Trip end date cannot be before its start date."); |
| 21 | } |
| 22 | if (ChronoUnit.DAYS.between(startsOn, endsOn) > 180) { |
| 23 | throw new IllegalArgumentException("A trip can be at most 180 days long."); |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | |