-
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.
- Loading branch information
Showing
13 changed files
with
282 additions
and
0 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
33 changes: 33 additions & 0 deletions
33
src/main/java/com/back/kukertonc/domain/summary/api/SummaryController.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,33 @@ | ||
package com.back.kukertonc.domain.summary.api; | ||
|
||
import com.back.kukertonc.domain.summary.dto.SummaryRequest; | ||
import com.back.kukertonc.domain.summary.dto.SummaryResponse; | ||
import com.back.kukertonc.domain.summary.dto.UserSummaryRequest; | ||
import com.back.kukertonc.domain.summary.dto.UserSummaryResponse; | ||
import com.back.kukertonc.domain.summary.service.SummaryService; | ||
import com.back.kukertonc.global.reponse.BaseResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
@Slf4j | ||
@RestController | ||
@RequestMapping(value = "/summary") | ||
@RequiredArgsConstructor | ||
public class SummaryController { | ||
private final SummaryService summaryService; | ||
@PostMapping("") | ||
public BaseResponse<SummaryResponse> getSummary(@RequestBody SummaryRequest summaryRequest) throws IOException { | ||
return new BaseResponse<>(summaryService.getSummary(summaryRequest)); | ||
} | ||
|
||
@PostMapping("/user") | ||
public BaseResponse<UserSummaryResponse> postUserSummary(@RequestBody UserSummaryRequest userSummaryRequest){ | ||
return new BaseResponse<>(summaryService.postUserSummary(userSummaryRequest)); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/back/kukertonc/domain/summary/dto/ClovaSummaryRequest.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,21 @@ | ||
package com.back.kukertonc.domain.summary.dto; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@Builder | ||
public class ClovaSummaryRequest { | ||
private DocumentObject documentObject; | ||
private OptionObject optionObject; | ||
|
||
public static ClovaSummaryRequest of( | ||
DocumentObject documentObject, | ||
OptionObject optionObject | ||
){ | ||
return ClovaSummaryRequest.builder() | ||
.documentObject(documentObject) | ||
.optionObject(optionObject) | ||
.build(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/back/kukertonc/domain/summary/dto/DocumentObject.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,18 @@ | ||
package com.back.kukertonc.domain.summary.dto; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@Builder | ||
public class DocumentObject { | ||
private String content; | ||
|
||
public static DocumentObject of( | ||
String content | ||
){ | ||
return DocumentObject.builder() | ||
.content(content) | ||
.build(); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/back/kukertonc/domain/summary/dto/OptionObject.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,18 @@ | ||
package com.back.kukertonc.domain.summary.dto; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@Builder | ||
public class OptionObject { | ||
private String language; | ||
|
||
public static OptionObject of( | ||
String language | ||
){ | ||
return OptionObject.builder() | ||
.language(language) | ||
.build(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/back/kukertonc/domain/summary/dto/SummaryRequest.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.back.kukertonc.domain.summary.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
public class SummaryRequest { | ||
private String contents; | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/back/kukertonc/domain/summary/dto/SummaryResponse.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,18 @@ | ||
package com.back.kukertonc.domain.summary.dto; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@Builder | ||
public class SummaryResponse { | ||
private String summary; | ||
|
||
public static SummaryResponse of( | ||
String summary | ||
){ | ||
return SummaryResponse.builder() | ||
.summary(summary) | ||
.build(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/back/kukertonc/domain/summary/dto/UserSummaryRequest.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,17 @@ | ||
package com.back.kukertonc.domain.summary.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Getter | ||
public class UserSummaryRequest { | ||
private Long userId; | ||
private Long writingId; | ||
private String summary; | ||
private Boolean isComplete; | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/back/kukertonc/domain/summary/dto/UserSummaryResponse.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,20 @@ | ||
package com.back.kukertonc.domain.summary.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@Builder | ||
public class UserSummaryResponse { | ||
private Boolean isComplete; | ||
|
||
public static UserSummaryResponse of( | ||
Boolean isComplete | ||
){ | ||
return UserSummaryResponse.builder() | ||
.isComplete(isComplete) | ||
.build(); | ||
} | ||
} |
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
7 changes: 7 additions & 0 deletions
7
src/main/java/com/back/kukertonc/domain/summary/repository/UserSummaryRepository.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,7 @@ | ||
package com.back.kukertonc.domain.summary.repository; | ||
|
||
import com.back.kukertonc.domain.summary.entity.UserSummary; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface UserSummaryRepository extends JpaRepository<UserSummary, Long> { | ||
} |
90 changes: 90 additions & 0 deletions
90
src/main/java/com/back/kukertonc/domain/summary/service/SummaryService.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,90 @@ | ||
package com.back.kukertonc.domain.summary.service; | ||
|
||
import com.back.kukertonc.domain.summary.dto.*; | ||
import com.back.kukertonc.domain.summary.entity.UserSummary; | ||
import com.back.kukertonc.domain.summary.entity.Writing; | ||
import com.back.kukertonc.domain.summary.repository.UserSummaryRepository; | ||
import com.back.kukertonc.domain.summary.repository.WritingRepository; | ||
import com.back.kukertonc.domain.user.entity.User; | ||
import com.back.kukertonc.domain.user.repository.UserRepository; | ||
import lombok.AllArgsConstructor; | ||
import lombok.NoArgsConstructor; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.*; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
|
||
import java.net.URI; | ||
|
||
@Slf4j | ||
@Service | ||
@RequiredArgsConstructor | ||
public class SummaryService { | ||
@Value("${clova.summary.client-id}") | ||
private String clientId; | ||
@Value("${clova.summary.client-secret}") | ||
private String clientSecret; | ||
@Value("${clova.summary.url}") | ||
private String url; | ||
private final UserSummaryRepository userSummaryRepository; | ||
private final UserRepository userRepository; | ||
private final WritingRepository writingRepository; | ||
|
||
private final RestTemplate restTemplate; | ||
|
||
|
||
public SummaryResponse getSummary(SummaryRequest summaryRequest) { | ||
String contents = summaryRequest.getContents(); | ||
contents = contents.replace("\n", ""); | ||
// 요청할 데이터를 정의합니다. | ||
String requestBody = "{\"document\": {\"content\": \""; | ||
requestBody += contents; | ||
requestBody += "\"},\"option\": {\"language\": \"ko\"}}"; | ||
|
||
log.info(requestBody); | ||
|
||
// 헤더를 정의합니다. | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setContentType(MediaType.APPLICATION_JSON); | ||
headers.set("X-NCP-APIGW-API-KEY-ID", clientId); | ||
headers.set("X-NCP-APIGW-API-KEY", clientSecret); | ||
|
||
// 헤더와 바디를 가진 HTTP 요청을 생성합니다. | ||
HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, headers); | ||
|
||
// 외부 API를 호출합니다. | ||
String apiUrl = "https://naveropenapi.apigw.ntruss.com/text-summary/v1/summarize"; | ||
|
||
String result = restTemplate.postForObject(apiUrl, requestEntity, String.class); | ||
String[] resultSlice = result.split(":"); | ||
String summaryResult = resultSlice[resultSlice.length -1].replace("\"", ""); | ||
summaryResult = summaryResult.replace("}", ""); | ||
|
||
return SummaryResponse.of(summaryResult); | ||
} | ||
|
||
public UserSummaryResponse postUserSummary(UserSummaryRequest userSummaryRequest) { | ||
Long userId = userSummaryRequest.getUserId(); | ||
Long writingId = userSummaryRequest.getWritingId(); | ||
|
||
User user = userRepository.findById(userId).get(); | ||
Writing writing = writingRepository.findById(writingId).get(); | ||
|
||
Boolean isComplete = userSummaryRequest.getIsComplete(); | ||
|
||
UserSummary userSummary = UserSummary.of( | ||
0, | ||
userSummaryRequest.getSummary(), | ||
isComplete, | ||
user, | ||
writing | ||
); | ||
|
||
userSummaryRepository.save(userSummary); | ||
|
||
return UserSummaryResponse.of(isComplete); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/back/kukertonc/domain/user/repository/UserRepository.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,7 @@ | ||
package com.back.kukertonc.domain.user.repository; | ||
|
||
import com.back.kukertonc.domain.user.entity.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface UserRepository extends JpaRepository<User, Long> { | ||
} |