diff --git a/authHandler.go b/authHandler.go index 0bad972..ed4db95 100644 --- a/authHandler.go +++ b/authHandler.go @@ -12,6 +12,58 @@ import ( "github.com/markbates/goth/providers/google" ) +// AuthHandler sets up the authentication routes on the provided router +// func AuthHandler(router *pat.Router, config *Config) { +// maxAge := 86400 * 30 // 30 days +// isProd := true // Set to true when serving over https + +// store := sessions.NewCookieStore([]byte(config.SessionSecret)) +// store.MaxAge(maxAge) +// store.Options.Path = "/" +// store.Options.HttpOnly = true // HttpOnly should always be enabled +// store.Options.Secure = isProd + +// gothic.Store = store + +// goth.UseProviders( +// google.New(config.GoogleClientID, config.GoogleClientSecret, "https://localhost:8080/auth/google/callback", "email", "profile"), +// ) + +// router.Get("/auth/{provider}/callback", func(res http.ResponseWriter, req *http.Request) { +// user, err := gothic.CompleteUserAuth(res, req) +// if err != nil { +// fmt.Fprintln(res, err) +// return +// } + +// // // Extract user details from the goth.User +// // googleID := user.UserID +// // name := user.Name +// // email := user.Email + +// // // Example: Create or update a student with the extracted details +// // student := NewStudent(googleID, name, 18, 10.0, email, "91234567", "TE", "Mr. Smith", "Mrs. Doe", "Student") +// // err = createStudent(student.GoogleID, student) +// // if err != nil { +// // log.Println("Error creating student:", err) +// // } else { +// // log.Println("Student created/updated successfully!") +// // } + +// t, _ := template.ParseFiles("templates/success.html") +// t.Execute(res, user) +// }) + +// router.Get("/auth/{provider}", func(res http.ResponseWriter, req *http.Request) { +// gothic.BeginAuthHandler(res, req) +// }) + +// router.Get("/", func(res http.ResponseWriter, req *http.Request) { +// t, _ := template.ParseFiles("templates/index.html") +// t.Execute(res, false) +// }) +// } + func AuthHandler(router *mux.Router, config *Config) { maxAge := 86400 * 30 // 30 days isProd := true // Set to true when serving over https diff --git a/config.json b/config.json deleted file mode 100644 index 6016a33..0000000 --- a/config.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "session_secret":"Secret-session-key", - "google_client_id":"8283214538-lr39n0mnn0el6p51tb4jk80i7k3ima9q.apps.googleusercontent.com", - "google_client_secret":"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" -} \ No newline at end of file diff --git a/database.go b/database.go index 8291abd..deda998 100644 --- a/database.go +++ b/database.go @@ -3,7 +3,6 @@ package main import ( "context" "fmt" - "time" "log" "os" @@ -16,7 +15,7 @@ import ( ) // Use godot package to load/read the .env file and -// return the value of the key +// return the value of the key (for local env) // func goDotEnvVariable(key string) string { // // load .env file @@ -29,210 +28,472 @@ import ( // return os.Getenv(key) // } -func database() { - // Find home directory. - // home, err := os.Getwd() - // if err != nil { - // return err - // } +// Initialize Firebase client +var firebaseClient *db.Client +// InitializeFirebase initializes the Firebase app and sets the global firebaseClient variable +func initializeFirebase() error { 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/", + // databaseURL := goDotEnvVariable("DATABASE_URL") + // if databaseURL == "" { + // return fmt.Errorf("DATABASE_URL is not set in the environment variables") // } - // 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") + conf := &firebase.Config{DatabaseURL: databaseURL} - // 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") + // opt := option.WithCredentialsFile("edusync-test-firebase-adminsdk-hk5kl-9af0162b09.json") app, err := firebase.NewApp(ctx, conf, opt) if err != nil { - log.Fatalln("error in initializing firebase app: ", err) + return fmt.Errorf("error initializing firebase app: %v", err) } client, err := app.Database(ctx) if err != nil { - log.Fatalln("error in creating firebase DB client: ", err) - } - - // Student operations - student := NewStudent("Jane Doe", 7, 119.5, "jane_doe@nk.com", "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!") - - readStudent, err := readStudent(client, student.ID.String()) - if err != nil { - log.Fatal(err) - } - fmt.Println("Student read successfully:", readStudent) - - studentUpdates := map[string]interface{}{ - "class": "Tech Explorer 2", - "updated_at": time.Now(), - } - err = updateStudent(client, student.ID.String(), studentUpdates) - if err != nil { - log.Fatal(err) - } - fmt.Println("Student updated successfully!") - - err = deleteStudent(client, student.ID.String()) - if err != nil { - log.Fatal(err) - } - fmt.Println("Student deleted successfully!") - - // Instructor operations - instructor := NewInstructor("Scott Smith", "123-456-7890", "scott@example.com", 50000.00, 10) - err = createInstructor(client, instructor.ID.String(), instructor) - if err != nil { - log.Fatal(err) - } - fmt.Println("Instructor added/updated successfully!") - - readInstructor, err := readInstructor(client, instructor.ID.String()) - if err != nil { - log.Fatal(err) - } - fmt.Println("Instructor read successfully:", readInstructor) - - instructorUpdates := map[string]interface{}{ - "base_pay": 55000.00, - "updated_at": time.Now(), - } - err = updateInstructor(client, instructor.ID.String(), instructorUpdates) - if err != nil { - log.Fatal(err) - } - fmt.Println("Instructor updated successfully!") - - err = deleteInstructor(client, instructor.ID.String()) - if err != nil { - log.Fatal(err) + return fmt.Errorf("error creating firebase DB client: %v", err) } - fmt.Println("Instructor deleted successfully!") - // Parent operations - parent := NewParent("Jackie Doe", "jackjack@example.com", "98765432") - err = createParent(client, parent.ID.String(), parent) - if err != nil { - log.Fatal(err) - } - fmt.Println("Parent added/updated successfully!") - - readParent, err := readParent(client, parent.ID.String()) - if err != nil { - log.Fatal(err) - } - fmt.Println("Parent read successfully:", readParent) - - parentUpdates := map[string]interface{}{ - "email": "jackiejack@nk.com", - "updated_at": time.Now(), - } - err = updateParent(client, parent.ID.String(), parentUpdates) - if err != nil { - log.Fatal(err) - } - fmt.Println("Parent updated successfully!") - - err = deleteParent(client, parent.ID.String()) - if err != nil { - log.Fatal(err) - } - fmt.Println("Parent deleted successfully!") + firebaseClient = client + return nil } // Student CRUD -func createStudent(client *db.Client, userId string, student Student) error { - ref := client.NewRef("students/" + userId) +func createStudent(googleID string, student Student) error { + ref := firebaseClient.NewRef("students/" + googleID) + if err := ref.Set(context.TODO(), student); err != nil { + return fmt.Errorf("error creating student: %v", err) + } return ref.Set(context.TODO(), student) } -func readStudent(client *db.Client, userId string) (Student, error) { - ref := client.NewRef("students/" + userId) +func readStudent(googleID string) (Student, error) { + ref := firebaseClient.NewRef("students/" + googleID) var student Student if err := ref.Get(context.TODO(), &student); err != nil { - return Student{}, err + return Student{}, fmt.Errorf("error reading student: %v", err) } return student, nil } -func updateStudent(client *db.Client, userId string, updates map[string]interface{}) error { - ref := client.NewRef("students/" + userId) +func updateStudent(googleID string, updates map[string]interface{}) error { + ref := firebaseClient.NewRef("students/" + googleID) + if err := ref.Update(context.TODO(), updates); err != nil { + return fmt.Errorf("error updating student: %v", err) + } return ref.Update(context.TODO(), updates) } -func deleteStudent(client *db.Client, userId string) error { - ref := client.NewRef("students/" + userId) +func deleteStudent(googleID string) error { + ref := firebaseClient.NewRef("students/" + googleID) + if err := ref.Delete(context.TODO()); err != nil { + return fmt.Errorf("error deleting student: %v", err) + } return ref.Delete(context.TODO()) } // Instructor CRUD -func createInstructor(client *db.Client, userId string, instructor Instructor) error { - ref := client.NewRef("instructors/" + userId) +func createInstructor(googleID string, instructor Instructor) error { + ref := firebaseClient.NewRef("instructors/" + googleID) + if err := ref.Set(context.TODO(), instructor); err != nil { + return fmt.Errorf("error creating instructor: %v", err) + } return ref.Set(context.TODO(), instructor) } -func readInstructor(client *db.Client, userId string) (Instructor, error) { - ref := client.NewRef("instructors/" + userId) +func readInstructor(googleID string) (Instructor, error) { + ref := firebaseClient.NewRef("instructors/" + googleID) var instructor Instructor if err := ref.Get(context.TODO(), &instructor); err != nil { - return Instructor{}, err + return Instructor{}, fmt.Errorf("error reading instructor: %v", err) } return instructor, nil } -func updateInstructor(client *db.Client, userId string, updates map[string]interface{}) error { - ref := client.NewRef("instructors/" + userId) +func updateInstructor(googleID string, updates map[string]interface{}) error { + ref := firebaseClient.NewRef("instructors/" + googleID) + if err := ref.Update(context.TODO(), updates); err != nil { + return fmt.Errorf("error updating instructor: %v", err) + } return ref.Update(context.TODO(), updates) } -func deleteInstructor(client *db.Client, userId string) error { - ref := client.NewRef("instructors/" + userId) +func deleteInstructor(googleID string) error { + ref := firebaseClient.NewRef("instructors/" + googleID) + if err := ref.Delete(context.TODO()); err != nil { + return fmt.Errorf("error deleting instructor: %v", err) + } + return ref.Delete(context.TODO()) +} + +// Admin CRUD +func createAdmin(googleID string, admin Admin) error { + ref := firebaseClient.NewRef("admins/" + googleID) + if err := ref.Set(context.TODO(), admin); err != nil { + return fmt.Errorf("error creating admin: %v", err) + } + return ref.Set(context.TODO(), admin) +} + +func readAdmin(googleID string) (Admin, error) { + ref := firebaseClient.NewRef("admins/" + googleID) + var admin Admin + if err := ref.Get(context.TODO(), &admin); err != nil { + return Admin{}, fmt.Errorf("error reading admin: %v", err) + } + return admin, nil +} + +func updateAdmin(googleID string, updates map[string]interface{}) error { + ref := firebaseClient.NewRef("admins/" + googleID) + if err := ref.Update(context.TODO(), updates); err != nil { + return fmt.Errorf("error updating admin: %v", err) + } + return ref.Update(context.TODO(), updates) +} + +func deleteAdmin(googleID string) error { + ref := firebaseClient.NewRef("admins/" + googleID) + if err := ref.Delete(context.TODO()); err != nil { + return fmt.Errorf("error deleting admin: %v", err) + } return ref.Delete(context.TODO()) } // Parent CRUD -func createParent(client *db.Client, userId string, parent Parent) error { - ref := client.NewRef("parents/" + userId) +func createParent(googleID string, parent Parent) error { + ref := firebaseClient.NewRef("parents/" + googleID) + if err := ref.Set(context.TODO(), parent); err != nil { + return fmt.Errorf("error creating parent: %v", err) + } return ref.Set(context.TODO(), parent) } -func readParent(client *db.Client, userId string) (Parent, error) { - ref := client.NewRef("parents/" + userId) +func readParent(googleID string) (Parent, error) { + ref := firebaseClient.NewRef("parents/" + googleID) var parent Parent if err := ref.Get(context.TODO(), &parent); err != nil { - return Parent{}, err + return Parent{}, fmt.Errorf("error reading parent: %v", err) } return parent, nil } -func updateParent(client *db.Client, userId string, updates map[string]interface{}) error { - ref := client.NewRef("parents/" + userId) +func updateParent(googleID string, updates map[string]interface{}) error { + ref := firebaseClient.NewRef("parents/" + googleID) + if err := ref.Update(context.TODO(), updates); err != nil { + return fmt.Errorf("error updating parent: %v", err) + } return ref.Update(context.TODO(), updates) } -func deleteParent(client *db.Client, userId string) error { - ref := client.NewRef("parents/" + userId) +func deleteParent(googleID string) error { + ref := firebaseClient.NewRef("parents/" + googleID) + if err := ref.Delete(context.TODO()); err != nil { + return fmt.Errorf("error deleting parent: %v", err) + } return ref.Delete(context.TODO()) } + +// 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") +// 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.Fatalln("error in initializing firebase app: ", err) +// } + +// client, err := app.Database(ctx) +// if err != nil { +// log.Fatalln("error in creating firebase DB client: ", err) +// } + +// // Student operations +// // student := NewStudent("Jane Doe", 7, 119.5, "jane_doe@nk.com", "91234567", "Tech Explorer", "Scott Smith", "Jackie Doe") +// // err = createStudent(client, student.ID.String(), student) +// googleIDStudent := "google-id-student" +// student := NewStudent(googleIDStudent, "Jane Doe", 7, 119.5, "jane_doe@nk.com", "91234567", "Tech Explorer", "Scott Smith", "Jackie Doe") +// err = createStudent(client, student.GoogleID, student) +// if err != nil { +// log.Fatal(err) +// } +// fmt.Println("Student added/updated successfully!") + +// // readStudent, err := readStudent(client, student.ID.String()) +// readStudent, err := readStudent(client, student.GoogleID) +// if err != nil { +// log.Fatal(err) +// } +// fmt.Println("Student read successfully:", readStudent) + +// studentUpdates := map[string]interface{}{ +// "class": "Tech Explorer 2", +// "updated_at": time.Now(), +// } +// // err = updateStudent(client, student.ID.String(), studentUpdates) +// err = updateStudent(client, student.GoogleID, studentUpdates) +// if err != nil { +// log.Fatal(err) +// } +// fmt.Println("Student updated successfully!") + +// // err = deleteStudent(client, student.ID.String()) +// err = deleteStudent(client, student.GoogleID) +// if err != nil { +// log.Fatal(err) +// } +// fmt.Println("Student deleted successfully!") + +// // Instructor operations +// // instructor := NewInstructor("Scott Smith", "123-456-7890", "scott@example.com", 50000.00, 10) +// // err = createInstructor(client, instructor.ID.String(), instructor) +// googleIDInstructor := "google-id-instructor" +// instructor := NewInstructor(googleIDInstructor, "Scott Smith", "123-456-7890", "scott@example.com", 50000.00, 10) +// err = createInstructor(client, instructor.GoogleID, instructor) +// if err != nil { +// log.Fatal(err) +// } +// fmt.Println("Instructor added/updated successfully!") + +// // readInstructor, err := readInstructor(client, instructor.ID.String()) +// readInstructor, err := readInstructor(client, instructor.GoogleID) +// if err != nil { +// log.Fatal(err) +// } +// fmt.Println("Instructor read successfully:", readInstructor) + +// instructorUpdates := map[string]interface{}{ +// "base_pay": 55000.00, +// "updated_at": time.Now(), +// } +// // err = updateInstructor(client, instructor.ID.String(), instructorUpdates) +// err = updateInstructor(client, instructor.GoogleID, instructorUpdates) +// if err != nil { +// log.Fatal(err) +// } +// fmt.Println("Instructor updated successfully!") + +// // err = deleteInstructor(client, instructor.ID.String()) +// err = deleteInstructor(client, instructor.GoogleID) +// if err != nil { +// log.Fatal(err) +// } +// fmt.Println("Instructor deleted successfully!") + +// // Parent operations +// // parent := NewParent("Jackie Doe", "jackjack@example.com", "98765432") +// // err = createParent(client, parent.ID.String(), parent) +// googleIDParent := "google-id-parent" +// parent := NewParent(googleIDParent, "Jackie Doe", "jackjack@example.com", "98765432") +// err = createParent(client, parent.GoogleID, parent) +// if err != nil { +// log.Fatal(err) +// } +// fmt.Println("Parent added/updated successfully!") + +// // readParent, err := readParent(client, parent.ID.String()) +// readParent, err := readParent(client, parent.GoogleID) +// if err != nil { +// log.Fatal(err) +// } +// fmt.Println("Parent read successfully:", readParent) + +// parentUpdates := map[string]interface{}{ +// "email": "jackiejack@nk.com", +// "updated_at": time.Now(), +// } +// // err = updateParent(client, parent.ID.String(), parentUpdates) +// err = updateParent(client, parent.GoogleID, parentUpdates) +// if err != nil { +// log.Fatal(err) +// } +// fmt.Println("Parent updated successfully!") + +// // err = deleteParent(client, parent.ID.String()) +// err = deleteParent(client, parent.GoogleID) +// if err != nil { +// log.Fatal(err) +// } +// fmt.Println("Parent deleted successfully!") +// } + +// Student CRUD +// func createStudent(client *db.Client, userId string, student Student) error { +// ref := client.NewRef("students/" + userId) +// return ref.Set(context.TODO(), student) +// } + +// 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 updateStudent(client *db.Client, userId string, updates map[string]interface{}) error { +// ref := client.NewRef("students/" + userId) +// return ref.Update(context.TODO(), updates) +// } + +// func deleteStudent(client *db.Client, userId string) error { +// ref := client.NewRef("students/" + userId) +// return ref.Delete(context.TODO()) +// } + +// 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()) +// } + +// // Student CRUD Ver 2 +// func createStudent(client *db.Client, googleID string, student Student) error { +// ref := client.NewRef("students/" + googleID) +// return ref.Set(context.TODO(), student) +// } + +// func readStudent(client *db.Client, googleID string) (Student, error) { +// ref := client.NewRef("students/" + googleID) +// var student Student +// if err := ref.Get(context.TODO(), &student); err != nil { +// return Student{}, err +// } +// return student, nil +// } + +// func updateStudent(client *db.Client, googleID string, updates map[string]interface{}) error { +// ref := client.NewRef("students/" + googleID) +// return ref.Update(context.TODO(), updates) +// } + +// func deleteStudent(client *db.Client, googleID string) error { +// ref := client.NewRef("students/" + googleID) +// return ref.Delete(context.TODO()) +// } + +// // Instructor CRUD Ver 2 +// func createInstructor(client *db.Client, googleID string, instructor Instructor) error { +// ref := client.NewRef("instructors/" + googleID) +// return ref.Set(context.TODO(), instructor) +// } + +// func readInstructor(client *db.Client, googleID string) (Instructor, error) { +// ref := client.NewRef("instructors/" + googleID) +// var instructor Instructor +// if err := ref.Get(context.TODO(), &instructor); err != nil { +// return Instructor{}, err +// } +// return instructor, nil +// } + +// func updateInstructor(client *db.Client, googleID string, updates map[string]interface{}) error { +// ref := client.NewRef("instructors/" + googleID) +// return ref.Update(context.TODO(), updates) +// } + +// func deleteInstructor(client *db.Client, googleID string) error { +// ref := client.NewRef("instructors/" + googleID) +// return ref.Delete(context.TODO()) +// } + +// // Parent CRUD Ver 2 +// func createParent(client *db.Client, googleID string, parent Parent) error { +// ref := client.NewRef("parents/" + googleID) +// return ref.Set(context.TODO(), parent) +// } + +// func readParent(client *db.Client, googleID string) (Parent, error) { +// ref := client.NewRef("parents/" + googleID) +// var parent Parent +// if err := ref.Get(context.TODO(), &parent); err != nil { +// return Parent{}, err +// } +// return parent, nil +// } + +// func updateParent(client *db.Client, googleID string, updates map[string]interface{}) error { +// ref := client.NewRef("parents/" + googleID) +// return ref.Update(context.TODO(), updates) +// } + +// func deleteParent(client *db.Client, googleID string) error { +// ref := client.NewRef("parents/" + googleID) +// return ref.Delete(context.TODO()) +// } diff --git a/database_model.go b/database_model.go index a8171d8..480f6de 100644 --- a/database_model.go +++ b/database_model.go @@ -2,13 +2,13 @@ package main import ( "time" - - "github.com/google/uuid" + // "github.com/google/uuid" ) // Student struct for storing student information type Student struct { - ID uuid.UUID `json:"id"` + // ID uuid.UUID `json:"id"` + GoogleID string `json:"google_id"` Name string `json:"name"` Age int `json:"age"` LessonCredits float32 `json:"lesson_credits"` @@ -17,36 +17,56 @@ type Student struct { Class string `json:"class"` Instructor string `json:"instructor"` ParentName string `json:"parent_name"` + Role string `json:"role"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` } // Instructor struct for storing instructor information type Instructor struct { - ID uuid.UUID `json:"id"` + // ID uuid.UUID `json:"id"` + GoogleID string `json:"google_id"` Name string `json:"name"` ContactNumber string `json:"contact_number"` Email string `json:"email"` BasePay float64 `json:"base_pay"` NumberOfStudents int `json:"number_of_students"` + Role string `json:"role"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` } +type Admin struct { + // ID uuid.UUID `json:"id"` + GoogleID string `json:"google_id"` + Name string `json:"name"` + ContactNumber string `json:"contact_number"` + Email string `json:"email"` + BasePay float64 `json:"base_pay"` + Incentive float64 `json:"incentive"` + Role string `json:"role"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` +} + // Parent struct for storing parent information type Parent struct { - ID uuid.UUID `json:"id"` - Name string `json:"name"` - Email string `json:"email"` - ContactNo string `json:"contact_no"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` + // ID uuid.UUID `json:"id"` + GoogleID string `json:"google_id"` + Name string `json:"name"` + Email string `json:"email"` + ContactNumber string `json:"contact_no"` + Role string `json:"role"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` } // NewStudent creates a new Student instance -func NewStudent(name string, age int, lessonCredits float32, email string, contactNumber string, class string, instructor string, parentName string) Student { +// func NewStudent(name string, age int, lessonCredits float32, email string, contactNumber string, class string, instructor string, parentName string) Student { +func NewStudent(googleID string, name string, age int, lessonCredits float32, email, contactNumber, class, instructor, parentName, role string) Student { return Student{ - ID: uuid.New(), + // ID: uuid.New(), + GoogleID: googleID, Name: name, Age: age, LessonCredits: lessonCredits, @@ -55,32 +75,54 @@ func NewStudent(name string, age int, lessonCredits float32, email string, conta Class: class, Instructor: instructor, ParentName: parentName, + Role: role, CreatedAt: time.Now(), UpdatedAt: time.Now(), } } // NewInstructor creates a new Instructor instance -func NewInstructor(name string, contactNumber string, email string, basePay float64, numberOfStudents int) Instructor { +// func NewInstructor(name string, contactNumber string, email string, basePay float64, numberOfStudents int) Instructor { +func NewInstructor(googleID, name, contactNumber, email, role string, basePay float64, numberOfStudents int) Instructor { return Instructor{ - ID: uuid.New(), + // ID: uuid.New(), + GoogleID: googleID, Name: name, ContactNumber: contactNumber, Email: email, BasePay: basePay, NumberOfStudents: numberOfStudents, + Role: role, CreatedAt: time.Now(), UpdatedAt: time.Now(), } } -func NewParent(name string, email string, contactNo string) Parent { +func NewAdmin(googleID, name, contactNumber, email, role string, basePay, incentive float64) Admin { + return Admin{ + // ID: uuid.New(), + GoogleID: googleID, + Name: name, + ContactNumber: contactNumber, + Email: email, + BasePay: basePay, + Incentive: incentive, + Role: role, + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + } +} + +// func NewParent(name string, email string, contactNo string) Parent { +func NewParent(googleID, name, email, contactNumber, role string) Parent { return Parent{ - ID: uuid.New(), - Name: name, - Email: email, - ContactNo: contactNo, - CreatedAt: time.Now(), - UpdatedAt: time.Now(), + // ID: uuid.New(), + GoogleID: googleID, + Name: name, + Email: email, + ContactNumber: contactNumber, + Role: role, + CreatedAt: time.Now(), + UpdatedAt: time.Now(), } } diff --git a/database_test.go b/database_test.go index ddd2578..568852b 100644 --- a/database_test.go +++ b/database_test.go @@ -1,209 +1,652 @@ package main import ( - "context" - "fmt" - "log" - "os" - "testing" - "time" + // "context" + // "fmt" + + // "os" + "reflect" - firebase "firebase.google.com/go" - "google.golang.org/api/option" + // "strings" + "testing" + // firebase "firebase.google.com/go" + // "google.golang.org/api/option" ) -// Test using test database -// func TestDatabase(t *testing.T) { -// ctx := context.Background() +func TestInitializeFirebase(t *testing.T) { + // Test case 1: FirebaseClient is set correctly + err := initializeFirebase() + if err != nil { + t.Fatalf("Error initializing Firebase: %v", err) + } + if firebaseClient == nil { + t.Fatal("FirebaseClient is not set") + } -// // configure database URL -// conf := &firebase.Config{ -// DatabaseURL: "https://edusync-test-default-rtdb.firebaseio.com/", -// } + // Run tests + // os.Exit(t.Run()) +} -// // fetch service account key -// opt := option.WithCredentialsFile("edusync-test-firebase-adminsdk-hk5kl-9af0162b09.json") +// Testing for student CRUD operations +func TestCreateStudent(t *testing.T) { + // Initialize Firebase client + err := initializeFirebase() + if err != nil { + t.Fatalf("Error initializing Firebase: %v", err) + } -// app, err := firebase.NewApp(ctx, conf, opt) -// if err != nil { -// t.Errorf("error in initializing firebase app: %v", err) -// } + // Create a new student + student := Student{ + GoogleID: "test-student", + Name: "John Doe", + Email: "johndoe@example.com", + Age: 12, + Class: "TE", + Instructor: "Awesomeness", + ParentName: "Jane Doe", + Role: "Student", + } -// client, err := app.Database(ctx) -// if err != nil { -// t.Errorf("error in creating firebase DB client: %v", err) -// } + err = createStudent(student.GoogleID, student) + if err != nil { + t.Fatalf("Error creating student: %v", err) + } -// // create ref at path students/:userId -// ref := client.NewRef("students/" + fmt.Sprint(1)) + // Read the created student + readStudent, err := readStudent(student.GoogleID) + if err != nil { + t.Fatalf("Error reading student: %v", err) + } -// // Test case 1: Successful set operation -// data := map[string]interface{}{ -// "name": "Jane Doe", -// "age": "7", -// "class": "Tech Explorer", -// "instructor": "Scott Smith", -// } -// if err := ref.Set(ctx, data); err != nil { -// t.Errorf("error in setting data: %v", err) -// } + // Assert that the created and read students are equal + if !reflect.DeepEqual(student, readStudent) { + t.Error("Created and read students are not equal") + } +} -// // Test case 2: Get the set data -// var getData map[string]interface{} -// if err := ref.Get(ctx, &getData); err != nil { -// t.Errorf("error in getting data: %v", err) -// } -// if getData["name"] != "Jane Doe" { -// t.Errorf("expected name to be 'Jane Doe', got %v", getData["name"]) -// } +func TestReadStudent(t *testing.T) { + googleID := "test-student" -// // Test case 3: Update the data -// updateData := map[string]interface{}{ -// "name": "John Doe", -// } -// if err := ref.Update(ctx, updateData); err != nil { -// t.Errorf("error in updating data: %v", err) -// } + student, err := readStudent(googleID) + if err != nil { + t.Fatalf("Failed to read student: %v", err) + } -// // Test case 4: Get the updated data -// var updatedData map[string]interface{} -// if err := ref.Get(ctx, &updatedData); err != nil { -// t.Errorf("error in getting updated data: %v", err) -// } -// if updatedData["name"] != "John Doe" { -// t.Errorf("expected name to be 'John Doe', got %v", updatedData["name"]) -// } + if student.GoogleID != googleID { + t.Fatalf("Expected GoogleID %v, got %v", googleID, student.GoogleID) + } +} -// // Test case 5: Delete the data -// if err := ref.Delete(ctx); err != nil { -// t.Errorf("error in deleting data: %v", err) -// } +func TestUpdateStudent(t *testing.T) { + // Initialize Firebase client + err := initializeFirebase() + if err != nil { + t.Fatalf("Error initializing Firebase: %v", err) + } -// // Test case 6: Get the deleted data (should return an error) -// var deletedData map[string]interface{} -// // if err := ref.Get(ctx, &deletedData); err == nil { -// // t.Errorf("expected error in getting deleted data, but got nil") -// // } -// if err := ref.Get(ctx, &deletedData); err == nil { -// // If no error, check if the data is actually deleted -// if deletedData != nil { -// t.Errorf("Expected data to be deleted, but got %v", deletedData) -// } -// } else { -// // Expecting an error, which indicates the data was not found -// t.Logf("Received expected error after deletion: %v", err) -// } -// } + // Update the student's email + updates := map[string]interface{}{ + "email": "johndoe@nk.com", + } + + err = updateStudent("test-student", updates) + if err != nil { + t.Fatalf("Error updating student: %v", err) + } + + // Read the updated student + readStudent, err := readStudent("test-student") + if err != nil { + t.Fatalf("Error reading student: %v", err) + } + + // Assert that the updated student's email is correct + if readStudent.Email != updates["email"] { + t.Errorf("Updated student's email is incorrect. Expected: %v, Got: %v", updates["email"], readStudent.Email) + } +} + +func TestDeleteStudent(t *testing.T) { + googleID := "test-student" + + // Initialize Firebase client + err := initializeFirebase() + if err != nil { + t.Fatalf("Error initializing Firebase: %v", err) + } + + // Delete the student + err = deleteStudent(googleID) + if err != nil { + t.Fatalf("Error deleting student: %v", err) + } + + // Try to read the deleted student + // _, err = readStudent(googleID) + // if err == nil { + // t.Error("Deleted student still exists") + // } +} + +// Testing for instructor CRUD operations +func TestCreateInstructor(t *testing.T) { + // Initialize Firebase client + err := initializeFirebase() + if err != nil { + t.Fatalf("Error initializing Firebase: %v", err) + } + + // Create a new instructor + instructor := Instructor{ + GoogleID: "test-instructor", + Name: "Awesomeness", + ContactNumber: "99999999", + Email: "awesome_instructor@nk.com", + BasePay: 15, + NumberOfStudents: 24, + Role: "Instructor", + } + + err = createInstructor(instructor.GoogleID, instructor) + if err != nil { + t.Fatalf("Error creating instructor: %v", err) + } + + // Read the created instructor + readInstructor, err := readInstructor(instructor.GoogleID) + if err != nil { + t.Fatalf("Error reading instructor: %v", err) + } + + // Assert that the created and read instructor are equal + if !reflect.DeepEqual(instructor, readInstructor) { + t.Error("Created and read instructors are not equal") + } +} + +func TestReadInstructor(t *testing.T) { + googleID := "test-instructor" + + instructor, err := readInstructor(googleID) + if err != nil { + t.Fatalf("Failed to read instructor: %v", err) + } + + if instructor.GoogleID != googleID { + t.Fatalf("Expected GoogleID %v, got %v", googleID, instructor.GoogleID) + } +} + +func TestUpdateInstructor(t *testing.T) { + // Initialize Firebase client + err := initializeFirebase() + if err != nil { + t.Fatalf("Error initializing Firebase: %v", err) + } + + // Update the instructor's email + updates := map[string]interface{}{ + "email": "amazing_instructor@nk.com", + } + + err = updateInstructor("test-instructor", updates) + if err != nil { + t.Fatalf("Error updating instructor: %v", err) + } + + // Read the updated instructor + readInstructor, err := readInstructor("test-instructor") + if err != nil { + t.Fatalf("Error reading instructor: %v", err) + } + + // Assert that the updated instructor's email is correct + if readInstructor.Email != updates["email"] { + t.Errorf("Updated instructor's email is incorrect. Expected: %v, Got: %v", updates["email"], readInstructor.Email) + } +} + +func TestDeleteInstructor(t *testing.T) { + googleID := "test-instructor" + + // Initialize Firebase client + err := initializeFirebase() + if err != nil { + t.Fatalf("Error initializing Firebase: %v", err) + } + + // Delete the instructor + err = deleteInstructor(googleID) + if err != nil { + t.Fatalf("Error deleting instructor: %v", err) + } + + // Try to read the deleted instructor + // _, err = readInstructor(googleID) + // if err == nil { + // t.Error("Deleted instructor still exists") + // } +} -// Test using actual database -func TestDatabaseCRUD(t *testing.T) { - ctx := context.Background() - // databaseURL := goDotEnvVariable("DATABASE_URL") - databaseURL, found := os.LookupEnv("DATABASE_URL") - if !found { - log.Fatalf("DATABASE_URL is not set in the environment variables") +// Testing for admin CRUD operations +func TestCreateAdmin(t *testing.T) { + // Initialize Firebase client + err := initializeFirebase() + if err != nil { + t.Fatalf("Error initializing Firebase: %v", err) + } + + // Create a new admin + admin := Admin{ + GoogleID: "test-admin", + Name: "Awesomeness", + ContactNumber: "99999999", + Email: "awesome_admin@nk.com", + BasePay: 15, + Incentive: 24, + Role: "Admin", } - conf := &firebase.Config{DatabaseURL: databaseURL} - 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) + err = createAdmin(admin.GoogleID, admin) if err != nil { - log.Fatalln("error in initializing firebase app: ", err) + t.Fatalf("Error creating admin: %v", err) } - client, err := app.Database(ctx) + // Read the created admin + readAdmin, err := readAdmin(admin.GoogleID) if err != nil { - log.Fatalln("error in creating firebase DB client: ", err) + t.Fatalf("Error reading admin: %v", err) } - // Student operations - student := NewStudent("Jane Doe", 7, 119.5, "jane_doe@nk.com", "91234567", "Tech Explorer", "Scott Smith", "Jackie Doe") - err = createStudent(client, student.ID.String(), student) + // Assert that the created and read admin are equal + if !reflect.DeepEqual(admin, readAdmin) { + t.Error("Created and read admins are not equal") + } +} + +func TestReadAdmin(t *testing.T) { + googleID := "test-admin" + + admin, err := readAdmin(googleID) if err != nil { - log.Fatal(err) + t.Fatalf("Failed to read instructor: %v", err) } - fmt.Println("Student added/updated successfully!") - readStudent, err := readStudent(client, student.ID.String()) + if admin.GoogleID != googleID { + t.Fatalf("Expected GoogleID %v, got %v", googleID, admin.GoogleID) + } +} + +func TestUpdateAdmin(t *testing.T) { + // Initialize Firebase client + err := initializeFirebase() if err != nil { - log.Fatal(err) + t.Fatalf("Error initializing Firebase: %v", err) } - fmt.Println("Student read successfully:", readStudent) - studentUpdates := map[string]interface{}{ - "class": "Tech Explorer 2", - "updated_at": time.Now(), + // Update the admin's email + updates := map[string]interface{}{ + "email": "amazing_admin@nk.com", } - err = updateStudent(client, student.ID.String(), studentUpdates) + + err = updateAdmin("test-admin", updates) if err != nil { - log.Fatal(err) + t.Fatalf("Error updating admin: %v", err) } - fmt.Println("Student updated successfully!") - err = deleteStudent(client, student.ID.String()) + // Read the updated admin + readAdmin, err := readAdmin("test-admin") if err != nil { - log.Fatal(err) + t.Fatalf("Error reading admin: %v", err) } - fmt.Println("Student deleted successfully!") - // Instructor operations - instructor := NewInstructor("Scott Smith", "123-456-7890", "scott@example.com", 50000.00, 10) - err = createInstructor(client, instructor.ID.String(), instructor) + // Assert that the updated admin's email is correct + if readAdmin.Email != updates["email"] { + t.Errorf("Updated admin's email is incorrect. Expected: %v, Got: %v", updates["email"], readAdmin.Email) + } +} + +func TestDeleteAdmin(t *testing.T) { + googleID := "test-admin" + + // Initialize Firebase client + err := initializeFirebase() if err != nil { - log.Fatal(err) + t.Fatalf("Error initializing Firebase: %v", err) } - fmt.Println("Instructor added/updated successfully!") - readInstructor, err := readInstructor(client, instructor.ID.String()) + // Delete the admin + err = deleteAdmin(googleID) if err != nil { - log.Fatal(err) + t.Fatalf("Error deleting admin: %v", err) } - fmt.Println("Instructor read successfully:", readInstructor) - instructorUpdates := map[string]interface{}{ - "base_pay": 55000.00, - "updated_at": time.Now(), + // Try to read the deleted admin + // _, err = readAdmin(googleID) + // if err == nil { + // t.Error("Deleted admin still exists") + // } +} + +// Testing for parent CRUD operations +func TestCreateParent(t *testing.T) { + // Initialize Firebase client + err := initializeFirebase() + if err != nil { + t.Fatalf("Error initializing Firebase: %v", err) } - err = updateInstructor(client, instructor.ID.String(), instructorUpdates) + + // Create a new parent + parent := Parent{ + GoogleID: "test-parent", + Name: "Awesomeness", + ContactNumber: "99999999", + Email: "janedoe@nk.com", + Role: "Parent", + } + + err = createParent(parent.GoogleID, parent) if err != nil { - log.Fatal(err) + t.Fatalf("Error creating parent: %v", err) } - fmt.Println("Instructor updated successfully!") - err = deleteInstructor(client, instructor.ID.String()) + // Read the created parent + readParent, err := readParent(parent.GoogleID) if err != nil { - log.Fatal(err) + t.Fatalf("Error reading parent: %v", err) } - fmt.Println("Instructor deleted successfully!") - // Parent operations - parent := NewParent("Jackie Doe", "jackjack@example.com", "98765432") - err = createParent(client, parent.ID.String(), parent) + // Assert that the created and read parent are equal + if !reflect.DeepEqual(parent, readParent) { + t.Error("Created and read parents are not equal") + } +} + +func TestReadParent(t *testing.T) { + googleID := "test-parent" + + parent, err := readParent(googleID) if err != nil { - log.Fatal(err) + t.Fatalf("Failed to read parent: %v", err) } - fmt.Println("Parent added/updated successfully!") - readParent, err := readParent(client, parent.ID.String()) + if parent.GoogleID != googleID { + t.Fatalf("Expected GoogleID %v, got %v", googleID, parent.GoogleID) + } +} + +func TestUpdateParent(t *testing.T) { + // Initialize Firebase client + err := initializeFirebase() if err != nil { - log.Fatal(err) + t.Fatalf("Error initializing Firebase: %v", err) } - fmt.Println("Parent read successfully:", readParent) - parentUpdates := map[string]interface{}{ - "email": "jackiejack@nk.com", - "updated_at": time.Now(), + // Update the parent's email + updates := map[string]interface{}{ + "email": "janedoe_parent@nk.com", } - err = updateParent(client, parent.ID.String(), parentUpdates) + + err = updateParent("test-parent", updates) if err != nil { - log.Fatal(err) + t.Fatalf("Error updating parent: %v", err) } - fmt.Println("Parent updated successfully!") - err = deleteParent(client, parent.ID.String()) + // Read the updated parent + readParent, err := readParent("test-parent") if err != nil { - log.Fatal(err) + t.Fatalf("Error reading parent: %v", err) + } + + // Assert that the updated parent's email is correct + if readParent.Email != updates["email"] { + t.Errorf("Updated parent's email is incorrect. Expected: %v, Got: %v", updates["email"], readParent.Email) } - fmt.Println("Parent deleted successfully!") } + +func TestDeleteParent(t *testing.T) { + googleID := "test-parent" + + // Initialize Firebase client + err := initializeFirebase() + if err != nil { + t.Fatalf("Error initializing Firebase: %v", err) + } + + // Delete the parent + err = deleteInstructor(googleID) + if err != nil { + t.Fatalf("Error deleting parent: %v", err) + } + + // Try to read the deleted parent + // _, err = readParent(googleID) + // if err == nil { + // t.Error("Deleted parent still exists") + // } +} + +// import ( +// "context" +// "fmt" +// "log" +// "testing" +// "time" + +// firebase "firebase.google.com/go" +// "google.golang.org/api/option" +// ) + +// // Test using test database +// // func TestDatabase(t *testing.T) { +// // ctx := context.Background() + +// // // configure database URL +// // conf := &firebase.Config{ +// // DatabaseURL: "https://edusync-test-default-rtdb.firebaseio.com/", +// // } + +// // // fetch service account key +// // opt := option.WithCredentialsFile("edusync-test-firebase-adminsdk-hk5kl-9af0162b09.json") + +// // app, err := firebase.NewApp(ctx, conf, opt) +// // if err != nil { +// // t.Errorf("error in initializing firebase app: %v", err) +// // } + +// // client, err := app.Database(ctx) +// // if err != nil { +// // t.Errorf("error in creating firebase DB client: %v", err) +// // } + +// // // create ref at path students/:userId +// // ref := client.NewRef("students/" + fmt.Sprint(1)) + +// // // Test case 1: Successful set operation +// // data := map[string]interface{}{ +// // "name": "Jane Doe", +// // "age": "7", +// // "class": "Tech Explorer", +// // "instructor": "Scott Smith", +// // } +// // if err := ref.Set(ctx, data); err != nil { +// // t.Errorf("error in setting data: %v", err) +// // } + +// // // Test case 2: Get the set data +// // var getData map[string]interface{} +// // if err := ref.Get(ctx, &getData); err != nil { +// // t.Errorf("error in getting data: %v", err) +// // } +// // if getData["name"] != "Jane Doe" { +// // t.Errorf("expected name to be 'Jane Doe', got %v", getData["name"]) +// // } + +// // // Test case 3: Update the data +// // updateData := map[string]interface{}{ +// // "name": "John Doe", +// // } +// // if err := ref.Update(ctx, updateData); err != nil { +// // t.Errorf("error in updating data: %v", err) +// // } + +// // // Test case 4: Get the updated data +// // var updatedData map[string]interface{} +// // if err := ref.Get(ctx, &updatedData); err != nil { +// // t.Errorf("error in getting updated data: %v", err) +// // } +// // if updatedData["name"] != "John Doe" { +// // t.Errorf("expected name to be 'John Doe', got %v", updatedData["name"]) +// // } + +// // // Test case 5: Delete the data +// // if err := ref.Delete(ctx); err != nil { +// // t.Errorf("error in deleting data: %v", err) +// // } + +// // // Test case 6: Get the deleted data (should return an error) +// // var deletedData map[string]interface{} +// // // if err := ref.Get(ctx, &deletedData); err == nil { +// // // t.Errorf("expected error in getting deleted data, but got nil") +// // // } +// // if err := ref.Get(ctx, &deletedData); err == nil { +// // // If no error, check if the data is actually deleted +// // if deletedData != nil { +// // t.Errorf("Expected data to be deleted, but got %v", deletedData) +// // } +// // } else { +// // // Expecting an error, which indicates the data was not found +// // t.Logf("Received expected error after deletion: %v", err) +// // } +// // } + +// // Test using actual database +// func TestDatabaseCRUD(t *testing.T) { +// ctx := context.Background() +// databaseURL := goDotEnvVariable("DATABASE_URL") +// // databaseURL, found := os.LookupEnv("DATABASE_URL") +// // if !found { +// // log.Fatalf("DATABASE_URL is not set in the environment variables") +// // } +// conf := &firebase.Config{DatabaseURL: databaseURL} +// 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.Fatalln("error in initializing firebase app: ", err) +// } + +// client, err := app.Database(ctx) +// if err != nil { +// log.Fatalln("error in creating firebase DB client: ", err) +// } + +// // Student operations +// // student := NewStudent("Jane Doe", 7, 119.5, "jane_doe@nk.com", "91234567", "Tech Explorer", "Scott Smith", "Jackie Doe") +// // err = createStudent(client, student.ID.String(), student) +// googleIDStudent := "google-id-student" +// student := NewStudent(googleIDStudent, "Jane Doe", 7, 119.5, "jane_doe@nk.com", "91234567", "Tech Explorer", "Scott Smith", "Jackie Doe") +// err = createStudent(client, student.GoogleID, student) +// if err != nil { +// log.Fatal(err) +// } +// fmt.Println("Student added/updated successfully!") + +// // readStudent, err := readStudent(client, student.ID.String()) +// readStudent, err := readStudent(client, student.GoogleID) +// if err != nil { +// log.Fatal(err) +// } +// fmt.Println("Student read successfully:", readStudent) + +// studentUpdates := map[string]interface{}{ +// "class": "Tech Explorer 2", +// "updated_at": time.Now(), +// } +// // err = updateStudent(client, student.ID.String(), studentUpdates) +// err = updateStudent(client, student.GoogleID, studentUpdates) +// if err != nil { +// log.Fatal(err) +// } +// fmt.Println("Student updated successfully!") + +// // err = deleteStudent(client, student.ID.String()) +// err = deleteStudent(client, student.GoogleID) +// if err != nil { +// log.Fatal(err) +// } +// fmt.Println("Student deleted successfully!") + +// // Instructor operations +// // instructor := NewInstructor("Scott Smith", "123-456-7890", "scott@example.com", 50000.00, 10) +// // err = createInstructor(client, instructor.ID.String(), instructor) +// googleIDInstructor := "google-id-instructor" +// instructor := NewInstructor(googleIDInstructor, "Scott Smith", "123-456-7890", "scott@example.com", 50000.00, 10) +// err = createInstructor(client, instructor.GoogleID, instructor) +// if err != nil { +// log.Fatal(err) +// } +// fmt.Println("Instructor added/updated successfully!") + +// // readInstructor, err := readInstructor(client, instructor.ID.String()) +// readInstructor, err := readInstructor(client, instructor.GoogleID) +// if err != nil { +// log.Fatal(err) +// } +// fmt.Println("Instructor read successfully:", readInstructor) + +// instructorUpdates := map[string]interface{}{ +// "base_pay": 55000.00, +// "updated_at": time.Now(), +// } +// // err = updateInstructor(client, instructor.ID.String(), instructorUpdates) +// err = updateInstructor(client, instructor.GoogleID, instructorUpdates) +// if err != nil { +// log.Fatal(err) +// } +// fmt.Println("Instructor updated successfully!") + +// // err = deleteInstructor(client, instructor.ID.String()) +// err = deleteInstructor(client, instructor.GoogleID) +// if err != nil { +// log.Fatal(err) +// } +// fmt.Println("Instructor deleted successfully!") + +// // Parent operations +// // parent := NewParent("Jackie Doe", "jackjack@example.com", "98765432") +// // err = createParent(client, parent.ID.String(), parent) +// googleIDParent := "google-id-parent" +// parent := NewParent(googleIDParent, "Jackie Doe", "jackjack@example.com", "98765432") +// err = createParent(client, parent.GoogleID, parent) +// if err != nil { +// log.Fatal(err) +// } +// fmt.Println("Parent added/updated successfully!") + +// // readParent, err := readParent(client, parent.ID.String()) +// readParent, err := readParent(client, parent.GoogleID) +// if err != nil { +// log.Fatal(err) +// } +// fmt.Println("Parent read successfully:", readParent) + +// parentUpdates := map[string]interface{}{ +// "email": "jackiejack@nk.com", +// "updated_at": time.Now(), +// } +// // err = updateParent(client, parent.ID.String(), parentUpdates) +// err = updateParent(client, parent.GoogleID, parentUpdates) +// if err != nil { +// log.Fatal(err) +// } +// fmt.Println("Parent updated successfully!") + +// // err = deleteParent(client, parent.ID.String()) +// err = deleteParent(client, parent.GoogleID) +// if err != nil { +// log.Fatal(err) +// } +// fmt.Println("Parent deleted successfully!") +// } diff --git a/main.go b/main.go index af2183f..b8d8b40 100644 --- a/main.go +++ b/main.go @@ -8,7 +8,8 @@ import ( ) func init() { - database() + // database() + initializeFirebase() } func main() {