-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Task 36 : Define Custom Annotation named TimestampAfterStoreCreation …
…and Revise relevant classes with respect to it
- Loading branch information
1 parent
ca5522c
commit 87e3a08
Showing
4 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
.../casestudy/migroscouriertracking/courier/utils/validator/TimestampAfterStoreCreation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.casestudy.migroscouriertracking.courier.utils.validator; | ||
|
||
import jakarta.validation.Constraint; | ||
import jakarta.validation.Payload; | ||
|
||
import java.lang.annotation.*; | ||
|
||
@Target({ ElementType.TYPE }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Constraint(validatedBy = TimestampAfterStoreCreationValidator.class) | ||
@Documented | ||
public @interface TimestampAfterStoreCreation { | ||
String message() default "Timestamp must be after the store creation time"; | ||
Class<?>[] groups() default {}; | ||
Class<? extends Payload>[] payload() default {}; | ||
} |
42 changes: 42 additions & 0 deletions
42
...y/migroscouriertracking/courier/utils/validator/TimestampAfterStoreCreationValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.casestudy.migroscouriertracking.courier.utils.validator; | ||
|
||
import com.casestudy.migroscouriertracking.courier.model.dto.request.LogCourierLocationRequest; | ||
import com.casestudy.migroscouriertracking.courier.model.entity.StoreEntity; | ||
import com.casestudy.migroscouriertracking.courier.repository.StoreRepository; | ||
import com.casestudy.migroscouriertracking.courier.utils.DistanceUtils; | ||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@RequiredArgsConstructor | ||
public class TimestampAfterStoreCreationValidator implements ConstraintValidator<TimestampAfterStoreCreation, LogCourierLocationRequest> { | ||
|
||
private final StoreRepository storeRepository; | ||
|
||
@Override | ||
public void initialize(TimestampAfterStoreCreation constraintAnnotation) { | ||
} | ||
|
||
@Override | ||
public boolean isValid(LogCourierLocationRequest request, ConstraintValidatorContext context) { | ||
double lat = request.getLat(); | ||
double lng = request.getLng(); | ||
LocalDateTime timestamp = request.getTimestamp(); | ||
|
||
|
||
// Find the first store within 100 meters radius of the courier's location | ||
Optional<StoreEntity> nearestStore = storeRepository.findAll().stream() | ||
.filter(store -> DistanceUtils.isWithinRadius(lat, lng, store.getLat(), store.getLng(), 100.0)) | ||
.findFirst(); | ||
|
||
// If a nearby store is found, validate the timestamp | ||
return nearestStore.map(store -> timestamp.isAfter(store.getCreatedAt())) | ||
.orElse(false); // No nearby store found, validation fails | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters