feat: (frontend) queries teacher + test controller teacher

This commit is contained in:
Gabriellvl 2025-03-30 22:26:26 +02:00
parent 5e0f284131
commit 44c242fc57
11 changed files with 184 additions and 59 deletions

View file

@ -28,27 +28,20 @@ export class BaseController {
return res.json();
}
protected async post<T>(path: string, body?: unknown): Promise<T> {
const options: RequestInit = {
protected async post(path: string, body: unknown): Promise<void> {
const res = await fetch(`${this.baseUrl}${path}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
};
if (body !== undefined) {
options.body = JSON.stringify(body);
}
const res = await fetch(`${this.baseUrl}${path}`, options);
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();
}
protected async delete<T>(path: string): Promise<T> {
protected async delete(path: string): Promise<void> {
const res = await fetch(`${this.baseUrl}${path}`, {
method: "DELETE",
});
@ -57,11 +50,9 @@ export class BaseController {
const errorData = await res.json().catch(() => ({}));
throw new Error(errorData?.error || `Error ${res.status}: ${res.statusText}`);
}
return res.json();
}
protected async put<T>(path: string, body: unknown): Promise<T> {
protected async put(path: string, body: unknown): Promise<void> {
const res = await fetch(`${this.baseUrl}${path}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
@ -72,7 +63,5 @@ export class BaseController {
const errorData = await res.json().catch(() => ({}));
throw new Error(errorData?.error || `Error ${res.status}: ${res.statusText}`);
}
return res.json();
}
}

View file

@ -1,18 +0,0 @@
import { StudentController } from "@/controllers/students.ts";
import { TeacherController } from "@/controllers/teachers.ts";
import {ThemeController} from "@/controllers/themes.ts";
export function controllerGetter<T>(Factory: new () => T): () => T {
let instance: T | undefined;
return (): T => {
if (!instance) {
instance = new Factory();
}
return instance;
};
}
export const getStudentController = controllerGetter(StudentController);
export const getTeacherController = controllerGetter(TeacherController);
export const getThemeController = controllerGetter(ThemeController);

View file

@ -14,11 +14,11 @@ export class StudentController extends BaseController {
}
createStudent(data: any) {
return this.post<{ student: any }>("/", data);
return this.post("/", data);
}
deleteStudent(username: string) {
return this.delete<{ student: any }>(`/${username}`);
return this.delete(`/${username}`);
}
getClasses(username: string, full = true) {
@ -46,10 +46,10 @@ export class StudentController extends BaseController {
}
createJoinRequest(username: string, classId: string) {
return this.post<any>(`/${username}/joinRequests/${classId}`);
return this.post(`/${username}/joinRequests}`, classId);
}
deleteJoinRequest(username: string, classId: string) {
return this.delete<any>(`/${username}/joinRequests/${classId}`);
return this.delete(`/${username}/joinRequests/${classId}`);
}
}

View file

@ -14,11 +14,11 @@ export class TeacherController extends BaseController {
}
createTeacher(data: any) {
return this.post<any>("/", data);
return this.post("/", data);
}
deleteTeacher(username: string) {
return this.delete<any>(`/${username}`);
return this.delete(`/${username}`);
}
getClasses(username: string, full = false) {
@ -33,5 +33,13 @@ export class TeacherController extends BaseController {
return this.get<{ questions: any[] }>(`/${username}/questions`, { full });
}
getStudentJoinRequests(username: string, classId: string){
return this.get<{ joinRequests: any[] }>(`/${username}/joinRequests/${classId}`);
}
updateStudentJoinRequest(teacherUsername: string, classId: string, studentUsername: string, accepted: boolean){
return this.put(`/${teacherUsername}/joinRequests/${classId}/${studentUsername}`, accepted)
}
// GetInvitations(id: string) {return this.get<{ invitations: string[] }>(`/${id}/invitations`);}
}