|
| 1 | +package com.loopin.api.events.shared.validation; |
| 2 | + |
| 3 | +import com.loopin.api.events.dto.request.EventRequest; |
| 4 | +import org.springframework.stereotype.Component; |
| 5 | + |
| 6 | +import java.math.BigDecimal; |
| 7 | +import java.net.URI; |
| 8 | +import java.net.URISyntaxException; |
| 9 | +import java.time.Clock; |
| 10 | +import java.time.Duration; |
| 11 | +import java.time.LocalDateTime; |
| 12 | +import java.util.LinkedHashMap; |
| 13 | +import java.util.List; |
| 14 | +import java.util.Map; |
| 15 | +import java.util.UUID; |
| 16 | + |
| 17 | +@Component |
| 18 | +public class EventRequestValidator { |
| 19 | + |
| 20 | + static final Duration MINIMUM_LEAD_TIME = Duration.ofMinutes(30); |
| 21 | + static final Duration MAXIMUM_DURATION = Duration.ofHours(24); |
| 22 | + static final int MAXIMUM_INTEREST_IDS = 10; |
| 23 | + static final int PRICE_SCALE = 2; |
| 24 | + static final BigDecimal MINIMUM_PAID_PRICE = new BigDecimal("0.01"); |
| 25 | + static final BigDecimal MAXIMUM_PRICE = new BigDecimal("1000000.00"); |
| 26 | + |
| 27 | + private final Clock clock; |
| 28 | + |
| 29 | + public EventRequestValidator(Clock clock) { |
| 30 | + this.clock = clock; |
| 31 | + } |
| 32 | + |
| 33 | + public void validate(EventRequest request) { |
| 34 | + Map<String, String> errors = new LinkedHashMap<>(); |
| 35 | + validateDates(request, errors); |
| 36 | + validatePrice(request, errors); |
| 37 | + validateImageUrl(request.getImageUrl(), errors); |
| 38 | + validateCoordinates(request, errors); |
| 39 | + validateInterestIds(request.getInterestIds(), errors); |
| 40 | + |
| 41 | + if (!errors.isEmpty()) { |
| 42 | + throw new EventRequestValidationException(errors); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private void validateDates(EventRequest request, Map<String, String> errors) { |
| 47 | + LocalDateTime start = request.getStartDateTime(); |
| 48 | + LocalDateTime end = request.getEndDateTime(); |
| 49 | + if (start == null || end == null) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + if (start.isBefore(LocalDateTime.now(clock).plus(MINIMUM_LEAD_TIME))) { |
| 54 | + errors.put("startDateTime", "Start date and time must be at least 30 minutes in the future"); |
| 55 | + } |
| 56 | + if (!end.isAfter(start)) { |
| 57 | + errors.put("endDateTime", "End date and time must be after start date and time"); |
| 58 | + } else if (Duration.between(start, end).compareTo(MAXIMUM_DURATION) > 0) { |
| 59 | + errors.put("endDateTime", "Event duration must not exceed 24 hours"); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private void validatePrice(EventRequest request, Map<String, String> errors) { |
| 64 | + Boolean isFree = request.getIsFree(); |
| 65 | + BigDecimal price = request.getPrice(); |
| 66 | + if (isFree == null) { |
| 67 | + return; |
| 68 | + } |
| 69 | + if (isFree) { |
| 70 | + if (price != null && price.compareTo(BigDecimal.ZERO) != 0) { |
| 71 | + errors.put("price", "Free events must have price 0 or null"); |
| 72 | + } |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + if (price == null || price.compareTo(MINIMUM_PAID_PRICE) < 0) { |
| 77 | + errors.put("price", "Paid events must have a price of at least 0.01"); |
| 78 | + } else if (price.scale() > PRICE_SCALE) { |
| 79 | + errors.put("price", "Price must have at most 2 decimal places"); |
| 80 | + } else if (price.compareTo(MAXIMUM_PRICE) > 0) { |
| 81 | + errors.put("price", "Price must not exceed 1000000.00"); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private void validateImageUrl(String imageUrl, Map<String, String> errors) { |
| 86 | + if (imageUrl == null || imageUrl.isBlank()) { |
| 87 | + return; |
| 88 | + } |
| 89 | + try { |
| 90 | + URI uri = new URI(imageUrl); |
| 91 | + if ((!"http".equalsIgnoreCase(uri.getScheme()) && !"https".equalsIgnoreCase(uri.getScheme())) |
| 92 | + || uri.getHost() == null) { |
| 93 | + errors.put("imageUrl", "Image URL must use HTTP or HTTPS"); |
| 94 | + } |
| 95 | + } catch (URISyntaxException exception) { |
| 96 | + errors.put("imageUrl", "Image URL must use HTTP or HTTPS"); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private void validateCoordinates(EventRequest request, Map<String, String> errors) { |
| 101 | + if ((request.getLatitude() == null) != (request.getLongitude() == null)) { |
| 102 | + errors.put("coordinates", "Latitude and longitude must be provided together"); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private void validateInterestIds(List<UUID> interestIds, Map<String, String> errors) { |
| 107 | + if (interestIds == null) { |
| 108 | + return; |
| 109 | + } |
| 110 | + if (interestIds.size() > MAXIMUM_INTEREST_IDS) { |
| 111 | + errors.put("interestIds", "At most 10 interests may be selected"); |
| 112 | + return; |
| 113 | + } |
| 114 | + if (interestIds.stream().anyMatch(java.util.Objects::isNull)) { |
| 115 | + errors.put("interestIds", "Interest IDs must not contain null values"); |
| 116 | + return; |
| 117 | + } |
| 118 | + if (interestIds.stream().distinct().count() != interestIds.size()) { |
| 119 | + errors.put("interestIds", "Duplicate interests are not allowed"); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments