-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from ani-hovhannisyan/feature/search-controller
create search controller
- Loading branch information
Showing
12 changed files
with
208 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[flake8] | ||
max-line-length=150 |
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 |
---|---|---|
@@ -1,4 +1,9 @@ | ||
{ | ||
"prettier.configPath": "./frontend/.prettierrc.json", | ||
"prettier.ignorePath": "./frontend/.gitignore", | ||
"python.analysis.extraPaths": [ | ||
"./backend/controller" | ||
], | ||
"python.linting.flake8Enabled": true, | ||
"python.linting.enabled": true, | ||
} |
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,10 @@ | ||
class InfoController: | ||
@staticmethod | ||
def get_kanji_info(kanji: str): | ||
# TODO: return acutual value | ||
try: | ||
kanji_info = {"onyomi": "さん", "detail": "やま"} | ||
return [True, None, kanji_info] | ||
except Exception: | ||
error_info = {"status_code": 400, "detail": "山 is not in data base"} | ||
return [False, error_info, None] |
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,37 @@ | ||
import re | ||
|
||
|
||
class SearchController: | ||
@staticmethod | ||
def check_input(kanji): | ||
print(len(kanji)) | ||
|
||
if kanji == "": | ||
return SearchController._create_result( | ||
False, 400, "Please include Kanji in the query parameters" | ||
) | ||
elif not SearchController._is_kanji(kanji): | ||
return SearchController._create_result( | ||
False, 400, "Only Kanji is permitted" | ||
) | ||
elif len(kanji) != 1: | ||
return SearchController._create_result( | ||
False, 400, "Only one character is allowed" | ||
) | ||
|
||
else: | ||
return SearchController._create_result(True) | ||
|
||
@staticmethod | ||
def _create_result(is_success: bool, status: int = None, detail: str = None): | ||
error_info = None if is_success else {"status_code": status, "detail": detail} | ||
|
||
return [is_success, error_info] | ||
|
||
@staticmethod | ||
def _is_kanji(kanji): | ||
p = re.compile( | ||
"[\u2E80-\u2FDF\u3005-\u3007\u3400-\u4DBF" | ||
"\u4E00-\u9FFF\uF900-\uFAFF\U00020000-\U0002EBEF]+" | ||
) | ||
return p.fullmatch(kanji) |
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,27 @@ | ||
from controller.SearchController import SearchController | ||
|
||
|
||
def test_normal(): | ||
is_success, error_info = SearchController.check_input("山") | ||
assert is_success | ||
|
||
|
||
def test_invalid1(): | ||
is_success, error_info = SearchController.check_input("a") | ||
assert not is_success | ||
assert error_info == {"status_code": 400, "detail": "Only Kanji is permitted"} | ||
|
||
|
||
def test_invalid2(): | ||
is_success, error_info = SearchController.check_input("") | ||
assert not is_success | ||
assert error_info == { | ||
"status_code": 400, | ||
"detail": "Please include Kanji in the query parameters", | ||
} | ||
|
||
|
||
def test_invalid3(): | ||
is_success, error_info = SearchController.check_input("登山") | ||
assert not is_success | ||
assert error_info == {"status_code": 400, "detail": "Only one character is allowed"} |
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import React, { useState } from "react"; | ||
import axios from "axios"; | ||
import { GraphType, KanjiType } from "../App"; | ||
|
||
type Props = { | ||
setKanji: React.Dispatch<React.SetStateAction<KanjiType | undefined>>; | ||
setGraph: React.Dispatch<React.SetStateAction<GraphType | undefined>>; | ||
}; | ||
|
||
const regexp = | ||
/([\u{3005}\u{3007}\u{303b}\u{3400}-\u{9FFF}\u{F900}-\u{FAFF}\u{20000}-\u{2FFFF}][\u{E0100}-\u{E01EF}\u{FE00}-\u{FE02}]?)/mu; | ||
const SearchField = (props: Props) => { | ||
const [kanjiInput, setKanjiInput] = useState<string>(""); | ||
const [error, setError] = useState<string>("Fill form"); | ||
|
||
const handleKanjiChange = (event: { target: { value: string } }) => { | ||
const input = event.target.value; | ||
if (typeof input != "string") { | ||
setError("Invalid Input"); | ||
} else if (input.length !== 1) { | ||
setError("Only one character is allowed"); | ||
} else if (!regexp.test(input)) { | ||
setError("Input Only Kanji"); | ||
} else { | ||
setError(""); | ||
} | ||
setKanjiInput(input); | ||
}; | ||
|
||
const handleSubmit = () => { | ||
axios | ||
.get( | ||
`${process.env.REACT_APP_API_URL}:${process.env.REACT_APP_API_PORT}/kanji-visualize?kanji=${kanjiInput}` | ||
) | ||
.then((res) => { | ||
const { data, status } = res; | ||
console.log(status); | ||
props.setKanji(data.kanjiInfo); | ||
props.setGraph(data.graphMatrix); | ||
}) | ||
.catch((error) => { | ||
console.log(error.response); | ||
setError(error.response.data.detail); | ||
}); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<h2>Input Kanji</h2> | ||
<div style={{ display: "flex" }}> | ||
<input type="text" value={kanjiInput} onChange={handleKanjiChange} /> | ||
<button type="submit" onClick={handleSubmit} disabled={!!error}> | ||
Search | ||
</button> | ||
</div> | ||
<div>{error}</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default SearchField; |
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