Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Entity, PrimaryGeneratedColumn, ManyToOne, JoinColumn } from "typeorm";
import { AssignmentTypeORM } from "./assignmentTypeorm";
import { StudentTypeORM } from "./studentTypeorm";
import { UserTypeORM } from "./userTypeorm";
import { Group } from "../../../../core/entities/group";

@Entity()
Expand All @@ -12,8 +12,8 @@ export class GroupTypeORM {
@JoinColumn({ name: "assignment" })
assignment!: AssignmentTypeORM;

public toEntity(studentModels: StudentTypeORM[]): Group {
const students: string[] = studentModels.map((studentModel: StudentTypeORM) => studentModel.id);
public toEntity(userModels: UserTypeORM[]): Group {
const students: string[] = userModels.map((userModel: UserTypeORM) => userModel.id);

return new Group(students, this.assignment.id, this.id);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn } from "typeorm";
import { AssignmentTypeORM } from "./assignmentTypeorm";
import { MessageTypeORM } from "./messageTypeorm";
import { StudentTypeORM } from "./studentTypeorm";
import { UserTypeORM } from "./userTypeorm";
import { QuestionThread } from "../../../../core/entities/questionThread";
import { VisibilityType } from "../../../../core/entities/questionThread";

Expand All @@ -16,9 +16,9 @@ export class QuestionThreadTypeORM {
@PrimaryGeneratedColumn("uuid")
id!: string;

@ManyToOne(() => StudentTypeORM, { cascade: true, onDelete: "CASCADE" })
@ManyToOne(() => UserTypeORM, { cascade: true, onDelete: "CASCADE" })
@JoinColumn({ name: "creator_id" })
student!: StudentTypeORM;
user!: UserTypeORM;

@ManyToOne(() => AssignmentTypeORM, { cascade: true, onDelete: "CASCADE" })
@JoinColumn({ name: "assignment_id" })
Expand All @@ -39,12 +39,12 @@ export class QuestionThreadTypeORM {

public static createTypeORM(
thread: QuestionThread,
studentModel: StudentTypeORM,
userModel: UserTypeORM,
assignmentModel: AssignmentTypeORM,
): QuestionThreadTypeORM {
const threadTypeORM: QuestionThreadTypeORM = new QuestionThreadTypeORM();
threadTypeORM.assignment = assignmentModel;
threadTypeORM.student = studentModel;
threadTypeORM.user = userModel;
threadTypeORM.is_closed = thread.isClosed;
threadTypeORM.learning_object_id = thread.learningObjectId;

Expand All @@ -66,7 +66,7 @@ export class QuestionThreadTypeORM {
visibilityType = VisibilityType.PRIVATE;
}
return new QuestionThread(
this.student.id,
this.user.id,
this.assignment.id,
this.learning_object_id,
this.is_closed,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Entity, PrimaryGeneratedColumn, JoinColumn, CreateDateColumn, ManyToOne } from "typeorm";
import { GroupTypeORM } from "./groupTypeorm";
import { StudentTypeORM } from "./studentTypeorm";
import { UserTypeORM } from "./userTypeorm";

@Entity()
export class StudentOfGroupTypeORM {
@PrimaryGeneratedColumn("uuid")
id!: string;

@ManyToOne(() => StudentTypeORM, { cascade: true, onDelete: "CASCADE" })
@ManyToOne(() => UserTypeORM, { cascade: true, onDelete: "CASCADE" })
@JoinColumn({ name: "student_id" })
student!: StudentTypeORM;
user!: UserTypeORM;

@ManyToOne(() => GroupTypeORM, { cascade: true, onDelete: "CASCADE" })
@JoinColumn({ name: "group_id" })
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Entity, PrimaryGeneratedColumn, JoinColumn, Column, CreateDateColumn, ManyToOne } from "typeorm";
import { AssignmentTypeORM } from "./assignmentTypeorm";
import { StudentTypeORM } from "./studentTypeorm";
import { UserTypeORM } from "./userTypeorm";
import { StatusType, Submission } from "../../../../core/entities/submission";

export enum SubmissionStatus {
Expand All @@ -13,9 +13,9 @@ export class SubmissionTypeORM {
@PrimaryGeneratedColumn("uuid")
id!: string;

@ManyToOne(() => StudentTypeORM, { cascade: true, onDelete: "CASCADE" })
@ManyToOne(() => UserTypeORM, { cascade: true, onDelete: "CASCADE" })
@JoinColumn({ name: "student_id" })
student!: StudentTypeORM;
user!: UserTypeORM;

@ManyToOne(() => AssignmentTypeORM, { cascade: true, onDelete: "CASCADE" })
@JoinColumn({ name: "assignment_id" })
Expand Down Expand Up @@ -45,7 +45,7 @@ export class SubmissionTypeORM {
status = StatusType.NOT_ACCEPTED;
}
return new Submission(
this.student.id,
this.user.id,
this.assignment.id,
this.learning_object_id,
this.time,
Expand All @@ -57,7 +57,7 @@ export class SubmissionTypeORM {

public static createTypeORM(
submission: Submission,
studentModel: StudentTypeORM,
userModel: UserTypeORM,
assignmentModel: AssignmentTypeORM,
): SubmissionTypeORM {
const submissionModel: SubmissionTypeORM = new SubmissionTypeORM();
Expand All @@ -69,7 +69,7 @@ export class SubmissionTypeORM {
status = SubmissionStatus.NOT_ACCEPTED;
}

submissionModel.student = studentModel;
submissionModel.user = userModel;
submissionModel.assignment = assignmentModel;
submissionModel.learning_object_id = submission.learningObjectId;
submissionModel.time = submission.time;
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { Entity, PrimaryGeneratedColumn, JoinColumn, CreateDateColumn, ManyToOne } from "typeorm";
import { ClassTypeORM } from "./classTypeorm";
import { StudentTypeORM } from "./studentTypeorm";
import { UserTypeORM } from "./userTypeorm";

@Entity()
export class StudentOfClassTypeORM {
export class UserOfClassTypeORM {
// A table with both the students and the teachers of the class
@PrimaryGeneratedColumn("uuid")
id!: string;

@ManyToOne(() => StudentTypeORM, { cascade: true, onDelete: "CASCADE" })
@JoinColumn({ name: "student_id" })
student!: StudentTypeORM;
@ManyToOne(() => UserTypeORM, { cascade: true, onDelete: "CASCADE" })
@JoinColumn({ name: "user_id" })
user!: UserTypeORM;

@ManyToOne(() => ClassTypeORM, { cascade: true, onDelete: "CASCADE" })
@JoinColumn({ name: "class_id" })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { Entity, PrimaryGeneratedColumn, Column } from "typeorm";
import { Student } from "../../../../core/entities/student";
import { Teacher } from "../../../../core/entities/teacher";
import { User } from "../../../../core/entities/user";

export enum UserType {
TEACHER = "teacher",
STUDENT = "student",
}

@Entity()
export class UserTypeORM {
@PrimaryGeneratedColumn("uuid")
Expand All @@ -21,6 +28,12 @@ export class UserTypeORM {
@Column({ type: "varchar", length: 64 }) // 256-bit hash => 32 bytes => 64 hexadecimals
password_hash!: string;

@Column({
type: "enum",
enum: UserType,
})
role!: UserType;

// Since multiple constructors isn't supported by Typescript
// https://stackoverflow.com/questions/12702548/constructor-overload-in-typescript
public static createUserTypeORM(user: User): UserTypeORM {
Expand All @@ -30,6 +43,38 @@ export class UserTypeORM {
userTypeORM.last_name = user.familyName;
if (user.schoolName) userTypeORM.school_name = user.schoolName;
userTypeORM.password_hash = user.passwordHash;
// Set the role of the user by checking the class of the object.
if (user instanceof Teacher) {
userTypeORM.role = UserType.TEACHER;
} else if (user instanceof Student) {
userTypeORM.role = UserType.STUDENT;
} else {
throw new Error("The user provided was neither a student or a teacher");
}
return userTypeORM;
}

public toEntity(): User {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be a good idea to remove the Student and Teacher Entity and only use the User entity. An example to why this could be a good idea can be seen in the following lines. The createn of the Student/Teacher entity is basically the same here.

if (this.role == UserType.TEACHER) {
return new Teacher(
this.email,
this.first_name,
this.last_name,
this.password_hash,
this.school_name,
this.id,
);
} else if (this.role == UserType.STUDENT) {
return new Student(
this.email,
this.first_name,
this.last_name,
this.password_hash,
this.school_name,
this.id,
);
} else {
throw new Error("The user in the database was neither a student or a teacher");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class DatasourceAssignmentTypeORM extends DatasourceTypeORM {
const assignmentsJoinResult = await datasource
.getRepository(StudentOfGroupTypeORM)
.createQueryBuilder()
.where("StudentOfGroupTypeORM.student = :id", { id: userId })
.where("StudentOfGroupTypeORM.user = :id", { id: userId })
// Join StudentOfGroup
.leftJoinAndSelect("StudentOfGroupTypeORM.group", "group") // Last one is alias
// Join Group to AssignmentGroup
Expand Down
Loading