feat: add, delete student route met user logic + .js in files
This commit is contained in:
parent
e0a5596994
commit
ecad27ea4d
29 changed files with 301 additions and 159 deletions
|
@ -1,5 +1,5 @@
|
|||
import { Request, Response } from 'express'
|
||||
import { getAllAssignments, getAssignment } from '../services/assignments';
|
||||
import { getAllAssignments, getAssignment } from '../services/assignments.js';
|
||||
|
||||
// typescript is annoy with with parameter forwarding from class.ts
|
||||
interface AssignmentParams {
|
||||
|
@ -41,4 +41,4 @@ export async function getAssignmentHandler(
|
|||
}
|
||||
|
||||
res.json(assignment);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
import { Request, Response } from 'express';
|
||||
import { getAllClasses, getClass, getClassStudents, getClassTeacherInvitations } from '../services/class';
|
||||
import { ClassDTO } from '../interfaces/classes';
|
||||
import { getAllClasses, getClass, getClassStudents, getClassTeacherInvitations } from '../services/class.js';
|
||||
|
||||
export async function getAllClassesHandler(
|
||||
req: Request,
|
||||
|
@ -61,10 +60,10 @@ export async function getTeacherInvitationsHandler(
|
|||
): Promise<void> {
|
||||
const classId = req.params.id;
|
||||
const full = req.query.full === "true"; // TODO: not implemented yet
|
||||
|
||||
|
||||
const invitations = await getClassTeacherInvitations(classId, full);
|
||||
|
||||
res.json({
|
||||
invitations: invitations,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { Request, Response } from 'express';
|
||||
import { getAllGroups, getGroup } from '../services/groups';
|
||||
import { getAllGroups, getGroup } from '../services/groups.js';
|
||||
|
||||
// typescript is annoywith with parameter forwarding from class.ts
|
||||
interface GroupParams {
|
||||
|
@ -52,4 +52,4 @@ export async function getAllGroupsHandler(
|
|||
res.json({
|
||||
groups: groups,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,53 +1,33 @@
|
|||
import { Request, Response } from 'express';
|
||||
import { getAllStudents, getStudent, getStudentClasses, getStudentClassIds } from '../services/students';
|
||||
import { ClassDTO } from '../interfaces/classes';
|
||||
import { getAllAssignments } from '../services/assignments';
|
||||
import {
|
||||
getStudentClasses,
|
||||
getStudentClassIds,
|
||||
StudentService
|
||||
} from '../services/students.js';
|
||||
import { ClassDTO } from '../interfaces/classes.js';
|
||||
import { getAllAssignments } from '../services/assignments.js';
|
||||
import {createUserHandler, deleteUserHandler, getAllUsersHandler, getUserHandler} from "./users.js";
|
||||
import { Student } from "../entities/users/student.entity.js";
|
||||
|
||||
// TODO: accept arguments (full, ...)
|
||||
// TODO: endpoints
|
||||
export async function getAllStudentsHandler (
|
||||
req: Request,
|
||||
res: Response,
|
||||
): Promise<void> {
|
||||
try {
|
||||
const students = await getAllStudents();
|
||||
|
||||
res.json({
|
||||
students: students
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Error fetching learning objects:', error);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
export async function getAllStudentsHandler (req: Request, res: Response): Promise<void> {
|
||||
await getAllUsersHandler<Student>(req, res, new StudentService());
|
||||
}
|
||||
|
||||
export async function getStudentHandler(
|
||||
req: Request,
|
||||
res: Response,
|
||||
): Promise<void> {
|
||||
try {
|
||||
const username = req.params.id;
|
||||
const student = await getStudent(username);
|
||||
|
||||
if (!student) {
|
||||
res.status(404).json({ error: "Student not found" });
|
||||
return;
|
||||
} else {
|
||||
student.endpoints = {
|
||||
classes: `/student/${req.params.id}/classes`,
|
||||
questions: `/student/${req.params.id}/submissions`,
|
||||
invitations: `/student/${req.params.id}/assignments`,
|
||||
groups: `/student/${req.params.id}/groups`,
|
||||
}
|
||||
res.json(student);
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error fetching learning objects:', error);
|
||||
res.status(500).json({ error: 'Internal server error' });
|
||||
}
|
||||
export async function getStudentHandler(req: Request, res: Response): Promise<void> {
|
||||
await getUserHandler<Student>(req, res, new StudentService());
|
||||
}
|
||||
|
||||
export async function createStudentHandler(req: Request, res: Response): Promise<void> {
|
||||
await createUserHandler<Student>(req, res, new StudentService(), Student);
|
||||
}
|
||||
|
||||
export async function deleteStudentHandler(req: Request, res: Response): Promise<void> {
|
||||
await deleteUserHandler<Student>(req, res, new StudentService());
|
||||
}
|
||||
|
||||
|
||||
export async function getStudentClassesHandler (
|
||||
req: Request,
|
||||
res: Response,
|
||||
|
@ -75,6 +55,7 @@ export async function getStudentClassesHandler (
|
|||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
// Might not be fully correct depending on if
|
||||
// a class has an assignment, that all students
|
||||
// have this assignment.
|
||||
|
@ -92,4 +73,6 @@ export async function getStudentAssignmentsHandler(
|
|||
res.json({
|
||||
assignments: assignments
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
106
backend/src/controllers/users.ts
Normal file
106
backend/src/controllers/users.ts
Normal file
|
@ -0,0 +1,106 @@
|
|||
import { Request, Response } from 'express';
|
||||
import { UserService } from "../services/users.js";
|
||||
import {UserDTO} from "../interfaces/user.js";
|
||||
import {User} from "../entities/users/user.entity.js";
|
||||
|
||||
export async function getAllUsersHandler<T extends User>(
|
||||
req: Request,
|
||||
res: Response,
|
||||
service: UserService<T>
|
||||
): Promise<void> {
|
||||
try {
|
||||
const full = req.query.full === 'true';
|
||||
|
||||
let users: UserDTO[] | string[] = full
|
||||
? await service.getAllUsers()
|
||||
: await service.getAllUserIds();
|
||||
|
||||
if (!users){
|
||||
res.status(404).json({ error: `Users not found.` });
|
||||
return;
|
||||
}
|
||||
|
||||
res.status(201).json(users);
|
||||
|
||||
} catch (error) {
|
||||
console.error("❌ Error fetching users:", error);
|
||||
res.status(500).json({ error: "Internal server error" });
|
||||
}
|
||||
}
|
||||
|
||||
export async function getUserHandler<T extends User>(
|
||||
req: Request,
|
||||
res: Response,
|
||||
service: UserService<T>
|
||||
): Promise<void> {
|
||||
try {
|
||||
const username = req.params.username as string;
|
||||
|
||||
if (!username) {
|
||||
res.status(400).json({ error: 'Missing required field: username' });
|
||||
return;
|
||||
}
|
||||
|
||||
const user = await service.getUserByUsername(username);
|
||||
|
||||
if (!user){
|
||||
res.status(404).json({ error: `User with username '${username}' not found.` });
|
||||
return;
|
||||
}
|
||||
|
||||
res.status(201).json(user);
|
||||
|
||||
} catch (error) {
|
||||
console.error("❌ Error fetching users:", error);
|
||||
res.status(500).json({ error: "Internal server error" });
|
||||
}
|
||||
}
|
||||
|
||||
export async function createUserHandler<T extends User>(
|
||||
req: Request,
|
||||
res: Response,
|
||||
service: UserService<T>,
|
||||
UserClass: new () => T
|
||||
) {
|
||||
try {
|
||||
console.log("req", req)
|
||||
const userData = req.body as UserDTO;
|
||||
|
||||
if (!userData.username || !userData.firstName || !userData.lastName) {
|
||||
res.status(400).json({ error: "Missing required fields: username, firstName, lastName" });
|
||||
return;
|
||||
}
|
||||
|
||||
const newUser = await service.createUser(userData, UserClass);
|
||||
res.status(201).json(newUser);
|
||||
} catch (error) {
|
||||
console.error("❌ Error creating user:", error);
|
||||
res.status(500).json({ error: "Internal server error" });
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteUserHandler<T extends User> (
|
||||
req: Request,
|
||||
res: Response,
|
||||
service: UserService<T>
|
||||
) {
|
||||
try {
|
||||
const username = req.params.username;
|
||||
|
||||
if (!username) {
|
||||
res.status(400).json({ error: "Missing required field: username" });
|
||||
return;
|
||||
}
|
||||
|
||||
const deletedUser = await service.deleteUser(username);
|
||||
if (!deletedUser) {
|
||||
res.status(404).json({ error: `User with username '${username}' not found.` });
|
||||
return;
|
||||
}
|
||||
|
||||
res.status(200).json(deletedUser);
|
||||
} catch (error) {
|
||||
console.error("❌ Error deleting user:", error);
|
||||
res.status(500).json({ error: "Internal server error" });
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue