Skip to content

Commit

Permalink
Merge pull request #50 from Solucitation/feature/reviews
Browse files Browse the repository at this point in the history
LGTM~๐Ÿ‘
  • Loading branch information
khee2 authored Jul 27, 2024
2 parents 83a7c35 + 164813d commit 17f570c
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 2 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ dependencies {
implementation 'org.springframework.security:spring-security-oauth2-client'
implementation 'org.springframework.security:spring-security-oauth2-jose'
implementation 'org.springframework.security:spring-security-oauth2-core'

// Jackson JSON ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€
implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.0'
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.11.5'
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.11.5'

Expand Down
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();
}
}
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."));
}
}
}
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
.cors(withDefaults())
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) // Stateless ์„ธ์…˜ ์„ค์ •
.authorizeHttpRequests(auth -> auth
.requestMatchers("/", "/api/auth/**", "/api/posts/**", "/midpoint/api/logic", "/api/s3/**", "/api/places").permitAll() // ์ธ์ฆ ์—†์ด ์ ‘๊ทผ ํ—ˆ์šฉ
.requestMatchers("/", "/api/auth/**", "/api/posts/**", "/midpoint/api/logic", "/api/s3/**", "/api/places", "/api/reviews").permitAll() // ์ธ์ฆ ์—†์ด ์ ‘๊ทผ ํ—ˆ์šฉ
.anyRequest().authenticated() // ๋‚˜๋จธ์ง€ ์š”์ฒญ์€ ์ธ์ฆ ํ•„์š”
)
.exceptionHandling(exception -> exception
Expand Down

0 comments on commit 17f570c

Please sign in to comment.