Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

7817 fix api response status 500 instead of 400 when coordinates not specified #7850

Conversation

Cr1stal423
Copy link
Contributor

@Cr1stal423 Cr1stal423 commented Dec 2, 2024

GreenCity PR
Issue Link:
7817
Changes:

Added logic to validate that coordinates are not empty while creating an event.
Added new error messages.
Updated tests related to the new logic.

Current Behavior:

The response now indicates when coordinates are not specified.

Screenshot from 2024-12-02 20-37-45

@Cr1stal423 Cr1stal423 changed the title 7817 fix api response status 500 instead of 400 when coordinates not specified 7850 fix api response status 500 instead of 400 when coordinates not specified Dec 3, 2024
@Cr1stal423 Cr1stal423 changed the title 7850 fix api response status 500 instead of 400 when coordinates not specified 7817 fix api response status 500 instead of 400 when coordinates not specified Dec 3, 2024
for (EventDateLocationDto eventDateLocationDto : eventDateLocationDtos) {
AddressDto coordinates = eventDateLocationDto.getCoordinates();

if (coordinates == null || coordinates.getLatitude() == null || coordinates.getLongitude() == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe make sens use Objects.isNull() ckeck

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected

double latitude = coordinates.getLatitude();
double longitude = coordinates.getLongitude();

if (latitude < -90.0 || latitude > 90.0 || longitude < -180.0 || longitude > 180.0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe move this check to separate method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected

.latitude(ModelUtils.getAddressDto().getLatitude())
.longitude(ModelUtils.getAddressDto().getLongitude())
.build();
when(modelMapper.map(ModelUtils.getAddressLatLngResponse(), AddressDto.class)).thenReturn(build);
Copy link
Contributor

@KizerovDmitriy KizerovDmitriy Dec 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move this AddressDto builder to ModelsUtils class

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected

… ModelUtils, replace '==' with Objects.isNull(), extract coordinate validation to a separate method
}

private boolean isValidCoordinate(double latitude, double longitude) {
return latitude >= -90.0 && latitude <= 90.0 && longitude >= -180.0 && longitude <= 180.0;
Copy link
Collaborator

@holotsvan holotsvan Dec 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return Math.abs(latitude) <= 90.0 && Math.abs(longitude) <= 180.0;

what about such solution?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

@@ -179,6 +179,9 @@ public class ErrorMessage {
public static final String USER_HAS_NO_FRIEND_WITH_ID = "User has no friend with this id: ";
public static final String INVALID_DURATION = "The duration for such habit is lower than previously set";
public static final String ADDRESS_NOT_FOUND_EXCEPTION = "No address found for the given coordinates.";
public static final String INVALID_COORDINATES = "The coordinates field must not be empty";
public static final String INVALID_LONGITUDE = "Longitude must be between -180 and 180 degrees";
public static final String INVALID_LATITUDE = "Latitude must be between -90 and 90 degrees";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need constants INVALID_LONGITUDE and INVALID_LATITUDE?
You don't use them anywhere.

@ChernenkoVitaliy ChernenkoVitaliy self-requested a review December 3, 2024 21:04
@Cr1stal423 Cr1stal423 merged commit f60459b into dev Dec 5, 2024
3 checks passed
@Cr1stal423 Cr1stal423 deleted the 7817-fix-api-response-status-500-instead-of-400-when-coordinates-not-specified branch December 5, 2024 11:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants