Skip to content

Commit

Permalink
feat: Firebase Integration
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSilentSage committed Feb 25, 2024
0 parents commit f6b2d5e
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 0 deletions.
39 changes: 39 additions & 0 deletions api/pkg/firebase/firebaseSvc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package firebase

import (
"capAble-scrapper/pkg/objects"
"context"
"log"

"cloud.google.com/go/firestore"
firebase "firebase.google.com/go"

"google.golang.org/api/option"
)

type FireBaseSvcImpl struct {
FirestoreClient *firestore.Client
}

type FireBaseSvc interface {
WriteToFirebase(data []objects.Opportunity) error
ReadFromFirebase() error
}

func NewService() FireBaseSvc {
opt := option.WithCredentialsFile(".firebase-cred.json")
app, err := firebase.NewApp(context.Background(), nil, opt)
if err != nil {
log.Fatalf("error initializing app: %v", err)
return nil
}
firestoreClient, err := app.Firestore(context.Background())
if err != nil {
log.Fatalf("error initializing firestore: %v", err)
return nil
}

return &FireBaseSvcImpl{
FirestoreClient: firestoreClient,
}
}
5 changes: 5 additions & 0 deletions api/pkg/firebase/readFirebase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package firebase

func (fb *FireBaseSvcImpl) ReadFromFirebase() error {
return nil
}
38 changes: 38 additions & 0 deletions api/pkg/firebase/writeFirebase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package firebase

import (
"capAble-scrapper/pkg/crypto"
"capAble-scrapper/pkg/objects"
"context"
"fmt"
)

func (fb *FireBaseSvcImpl) WriteToFirebase(data []objects.Opportunity) error {
collectionName := "opportunities"

batch := fb.FirestoreClient.BulkWriter(context.Background())
opportunitiesCollection := fb.FirestoreClient.Collection(collectionName)

fmt.Printf("Received %d\n", len(data))
for count, obj := range data {
if obj.Company == "" {
continue
}
docID, err := crypto.HashObject(obj)
if err != nil {
return err
}
docRef := opportunitiesCollection.Doc(docID)
_, err = batch.Create(docRef, obj)
if err != nil {
return err
}

fmt.Printf("Writing Object: %d\n", count)
}

batch.End()
fmt.Println("Objects written to Firestore.")

return nil
}
22 changes: 22 additions & 0 deletions database/connection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package database

import (
"context"
"fmt"

"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)

func Connection() *mongo.Client {
options := options.Client().ApplyURI("mongodb://user:password@localhost:27017")
client, err := mongo.Connect(context.TODO(), options)
if err != nil {
fmt.Println(err)
}
if err := client.Ping(context.TODO(), readpref.Primary()); err != nil {
panic(err)
}
return client
}
17 changes: 17 additions & 0 deletions pkg/crypto/hashObject.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package crypto

import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
)

func HashObject(object interface{}) (string, error) {
hasher := sha256.New()
serializedObject, err := json.Marshal(object)
if err != nil {
return "", err
}
hasher.Write(serializedObject)
return hex.EncodeToString(hasher.Sum(nil)), nil
}
20 changes: 20 additions & 0 deletions pkg/objects/opportunity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package objects

import "time"

type Opportunity struct {
Company string
CompanyLogo string
CompanyURL string
JobPostingUrl string
Location string
Industry string
Role string
Description string
Experience string
SeniorityLevel string
EmploymentType string
Job string
Pay string
Posted time.Time
}

0 comments on commit f6b2d5e

Please sign in to comment.