@@ -32,41 +32,41 @@ def __call__(self, flow: Flow) -> None:
3232 Args:
3333 flow (Flow): the flow to be added.
3434 """
35- self . check_flow_already_exists (flow )
35+ check_flow_already_exists (flow , self . _flow_repository . get_all () )
3636
3737 self ._flow_repository .add (flow )
3838
39- def check_flow_already_exists (self , flow : Flow ) -> None :
40- if not self .is_flow_name_valid (flow .name ):
41- raise FlowAlreadyExists (
42- f"A flow with the name { flow .name } already exists. "
43- "Choose another name."
44- )
45- if not self .is_flow_id_valid (flow .id ):
46- raise FlowIdAlreadyExists (f"A flow with id { flow .id } already exists." )
47-
48- if self .flow_with_same_start_end_section_exists (flow ):
49- raise FlowAlreadyExists (
50- "Flow with same start and end section already exists."
51- )
52-
53- def flow_with_same_start_end_section_exists (self , flow : Flow ) -> bool :
54- existing_flows = self ._flow_repository .get_all ()
55- for existing_flow in existing_flows :
56- if existing_flow .start == flow .start and existing_flow .end == flow .end :
57- return True
58- return False
5939
60- def is_flow_name_valid (self , flow_name : str ) -> bool :
61- if not flow_name :
62- return False
63- return all (
64- stored_flow .name != flow_name
65- for stored_flow in self ._flow_repository .get_all ()
40+ def check_flow_already_exists (given : Flow , existing_flows : Iterable [Flow ]) -> None :
41+ if not is_flow_name_valid (given .name , existing_flows ):
42+ raise FlowAlreadyExists (
43+ f"A flow with the name { given .name } already exists. " "Choose another name."
6644 )
45+ existing_flow_ids = {flow .id for flow in existing_flows }
46+ if not is_flow_id_valid (given .id , existing_flow_ids ):
47+ raise FlowIdAlreadyExists (f"A flow with id { given .id } already exists." )
48+
49+ if flow_with_same_start_end_section_exists (given , existing_flows ):
50+ raise FlowAlreadyExists ("Flow with same start and end section already exists." )
51+
52+
53+ def flow_with_same_start_end_section_exists (
54+ given : Flow , existing_flows : Iterable [Flow ]
55+ ) -> bool :
56+ for existing_flow in existing_flows :
57+ if existing_flow .start == given .start and existing_flow .end == given .end :
58+ return True
59+ return False
60+
61+
62+ def is_flow_name_valid (flow_name : str , existing_flows : Iterable [Flow ]) -> bool :
63+ if not flow_name :
64+ return False
65+ return all (stored_flow .name != flow_name for stored_flow in existing_flows )
66+
6767
68- def is_flow_id_valid (self , flow_id : FlowId ) -> bool :
69- return not ( flow_id in self . _flow_repository . get_flow_ids ())
68+ def is_flow_id_valid (given : FlowId , existing_ids : Iterable [ FlowId ] ) -> bool :
69+ return given not in existing_ids
7070
7171
7272class ClearAllFlows :
@@ -93,9 +93,24 @@ def get(self) -> list[Flow]:
9393
9494
9595class AddAllFlows :
96- def __init__ (self , add_flow : AddFlow ) -> None :
97- self ._add_flow = add_flow
96+ def __init__ (self , flow_repository : FlowRepository ) -> None :
97+ self ._flow_repository = flow_repository
9898
9999 def add (self , flows : Iterable [Flow ]) -> None :
100- for flow in flows :
101- self ._add_flow (flow )
100+ flow_list = list (flows )
101+
102+ if not flow_list :
103+ return
104+
105+ if not flows_are_unique (flow_list ):
106+ raise FlowAlreadyExists ("Flows to be added are not unique." )
107+
108+ existing_flows = self ._flow_repository .get_all ()
109+ for flow in flow_list :
110+ check_flow_already_exists (flow , existing_flows )
111+
112+ self ._flow_repository .add_all (flow_list )
113+
114+
115+ def flows_are_unique (flows : list [Flow ]) -> bool :
116+ return len (flows ) == len (set (flows ))
0 commit comments