Skip to content

Commit

Permalink
refactoring the firebase and database
Browse files Browse the repository at this point in the history
  • Loading branch information
bhattaraibishal50 committed Aug 29, 2020
1 parent 9a12b33 commit b94be3d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 33 deletions.
15 changes: 5 additions & 10 deletions common/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@ import (
"github.com/jinzhu/gorm"
)

/*
Database struct
@description
- Database has attribute DB which is equvalent to grom.DB
- Struct for gorm DB
*/
type Database struct {
*gorm.DB
}

// DB database
var DB *gorm.DB

Expand All @@ -37,3 +27,8 @@ func DbConnection() *gorm.DB {
func MigrateDatabase(db *gorm.DB) {
db.AutoMigrate()
}

// GetDatabase returns the db var
func GetDatabase() *gorm.DB {
return DB
}
49 changes: 47 additions & 2 deletions common/firebase.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,59 @@
package common

import (
"log"
"path/filepath"

"firebase.google.com/go/auth"

"cloud.google.com/go/firestore"
firebase "firebase.google.com/go"
"golang.org/x/net/context"
"google.golang.org/api/option"
)

// InitFirebase - Opening a database and save the reference to `Database` struct.
// Firebase interface
type Firebase interface {
GetFirestore() *firestore.Client
GetFirebaseAuth() *auth.Client
}

type fb struct{}

var ctx = context.Background()

// NewFirebaseApp returns Firebase struct implemnting firebase interface
func NewFirebaseApp() Firebase {
return &fb{}
}

// GetFirestore - returns firestore.Client
func (*fb) GetFirestore() *firestore.Client {
app := InitFirebase()
client, err := app.Firestore(ctx)
if err != nil {
log.Printf("An error has occurred on setting client: %s", err)
}
return client
}

// GetFirebaseAuth - returns auth.Client
func (*fb) GetFirebaseAuth() *auth.Client {
app := InitFirebase()
auth, err := app.Auth(ctx)
if err != nil {
log.Printf("An error has occurred on setting client: %s", err)
}
return auth
}

// InitFirebase - initlise the firebase & returns firebase.app
func InitFirebase() *firebase.App {
opt := option.WithCredentialsFile("/home/bishal/go/src/github/bhattaraibishal50/blog/serviceAccountKey.json")
serviceAccountKeyFilePath, err := filepath.Abs("./serviceAccountKey.json") //shows the full path
if err != nil {
panic("Unable to load serviceAccountKey.json file")
}
opt := option.WithCredentialsFile(serviceAccountKeyFilePath)
config := &firebase.Config{ProjectID: "blog-goland"}
app, err := firebase.NewApp(context.Background(), config, opt)
if err != nil {
Expand Down
29 changes: 8 additions & 21 deletions post/repo/postRepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package repo

import (
"context"
"fmt"
"github/bhattaraibishal50/blog/common"
"github/bhattaraibishal50/blog/post/model"
"log"
Expand All @@ -16,25 +15,22 @@ type PostRepository interface {
FindAll() []*model.Post
}

const collectionName string = "posts"

type firebaseRepo struct{}

var app = common.NewFirebaseApp()
var client = app.GetFirestore()
var ctx = context.Background()

const collectionName string = "posts"

// NewFirebasePostRepository constructor returns the postRepositoory
func NewFirebasePostRepository() PostRepository {
return &firebaseRepo{}
}

// save function
func (r *firebaseRepo) Save(post *model.Post) *model.Post {
app := common.InitFirebase()
ctx := context.Background()
client, err := app.Firestore(ctx)
defer client.Close()
if err != nil {
log.Printf("An error has occurred on setting client: %s", err)
}
_, err = client.Collection(collectionName).Doc(post.ID).Set(ctx, map[string]interface{}{
_, err := client.Collection(collectionName).Doc(post.ID).Set(ctx, map[string]interface{}{
"ID": post.ID,
"Title": post.Title,
"Description": post.Description,
Expand All @@ -48,13 +44,6 @@ func (r *firebaseRepo) Save(post *model.Post) *model.Post {

// find all function
func (r *firebaseRepo) FindAll() []*model.Post {
app := common.InitFirebase()
ctx := context.Background()
client, err := app.Firestore(ctx)
defer client.Close()
if err != nil {
log.Printf("An error has occurred on setting client: %s", err)
}
var posts []*model.Post
iter := client.Collection(collectionName).Documents(ctx)
for {
Expand All @@ -65,10 +54,8 @@ func (r *firebaseRepo) FindAll() []*model.Post {
if err != nil {
log.Fatalf("Failed to iterate: %v", err)
}

fmt.Println("single loop ::", doc.Data())
var post model.Post
doc.DataTo(&post)
doc.DataTo(&post) //map to the post struct directory
posts = append(posts, &post)
}
return posts
Expand Down
Binary file modified tmp/runner-build
Binary file not shown.

0 comments on commit b94be3d

Please sign in to comment.