feat: class join req controller + fixes tests

This commit is contained in:
Gabriellvl 2025-03-29 15:09:57 +01:00
parent 3093a6c131
commit f679a324ab
11 changed files with 116 additions and 41 deletions

View file

@ -28,12 +28,17 @@ export class BaseController {
return res.json();
}
protected async post<T>(path: string, body: unknown): Promise<T> {
const res = await fetch(`${this.baseUrl}${path}`, {
protected async post<T>(path: string, body?: unknown): Promise<T> {
const options: RequestInit = {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
};
if (body !== undefined) {
options.body = JSON.stringify(body);
}
const res = await fetch(`${this.baseUrl}${path}`, options);
if (!res.ok) {
const errorData = await res.json().catch(() => ({}));

View file

@ -2,7 +2,7 @@ import { BaseController } from "@/controllers/base-controller.ts";
export class StudentController extends BaseController {
constructor() {
super("students");
super("student");
}
getAll(full = true) {
@ -10,15 +10,15 @@ export class StudentController extends BaseController {
}
getByUsername(username: string) {
return this.get<any>(`/${username}`);
return this.get<{ student: any }>(`/${username}`);
}
createStudent(data: any) {
return this.post<any>("/", data);
return this.post<{ student: any }>("/", data);
}
deleteStudent(username: string) {
return this.delete<any>(`/${username}`);
return this.delete<{ student: any }>(`/${username}`);
}
getClasses(username: string, full = true) {
@ -40,4 +40,16 @@ export class StudentController extends BaseController {
getQuestions(username: string, full = true) {
return this.get<{ questions: any[] }>(`/${username}/questions`, { full });
}
getJoinRequests(username: string) {
return this.get<{ requests: any[] }>(`/${username}/joinRequests`);
}
createJoinRequest(username: string, classId: string) {
return this.post<any>(`/${username}/joinRequests/${classId}`);
}
deleteJoinRequest(username: string, classId: string) {
return this.delete<any>(`/${username}/joinRequests/${classId}`);
}
}

View file

@ -2,7 +2,7 @@ import { BaseController } from "@/controllers/base-controller.ts";
export class TeacherController extends BaseController {
constructor() {
super("teachers");
super("teacher");
}
getAll(full = false) {

View file

@ -1,6 +1,6 @@
import { computed, toValue } from "vue";
import type { MaybeRefOrGetter } from "vue";
import { useQuery } from "@tanstack/vue-query";
import {useMutation, useQuery, useQueryClient} from "@tanstack/vue-query";
import { getStudentController } from "@/controllers/controllers.ts";
const studentController = getStudentController();
@ -75,7 +75,7 @@ export function useCreateStudentMutation() {
return useMutation({
mutationFn: (data: any) => studentController.createStudent(data),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['students'] });
await queryClient.invalidateQueries({ queryKey: ['students'] });
},
onError: (err) => {
alert("Create student failed:", err);
@ -92,10 +92,13 @@ export function useDeleteStudentMutation() {
return useMutation({
mutationFn: (username: string) => studentController.deleteStudent(username),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['students'] });
await queryClient.invalidateQueries({ queryKey: ['students'] });
},
onError: (err) => {
alert("Delete student failed:", err);
},
});
}

View file

@ -0,0 +1,41 @@
import { describe, it, expect, beforeAll } from 'vitest';
import {getStudentController} from "../../src/controllers/controllers";
const controller = getStudentController();
describe('StudentController', () => {
const newStudent = {
username: 'TestStudent',
firstName: 'Testy',
lastName: 'McTestface',
};
beforeAll(() => {
// Zet eventueel mock server op hier als je dat gebruikt
});
it('creates a student and fetches it by username', async () => {
// Create student
const created = await controller.createStudent(newStudent);
expect(created).toBeDefined();
expect(created.username).toBe(newStudent.username);
// Fetch same student
const fetched = await controller.getByUsername(newStudent.username);
expect(fetched).toBeDefined();
expect(fetched.student).toBeDefined();
const student = fetched.student;
expect(student.username).toBe(newStudent.username);
expect(student.firstName).toBe(newStudent.firstName);
expect(student.lastName).toBe(newStudent.lastName);
await controller.deleteStudent(newStudent.username);
await expect(controller.getByUsername(newStudent.username)).rejects.toThrow();
});
});