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 { getAssignmentRepository, getClassRepository } from "../data/repositories";
|
||||
import { AssignmentDTO, mapToAssignmentDTO, mapToAssignmentDTOId } from "../interfaces/assignments";
|
||||
import { getAssignmentRepository, getClassRepository } from "../data/repositories.js";
|
||||
import { AssignmentDTO, mapToAssignmentDTO, mapToAssignmentDTOId } from "../interfaces/assignments.js";
|
||||
|
||||
export async function getAllAssignments(classid: string, full: boolean): Promise<AssignmentDTO[]> {
|
||||
const classRepository = getClassRepository();
|
||||
|
|
@ -8,7 +8,7 @@ export async function getAllAssignments(classid: string, full: boolean): Promise
|
|||
if (!cls) {
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
const assignmentRepository = getAssignmentRepository();
|
||||
const assignments = await assignmentRepository.findAllAssignmentsInClass(cls);
|
||||
|
||||
|
|
@ -26,7 +26,7 @@ export async function getAssignment(classid: string, id: number): Promise<Assign
|
|||
if (!cls) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
const assignmentRepository = getAssignmentRepository();
|
||||
const assignment = await assignmentRepository.findByClassAndId(cls, id);
|
||||
|
||||
|
|
@ -35,4 +35,4 @@ export async function getAssignment(classid: string, id: number): Promise<Assign
|
|||
}
|
||||
|
||||
return mapToAssignmentDTO(assignment);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,7 @@
|
|||
import { getClassRepository, getTeacherInvitationRepository } from "../data/repositories";
|
||||
import { Class } from "../entities/classes/class.entity";
|
||||
import { ClassDTO, mapToClassDTO } from "../interfaces/classes";
|
||||
import { mapToStudentDTO, StudentDTO } from "../interfaces/students";
|
||||
import { mapToTeacherInvitationDTO, mapToTeacherInvitationDTOIds, TeacherInvitationDTO } from "../interfaces/teacher-invitation";
|
||||
import { getClassRepository, getTeacherInvitationRepository } from "../data/repositories.js";
|
||||
import { ClassDTO, mapToClassDTO } from "../interfaces/classes.js";
|
||||
import { mapToStudentDTO, StudentDTO } from "../interfaces/students.js";
|
||||
import { mapToTeacherInvitationDTO, mapToTeacherInvitationDTOIds, TeacherInvitationDTO } from "../interfaces/teacher-invitation.js";
|
||||
|
||||
export async function getAllClasses(full: boolean): Promise<ClassDTO[] | string[]> {
|
||||
const classRepository = getClassRepository();
|
||||
|
|
@ -11,7 +10,7 @@ export async function getAllClasses(full: boolean): Promise<ClassDTO[] | string[
|
|||
if (!classes) {
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
if (full) {
|
||||
return classes.map(mapToClassDTO);
|
||||
} else {
|
||||
|
|
@ -66,4 +65,4 @@ export async function getClassTeacherInvitations(classId: string, full: boolean)
|
|||
}
|
||||
|
||||
return invitations.map(mapToTeacherInvitationDTOIds);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
import { getAssignmentRepository, getClassRepository, getGroupRepository } from "../data/repositories";
|
||||
import { GroupDTO, mapToGroupDTO, mapToGroupDTOId } from "../interfaces/groups";
|
||||
import { getAssignmentRepository, getClassRepository, getGroupRepository } from "../data/repositories.js";
|
||||
import { GroupDTO, mapToGroupDTO, mapToGroupDTOId } from "../interfaces/groups.js";
|
||||
|
||||
export async function getGroup(
|
||||
classId: string,
|
||||
assignmentNumber: number,
|
||||
classId: string,
|
||||
assignmentNumber: number,
|
||||
groupNumber: number,
|
||||
full: boolean,
|
||||
): Promise<GroupDTO | null> {
|
||||
|
|
@ -64,4 +64,4 @@ export async function getAllGroups(
|
|||
}
|
||||
|
||||
return groups.map(mapToGroupDTOId);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,26 +1,13 @@
|
|||
import { getClassRepository, getStudentRepository } from "../data/repositories";
|
||||
import { Class } from "../entities/classes/class.entity";
|
||||
import { Student } from "../entities/users/student.entity";
|
||||
import { ClassDTO, mapToClassDTO } from "../interfaces/classes";
|
||||
import { StudentDTO, mapToStudentDTO } from "../interfaces/students";
|
||||
import { getClassRepository, getStudentRepository } from "../data/repositories.js";
|
||||
import { Class } from "../entities/classes/class.entity.js";
|
||||
import { Student } from "../entities/users/student.entity.js";
|
||||
import { ClassDTO, mapToClassDTO } from "../interfaces/classes.js";
|
||||
import {UserService} from "./users.js";
|
||||
|
||||
|
||||
export async function getAllStudents(): Promise<StudentDTO[]> {
|
||||
const studentRepository = getStudentRepository();
|
||||
const students = await studentRepository.find({});
|
||||
|
||||
return students.map(mapToStudentDTO);
|
||||
}
|
||||
|
||||
export async function getStudent(username: string): Promise<StudentDTO | null> {
|
||||
const studentRepository = getStudentRepository();
|
||||
const student = await studentRepository.findByUsername(username);
|
||||
|
||||
if (!student) {
|
||||
return null;
|
||||
export class StudentService extends UserService<Student> {
|
||||
constructor() {
|
||||
super(getStudentRepository());
|
||||
}
|
||||
|
||||
return mapToStudentDTO(student);
|
||||
}
|
||||
|
||||
async function fetchStudentClasses(username: string): Promise<Class[]> {
|
||||
|
|
|
|||
39
backend/src/services/users.ts
Normal file
39
backend/src/services/users.ts
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import { UserRepository } from "../data/users/user-repository.js";
|
||||
import { UserDTO, mapToUser, mapToUserDTO } from "../interfaces/user.js";
|
||||
import {User} from "../entities/users/user.entity.js";
|
||||
|
||||
export class UserService<T extends User> {
|
||||
protected repository: UserRepository<T>;
|
||||
|
||||
constructor(repository: UserRepository<T>) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
async getAllUsers(): Promise<UserDTO[]> {
|
||||
const users = await this.repository.findAll();
|
||||
return users.map(mapToUserDTO);
|
||||
}
|
||||
|
||||
async getAllUserIds(): Promise<string[]> {
|
||||
const users = await this.getAllUsers();
|
||||
return users.map((user) => user.username);
|
||||
}
|
||||
|
||||
async getUserByUsername(username: string): Promise<UserDTO | null> {
|
||||
const user = await this.repository.findByUsername(username)
|
||||
return user ? mapToUserDTO(user) : null;
|
||||
}
|
||||
|
||||
async createUser(userData: UserDTO, UserClass: new () => T): Promise<T> {
|
||||
const newUser = mapToUser(userData, new UserClass());
|
||||
await this.repository.save(newUser);
|
||||
return newUser;
|
||||
}
|
||||
|
||||
async deleteUser(username: string): Promise<UserDTO | null> {
|
||||
const user = await this.getUserByUsername(username);
|
||||
if (!user) return null;
|
||||
await this.repository.deleteByUsername(username)
|
||||
return mapToUserDTO(user);
|
||||
}
|
||||
}
|
||||
Reference in a new issue