|
1 |
| -package main |
| 1 | +package utils |
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "cloud.google.com/go/firestore" |
5 | 4 | "context"
|
6 |
| - firebase "firebase.google.com/go/v4" |
7 |
| - "fmt" |
8 |
| - "google.golang.org/api/iterator" |
9 |
| - "google.golang.org/api/option" |
10 | 5 | "log"
|
11 | 6 | "question-service/models"
|
12 | 7 | "time"
|
| 8 | + |
| 9 | + "cloud.google.com/go/firestore" |
| 10 | + "google.golang.org/api/iterator" |
13 | 11 | )
|
14 | 12 |
|
15 | 13 | // PopulateSampleQuestionsInTransaction deletes all existing questions and then adds new ones in a single transaction
|
@@ -295,32 +293,11 @@ Return the result table in any order.`,
|
295 | 293 | })
|
296 | 294 | }
|
297 | 295 |
|
298 |
| -// initFirestore initializes the Firestore client |
299 |
| -func initFirestore(ctx context.Context, credentialsPath string) (*firestore.Client, error) { |
300 |
| - opt := option.WithCredentialsFile(credentialsPath) |
301 |
| - app, err := firebase.NewApp(ctx, nil, opt) |
302 |
| - if err != nil { |
303 |
| - return nil, fmt.Errorf("failed to initialize Firebase App: %v", err) |
304 |
| - } |
305 |
| - |
306 |
| - client, err := app.Firestore(ctx) |
307 |
| - if err != nil { |
308 |
| - return nil, fmt.Errorf("failed to get Firestore client: %v", err) |
309 |
| - } |
310 |
| - return client, nil |
311 |
| -} |
312 |
| - |
313 |
| -func main() { |
314 |
| - // Initialize Firestore client |
| 296 | +func Populate(client *firestore.Client) { |
315 | 297 | ctx := context.Background()
|
316 |
| - client, err := initFirestore(ctx, "cs3219-g24-firebase-adminsdk-9cm7h-b1675603ab.json") |
317 |
| - if err != nil { |
318 |
| - log.Fatalf("Failed to initialize Firestore client: %v", err) |
319 |
| - } |
320 |
| - defer client.Close() |
321 | 298 |
|
322 | 299 | // Run the transaction to delete all questions and add new ones
|
323 |
| - err = populateSampleQuestionsInTransaction(ctx, client) |
| 300 | + err := populateSampleQuestionsInTransaction(ctx, client) |
324 | 301 | if err != nil {
|
325 | 302 | log.Fatalf("Failed to populate sample questions in transaction: %v", err)
|
326 | 303 | }
|
|
0 commit comments