1
1
import { Container } from "@azure/cosmos" ;
2
2
import { fetchQuestions } from "./repoQuestions" ;
3
+ import { getContainer } from "./cosmos-client" ;
3
4
4
5
export const QuestionsDataSource = ( container : Container ) => {
5
6
return {
@@ -67,18 +68,25 @@ export const RepoQuestionsDataSource = (container: any) => {
67
68
} ;
68
69
} ;
69
70
70
- export const CombinedQuestionsDataSource = ( container : Container ) => {
71
+ export const CombinedQuestionsDataSource = ( ) => {
71
72
return {
72
73
async getQuestion ( id : string , link : string ) {
73
74
try {
75
+ // Extract exam name from URL and create a safe container name
76
+ const segments = link . split ( "/" ) ;
77
+ const examName = segments [ segments . length - 3 ]
78
+ . replace ( / - / g, "_" )
79
+ . toLowerCase ( ) ;
80
+ const examContainer = await getContainer ( examName ) ;
81
+
74
82
// Try GitHub first
75
83
const questions = await fetchQuestions ( link ) ;
76
84
if ( questions ) {
77
85
const question = questions . find ( ( q : any ) => q . id === id ) ;
78
86
if ( question ) {
79
- // Upload to Cosmos DB for future use
87
+ // Upload to exam-specific container
80
88
try {
81
- await container . items . upsert ( question ) ;
89
+ await examContainer . items . upsert ( question ) ;
82
90
} catch ( err ) {
83
91
console . warn ( "Failed to upload question to Cosmos DB:" , err ) ;
84
92
}
@@ -91,7 +99,7 @@ export const CombinedQuestionsDataSource = (container: Container) => {
91
99
query : "SELECT * FROM c WHERE c.id = @id" ,
92
100
parameters : [ { name : "@id" , value : id } ] ,
93
101
} ;
94
- const { resources : items } = await container . items
102
+ const { resources : items } = await examContainer . items
95
103
. query ( querySpec )
96
104
. fetchAll ( ) ;
97
105
return items [ 0 ] ;
@@ -102,13 +110,20 @@ export const CombinedQuestionsDataSource = (container: Container) => {
102
110
103
111
async getQuestions ( link : string ) {
104
112
try {
113
+ // Extract exam name from URL and create a safe container name
114
+ const segments = link . split ( "/" ) ;
115
+ const examName = segments [ segments . length - 3 ]
116
+ . replace ( / - / g, "_" )
117
+ . toLowerCase ( ) ;
118
+ const examContainer = await getContainer ( examName ) ;
119
+
105
120
// Try GitHub first
106
121
const questions = await fetchQuestions ( link ) ;
107
122
if ( questions ) {
108
- // Upload all questions to Cosmos DB
123
+ // Upload all questions to exam-specific container
109
124
try {
110
125
for ( const question of questions ) {
111
- await container . items . upsert ( question ) ;
126
+ await examContainer . items . upsert ( question ) ;
112
127
}
113
128
} catch ( err ) {
114
129
console . warn ( "Failed to upload questions to Cosmos DB:" , err ) ;
@@ -120,7 +135,7 @@ export const CombinedQuestionsDataSource = (container: Container) => {
120
135
const querySpec = {
121
136
query : "SELECT VALUE COUNT(c.id) FROM c" ,
122
137
} ;
123
- const { resources : items } = await container . items
138
+ const { resources : items } = await examContainer . items
124
139
. query ( querySpec )
125
140
. fetchAll ( ) ;
126
141
return { count : items [ 0 ] } ;
@@ -131,16 +146,23 @@ export const CombinedQuestionsDataSource = (container: Container) => {
131
146
132
147
async getRandomQuestions ( range : number , link : string ) {
133
148
try {
149
+ // Extract exam name from URL and create a safe container name
150
+ const segments = link . split ( "/" ) ;
151
+ const examName = segments [ segments . length - 3 ]
152
+ . replace ( / - / g, "_" )
153
+ . toLowerCase ( ) ;
154
+ const examContainer = await getContainer ( examName ) ;
155
+
134
156
// Try GitHub first
135
157
const questions = await fetchQuestions ( link ) ;
136
158
if ( questions ) {
137
159
const shuffled = [ ...questions ] . sort ( ( ) => 0.5 - Math . random ( ) ) ;
138
160
const selected = shuffled . slice ( 0 , range ) ;
139
161
140
- // Upload selected questions to Cosmos DB
162
+ // Upload selected questions to exam-specific container
141
163
try {
142
164
for ( const question of selected ) {
143
- await container . items . upsert ( question ) ;
165
+ await examContainer . items . upsert ( question ) ;
144
166
}
145
167
} catch ( err ) {
146
168
console . warn ( "Failed to upload questions to Cosmos DB:" , err ) ;
@@ -153,7 +175,7 @@ export const CombinedQuestionsDataSource = (container: Container) => {
153
175
const querySpec = {
154
176
query : "SELECT * FROM c" ,
155
177
} ;
156
- const { resources : items } = await container . items
178
+ const { resources : items } = await examContainer . items
157
179
. query ( querySpec )
158
180
. fetchAll ( ) ;
159
181
const shuffled = [ ...items ] . sort ( ( ) => 0.5 - Math . random ( ) ) ;
0 commit comments