File size: 4,570 Bytes
6931ba0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore


class FirebaseClient:
    def __init__(self, path_to_certificate):
        # Initialize Firebase Admin SDK
        cred = credentials.Certificate(path_to_certificate)  # Path to your service account key JSON file
        firebase_admin.initialize_app(cred)

        # Initialize Firestore database
        self.db = firestore.client()

    def add_task(self, task_data):
        """
        Add a new task to Firestore.
        
        Args:
            task_data (dict): Dictionary containing task data.
                Example: {'title': 'Task Title', 'description': 'Task Description', 'status': 'pending'}
        """
        # Add task data to Firestore
        doc_ref = self.db.collection('tasks').document()
        doc_ref.set(task_data)
        return doc_ref.id
    
    def get_task_by_status(self, status):
        # Reference to the tasks collection
        tasks_ref = self.db.collection('tasks')

        # Query tasks with status 'pending'
        query = tasks_ref.where('status', '==', status)

        # Get documents that match the query
        pending_tasks = query.stream()

        # Convert documents to dictionaries
        pending_tasks_data = []
        for doc in pending_tasks:
            task_data = doc.to_dict() 
            task_data['id'] = doc.id
            pending_tasks_data.append(task_data)

        return pending_tasks_data

    def get_all_tasks(self):
        """
        Retrieve all tasks from Firestore.
        
        Returns:
            list: A list containing dictionaries, each representing a task.
        """
        # Reference to the 'tasks' collection
        tasks_ref = self.db.collection('tasks')
        
        # Get all documents in the collection
        docs = tasks_ref.stream()
        
        # Initialize an empty list to store tasks
        tasks = []
        
        # Iterate over each document and add it to the tasks list
        for doc in docs:
            doc_dict = doc.to_dict()
            doc_dict['id'] = doc.id
            tasks.append(doc_dict)
        
        return tasks

    def update(self, task_id, data):
        """
        Reserve a task by a worker.
        
        Args:
            task_id (str): ID of the task to be reserved.
            worker_id (str): ID of the worker reserving the task.
        """
        # Reference to the task document
        task_ref = self.db.collection('tasks').document(task_id)
        
        # Update the task document to indicate it has been reserved by the worker
        task_ref.update(data)
    
    def delete_task(self, task_id):
        """
        Delete a task from Firestore by its ID.
        
        Args:
            task_id (str): ID of the task to be deleted.
        """
        # Reference to the task document
        task_ref = self.db.collection('tasks').document(task_id)
        
        # Delete the task document
        task_ref.delete()
    
    def get_task_by_id(self, task_id):
        """
        Retrieve a task from Firestore by its ID.
        
        Args:
            task_id (str): ID of the task to be retrieved.
        
        Returns:
            dict or None: Dictionary containing the task data if found, None otherwise.
        """
        # Reference to the task document
        task_ref = self.db.collection('tasks').document(task_id)
        
        # Retrieve the task document
        task_doc = task_ref.get()
        
        # Check if the task document exists
        if task_doc.exists:
            return task_doc.to_dict()
        else:
            return None
        
    def find_tasks_by_status(self, status):
        """
        Find all tasks in Firestore with the specified status.
        
        Args:
            status (str): Status value to filter tasks by.
        
        Returns:
            list: List of dictionaries containing task data.
        """
        # Reference to the 'tasks' collection
        tasks_ref = self.db.collection('tasks')
        
        # Query tasks with the specified status
        query = tasks_ref.where('status', '==', status)
        
        # Get documents that match the query
        docs = query.stream()
        
        # Initialize an empty list to store tasks
        tasks = []
        
        # Iterate over each document and add it to the tasks list
        for doc in docs:
            task = doc.to_dict()
            task['id'] = doc.id
            tasks.append(task)
        
        return tasks