-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* successfully added data to database, test successful, renamed model file * Added instructor CRUD * minor changes, added parent's name for student * added parents * moved over from test database, added testing * testing change env to repo secrets * hopefully this fixes env error * rectifying secrets issue * let's try again * test print * what about now * so butter fingers today * try harder * let's try another way * let's see what's the error * 6th diff error? * json formatting error? * hmmmmmm * huh * test nth * Fixing github treating {} as secrets try 1 * Fixing github treating {} as secrets try 2 * try with encrypt/decrypt instead * chmod +x using git, will it work? * forgot abt this * silly me * what if chmod in workflow * hmmm identifying new error * changed file path * changed file path in database code, omg workflow finally proceeded * changed 1 path and forgot the others lmaopain * removing potential error to see if that's the error * swapping env code over * try again * lets try this * reverting some changes to see progress * removing testing for test database
- Loading branch information
1 parent
f2b5bfe
commit b8aaf58
Showing
15 changed files
with
608 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,117 +3,236 @@ package main | |
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"log" | ||
"os" | ||
|
||
"github.com/joho/godotenv" | ||
// "github.com/joho/godotenv" | ||
|
||
firebase "firebase.google.com/go" | ||
"firebase.google.com/go/db" | ||
"google.golang.org/api/option" | ||
) | ||
|
||
// use godot package to load/read the .env file and | ||
// Use godot package to load/read the .env file and | ||
// return the value of the key | ||
func goDotEnvVariable(key string) string { | ||
// func goDotEnvVariable(key string) string { | ||
|
||
// load .env file | ||
err := godotenv.Load(".env") | ||
// // load .env file | ||
// err := godotenv.Load(".env") | ||
|
||
// if err != nil { | ||
// log.Fatalf("Error loading .env file") | ||
// } | ||
|
||
// return os.Getenv(key) | ||
// } | ||
|
||
func database() { | ||
// Find home directory. | ||
// home, err := os.Getwd() | ||
// if err != nil { | ||
// return err | ||
// } | ||
|
||
ctx := context.Background() | ||
|
||
// configure database URL | ||
// databaseURL := goDotEnvVariable("DATABASE_URL") | ||
// if databaseURL == "" { | ||
// return fmt.Errorf("DATABASE_URL is not set in the .env file") | ||
// } | ||
databaseURL, found := os.LookupEnv("DATABASE_URL") | ||
if !found { | ||
log.Fatalf("DATABASE_URL is not set in the environment variables") | ||
} | ||
conf := &firebase.Config{DatabaseURL: databaseURL} | ||
|
||
// conf := &firebase.Config{ | ||
// DatabaseURL: "https://edusync-test-default-rtdb.firebaseio.com/", | ||
// } | ||
|
||
// Set up the Firebase app with the provided JSON file containing the service account key. | ||
// opt := option.WithCredentialsFile(home + "edusync-test-firebase-adminsdk-hk5kl-9af0162b09.json") | ||
|
||
// fetch service account key | ||
// opt := option.WithCredentialsFile("edusync-test-firebase-adminsdk-hk5kl-9af0162b09.json") | ||
opt := option.WithCredentialsFile("edusync-7bd5e-firebase-adminsdk-x49uh-af084a6314.json") | ||
// opt := option.WithCredentialsFile("$HOME/secrets/edusync-7bd5e-firebase-adminsdk-x49uh-af084a6314.json") | ||
|
||
app, err := firebase.NewApp(ctx, conf, opt) | ||
if err != nil { | ||
log.Fatalf("Error loading .env file") | ||
log.Fatalln("error in initializing firebase app: ", err) | ||
} | ||
|
||
return os.Getenv(key) | ||
} | ||
client, err := app.Database(ctx) | ||
if err != nil { | ||
log.Fatalln("error in creating firebase DB client: ", err) | ||
} | ||
|
||
type FireDB struct { | ||
*db.Client | ||
} | ||
// Student operations | ||
student := NewStudent("Jane Doe", 7, 119.5, "[email protected]", "91234567", "Tech Explorer", "Scott Smith", "Jackie Doe") | ||
err = createStudent(client, student.ID.String(), student) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Println("Student added/updated successfully!") | ||
|
||
var fireDB FireDB | ||
readStudent, err := readStudent(client, student.ID.String()) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Println("Student read successfully:", readStudent) | ||
|
||
// Connect initializes and connects to the Firebase Realtime Database. | ||
// It uses the provided JSON file containing the service account key to authenticate with Firebase. | ||
// The DatabaseURL is set to the Firebase project's Realtime Database URL. | ||
// The function returns an error if any step fails during the initialization or connection process. | ||
func (db *FireDB) Connect() error { | ||
// Find home directory. | ||
home, err := os.Getwd() | ||
studentUpdates := map[string]interface{}{ | ||
"class": "Tech Explorer 2", | ||
"updated_at": time.Now(), | ||
} | ||
err = updateStudent(client, student.ID.String(), studentUpdates) | ||
if err != nil { | ||
return err | ||
log.Fatal(err) | ||
} | ||
fmt.Println("Student updated successfully!") | ||
|
||
// Create a context for the Firebase app. | ||
ctx := context.Background() | ||
err = deleteStudent(client, student.ID.String()) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Println("Student deleted successfully!") | ||
|
||
// Set up the Firebase app with the provided JSON file containing the service account key. | ||
opt := option.WithCredentialsFile(home + "edusync-firebase.json") | ||
dotenv := goDotEnvVariable("DATABASE_URL") | ||
config := &firebase.Config{DatabaseURL: dotenv} | ||
// Instructor operations | ||
instructor := NewInstructor("Scott Smith", "123-456-7890", "[email protected]", 50000.00, 10) | ||
err = createInstructor(client, instructor.ID.String(), instructor) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Println("Instructor added/updated successfully!") | ||
|
||
app, err := firebase.NewApp(ctx, config, opt) | ||
// opt := option.WithCredentialsFile("edusync-firebase.json") | ||
// app, err := firebase.NewApp(context.Background(), nil, opt) | ||
readInstructor, err := readInstructor(client, instructor.ID.String()) | ||
if err != nil { | ||
return fmt.Errorf("error initializing app: %v", err) | ||
log.Fatal(err) | ||
} | ||
fmt.Println("Instructor read successfully:", readInstructor) | ||
|
||
// Initialize the Firebase Realtime Database client. | ||
client, err := app.Database(ctx) | ||
instructorUpdates := map[string]interface{}{ | ||
"base_pay": 55000.00, | ||
"updated_at": time.Now(), | ||
} | ||
err = updateInstructor(client, instructor.ID.String(), instructorUpdates) | ||
if err != nil { | ||
return fmt.Errorf("error initializing database: %v", err) | ||
log.Fatal(err) | ||
} | ||
fmt.Println("Instructor updated successfully!") | ||
|
||
// Assign the Firebase Realtime Database client to the FireDB struct. | ||
db.Client = client | ||
return nil | ||
} | ||
err = deleteInstructor(client, instructor.ID.String()) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Println("Instructor deleted successfully!") | ||
|
||
func FirebaseDB() *FireDB { | ||
return &fireDB | ||
} | ||
// Parent operations | ||
parent := NewParent("Jackie Doe", "[email protected]", "98765432") | ||
err = createParent(client, parent.ID.String(), parent) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Println("Parent added/updated successfully!") | ||
|
||
func connectToFirebase() error { | ||
fireDB := FirebaseDB() | ||
err := fireDB.Connect() | ||
readParent, err := readParent(client, parent.ID.String()) | ||
if err != nil { | ||
return err | ||
log.Fatal(err) | ||
} | ||
fmt.Println("Parent read successfully:", readParent) | ||
|
||
ref := fireDB.NewRef("/") | ||
err = ref.Set(context.Background(), map[string]string{ | ||
"name": "Jane Doe", | ||
"age": "7", | ||
"class": "Tech Explorer", | ||
"instructor": "Scott Smith", | ||
}) | ||
parentUpdates := map[string]interface{}{ | ||
"email": "[email protected]", | ||
"updated_at": time.Now(), | ||
} | ||
err = updateParent(client, parent.ID.String(), parentUpdates) | ||
if err != nil { | ||
return err | ||
log.Fatal(err) | ||
} | ||
return nil | ||
fmt.Println("Parent updated successfully!") | ||
|
||
err = deleteParent(client, parent.ID.String()) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
fmt.Println("Parent deleted successfully!") | ||
} | ||
|
||
// CRUD operations | ||
func (db *FireDB) Create(refPath string, data interface{}) error { | ||
ref := db.NewRef(refPath) | ||
return ref.Set(context.Background(), data) | ||
// Student CRUD | ||
func createStudent(client *db.Client, userId string, student Student) error { | ||
ref := client.NewRef("students/" + userId) | ||
return ref.Set(context.TODO(), student) | ||
} | ||
|
||
func (db *FireDB) Read(refPath string, dest interface{}) error { | ||
ref := db.NewRef(refPath) | ||
return ref.Get(context.Background(), dest) | ||
func readStudent(client *db.Client, userId string) (Student, error) { | ||
ref := client.NewRef("students/" + userId) | ||
var student Student | ||
if err := ref.Get(context.TODO(), &student); err != nil { | ||
return Student{}, err | ||
} | ||
return student, nil | ||
} | ||
|
||
func (db *FireDB) Update(refPath string, data map[string]interface{}) error { | ||
ref := db.NewRef(refPath) | ||
return ref.Update(context.Background(), data) | ||
func updateStudent(client *db.Client, userId string, updates map[string]interface{}) error { | ||
ref := client.NewRef("students/" + userId) | ||
return ref.Update(context.TODO(), updates) | ||
} | ||
|
||
func (db *FireDB) Delete(refPath string) error { | ||
ref := db.NewRef(refPath) | ||
return ref.Delete(context.Background()) | ||
func deleteStudent(client *db.Client, userId string) error { | ||
ref := client.NewRef("students/" + userId) | ||
return ref.Delete(context.TODO()) | ||
} | ||
|
||
//todo: set up firebase, finish connecting to database | ||
// Instructor CRUD | ||
func createInstructor(client *db.Client, userId string, instructor Instructor) error { | ||
ref := client.NewRef("instructors/" + userId) | ||
return ref.Set(context.TODO(), instructor) | ||
} | ||
|
||
func readInstructor(client *db.Client, userId string) (Instructor, error) { | ||
ref := client.NewRef("instructors/" + userId) | ||
var instructor Instructor | ||
if err := ref.Get(context.TODO(), &instructor); err != nil { | ||
return Instructor{}, err | ||
} | ||
return instructor, nil | ||
} | ||
|
||
func updateInstructor(client *db.Client, userId string, updates map[string]interface{}) error { | ||
ref := client.NewRef("instructors/" + userId) | ||
return ref.Update(context.TODO(), updates) | ||
} | ||
|
||
func deleteInstructor(client *db.Client, userId string) error { | ||
ref := client.NewRef("instructors/" + userId) | ||
return ref.Delete(context.TODO()) | ||
} | ||
|
||
// Parent CRUD | ||
func createParent(client *db.Client, userId string, parent Parent) error { | ||
ref := client.NewRef("parents/" + userId) | ||
return ref.Set(context.TODO(), parent) | ||
} | ||
|
||
func readParent(client *db.Client, userId string) (Parent, error) { | ||
ref := client.NewRef("parents/" + userId) | ||
var parent Parent | ||
if err := ref.Get(context.TODO(), &parent); err != nil { | ||
return Parent{}, err | ||
} | ||
return parent, nil | ||
} | ||
|
||
func updateParent(client *db.Client, userId string, updates map[string]interface{}) error { | ||
ref := client.NewRef("parents/" + userId) | ||
return ref.Update(context.TODO(), updates) | ||
} | ||
|
||
func deleteParent(client *db.Client, userId string) error { | ||
ref := client.NewRef("parents/" + userId) | ||
return ref.Delete(context.TODO()) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.