Skip to content

Commit

Permalink
feat: add new Numbas Feature
Browse files Browse the repository at this point in the history
Added new Numbas Service to the frontend as part of Integration

Changed by: Daniel Maddern
  • Loading branch information
maddernd committed Sep 10, 2023
1 parent 96d982d commit 639f428
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/app/api/services/numbas.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError, map, retry } from 'rxjs/operators';

@Injectable({
providedIn: 'root'
})
export class NumbasService {
private readonly API_URL = 'http://localhost:3000/api/numbas_api';

constructor(private http: HttpClient) {}

fetchResource(unitId: string, taskId: string, resourcePath: string): Observable<any> {
const resourceUrl = `${this.API_URL}/${unitId}/${taskId}/${resourcePath}`;
const resourceMimeType = this.getMimeType(resourcePath);

return this.http.get(resourceUrl, { responseType: 'blob' }).pipe(
retry(3), // Retrying up to 3 times before failing
map((blob) => new Blob([blob], { type: resourceMimeType })),
catchError((error: HttpErrorResponse) => {
console.error('Error fetching Numbas resource:', error);
return throwError('Error fetching Numbas resource.');
})
);
}

getMimeType(resourcePath: string): string {
const extension = resourcePath.split('.').pop()?.toLowerCase();
const mimeTypeMap: { [key: string]: string } = {
'html': 'text/html',
'css': 'text/css',
'js': 'application/javascript',
'json': 'application/json',
'png': 'image/png',
'jpg': 'image/jpeg',
'gif': 'image/gif',
'svg': 'image/svg+xml'
};

return mimeTypeMap[extension || ''] || 'text/plain';
}
uploadTest(unitId: string, taskId: string, file: File): Observable<any> {
const uploadUrl = `${this.API_URL}/uploadNumbasTest`;
const formData = new FormData();

formData.append('file', file);
formData.append('unit_code', unitId);
formData.append('task_definition_id', taskId);

const httpOptions = {
headers: new HttpHeaders({
// You might need to set some headers here depending on your backend requirements
'Accept': 'application/json'
})
};

return this.http.post(uploadUrl, formData, httpOptions).pipe(
retry(3),
catchError((error: HttpErrorResponse) => {
console.error('Error uploading Numbas test:', error);
return throwError('Error uploading Numbas test.');
})
);
}
}

0 comments on commit 639f428

Please sign in to comment.