-
Notifications
You must be signed in to change notification settings - Fork 0
feat: 리뷰 번역기능 추가 #417
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
Merged
+335
−1
Merged
feat: 리뷰 번역기능 추가 #417
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5ad5916
[Feat] 리뷰 번역기능 추가
sjinssun 4fc047d
[Mod] 리뷰 적용
sjinssun 1069cb9
[Mod] test.yml 환경변수 설정
sjinssun 2fe3c62
[Mod] ci.yml deepl 설정
sjinssun 561720d
[Fix]DeepL 커넥션 풀 사이즈 및 타임아웃 조정
sjinssun 1aa7525
Merge remote-tracking branch 'origin/develop' into feat/#416-review-t…
sjinssun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
11 changes: 11 additions & 0 deletions
11
src/main/java/ssu/eatssu/domain/review/dto/ReviewTranslationResponse.java
This file contains hidden or 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,11 @@ | ||
| package ssu.eatssu.domain.review.dto; | ||
|
|
||
| import ssu.eatssu.domain.user.entity.Language; | ||
|
|
||
| public record ReviewTranslationResponse( | ||
| Long reviewId, | ||
| Language language, | ||
| String translatedContent, | ||
| boolean cached | ||
| ) { | ||
| } |
41 changes: 41 additions & 0 deletions
41
src/main/java/ssu/eatssu/domain/review/entity/ReviewTranslation.java
This file contains hidden or 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,41 @@ | ||
| package ssu.eatssu.domain.review.entity; | ||
|
|
||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.EnumType; | ||
| import jakarta.persistence.Enumerated; | ||
| import jakarta.persistence.FetchType; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.JoinColumn; | ||
| import jakarta.persistence.ManyToOne; | ||
| import lombok.AccessLevel; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import ssu.eatssu.domain.user.entity.BaseTimeEntity; | ||
| import ssu.eatssu.domain.user.entity.Language; | ||
|
|
||
| @Entity | ||
| @Getter | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| @Builder | ||
| @AllArgsConstructor | ||
| public class ReviewTranslation extends BaseTimeEntity { | ||
|
|
||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "review_id") | ||
| private Review review; | ||
|
|
||
| @Enumerated(EnumType.STRING) | ||
| private Language language; | ||
|
|
||
| private String translatedContent; | ||
|
|
||
| private Integer charCount; | ||
| } |
37 changes: 37 additions & 0 deletions
37
src/main/java/ssu/eatssu/domain/review/infrastructure/DeepLConfig.java
This file contains hidden or 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,37 @@ | ||
| package ssu.eatssu.domain.review.infrastructure; | ||
|
|
||
| import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; | ||
| import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; | ||
| import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager; | ||
| import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; | ||
| import org.springframework.web.client.RestTemplate; | ||
|
|
||
| @Configuration | ||
| public class DeepLConfig { | ||
|
|
||
| private static final int MAX_CONN_PER_ROUTE = 15; | ||
| private static final int MAX_CONN_TOTAL = 15; | ||
| private static final int CONNECT_TIMEOUT_MS = 2000; | ||
| private static final int CONNECTION_REQUEST_TIMEOUT_MS = 3000; | ||
| private static final int READ_TIMEOUT_MS = 3000; | ||
|
|
||
| @Bean | ||
| public RestTemplate deeplRestTemplate() { | ||
| PoolingHttpClientConnectionManager connectionManager = PoolingHttpClientConnectionManagerBuilder.create() | ||
| .setMaxConnPerRoute(MAX_CONN_PER_ROUTE) | ||
| .setMaxConnTotal(MAX_CONN_TOTAL) | ||
| .build(); | ||
| CloseableHttpClient client = HttpClientBuilder.create() | ||
| .setConnectionManager(connectionManager) | ||
| .build(); | ||
|
|
||
| HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(client); | ||
| factory.setConnectTimeout(CONNECT_TIMEOUT_MS); | ||
| factory.setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT_MS); | ||
| factory.setReadTimeout(READ_TIMEOUT_MS); | ||
| return new RestTemplate(factory); | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
src/main/java/ssu/eatssu/domain/review/infrastructure/DeepLTranslateResponse.java
This file contains hidden or 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,13 @@ | ||
| package ssu.eatssu.domain.review.infrastructure; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import java.util.List; | ||
|
|
||
| public record DeepLTranslateResponse(List<Translation> translations) { | ||
|
|
||
| public record Translation( | ||
| @JsonProperty("detected_source_language") String detectedSourceLanguage, | ||
| String text | ||
| ) { | ||
| } | ||
| } |
68 changes: 68 additions & 0 deletions
68
src/main/java/ssu/eatssu/domain/review/infrastructure/DeepLTranslationClient.java
This file contains hidden or 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,68 @@ | ||
| package ssu.eatssu.domain.review.infrastructure; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.http.HttpEntity; | ||
| import org.springframework.http.HttpHeaders; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.util.LinkedMultiValueMap; | ||
| import org.springframework.util.MultiValueMap; | ||
| import org.springframework.web.client.HttpClientErrorException; | ||
| import org.springframework.web.client.HttpServerErrorException; | ||
| import org.springframework.web.client.ResourceAccessException; | ||
| import org.springframework.web.client.RestClientException; | ||
| import org.springframework.web.client.RestTemplate; | ||
| import ssu.eatssu.domain.user.entity.Language; | ||
| import ssu.eatssu.global.handler.response.BaseException; | ||
|
|
||
| import static ssu.eatssu.global.handler.response.BaseResponseStatus.TRANSLATION_FAILED; | ||
| import static ssu.eatssu.global.handler.response.BaseResponseStatus.TRANSLATION_QUOTA_EXCEEDED; | ||
| import static ssu.eatssu.global.handler.response.BaseResponseStatus.TRANSLATION_TIMEOUT; | ||
|
|
||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class DeepLTranslationClient { | ||
|
|
||
| private static final int QUOTA_EXCEEDED_STATUS = 456; | ||
|
|
||
| private final RestTemplate deeplRestTemplate; | ||
|
|
||
| @Value("${deepl.api-key}") | ||
| private String apiKey; | ||
|
|
||
| @Value("${deepl.base-url:https://api-free.deepl.com}") | ||
| private String baseUrl; | ||
|
|
||
| public String translate(String text, Language targetLanguage) { | ||
| HttpHeaders headers = new HttpHeaders(); | ||
| headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); | ||
| headers.set("Authorization", "DeepL-Auth-Key " + apiKey); | ||
|
|
||
| MultiValueMap<String, String> body = new LinkedMultiValueMap<>(); | ||
| body.add("text", text); | ||
| body.add("target_lang", targetLanguage.name()); | ||
|
|
||
| HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(body, headers); | ||
|
|
||
| try { | ||
| DeepLTranslateResponse response = deeplRestTemplate.postForObject( | ||
| baseUrl + "/v2/translate", request, DeepLTranslateResponse.class); | ||
|
|
||
| if (response == null || response.translations().isEmpty()) { | ||
| throw new BaseException(TRANSLATION_FAILED); | ||
| } | ||
| return response.translations().get(0).text(); | ||
| } catch (HttpClientErrorException | HttpServerErrorException e) { | ||
| int statusCode = e.getStatusCode().value(); | ||
| if (statusCode == 429 || statusCode == QUOTA_EXCEEDED_STATUS) { | ||
| throw new BaseException(TRANSLATION_QUOTA_EXCEEDED); | ||
| } | ||
| throw new BaseException(TRANSLATION_FAILED); | ||
| } catch (ResourceAccessException e) { | ||
| throw new BaseException(TRANSLATION_TIMEOUT); | ||
| } catch (RestClientException e) { | ||
| throw new BaseException(TRANSLATION_FAILED); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -34,11 +34,14 @@ | |
| import ssu.eatssu.domain.review.dto.MenuReviewsV2Response; | ||
| import ssu.eatssu.domain.review.dto.RestaurantReviewResponse; | ||
| import ssu.eatssu.domain.review.dto.ReviewDetail; | ||
| import ssu.eatssu.domain.review.dto.ReviewTranslationResponse; | ||
| import ssu.eatssu.domain.review.dto.UpdateMealReviewRequest; | ||
| import ssu.eatssu.domain.review.dto.ValidMenuForViewResponse; | ||
| import ssu.eatssu.domain.review.service.ReviewServiceV2; | ||
| import ssu.eatssu.domain.review.service.ReviewTranslationService; | ||
| import ssu.eatssu.domain.slice.dto.SliceResponse; | ||
| import ssu.eatssu.domain.user.dto.MyMealReviewResponse; | ||
| import ssu.eatssu.domain.user.entity.Language; | ||
| import ssu.eatssu.global.handler.response.BaseResponse; | ||
|
|
||
| @RestController | ||
|
|
@@ -47,6 +50,7 @@ | |
| @Tag(name = "Review V2", description = "리뷰 V2 API") | ||
| public class ReviewControllerV2 { | ||
| private final ReviewServiceV2 reviewServiceV2; | ||
| private final ReviewTranslationService reviewTranslationService; | ||
|
|
||
| @Operation(summary = "meal(식단)에 대한 리뷰 작성", description = "리뷰를 작성하는 API 입니다.") | ||
| @ApiResponses(value = { | ||
|
|
@@ -139,6 +143,25 @@ public BaseResponse<?> deleteReview( | |
| return BaseResponse.success(); | ||
| } | ||
|
|
||
| @Operation(summary = "리뷰 번역", description = """ | ||
| 리뷰 내용을 DeepL로 번역하는 API 입니다.<br><br> | ||
| 같은 리뷰/언어 조합은 캐시된 결과를 반환합니다.<br><br> | ||
| 현재는 language=EN만 지원합니다. | ||
| """) | ||
| @ApiResponses(value = { | ||
| @ApiResponse(responseCode = "200", description = "리뷰 번역 성공"), | ||
| @ApiResponse(responseCode = "400", description = "지원하지 않는 언어이거나 번역할 내용이 없음", content = @Content(schema = @Schema(implementation = BaseResponse.class))), | ||
| @ApiResponse(responseCode = "404", description = "존재하지 않는 리뷰", content = @Content(schema = @Schema(implementation = BaseResponse.class))), | ||
| @ApiResponse(responseCode = "429", description = "번역 API 사용량 한도 초과", content = @Content(schema = @Schema(implementation = BaseResponse.class))), | ||
| @ApiResponse(responseCode = "503", description = "번역 시간 초과 또는 실패", content = @Content(schema = @Schema(implementation = BaseResponse.class))) | ||
| }) | ||
| @PostMapping("/{reviewId}/translate") | ||
| public BaseResponse<ReviewTranslationResponse> translateReview( | ||
| @Parameter(description = "reviewId") @PathVariable("reviewId") Long reviewId, | ||
| @Parameter(description = "번역 대상 언어(현재 EN만 지원)") @RequestParam Language language) { | ||
| return BaseResponse.success(reviewTranslationService.translateReview(reviewId, language)); | ||
| } | ||
|
Comment on lines
+158
to
+163
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [작성자: 리뷰어, 날짜: 2025-05-15] 컨트롤러 메서드에서 사용되지 않는 @AuthenticationPrincipal CustomUserDetails 매개변수는 제거하여 코드를 깔끔하게 유지하는 것이 좋습니다. @PostMapping("/{reviewId}/translate")
public BaseResponse<ReviewTranslationResponse> translateReview(
@Parameter(description = "reviewId") @PathVariable("reviewId") Long reviewId,
@Parameter(description = "번역 대상 언어(현재 EN만 지원)") @RequestParam Language language) {
return BaseResponse.success(reviewTranslationService.translateReview(reviewId, language));
} |
||
|
|
||
| @Operation(summary = "식단(변동 메뉴) 리뷰 정보 조회 V2(메뉴명, 평점 등등) [인증 토큰 필요 X]", description = """ | ||
| 식단 리뷰 정보를 조회하는 API 입니다.<br><br> | ||
| 메뉴명 리스트, 리뷰 수, 메인 평점, 좋아요 개수, 싫어요 개수, 각 평점의 개수를 조회합니다. | ||
|
|
||
13 changes: 13 additions & 0 deletions
13
src/main/java/ssu/eatssu/domain/review/repository/ReviewTranslationRepository.java
This file contains hidden or 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,13 @@ | ||
| package ssu.eatssu.domain.review.repository; | ||
|
|
||
| import java.util.Optional; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import ssu.eatssu.domain.review.entity.ReviewTranslation; | ||
| import ssu.eatssu.domain.user.entity.Language; | ||
|
|
||
| public interface ReviewTranslationRepository extends JpaRepository<ReviewTranslation, Long> { | ||
|
|
||
| Optional<ReviewTranslation> findByReview_IdAndLanguage(Long reviewId, Language language); | ||
|
|
||
| void deleteAllByReview_Id(Long reviewId); | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.