Skip to content

Commit

Permalink
Merge pull request #33 from goormthon-Univ/feature/28
Browse files Browse the repository at this point in the history
[FIX] Transactional BUG
  • Loading branch information
chaeeun-Han authored Mar 21, 2024
2 parents c51573d + 3939490 commit 696703c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
21 changes: 12 additions & 9 deletions src/main/java/com/beotkkot/qtudy/service/posts/PostsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class PostsService {
@Transactional
public ResponseEntity<? super PostsResponseDto> savePost(Long kakao_uid, PostsRequestDto dto) {
Long postId;
List<Tags> newTagList = new ArrayList<>();
List<String> increasedTag = new ArrayList<>();
try {

if (userRepo.findByKakaoId(kakao_uid) != null) {
Expand All @@ -47,35 +49,30 @@ public ResponseEntity<? super PostsResponseDto> savePost(Long kakao_uid, PostsRe

// ํƒœ๊ทธ ์ฒ˜๋ฆฌ
List<String> postTags = dto.getTag();
List<String> savedTags = new ArrayList<>();

for (String tagName : postTags) {
Optional<Tags> existingTag = tagRepo.findByName(tagName);
if (existingTag.isPresent()) {
// ๊ธฐ์กด์— ์žˆ๋Š” ํƒœ๊ทธ์ธ ๊ฒฝ์šฐ count๋ฅผ ์ฆ๊ฐ€์‹œํ‚ด
Tags tag = existingTag.get();
tag.increaseTagCount();
savedTags.add(tagName);
increasedTag.add(tagName);
} else {
// ์ƒˆ๋กœ์šด ํƒœ๊ทธ์ธ ๊ฒฝ์šฐ ํƒœ๊ทธ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  count๋ฅผ 1๋กœ ์ดˆ๊ธฐํ™”ํ•จ
Tags newTag = new Tags();
newTag.setName(tagName);
newTag.setCount(1); // ์ƒˆ๋กœ์šด ํƒœ๊ทธ์˜ count๋ฅผ 1๋กœ ์ดˆ๊ธฐํ™”
newTag.setCategoryId(dto.getCategoryId());
savedTags.add(tagName);

// ์ƒˆ๋กœ์šด ํƒœ๊ทธ๋ฅผ ์ €์žฅ
tagRepo.save(newTag);
newTagList.add(newTag);
}
}

// ์ €์žฅ๋œ ํƒœ๊ทธ ๋ชฉ๋ก์„ ํฌ์ŠคํŠธ์— ์„ค์ •
String tagString = String.join(",", savedTags);
String tagString = String.join(",", postTags);
post.setTag(tagString);

/**
* postRepo์— ํ•ด๋‹น ์œ ์ €๊ฐ€ ์ž‘์„ฑํ•œ ๊ธ€์— ๋Œ€ํ•œ ์š”์•ฝ๋ณธ ์ €์žฅํ•˜๋Š” ๋ถ€๋ถ„ ์ถ”๊ฐ€
*/
// postRepo์— ํ•ด๋‹น ์œ ์ €๊ฐ€ ์ž‘์„ฑํ•œ ๊ธ€์— ๋Œ€ํ•œ ์š”์•ฝ๋ณธ ์ €์žฅํ•˜๋Š” ๋ถ€๋ถ„ ์ถ”๊ฐ€
String summary = summaryService.summary(dto.getContent());
post.setContent(dto.getContent());
post.setSummary(summary);
Expand All @@ -84,11 +81,17 @@ public ResponseEntity<? super PostsResponseDto> savePost(Long kakao_uid, PostsRe
Posts savedPost = postsRepo.save(post);
postId = savedPost.getPostId();

tagRepo.saveAll(newTagList);
} else {
return PostsResponseDto.notExistUser();
}
} catch (Exception exception) {
exception.printStackTrace();
for (String tagName : increasedTag) {
Optional<Tags> existingTag = tagRepo.findByName(tagName);
Tags tag = existingTag.get();
tag.decreaseTagCount();
}
return ResponseDto.databaseError();
}
return PostsResponseDto.success(postId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
Expand All @@ -16,19 +17,22 @@
@RequiredArgsConstructor
@Service
public class SummaryService {
@Value("${CLOVA_API_KEY_ID}")
private String CLOVA_API_KEY_ID;
@Value("${CLOVA_API_KEY}")
private String CLOVA_API_KEY;
private static final String ENDPOINT = "https://naveropenapi.apigw.ntruss.com/text-summary/v1/summarize";

// ํ…์ŠคํŠธ ๋ฐ์ดํ„ฐ ๋ฐ›์•„์™€์„œ ์š”์•ฝ
public String summary(String content) {

String requestURL = "https://naveropenapi.apigw.ntruss.com/text-summary/v1/summarize";

RestTemplate rt = new RestTemplate();

// HTTPHeader
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json;UTF-8");
headers.add("X-NCP-APIGW-API-KEY-ID", "m099ib4o3p");
headers.add("X-NCP-APIGW-API-KEY", "lNTRtcRh3AIVZfCysWBmO73eoRHnEF0NVyJTYoSx");
headers.add("X-NCP-APIGW-API-KEY-ID", CLOVA_API_KEY_ID);
headers.add("X-NCP-APIGW-API-KEY", CLOVA_API_KEY);

// HTTPBody
Map<String, Object> document = new HashMap<>();
Expand All @@ -46,7 +50,7 @@ public String summary(String content) {
System.out.println("params = " + params);

HttpEntity<Map<String, Object>> summaryTextRequest = new HttpEntity<>(params, headers);
ResponseEntity<String> response = rt.exchange(requestURL, HttpMethod.POST, summaryTextRequest, String.class);
ResponseEntity<String> response = rt.exchange(ENDPOINT, HttpMethod.POST, summaryTextRequest, String.class);

String responseBody = response.getBody();

Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ spring:
ddl-auto: update

# OPEN AI API KEY
GPT_API_KEY: ${GPT_API_KEY}
GPT_API_KEY: ${GPT_API_KEY}
CLOVA_API_KEY_ID: ${CLOVA_API_KEY_ID}
CLOVA_API_KEY: ${CLOVA_API_KEY}

0 comments on commit 696703c

Please sign in to comment.