jseop-lim / anam-earth-be-fastapi Goto Github PK
View Code? Open in Web Editor NEWRefactor anam-earth backend
Refactor anam-earth backend
list[Node] vs. set[Node]
Node.edges 원소를 추가할 때 다른 노드에 대한 중복성 검사는 수행하므로 유일성은 보장된다.
__eq__()
를 정의하고 edges는 비교 대상에서 제외해주어야 한다.__hash__()
함수를 정의해야 한다.tuple[Node, Node] vs. set[Node]
Edge.nodes 초기화할 때 원소 중복성 검사는 수행하므로 두 원소의 상이함은 보장된다.
__eq__()
를 오버라이딩해야 한다.__hash__()
함수를 정의해야 한다.tag?
class UpdateNodeUseCase(PartialUpdateNodeInputBoundary):
def __init__(self, node_repo: NodeRepository) -> None:
self.node_repo = node_repo
def execute(self, input_data: PartialUpdateNodeInputData) -> None:
old_node: Node = self.node_repo.get_node_by_id(node_id=input_data.id)
new_node = Node(
id=input_data.id,
name=input_data.name or old_node.name,
point=Point(
longitude=input_data.longitude or old_node.point.longitude,
latitude=input_data.latitude or old_node.point.latitude,
),
)
if old_node != new_node:
self.node_repo.update_node(node=new_node)
class UpdateNodeUseCase(UpdateNodeInputBoundary):
def __init__(self, node_repo: NodeRepository) -> None:
self.node_repo = node_repo
def execute(self, input_data: UpdateNodeInputData) -> None:
node: Node = self.node_repo.get_node_by_id(node_id=input_data.id)
if input_data.name:
node.update_name(name=input_data.name)
if input_data.longitude or input_data.latitude:
node.update_point(
point=Point(
longitude=input_data.longitude or node.point.longitude,
latitude=input_data.latitude or node.point.latitude,
),
)
self.node_repo.update_node(node=node)
노드 삭제 레포지토리 구현체에서 cascade 속성을 미리 설정해놓고 실행.
혹은 해당 노드 행을 외래키로 참조하는 간선 테이블의 행을 함께 삭제하는 동작 실행
레포지토리를 통해 DB에서 해당 노드 엔티티를 참조하는 간선 엔티티 객체들을 반환
레포지토리를 통해 DB에서 해당 간선 엔티티에 대응하는 간선 엔티티의 행을 삭제
필요한가?
필요하다면 어디서 생성하는가 - 도메인? 인프라?
Git 부가기능 활용하기
파이썬 정적 도구 활용하기
파이썬 관련 도구 활용하기
프로그래밍 기타
인프라 신기술 연습
관련 이슈: #29
간선과 노드는 하나의 aggregate에 속한다. aggregate root는 노드이다. 따라서 use case에서는 간선 엔티티를 import하여 객체를 생성하고 접근할 수 없다.
노드 레포지토리에 간선 목록 조회 메서드를 추가할 것인지 결정한다. 간선 목록 유스케이스에서 간선을 가져오는 방법이 달라진다.
노드 레포지토리에 이미 정의된 get_all_nodes() 메서드를 유스케이스에서 사용한다.
노드 레포지토리에 메서드 get_all_edges() 메서드를 정의하고, 유스케이스에서 사용한다.
입력: 노드 ID 두 개
id에 대응하는 Node 객체가 저장소에 존재하지 않을 때 repository 메서드의 처리
@pytest.fixture()
def mock_node_repo() -> mock.Mock:
mock_node_repo: mock.Mock = mock.Mock(spec_set=NodeRepository)
mock_node_repo.get_all_nodes.return_value = [
Node(name='A', point=Point(longitude=Decimal('1.0'), latitude=Decimal('2.0'))),
Node(name='B', point=Point(longitude=Decimal('3.0'), latitude=Decimal('4.0'))),
]
return mock_node_repo
@pytest.fixture()
def mock_list_nodes_presenter() -> mock.Mock:
return mock.Mock(spec_set=ListNodesOutputBoundary)
def test_list_nodes(
mock_node_repo: mock.Mock,
mock_list_nodes_presenter: mock.Mock,
) -> None:
ListNodesUseCase(
node_repo=mock_node_repo,
output_boundary=mock_list_nodes_presenter,
).execute()
assert mock_node_repo.get_all_nodes.called
assert mock_list_nodes_presenter.present.call_args_list == [
mock.call(
output_data_list=[
ListNodesOutputData(name='A', longitude=Decimal('1.0'), latitude=Decimal('2.0')),
ListNodesOutputData(name='B', longitude=Decimal('3.0'), latitude=Decimal('4.0')),
],
),
]
환경변수, 레포지토리 주입 방식 고민
현재는 .env 파일을 불러온다.
local, develop, prod 등 분리하고, 의존성 주입 구조 만들기
DB URL, AWS KEY 등 민감 정보는 .env/.yaml/.json 등 Git에 기록되지 않는 별도 파일에 보관한다.
이러한 파일로부터 불러온 값을 dependency_injector Container의 필드로 등록할 필요가 있을까?
API에서 응답 명세의 표현?
응답에 필요한 데이터의 묶음?
Django나 FastAPI 같은 웹 프레임워크에 종속적인가?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.