feat: teacher en student frontend controllers

This commit is contained in:
Gabriellvl 2025-03-21 23:23:33 +01:00
parent bfb9598fa1
commit 52364d717c
6 changed files with 172 additions and 0 deletions

View file

@ -1,5 +1,6 @@
import { Response, Router } from 'express';
import studentRouter from './students.js';
import teacherRouter from './teachers.js';
import groupRouter from './groups.js';
import assignmentRouter from './assignments.js';
import submissionRouter from './submissions.js';
@ -22,6 +23,7 @@ router.get('/', (_, res: Response) => {
});
router.use('/student', studentRouter /* #swagger.tags = ['Student'] */);
router.use('/teacher', teacherRouter /* #swagger.tags = ['Teacher'] */);
router.use('/group', groupRouter /* #swagger.tags = ['Group'] */);
router.use('/assignment', assignmentRouter /* #swagger.tags = ['Assignment'] */);
router.use('/submission', submissionRouter /* #swagger.tags = ['Submission'] */);

1
frontend/config.ts Normal file
View file

@ -0,0 +1 @@
export const API_BASE = "http://localhost:3000/api";

View file

@ -0,0 +1,72 @@
import {API_BASE} from "../../config.ts";
export class BaseController {
protected baseUrl: string;
constructor(basePath: string) {
this.baseUrl = `${API_BASE}/${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()}`;
}
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 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();
}
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();
}
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),
});
if (!res.ok) {
const errorData = await res.json().catch(() => ({}));
throw new Error(errorData?.error || `Error ${res.status}: ${res.statusText}`);
}
return res.json();
}
}

View file

@ -0,0 +1,16 @@
import {StudentController} from "@/controllers/student-controller.ts";
import {TeacherController} from "@/controllers/teacher-controller.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);

View file

@ -0,0 +1,44 @@
import {BaseController} from "@/controllers/base-controller.ts";
export class StudentController extends BaseController {
constructor() {
super("students");
}
getAll(full = true) {
return this.get<{ students: any[] }>("/", { full });
}
getByUsername(username: string) {
return this.get<any>(`/${username}`);
}
createStudent(data: any) {
return this.post<any>("/", data);
}
deleteStudent(username: string) {
return this.delete<any>(`/${username}`);
}
getClasses(username: string, full = true) {
return this.get<{ classes: any[] }>(`/${username}/classes`, { full });
}
getAssignments(username: string, full = true) {
return this.get<{ assignments: any[] }>(`/${username}/assignments`, { full });
}
getGroups(username: string, full = true) {
return this.get<{ groups: any[] }>(`/${username}/groups`, { full });
}
getSubmissions(username: string) {
return this.get<{ submissions: any[] }>(`/${username}/submissions`);
}
getQuestions(username: string, full = true) {
return this.get<{ questions: any[] }>(`/${username}/questions`, { full });
}
}

View file

@ -0,0 +1,37 @@
import {BaseController} from "@/controllers/base-controller.ts";
export class TeacherController extends BaseController {
constructor() {
super("teachers");
}
getAll(full = false) {
return this.get<{ teachers: any[] }>("/", { full });
}
getByUsername(username: string) {
return this.get<any>(`/${username}`);
}
createTeacher(data: any) {
return this.post<any>("/", data);
}
deleteTeacher(username: string) {
return this.delete<any>(`/${username}`);
}
getClasses(username: string, full = false) {
return this.get<any[]>(`/${username}/classes`, { full });
}
getStudents(username: string, full = false) {
return this.get<{ students: any[] }>(`/${username}/students`, { full });
}
getQuestions(username: string, full = false) {
return this.get<{ questions: any[] }>(`/${username}/questions`, { full });
}
// getInvitations(id: string) {return this.get<{ invitations: string[] }>(`/${id}/invitations`);}
}