Skip to content

Commit 1c45687

Browse files
feat: add method to retrieve multiple tasks by their IDs in a single query (#223)
- Implemented `get_by_ids` method in `TaskRepository` to fetch multiple tasks based on a list of IDs, returning only existing tasks. - This enhancement improves efficiency by allowing batch retrieval of tasks from the database, streamlining task management operations. Co-authored-by: Amit Prakash <[email protected]>
1 parent efc4c8f commit 1c45687

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

todo/repositories/task_repository.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,16 @@ def get_tasks_for_user(cls, user_id: str, page: int, limit: int) -> List[TaskMod
214214
query = {"_id": {"$in": assigned_task_ids}}
215215
tasks_cursor = tasks_collection.find(query).skip((page - 1) * limit).limit(limit)
216216
return [TaskModel(**task) for task in tasks_cursor]
217+
218+
@classmethod
219+
def get_by_ids(cls, task_ids: List[str]) -> List[TaskModel]:
220+
"""
221+
Get multiple tasks by their IDs in a single database query.
222+
Returns only the tasks that exist.
223+
"""
224+
if not task_ids:
225+
return []
226+
tasks_collection = cls.get_collection()
227+
object_ids = [ObjectId(task_id) for task_id in task_ids]
228+
cursor = tasks_collection.find({"_id": {"$in": object_ids}})
229+
return [TaskModel(**doc) for doc in cursor]

0 commit comments

Comments
 (0)