66class TaskService :
77 @staticmethod
88 async def create_task (user_id : int , task_name : str , task_status : bool = False , start_time : Optional [datetime ] = None ) -> int :
9- async for session in get_session ():
9+ async with get_session () as session :
1010 result : Any = await session .execute (
1111 text (
1212 """
@@ -25,11 +25,10 @@ async def create_task(user_id: int, task_name: str, task_status: bool = False, s
2525 task_id : int = result .scalar_one ()
2626 await session .commit ()
2727 return task_id
28- return None
2928
3029 @staticmethod
3130 async def get_task_by_id (task_id : int ) -> Optional [dict ]:
32- async for session in get_session ():
31+ async with get_session () as session :
3332 result : Any = await session .execute (
3433 text (
3534 """
@@ -40,7 +39,7 @@ async def get_task_by_id(task_id: int) -> Optional[dict]:
4039 ),
4140 {"task_id" : task_id }
4241 )
43- task : int = result .fetchone ()
42+ task = result .fetchone ()
4443 if task :
4544 return {
4645 "id" : task .id ,
@@ -51,11 +50,10 @@ async def get_task_by_id(task_id: int) -> Optional[dict]:
5150 "creation_date" : task .creation_date
5251 }
5352 return None
54- return None
5553
5654 @staticmethod
5755 async def get_user_tasks (user_id : int ) -> List [dict ]:
58- async for session in get_session ():
56+ async with get_session () as session :
5957 result : Any = await session .execute (
6058 text (
6159 """
@@ -67,6 +65,7 @@ async def get_user_tasks(user_id: int) -> List[dict]:
6765 ),
6866 {"user_id" : user_id }
6967 )
68+ tasks = result .fetchall ()
7069 return [
7170 {
7271 "id" : task .id ,
@@ -76,23 +75,22 @@ async def get_user_tasks(user_id: int) -> List[dict]:
7675 "start_time" : task .start_time ,
7776 "creation_date" : task .creation_date
7877 }
79- for task in result . fetchall ()
78+ for task in tasks
8079 ]
81- return None
8280
8381 @staticmethod
8482 async def update_task (task_id : int , task_name : Optional [str ] = None ,
85- status : Optional [bool ] = None ,
86- start_time : Optional [datetime ] = None ) -> bool :
87- async for session in get_session ():
83+ status : Optional [bool ] = None ,
84+ start_time : Optional [datetime ] = None ) -> bool :
85+ async with get_session () as session :
8886 exists : Any = await session .execute (
8987 text ("SELECT 1 FROM tasks WHERE id = :task_id" ),
9088 {"task_id" : task_id }
9189 )
9290 if not exists .scalar ():
9391 return False
9492
95- update_fields : Any = {}
93+ update_fields : dict = {}
9694 if task_name is not None :
9795 update_fields ["task_name" ] = task_name
9896 if status is not None :
@@ -101,7 +99,7 @@ async def update_task(task_id: int, task_name: Optional[str] = None,
10199 update_fields ["start_time" ] = start_time
102100
103101 if update_fields :
104- query : Any = text (
102+ query = text (
105103 f"""
106104 UPDATE tasks
107105 SET { ', ' .join (f'{ k } = :{ k } ' for k in update_fields .keys ())}
@@ -112,11 +110,10 @@ async def update_task(task_id: int, task_name: Optional[str] = None,
112110 await session .execute (query , update_fields )
113111 await session .commit ()
114112 return True
115- return None
116113
117114 @staticmethod
118115 async def delete_task (task_id : int ) -> bool :
119- async for session in get_session ():
116+ async with get_session () as session :
120117 result : Any = await session .execute (
121118 text (
122119 """
@@ -127,14 +124,13 @@ async def delete_task(task_id: int) -> bool:
127124 ),
128125 {"task_id" : task_id }
129126 )
130- deleted : Any = result .scalar_one_or_none ()
127+ deleted = result .scalar_one_or_none ()
131128 await session .commit ()
132129 return deleted is not None
133- return None
134130
135131 @staticmethod
136132 async def toggle_task_status (task_id : int ) -> bool :
137- async for session in get_session ():
133+ async with get_session () as session :
138134 result : Any = await session .execute (
139135 text (
140136 """
@@ -146,7 +142,6 @@ async def toggle_task_status(task_id: int) -> bool:
146142 ),
147143 {"task_id" : task_id }
148144 )
149- updated : Any = result .scalar_one_or_none ()
145+ updated = result .scalar_one_or_none ()
150146 await session .commit ()
151147 return updated is not None
152- return None
0 commit comments