-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Feature/#6 Notion Database API 서비스 코드 작성
- Loading branch information
Showing
5 changed files
with
181 additions
and
13 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
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.gdscpknu.gdscpknu.domain; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class Member { | ||
private String name; | ||
private String email; | ||
private String role; | ||
|
||
@Builder | ||
public Member(String name, String email, String role) { | ||
this.name = name; | ||
this.email = email; | ||
this.role = role; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/com/gdscpknu/gdscpknu/notion/GdscNotion.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,44 @@ | ||
package com.gdscpknu.gdscpknu.notion; | ||
|
||
import org.jraf.klibnotion.client.Authentication; | ||
import org.jraf.klibnotion.client.ClientConfiguration; | ||
import org.jraf.klibnotion.client.NotionClient; | ||
import org.jraf.klibnotion.client.blocking.BlockingNotionClient; | ||
import org.jraf.klibnotion.client.blocking.BlockingNotionClientUtils; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class GdscNotion { | ||
|
||
@Value("${testNotion.introduce.token}") | ||
private String TOKEN; | ||
@Value("${testNotion.introduce.databaseId}") | ||
private String DATABASE_ID; | ||
private final BlockingNotionClient client; | ||
|
||
public GdscNotion(BlockingNotionClient client) { | ||
this.client = initClient(); | ||
} | ||
|
||
public BlockingNotionClient initClient() { | ||
NotionClient notionClient = NotionClient.newInstance( | ||
new ClientConfiguration( | ||
new Authentication(TOKEN) | ||
) | ||
); | ||
return BlockingNotionClientUtils.asBlockingNotionClient(notionClient); | ||
} | ||
|
||
public BlockingNotionClient getClient() { | ||
return client; | ||
} | ||
|
||
public String getTOKEN() { | ||
return TOKEN; | ||
} | ||
|
||
public String getDATABASE_ID() { | ||
return DATABASE_ID; | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
src/main/java/com/gdscpknu/gdscpknu/service/MemberService.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,97 @@ | ||
package com.gdscpknu.gdscpknu.service; | ||
|
||
import com.gdscpknu.gdscpknu.domain.Member; | ||
import com.gdscpknu.gdscpknu.notion.GdscNotion; | ||
import org.jraf.klibnotion.client.blocking.BlockingNotionClient; | ||
import org.jraf.klibnotion.model.page.Page; | ||
import org.jraf.klibnotion.model.pagination.Pagination; | ||
import org.jraf.klibnotion.model.pagination.ResultPage; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
@Service | ||
public class MemberService { | ||
|
||
private final GdscNotion gdscNotion; | ||
private final BlockingNotionClient client; | ||
private List<Member> members; | ||
private List<String> nameList; | ||
private List<String> emailList; | ||
private List<String> roleList; | ||
private ResultPage<Page> memberIntroPage; | ||
|
||
@Autowired | ||
public MemberService(GdscNotion gdscNotion) { | ||
this.gdscNotion = gdscNotion; | ||
client = gdscNotion.getClient(); | ||
} | ||
|
||
public List<Member> getAllMember() { | ||
members = new ArrayList<>(); | ||
nameList = new ArrayList<>(); | ||
emailList = new ArrayList<>(); | ||
roleList = new ArrayList<>(); | ||
|
||
getMemberIntroPage(); | ||
|
||
/** | ||
* KlibNotion 라이브러리에서 name, email, role 값을 정규식으로 추출하였음 | ||
*/ | ||
String resultInString = memberIntroPage.results.toString(); | ||
extractName(resultInString); | ||
extractEmail(resultInString); | ||
extractRole(resultInString); | ||
addMember(); | ||
|
||
return members; | ||
} | ||
|
||
private void getMemberIntroPage() { | ||
memberIntroPage = client.getDatabases().queryDatabase( | ||
gdscNotion.getDATABASE_ID(), | ||
null, | ||
null, | ||
new Pagination() | ||
); | ||
} | ||
|
||
private void extractName(String resultInString) { | ||
Pattern namePattern = Pattern.compile("(?<=\\(plainText=)(.*?)(?=,)"); | ||
Matcher nameMatcher = namePattern.matcher(resultInString); | ||
while (nameMatcher.find()) { | ||
nameList.add(nameMatcher.group()); | ||
} | ||
} | ||
|
||
private void extractEmail(String resultInString) { | ||
Pattern emailPattern = Pattern.compile("(?<=\\bname=이메일, value=)(.*?)(?=\\))"); | ||
Matcher emailMatcher = emailPattern.matcher(resultInString); | ||
while (emailMatcher.find()) { | ||
emailList.add(emailMatcher.group()); | ||
} | ||
} | ||
|
||
private void extractRole(String resultInString) { | ||
Pattern rolePattern = Pattern.compile("(?<=\\bname=역할, value=SelectOptionImpl\\(name=)(.*?)(?=,)"); | ||
Matcher roleMatcher = rolePattern.matcher(resultInString); | ||
while (roleMatcher.find()) { | ||
roleList.add(roleMatcher.group()); | ||
} | ||
} | ||
|
||
private void addMember() { | ||
for (int i = 0; i < nameList.size(); i++) { | ||
Member member = Member.builder() | ||
.name(nameList.get(i)) | ||
.email(emailList.get(i)) | ||
.role(roleList.get(i)) | ||
.build(); | ||
members.add(member); | ||
} | ||
} | ||
} |
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