-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from Solucitation/feature/reviews
LGTM~๐
- Loading branch information
Showing
5 changed files
with
108 additions
and
2 deletions.
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
14 changes: 14 additions & 0 deletions
14
src/main/java/com/solucitation/midpoint_backend/domain/reviews/RestTemplateConfig.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,14 @@ | ||
package com.solucitation.midpoint_backend.domain.reviews; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Configuration | ||
public class RestTemplateConfig { | ||
|
||
@Bean | ||
public RestTemplate restTemplate() { | ||
return new RestTemplate(); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/solucitation/midpoint_backend/domain/reviews/ReviewController.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,38 @@ | ||
package com.solucitation.midpoint_backend.domain.reviews; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.server.ResponseStatusException; | ||
|
||
import java.util.Map; | ||
|
||
@RestController | ||
@RequestMapping("/api/reviews") | ||
public class ReviewController { | ||
|
||
private final ReviewService reviewService; | ||
|
||
public ReviewController(ReviewService reviewService) { | ||
this.reviewService = reviewService; | ||
} | ||
|
||
@GetMapping | ||
public ResponseEntity<Map<String, String>> getReviewUrl(@RequestParam String placeId) { | ||
try { | ||
Map<String, String> response = reviewService.getReviewUrl(placeId); | ||
return ResponseEntity.ok(response); | ||
} catch (ResponseStatusException e) { | ||
return ResponseEntity | ||
.status(e.getStatusCode()) | ||
.body(Map.of("error", e.getReason())); | ||
} catch (Exception e) { | ||
return ResponseEntity | ||
.status(HttpStatus.INTERNAL_SERVER_ERROR) | ||
.body(Map.of("error", "An unexpected error occurred.")); | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/com/solucitation/midpoint_backend/domain/reviews/ReviewService.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,53 @@ | ||
package com.solucitation.midpoint_backend.domain.reviews; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
import org.springframework.web.server.ResponseStatusException; | ||
|
||
import java.util.Map; | ||
|
||
@Service | ||
public class ReviewService { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(ReviewService.class); | ||
|
||
@Value("${google.api.key}") | ||
private String apiKey; | ||
|
||
private final RestTemplate restTemplate; | ||
private final ObjectMapper objectMapper; | ||
|
||
public ReviewService(RestTemplate restTemplate, ObjectMapper objectMapper) { | ||
this.restTemplate = restTemplate; | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
public Map<String, String> getReviewUrl(String placeId) { | ||
String url = String.format("https://maps.googleapis.com/maps/api/place/details/json?place_id=%s&key=%s", placeId, apiKey); | ||
|
||
try { | ||
String response = restTemplate.getForObject(url, String.class); | ||
JsonNode jsonResponse = objectMapper.readTree(response); | ||
JsonNode result = jsonResponse.path("result"); | ||
|
||
String placeUrl = result.path("url").asText(null); | ||
|
||
if (placeUrl == null || placeUrl.isEmpty()) { | ||
logger.error("No URL found for placeId: {}", placeId); | ||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid placeId: " + placeId); | ||
} | ||
|
||
return Map.of("url", placeUrl); | ||
} catch (Exception e) { | ||
logger.error("Error retrieving place details for placeId: {}", placeId, e); | ||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Invalid placeId: " + placeId); | ||
} | ||
} | ||
} |
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