-
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
11 changed files
with
206 additions
and
40 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
36 changes: 36 additions & 0 deletions
36
src/main/java/com/gdscpknu/gdscpknu/controller/ApiError.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,36 @@ | ||
package com.gdscpknu.gdscpknu.controller; | ||
|
||
import lombok.Getter; | ||
import org.apache.commons.lang3.builder.ToStringBuilder; | ||
import org.apache.commons.lang3.builder.ToStringStyle; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
public class ApiError { | ||
|
||
private final String MESSAGE = "message"; | ||
private final String STATUS = "status"; | ||
private final String msg; | ||
private final int status; | ||
|
||
public ApiError(String msg, int status) { | ||
this.msg = msg; | ||
this.status = status; | ||
} | ||
|
||
public ApiError(String msg, HttpStatus status) { | ||
this(msg, status.value()); | ||
} | ||
|
||
public ApiError(Throwable throwable, HttpStatus status) { | ||
this(throwable.getMessage(), status.value()); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) | ||
.append(MESSAGE, msg) | ||
.append(STATUS, status) | ||
.toString(); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/gdscpknu/gdscpknu/controller/ApiResponse.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,42 @@ | ||
package com.gdscpknu.gdscpknu.controller; | ||
|
||
import lombok.Getter; | ||
import org.apache.commons.lang3.builder.ToStringBuilder; | ||
import org.apache.commons.lang3.builder.ToStringStyle; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
public class ApiResponse<T> { | ||
|
||
private final boolean success; | ||
|
||
private final T response; | ||
|
||
private final ApiError error; | ||
|
||
public ApiResponse(boolean success, T response, ApiError error) { | ||
this.success = success; | ||
this.response = response; | ||
this.error = error; | ||
} | ||
|
||
public static <T> ApiResponse<T> SUCCESS(T response){ | ||
return new ApiResponse<>(true, response, null); | ||
} | ||
|
||
public static <T> ApiResponse<T> ERROR(String msg, HttpStatus status){ | ||
return new ApiResponse<>(false, null, new ApiError(msg, status)); | ||
} | ||
|
||
public static <T> ApiResponse<T> ERROR(Throwable throwable, HttpStatus status){ | ||
return new ApiResponse<>(false, null, new ApiError(throwable, status)); | ||
} | ||
|
||
public String toString() { | ||
return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) | ||
.append("success", success) | ||
.append("response", response) | ||
.append("error", error) | ||
.toString(); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/gdscpknu/gdscpknu/controller/GlobalExceptionHandler.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,32 @@ | ||
package com.gdscpknu.gdscpknu.controller; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import static com.gdscpknu.gdscpknu.controller.ApiResponse.ERROR; | ||
|
||
|
||
@ControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
private final Logger log = LoggerFactory.getLogger(getClass()); | ||
|
||
private ResponseEntity<ApiResponse<?>> newResponse(Throwable throwable, HttpStatus status) { | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.add("Content-Type", "application/json"); | ||
return new ResponseEntity<>(ERROR(throwable, status), headers, status); | ||
} | ||
|
||
@ExceptionHandler({ | ||
IllegalArgumentException.class, | ||
}) | ||
public ResponseEntity<?> handleBadRequestException(Exception e) { | ||
log.debug("Bad request exception occurred: {}", e.getMessage(), e); | ||
return newResponse(e, HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/gdscpknu/gdscpknu/controller/MemberController.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,27 @@ | ||
package com.gdscpknu.gdscpknu.controller; | ||
|
||
import com.gdscpknu.gdscpknu.domain.Member; | ||
import com.gdscpknu.gdscpknu.service.MemberService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/member") | ||
public class MemberController { | ||
|
||
private final MemberService memberService; | ||
|
||
@Autowired | ||
public MemberController(MemberService memberService) { | ||
this.memberService = memberService; | ||
} | ||
|
||
|
||
@GetMapping | ||
public ApiResponse<List<Member>> getProfiles() { | ||
return ApiResponse.SUCCESS(memberService.getAllMember()); | ||
} | ||
} |
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
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 |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
import org.jraf.klibnotion.model.page.Page; | ||
import org.jraf.klibnotion.model.pagination.Pagination; | ||
import org.jraf.klibnotion.model.pagination.ResultPage; | ||
import org.jraf.klibnotion.model.property.sort.PropertySort; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.ArrayList; | ||
|
@@ -36,17 +37,16 @@ public void getTable() { | |
); | ||
|
||
//then | ||
// System.out.println("simpleQueryResultPage = " + simpleQueryResultPage.results); | ||
assertThat(database.getTitle().getPlainText()).isEqualTo(notionTable.getTableName()); | ||
assertThat(simpleQueryResultPage.results.size()).isEqualTo(notionTable.getMemberNum()); | ||
} | ||
|
||
@Test | ||
public void getRowByEmail() { | ||
//given | ||
String name = "가나다"; | ||
String role = "Core"; | ||
String email = "[email protected]"; | ||
String name = "남우진"; | ||
String role = "Lead"; | ||
String email = "[email protected]"; | ||
|
||
//when | ||
ResultPage<Page> simpleQueryResultPage = client.getDatabases().queryDatabase( | ||
|
@@ -73,14 +73,16 @@ public void getRowByEmail() { | |
@Test | ||
void getPlainTexts() { | ||
//given | ||
String name = "가나다"; | ||
String role = "Core"; | ||
String email = "[email protected]"; | ||
String name = "남우진"; | ||
String role = "Lead"; | ||
String email = "[email protected]"; | ||
|
||
// "역할" 기준으로 정렬 | ||
|
||
ResultPage<Page> simpleQueryResultPage = client.getDatabases().queryDatabase( | ||
notionTable.getDATABASE_ID(), | ||
null, | ||
null, | ||
new PropertySort().ascending("역할"), | ||
new Pagination() | ||
); | ||
|
||
|
@@ -89,7 +91,9 @@ void getPlainTexts() { | |
* KlibNotion 라이브러리에서 name, email, role 값을 정규식으로 추출하였음 | ||
*/ | ||
String resultInString = simpleQueryResultPage.results.toString(); | ||
List<String> plainTextList = new ArrayList<>(); | ||
List<String> names = new ArrayList<>(); | ||
List<String> roles = new ArrayList<>(); | ||
List<String> emails = new ArrayList<>(); | ||
// 문자열 중 "(plainText=" ~ "," 범위(이름, 이메일) OR " "name=" ~ "," 범위(역할) 문자열을 추출 | ||
Pattern namePattern = Pattern.compile("(?<=\\(plainText=)(.*?)(?=,)"); | ||
Pattern emailPattern = Pattern.compile("(?<=\\bname=이메일, value=)(.*?)(?=\\))"); | ||
|
@@ -98,20 +102,23 @@ void getPlainTexts() { | |
Matcher emailMatcher = emailPattern.matcher(resultInString); | ||
Matcher roleMatcher = rolePattern.matcher(resultInString); | ||
while (nameMatcher.find()) { | ||
plainTextList.add(nameMatcher.group()); | ||
} | ||
while (emailMatcher.find()) { | ||
plainTextList.add(emailMatcher.group()); | ||
names.add(nameMatcher.group()); | ||
} | ||
while (roleMatcher.find()) { | ||
plainTextList.add(roleMatcher.group()); | ||
roles.add(roleMatcher.group()); | ||
} | ||
while (emailMatcher.find()) { | ||
emails.add(emailMatcher.group()); | ||
} | ||
|
||
|
||
//then | ||
assertThat(plainTextList.size()).isEqualTo(notionTable.getMemberNum() * 3); | ||
assertThat(plainTextList.get(0)).isEqualTo(name); | ||
assertThat(plainTextList.get(3)).isEqualTo(email); | ||
assertThat(plainTextList.get(6)).isEqualTo(role); | ||
assertThat(names.size()).isEqualTo(notionTable.getMemberNum()); | ||
assertThat(roles.size()).isEqualTo(notionTable.getMemberNum()); | ||
assertThat(emails.size()).isEqualTo(notionTable.getMemberNum()); | ||
assertThat(names.get(0)).isEqualTo(name); | ||
assertThat(roles.get(0)).isEqualTo(role); | ||
assertThat(emails.get(0)).isEqualTo(email); | ||
} | ||
|
||
} |
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