Merge branch 'dev' into feat/frontend-controllers-adriaan

This commit is contained in:
Adriaan J. 2025-04-03 11:38:31 +02:00 committed by GitHub
commit 9f975977e0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
257 changed files with 6698 additions and 3672 deletions

View file

@ -1,4 +1,9 @@
import { BaseController } from "./base-controller";
import type { AssignmentDTO } from "@dwengo-1/interfaces/assignment";
export interface AssignmentsResponse {
assignments: AssignmentDTO[];
} // TODO ID
export class AssignmentController extends BaseController {
constructor(classid: string) {
@ -32,4 +37,4 @@ export class AssignmentController extends BaseController {
getGroups(assignmentNumber: number, full = true) {
return this.get<{ groups: any[] }>(`/${assignmentNumber}/groups`, { full });
}
}
}

View file

@ -1,73 +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 class BaseController {
protected baseUrl: string;
export abstract class BaseController {
protected basePath: string;
constructor(basePath: string) {
this.baseUrl = `${apiConfig.baseUrl}/${basePath}`;
protected constructor(basePath: string) {
this.basePath = basePath;
}
protected async get<T>(path: string, queryParams?: Record<string, any>): Promise<T> {
let url = `${this.baseUrl}${path}`;
if (queryParams) {
const query = new URLSearchParams();
Object.entries(queryParams).forEach(([key, value]) => {
if (value !== undefined && value !== null) {
query.append(key, value.toString());
}
});
url += `?${query.toString()}`;
private static assertSuccessResponse(response: AxiosResponse<unknown, unknown>): void {
if (response.status / 100 !== 2) {
throw new HttpErrorResponseException(response);
}
}
const res = await fetch(url);
if (!res.ok) {
const errorData = await res.json().catch(() => ({}));
throw new Error(errorData?.error || `Error ${res.status}: ${res.statusText}`);
}
return res.json();
protected async get<T>(path: string, queryParams?: QueryParams, responseType?: ResponseType): Promise<T> {
const response = await apiClient.get<T>(this.absolutePathFor(path), { params: queryParams, responseType });
BaseController.assertSuccessResponse(response);
return response.data;
}
protected async post<T>(path: string, body: unknown): Promise<T> {
const res = await fetch(`${this.baseUrl}${path}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
if (!res.ok) {
const errorData = await res.json().catch(() => ({}));
throw new Error(errorData?.error || `Error ${res.status}: ${res.statusText}`);
}
return res.json();
const response = await apiClient.post<T>(this.absolutePathFor(path), body);
BaseController.assertSuccessResponse(response);
return response.data;
}
protected async delete<T>(path: string): Promise<T> {
const res = await fetch(`${this.baseUrl}${path}`, {
method: "DELETE",
});
if (!res.ok) {
const errorData = await res.json().catch(() => ({}));
throw new Error(errorData?.error || `Error ${res.status}: ${res.statusText}`);
}
return res.json();
const response = await apiClient.delete<T>(this.absolutePathFor(path));
BaseController.assertSuccessResponse(response);
return response.data;
}
protected async put<T>(path: string, body: unknown): Promise<T> {
const res = await fetch(`${this.baseUrl}${path}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
const response = await apiClient.put<T>(this.absolutePathFor(path), body);
BaseController.assertSuccessResponse(response);
return response.data;
}
if (!res.ok) {
const errorData = await res.json().catch(() => ({}));
throw new Error(errorData?.error || `Error ${res.status}: ${res.statusText}`);
}
return res.json();
private absolutePathFor(path: string): string {
return "/" + this.basePath + path;
}
}
type QueryParams = Record<string, string | number | boolean | undefined>;

View file

@ -1,4 +1,9 @@
import { BaseController } from "./base-controller";
import type { ClassDTO } from "@dwengo-1/interfaces/class";
export interface ClassesResponse {
classes: ClassDTO[] | string[];
}
export class ClassController extends BaseController {
constructor() {
@ -32,4 +37,4 @@ export class ClassController extends BaseController {
getAssignments(id: string, full = true) {
return this.get<{ assignments: any[] }>(`/${id}/assignments`, { full });
}
}
}

View file

@ -1,14 +1,18 @@
import { ThemeController } from "@/controllers/themes.ts";
import { LearningObjectController } from "@/controllers/learning-objects.ts";
import { LearningPathController } from "@/controllers/learning-paths.ts";
export function controllerGetter<T>(Factory: new () => T): () => T {
export function controllerGetter<T>(factory: new () => T): () => T {
let instance: T | undefined;
return (): T => {
if (!instance) {
instance = new Factory();
instance = new factory();
}
return instance;
};
}
export const getThemeController = controllerGetter(ThemeController);
export const getLearningObjectController = controllerGetter(LearningObjectController);
export const getLearningPathController = controllerGetter(LearningPathController);

View file

@ -1,4 +1,9 @@
import { BaseController } from "./base-controller";
import type { GroupDTO } from "@dwengo-1/interfaces/group";
export interface GroupsResponse {
groups: GroupDTO[];
} // | TODO id
export class GroupController extends BaseController {
constructor(classid: string, assignmentNumber: number) {

View file

@ -0,0 +1,17 @@
import { BaseController } from "@/controllers/base-controller.ts";
import type { Language } from "@/data-objects/language.ts";
import type { LearningObject } from "@/data-objects/learning-objects/learning-object.ts";
export class LearningObjectController extends BaseController {
constructor() {
super("learningObject");
}
async getMetadata(hruid: string, language: Language, version: number): Promise<LearningObject> {
return this.get<LearningObject>(`/${hruid}`, { language, version });
}
async getHTML(hruid: string, language: Language, version: number): Promise<Document> {
return this.get<Document>(`/${hruid}/html`, { language, version }, "document");
}
}

View file

@ -0,0 +1,32 @@
import { BaseController } from "@/controllers/base-controller.ts";
import { LearningPath } from "@/data-objects/learning-paths/learning-path.ts";
import type { Language } from "@/data-objects/language.ts";
import { single } from "@/utils/response-assertions.ts";
import type { LearningPathDTO } from "@/data-objects/learning-paths/learning-path-dto.ts";
export class LearningPathController extends BaseController {
constructor() {
super("learningPath");
}
async search(query: string): Promise<LearningPath[]> {
const dtos = await this.get<LearningPathDTO[]>("/", { search: query });
return dtos.map((dto) => LearningPath.fromDTO(dto));
}
async getBy(
hruid: string,
language: Language,
options?: { forGroup?: string; forStudent?: string },
): Promise<LearningPath> {
const dtos = await this.get<LearningPathDTO[]>("/", {
hruid,
language,
forGroup: options?.forGroup,
forStudent: options?.forStudent,
});
return LearningPath.fromDTO(single(dtos));
}
async getAllByTheme(theme: string): Promise<LearningPath[]> {
const dtos = await this.get<LearningPathDTO[]>("/", { theme });
return dtos.map((dto) => LearningPath.fromDTO(dto));
}
}

View file

@ -0,0 +1,5 @@
import type { QuestionDTO, QuestionId } from "@dwengo-1/interfaces/question";
export interface QuestionsResponse {
questions: QuestionDTO[] | QuestionId[];
}

View file

@ -0,0 +1,79 @@
import { BaseController } from "@/controllers/base-controller.ts";
import type { ClassesResponse } from "@/controllers/classes.ts";
import type { AssignmentsResponse } from "@/controllers/assignments.ts";
import type { GroupsResponse } from "@/controllers/groups.ts";
import type { SubmissionsResponse } from "@/controllers/submissions.ts";
import type { QuestionsResponse } from "@/controllers/questions.ts";
import type { StudentDTO } from "@dwengo-1/interfaces/student";
import type { ClassJoinRequestDTO } from "@dwengo-1/interfaces/class-join-request";
export interface StudentsResponse {
students: StudentDTO[] | string[];
}
export interface StudentResponse {
student: StudentDTO;
}
export interface JoinRequestsResponse {
requests: ClassJoinRequestDTO[];
}
export interface JoinRequestResponse {
request: ClassJoinRequestDTO;
}
export class StudentController extends BaseController {
constructor() {
super("student");
}
async getAll(full = true): Promise<StudentsResponse> {
return this.get<StudentsResponse>("/", { full });
}
async getByUsername(username: string): Promise<StudentResponse> {
return this.get<StudentResponse>(`/${username}`);
}
async createStudent(data: StudentDTO): Promise<StudentResponse> {
return this.post<StudentResponse>("/", data);
}
async deleteStudent(username: string): Promise<StudentResponse> {
return this.delete<StudentResponse>(`/${username}`);
}
async getClasses(username: string, full = true): Promise<ClassesResponse> {
return this.get<ClassesResponse>(`/${username}/classes`, { full });
}
async getAssignments(username: string, full = true): Promise<AssignmentsResponse> {
return this.get<AssignmentsResponse>(`/${username}/assignments`, { full });
}
async getGroups(username: string, full = true): Promise<GroupsResponse> {
return this.get<GroupsResponse>(`/${username}/groups`, { full });
}
async getSubmissions(username: string): Promise<SubmissionsResponse> {
return this.get<SubmissionsResponse>(`/${username}/submissions`);
}
async getQuestions(username: string, full = true): Promise<QuestionsResponse> {
return this.get<QuestionsResponse>(`/${username}/questions`, { full });
}
async getJoinRequests(username: string): Promise<JoinRequestsResponse> {
return this.get<JoinRequestsResponse>(`/${username}/joinRequests`);
}
async getJoinRequest(username: string, classId: string): Promise<JoinRequestResponse> {
return this.get<JoinRequestResponse>(`/${username}/joinRequests/${classId}`);
}
async createJoinRequest(username: string, classId: string): Promise<JoinRequestResponse> {
return this.post<JoinRequestResponse>(`/${username}/joinRequests}`, classId);
}
async deleteJoinRequest(username: string, classId: string): Promise<JoinRequestResponse> {
return this.delete<JoinRequestResponse>(`/${username}/joinRequests/${classId}`);
}
}

View file

@ -0,0 +1,5 @@
import { type SubmissionDTO, SubmissionDTOId } from "@dwengo-1/interfaces/submission";
export interface SubmissionsResponse {
submissions: SubmissionDTO[] | SubmissionDTOId[];
}

View file

@ -0,0 +1,64 @@
import { BaseController } from "@/controllers/base-controller.ts";
import type { JoinRequestResponse, JoinRequestsResponse, StudentsResponse } from "@/controllers/students.ts";
import type { QuestionsResponse } from "@/controllers/questions.ts";
import type { ClassesResponse } from "@/controllers/classes.ts";
import type { TeacherDTO } from "@dwengo-1/interfaces/teacher";
export interface TeachersResponse {
teachers: TeacherDTO[] | string[];
}
export interface TeacherResponse {
teacher: TeacherDTO;
}
export class TeacherController extends BaseController {
constructor() {
super("teacher");
}
async getAll(full = false): Promise<TeachersResponse> {
return this.get<TeachersResponse>("/", { full });
}
async getByUsername(username: string): Promise<TeacherResponse> {
return this.get<TeacherResponse>(`/${username}`);
}
async createTeacher(data: TeacherDTO): Promise<TeacherResponse> {
return this.post<TeacherResponse>("/", data);
}
async deleteTeacher(username: string): Promise<TeacherResponse> {
return this.delete<TeacherResponse>(`/${username}`);
}
async getClasses(username: string, full = false): Promise<ClassesResponse> {
return this.get<ClassesResponse>(`/${username}/classes`, { full });
}
async getStudents(username: string, full = false): Promise<StudentsResponse> {
return this.get<StudentsResponse>(`/${username}/students`, { full });
}
async getQuestions(username: string, full = false): Promise<QuestionsResponse> {
return this.get<QuestionsResponse>(`/${username}/questions`, { full });
}
async getStudentJoinRequests(username: string, classId: string): Promise<JoinRequestsResponse> {
return this.get<JoinRequestsResponse>(`/${username}/joinRequests/${classId}`);
}
async updateStudentJoinRequest(
teacherUsername: string,
classId: string,
studentUsername: string,
accepted: boolean,
): Promise<JoinRequestResponse> {
return this.put<JoinRequestResponse>(
`/${teacherUsername}/joinRequests/${classId}/${studentUsername}`,
accepted,
);
}
// GetInvitations(id: string) {return this.get<{ invitations: string[] }>(`/${id}/invitations`);}
}

View file

@ -1,16 +1,17 @@
import { BaseController } from "@/controllers/base-controller.ts";
import type { Theme } from "@dwengo-1/interfaces/theme";
export class ThemeController extends BaseController {
constructor() {
super("theme");
}
getAll(language: string | null = null) {
async getAll(language: string | null = null): Promise<Theme[]> {
const query = language ? { language } : undefined;
return this.get<any[]>("/", query);
return this.get("/", query);
}
getHruidsByKey(themeKey: string) {
async getHruidsByKey(themeKey: string): Promise<string[]> {
return this.get<string[]>(`/${encodeURIComponent(themeKey)}`);
}
}