11import { Container } from "@azure/cosmos" ;
22import { fetchQuestions } from "./repoQuestions" ;
3+ import { getContainer } from "./cosmos-client" ;
34
45export const QuestionsDataSource = ( container : Container ) => {
56 return {
@@ -67,18 +68,25 @@ export const RepoQuestionsDataSource = (container: any) => {
6768 } ;
6869} ;
6970
70- export const CombinedQuestionsDataSource = ( container : Container ) => {
71+ export const CombinedQuestionsDataSource = ( ) => {
7172 return {
7273 async getQuestion ( id : string , link : string ) {
7374 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+
7482 // Try GitHub first
7583 const questions = await fetchQuestions ( link ) ;
7684 if ( questions ) {
7785 const question = questions . find ( ( q : any ) => q . id === id ) ;
7886 if ( question ) {
79- // Upload to Cosmos DB for future use
87+ // Upload to exam-specific container
8088 try {
81- await container . items . upsert ( question ) ;
89+ await examContainer . items . upsert ( question ) ;
8290 } catch ( err ) {
8391 console . warn ( "Failed to upload question to Cosmos DB:" , err ) ;
8492 }
@@ -91,7 +99,7 @@ export const CombinedQuestionsDataSource = (container: Container) => {
9199 query : "SELECT * FROM c WHERE c.id = @id" ,
92100 parameters : [ { name : "@id" , value : id } ] ,
93101 } ;
94- const { resources : items } = await container . items
102+ const { resources : items } = await examContainer . items
95103 . query ( querySpec )
96104 . fetchAll ( ) ;
97105 return items [ 0 ] ;
@@ -102,13 +110,20 @@ export const CombinedQuestionsDataSource = (container: Container) => {
102110
103111 async getQuestions ( link : string ) {
104112 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+
105120 // Try GitHub first
106121 const questions = await fetchQuestions ( link ) ;
107122 if ( questions ) {
108- // Upload all questions to Cosmos DB
123+ // Upload all questions to exam-specific container
109124 try {
110125 for ( const question of questions ) {
111- await container . items . upsert ( question ) ;
126+ await examContainer . items . upsert ( question ) ;
112127 }
113128 } catch ( err ) {
114129 console . warn ( "Failed to upload questions to Cosmos DB:" , err ) ;
@@ -120,7 +135,7 @@ export const CombinedQuestionsDataSource = (container: Container) => {
120135 const querySpec = {
121136 query : "SELECT VALUE COUNT(c.id) FROM c" ,
122137 } ;
123- const { resources : items } = await container . items
138+ const { resources : items } = await examContainer . items
124139 . query ( querySpec )
125140 . fetchAll ( ) ;
126141 return { count : items [ 0 ] } ;
@@ -131,16 +146,23 @@ export const CombinedQuestionsDataSource = (container: Container) => {
131146
132147 async getRandomQuestions ( range : number , link : string ) {
133148 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+
134156 // Try GitHub first
135157 const questions = await fetchQuestions ( link ) ;
136158 if ( questions ) {
137159 const shuffled = [ ...questions ] . sort ( ( ) => 0.5 - Math . random ( ) ) ;
138160 const selected = shuffled . slice ( 0 , range ) ;
139161
140- // Upload selected questions to Cosmos DB
162+ // Upload selected questions to exam-specific container
141163 try {
142164 for ( const question of selected ) {
143- await container . items . upsert ( question ) ;
165+ await examContainer . items . upsert ( question ) ;
144166 }
145167 } catch ( err ) {
146168 console . warn ( "Failed to upload questions to Cosmos DB:" , err ) ;
@@ -153,7 +175,7 @@ export const CombinedQuestionsDataSource = (container: Container) => {
153175 const querySpec = {
154176 query : "SELECT * FROM c" ,
155177 } ;
156- const { resources : items } = await container . items
178+ const { resources : items } = await examContainer . items
157179 . query ( querySpec )
158180 . fetchAll ( ) ;
159181 const shuffled = [ ...items ] . sort ( ( ) => 0.5 - Math . random ( ) ) ;
0 commit comments