@@ -18,31 +18,31 @@ const firebaseConfig = {
1818const app = initializeApp ( firebaseConfig ) ;
1919export const auth = getAuth ( app ) ;
2020export const db = getDatabase ( app ) ;
21- // export const googleProvider = new GoogleAuthProvider();
22- // googleProvider.addScope("profile");
23- // googleProvider.addScope("email");
21+ export const googleProvider = new GoogleAuthProvider ( ) ;
22+ googleProvider . addScope ( "profile" ) ;
23+ googleProvider . addScope ( "email" ) ;
2424
2525// fetches all relevant information to create the model
2626async function firebaseToModel ( model ) {
27- const courses = await fetchCourses ( ) ;
27+ const courses = await fetchAllCourses ( ) ;
2828 model . setCourses ( courses ) ;
2929}
3030
3131export function connectToFirebase ( model ) {
32- // onAuthStateChanged(auth, (user) => {
33- // model.setUser(user);
34- // }); !can be used for auth!
35-
36- firebaseToModel ( model ) ;
32+ onAuthStateChanged ( auth , ( user ) => {
33+ model . setUser ( user ) ;
34+ } ) ;
35+ firebaseToModel ( model ) ;
3736}
3837
3938export async function addCourse ( course ) {
40- if ( ! course ?. courseCode )
41- return ;
42- const myRef = ref ( db , `courses/${ course . courseCode } ` ) ;
43- await set ( myRef , course ) ;
39+ if ( ! course ?. code )
40+ return ;
41+ const myRef = ref ( db , `courses/${ course . code } ` ) ;
42+ await set ( myRef , course ) ;
4443}
4544
45+ < << << << HEAD
4646export async function fetchCourses ( ) {
4747 const myRef = ref ( db , `courses` ) ;
4848 const snapshot = await get ( myRef ) ;
@@ -53,4 +53,58 @@ export async function fetchCourses() {
5353 courses = [ ...courses , courses [ id ] ] ;
5454 }
5555 return courses ;
56+ = === ===
57+ export async function fetchAllCourses ( ) {
58+ const myRef = ref ( db , `courses` ) ;
59+ const snapshot = await get ( myRef ) ;
60+
61+ if ( ! snapshot . exists ( ) ) return [ ] ;
62+
63+ const value = snapshot . val ( ) ; // Firebase returns an object where keys are course IDs
64+ const courses = [ ] ;
65+
66+ for ( const id of Object . keys ( value ) ) {
67+ courses . push ( { id, ...value [ id ] } ) ;
68+ }
69+
70+ return courses ;
71+ > >>> >>> 27 a5b77bd1478c3b8493b19385b77340472d7591
5672}
73+
74+ // Before: [ {courseCode: "CS101", name: "Intro to CS"}, {...} ]
75+ // After: { "CS101": { name: "Intro to CS" }, "CS102": {...} }
76+
77+
78+ export async function fetchCoursesSnapshot ( ) {
79+ const myRef = ref ( db , `courses` ) ;
80+ const snapshot = await get ( myRef ) ;
81+ if ( ! snapshot . exists ( ) ) return { } ; // Return empty object instead of array
82+
83+ return snapshot . val ( ) ; // Return the object directly
84+ }
85+
86+ export async function saveJSONCoursesToFirebase ( model , data ) {
87+ if ( ! data || ! model ) {
88+ console . log ( "no model or data" )
89+ return ;
90+ }
91+ const entries = Object . entries ( data ) ;
92+ entries . forEach ( entry => {
93+ const course = {
94+ code : entry [ 1 ] . code ,
95+ name : entry [ 1 ] ?. name ?? "" ,
96+ location : entry [ 1 ] ?. location ?? "" ,
97+ department : entry [ 1 ] ?. department ?? "" ,
98+ language : entry [ 1 ] ?. language ?? "" ,
99+ description : entry [ 1 ] ?. description ?? "" ,
100+ academicLevel : entry [ 1 ] ?. academic_level ?? "" ,
101+ period : entry [ 1 ] ?. period ?? "" ,
102+ credits : entry [ 1 ] ?. credits ?? 0 ,
103+ //lectureCount:entry[1].courseLectureCount,
104+ //prerequisites:entry.coursePrerequisites
105+ }
106+ model . addCourse ( course ) ;
107+
108+ } ) ;
109+ }
110+
0 commit comments