Skip to content

Commit

Permalink
Fix: 구글독스 로직 중 카프카 task 전달 시 Dto로 연결 Merge
Browse files Browse the repository at this point in the history
Fix: 구글독스 로직 중 카프카 task 전달 시 Dto로 연결
  • Loading branch information
Train0303 authored Nov 14, 2023
2 parents 18ca11a + 857e61f commit c7606e7
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.kakao.linknamu.bookmark.entity.Bookmark;
import com.kakao.linknamu.bookmark.service.BookmarkService;
import com.kakao.linknamu.category.entity.Category;
import com.kakao.linknamu.thirdparty.googledocs.dto.GoogleDocsKafkaRequestDto;
import com.kakao.linknamu.thirdparty.googledocs.entity.GooglePage;
import com.kakao.linknamu.thirdparty.googledocs.util.GoogleDocsProvider;

Expand All @@ -25,8 +27,11 @@ public class GoogleDocsConsumer {

@KafkaListener(topics = {GOOGLE_DOCS_TOPIC}, groupId = "group-id-linknamu")
public void googleDocsConsumer(String message) throws JsonProcessingException {
GooglePage googlePage = om.readValue(message, GooglePage.class);
List<Bookmark> bookmarkList = googleDocsProvider.getLinks(googlePage);
GoogleDocsKafkaRequestDto requestDto = om.readValue(message, GoogleDocsKafkaRequestDto.class);
Category category = Category.builder()
.categoryId(requestDto.categoryId())
.build();
List<Bookmark> bookmarkList = googleDocsProvider.getLinks(requestDto.documentId(), category);
bookmarkService.batchInsertBookmark(bookmarkList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.kakao.linknamu.thirdparty.googledocs.dto;

import lombok.Builder;

public record GoogleDocsKafkaRequestDto(
String documentId,
Long categoryId
) {
@Builder
public GoogleDocsKafkaRequestDto {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void googleDocsApiCronJob() {
// 활성화된 구글 독스 페이지들에 대해 배치를 실행한다.
activeGoogleDocsPages.forEach((GooglePage gp) -> {
try {
List<Bookmark> resultBookmarks = googleDocsProvider.getLinks(gp);
List<Bookmark> resultBookmarks = googleDocsProvider.getLinks(gp.getDocumentId(), gp.getCategory());
bookmarkService.batchInsertBookmark(resultBookmarks);
} catch (InvalidGoogleDocsApiException e) {
gp.deactivate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.kakao.linknamu.core.exception.Exception403;
import com.kakao.linknamu.core.exception.Exception404;
import com.kakao.linknamu.thirdparty.googledocs.GoogleDocsExceptionStatus;
import com.kakao.linknamu.thirdparty.googledocs.dto.GoogleDocsKafkaRequestDto;
import com.kakao.linknamu.thirdparty.googledocs.dto.RegisterGoogleDocsRequestDto;
import com.kakao.linknamu.thirdparty.googledocs.entity.GooglePage;
import com.kakao.linknamu.thirdparty.googledocs.repository.GooglePageJpaRepository;
Expand Down Expand Up @@ -94,10 +95,13 @@ private void validUser(GooglePage googlePage, User user) {
// 초기 구글문서 연동 생성 시 데이터를 가져오는 것을 다른 쓰레드에 위임
private void googleDocsRequestToKafka(GooglePage googlePage) {
try {
String message = om.writeValueAsString(googlePage);
GoogleDocsKafkaRequestDto googleDocsKafkaRequestDto = GoogleDocsKafkaRequestDto.builder()
.documentId(googlePage.getDocumentId())
.categoryId(googlePage.getCategory().getCategoryId())
.build();
CompletableFuture<SendResult<String, String>> future = kafkaTemplate.send(
GOOGLE_DOCS_TOPIC,
message
om.writeValueAsString(googleDocsKafkaRequestDto)
);
} catch (JsonProcessingException ignored) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.google.api.services.docs.v1.model.StructuralElement;
import com.kakao.linknamu.bookmark.entity.Bookmark;
import com.kakao.linknamu.bookmark.service.BookmarkService;
import com.kakao.linknamu.category.entity.Category;
import com.kakao.linknamu.core.config.GoogleDocsConfig;
import com.kakao.linknamu.core.exception.Exception400;
import com.kakao.linknamu.core.exception.Exception500;
Expand Down Expand Up @@ -66,7 +67,7 @@ public String getGoogleDocsTitle(String documentId) {
}
}

public List<Bookmark> getLinks(GooglePage googlePage) {
public List<Bookmark> getLinks(String documentId, Category category) {
Set<Bookmark> resultBookmarks = new HashSet<>();
try {
// 서비스 생성
Expand All @@ -79,7 +80,7 @@ public List<Bookmark> getLinks(GooglePage googlePage) {
.build();

// google docs 객체 생성 및 get API를 사용해서 link 항목 불러오기
Document response = service.documents().get(googlePage.getDocumentId()).execute();
Document response = service.documents().get(documentId).execute();
List<StructuralElement> contents = response.getBody().getContent();
for (StructuralElement e : contents) {
if (e.getParagraph() != null && e.getParagraph().getElements() != null) {
Expand All @@ -91,7 +92,7 @@ public List<Bookmark> getLinks(GooglePage googlePage) {
if (link != null) {
// 만약 한번 연동한 링크라면 더 이상 진행하지 않는다.
if (bookmarkService.existByBookmarkLinkAndCategoryId(link,
googlePage.getCategory().getCategoryId())) {
category.getCategoryId())) {
continue;
}

Expand All @@ -100,7 +101,7 @@ public List<Bookmark> getLinks(GooglePage googlePage) {
.bookmarkLink(link)
.bookmarkName(jsoupResult.getTitle())
.bookmarkThumbnail(jsoupResult.getImageUrl())
.category(googlePage.getCategory())
.category(category)
.build());
}
}
Expand Down

0 comments on commit c7606e7

Please sign in to comment.