fix(backend): Diverse bugfixes omtrent LearningPathPage
This commit is contained in:
parent
27b9cdf833
commit
12b9f31f5f
9 changed files with 34 additions and 33 deletions
|
@ -1,41 +1,47 @@
|
|||
import { apiConfig } from "@/config.ts";
|
||||
import apiClient from "@/services/api-client/api-client.ts";
|
||||
import type {AxiosResponse, ResponseType} from "axios";
|
||||
import {HttpErrorResponseException} from "@/exception/http-error-response-exception.ts";
|
||||
|
||||
export abstract class BaseController {
|
||||
protected baseUrl: string;
|
||||
protected basePath: string;
|
||||
|
||||
protected constructor(basePath: string) {
|
||||
this.baseUrl = `${apiConfig.baseUrl}/${basePath}`;
|
||||
this.basePath = basePath;
|
||||
}
|
||||
|
||||
private assertSuccessResponse(response: AxiosResponse<unknown, unknown>) {
|
||||
if (response.status / 200 !== 2) {
|
||||
if (response.status / 100 !== 2) {
|
||||
throw new HttpErrorResponseException(response);
|
||||
}
|
||||
}
|
||||
|
||||
private absolutePathFor(path: string) {
|
||||
return "/" + this.basePath + path;
|
||||
}
|
||||
|
||||
protected async get<T>(path: string, queryParams?: Record<string, any>, responseType?: ResponseType): Promise<T> {
|
||||
let response = await apiClient.get<T>(path, {params: queryParams, responseType});
|
||||
let response = await apiClient.get<T>(
|
||||
this.absolutePathFor(path),
|
||||
{params: queryParams, responseType}
|
||||
);
|
||||
this.assertSuccessResponse(response);
|
||||
return response.data;
|
||||
}
|
||||
|
||||
protected async post<T>(path: string, body: unknown): Promise<T> {
|
||||
let response = await apiClient.post<T>(path, body);
|
||||
let response = await apiClient.post<T>(this.absolutePathFor(path), body);
|
||||
this.assertSuccessResponse(response);
|
||||
return response.data;
|
||||
}
|
||||
|
||||
protected async delete<T>(path: string): Promise<T> {
|
||||
let response = await apiClient.delete<T>(path)
|
||||
let response = await apiClient.delete<T>(this.absolutePathFor(path))
|
||||
this.assertSuccessResponse(response);
|
||||
return response.data;
|
||||
}
|
||||
|
||||
protected async put<T>(path: string, body: unknown): Promise<T> {
|
||||
let response = await apiClient.put<T>(path, body);
|
||||
let response = await apiClient.put<T>(this.absolutePathFor(path), body);
|
||||
this.assertSuccessResponse(response);
|
||||
return response.data;
|
||||
}
|
||||
|
|
|
@ -8,10 +8,10 @@ export class LearningObjectController extends BaseController {
|
|||
}
|
||||
|
||||
async getMetadata(hruid: string, language: Language, version: number): Promise<LearningObject> {
|
||||
return this.get<LearningObject>(`/learningObject/${hruid}`, {language, version});
|
||||
return this.get<LearningObject>(`/${hruid}`, {language, version});
|
||||
}
|
||||
|
||||
async getHTML(hruid: string, language: Language, version: number): Promise<Document> {
|
||||
return this.get<Document>(`/learningObject/${hruid}/html`, {language, version}, "document");
|
||||
return this.get<Document>(`/${hruid}/html`, {language, version}, "document");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ import {BaseController} from "@/controllers/base-controller.ts";
|
|||
import {LearningPath} from "@/data-objects/learning-path.ts";
|
||||
import type {LearningPathDTO} from "@/data-objects/learning-path.ts";
|
||||
import type {Language} from "@/data-objects/language.ts";
|
||||
import {single} from "@/utils/response-assertions.ts";
|
||||
|
||||
export class LearningPathController extends BaseController {
|
||||
constructor() {
|
||||
|
@ -18,6 +19,6 @@ export class LearningPathController extends BaseController {
|
|||
forGroup: options?.forGroup,
|
||||
forStudent: options?.forStudent
|
||||
});
|
||||
return dtos.map(dto => LearningPath.fromDTO(dto))
|
||||
return LearningPath.fromDTO(single(dtos));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue