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
50 changes: 50 additions & 0 deletions backend/src/core/entities/assignment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
export class Assignment {
public constructor(
private _classId: string,
private _learningPathId: string,
private _startDate: Date,
private _deadline: Date,
private _extraInstructions: string,
private _id?: string,
){}

// Getters
public get id():string|undefined{
return this._id;
}
public get classId():string{
return this._classId;
}
public get learningPathId():string{
return this._learningPathId;
}
public get startDate():Date{
return this._startDate;
}
public get deadline():Date{
return this._deadline;
}
public get extraInstructions():string{
return this._extraInstructions;
}

// Setters
public set id(newId:string){
this._id = newId;
}
public set classId(newClassId:string){
this._classId = newClassId;
}
public set learningPathId(newLearningPathId:string){
this._learningPathId = newLearningPathId;
}
public set startDate(newStartDate:Date){
this._startDate = newStartDate;
}
public set deadline(newDeadline:Date){
this._deadline = newDeadline;
}
public set extraInstructions(newExtraInstructions:string){
this._extraInstructions = newExtraInstructions;
}
}
16 changes: 0 additions & 16 deletions backend/src/core/entities/assignmentInterface.ts

This file was deleted.

49 changes: 49 additions & 0 deletions backend/src/core/repositories/assignmentRepositoryInterface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Assignment } from "../entities/assignment";
import { AbstractRepository } from "./AbstractRepository";

/**
* Interface for assignment repositories.
* Allows CRUD operations on assignment entities.
*/
export abstract class IAssignmentRepository extends AbstractRepository {

/**
* Inserts a new assignment into the repository. The `id` field of the assignment should be empty.
* @param assignment The assignment object to be created.
* @param teacherId The id of the teacher that is creating the assignment.
* @throws EntityNotFoundError when no the class that the assigment belongs to does not exist.
* @throws EntityNotFoundError when the teacher does not exist.
* @returns A promise that resolves to the created assignment.
*/
public abstract createAssignment(assignment: Assignment, teacherId: string): Promise<Assignment>;

/**
* Get an assignment by its id.
* @param id The id of the assignment.
* @throws EntityNotFoundError when no assignment is found.
* @returns A promise that resolves to the retrieved assignment.
*/
public abstract getAssignmentById(id: string): Promise<Assignment>;

/**
* Get all assignments associated with a specific class id.
* @param classId The id of the class
* @returns A promise that resolves to an array of assignments.
*/
public abstract getAssignmentsByClassId(classId: string): Promise<Assignment[]>;

/**
* Get all assignments associated with a specific learning path id.
* @param learningPathId The id of the learning path
* @returns A promise that resolves to an array of assignments.
*/
public abstract getAssignmentsByLearningPathId(learningPathId: string): Promise<Assignment[]>;

/**
* Deletes an assignment by its id.
* @param id - The id of the assignment to delete.
* @returns A promise that resolves when the assignment is deleted.
*/
public abstract deleteAssignmentById(id: string): Promise<void>;

}
27 changes: 6 additions & 21 deletions backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,9 @@

import express from "express";
import dotenv from "dotenv";
import { IDatasource } from "./infrastructure/database/data/data_sources/datasourceInterface";
import { DatasourceTypeORM } from "./infrastructure/database/data/data_sources/typeorm/datasourceTypeORM";
import { JoinRequestRepositoryTypeORM } from "./infrastructure/repositories/joinRequestRepositoryTypeORM";
import { JoinRequest, JoinRequestType } from "./core/entities/joinRequest";
import { ITeacherRepository } from "./core/repositories/teacherRepositoryInterface";
import { TeacherRepositoryTypeORM } from "./infrastructure/repositories/teacherRepositoryTypeORM";
import { IClassRepository } from "./core/repositories/classRepositoryInterface";
import { ClassRepositoryTypeORM } from "./infrastructure/repositories/classRepositoryTypeORM";
import { Teacher } from "./core/entities/teacher";
import { Class } from "./core/entities/class";
import { IAssignmentRepository } from "./core/repositories/assignmentRepositoryInterface";
import { AssignmentRepositoryTypeORM } from "./infrastructure/repositories/assignmentRepositoryTypeORM";

dotenv.config();

Expand All @@ -20,25 +13,17 @@ const port = process.env.PORT || 3000;

// TODO: implement backend application

// Initialize the datasource
// Initialize a datasource
const datasource = new DatasourceTypeORM();

// TODO: remove, this is a stupid small test function
async function test() {
const datasource: IDatasource = new DatasourceTypeORM();
const assignmentRepo: IAssignmentRepository = new AssignmentRepositoryTypeORM();

const teacherRepo: ITeacherRepository = new TeacherRepositoryTypeORM();
const classRepo: IClassRepository = new ClassRepositoryTypeORM();
const joinRequestRepo = new JoinRequestRepositoryTypeORM();

const teacher: Teacher = await teacherRepo.getTeacherByEmail("email@email.com");
const _class: Class = await classRepo.getClassByName("Math");

let joinRequest: JoinRequest = new JoinRequest(teacher.id!, _class.id!, JoinRequestType.TEACHER);

joinRequest = await joinRequestRepo.createJoinRequest(joinRequest);
console.log(await assignmentRepo.getAssignmentsByLearningPathId("123"));
}


app.get('/', (req, res) => {
res.send("Hello, World!\n");
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Entity, PrimaryGeneratedColumn, Column, OneToOne, JoinColumn } from "typeorm"
import { ClassTypeORM } from "./classTypeorm"
import { Assignment } from "../../../../core/entities/assignment"

@Entity()
export class AssignmentTypeORM {
Expand All @@ -21,4 +22,16 @@ export class AssignmentTypeORM {

@Column({ type: "text" })
extra_instructions!: string

public toAssignmentEntity(): Assignment {
return new Assignment(
this.class.id!,
this.learning_path_id,
this.start,
this.deadline,
this.extra_instructions,
this.id
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,4 @@ export class JoinRequestTypeORM {
);
}

public static createJoinRequestTypeORM(joinRequest: JoinRequest, user: UserTypeORM, class_: ClassTypeORM): JoinRequestTypeORM {
const joinRequestTypeORM = new JoinRequestTypeORM();
joinRequestTypeORM.requester = user;
joinRequestTypeORM.class = class_;
joinRequestTypeORM.type = joinRequest.type === JoinRequestType.TEACHER ? JoinAsType.TEACHER : JoinAsType.STUDENT;
return joinRequestTypeORM;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { DataSource } from "typeorm";
import { Assignment } from "../../../../core/entities/assignment";

/**
* Interface for the assignment data source
*/
export abstract class IDatasourceAssignment {

public constructor(
protected datasource: DataSource
) {}

/**
* Insert a new assignment in the database. The `id` field of the assignment should be empty.
* The `id` field will be set by the database to a uuid.
*
* @param assignment The new assignment to insert.
* @param teacherId The id of the teacher that is creating the assignment.
* @returns A promise that resolves to the inserted assignment.
*/
public abstract createAssignment(assignment: Assignment, teacherId: string): Promise<Assignment>;

/**
* Get an assignment by its id.
*
* @param id The id of the assignment.
* @returns A promise that resolves to the assignment with the given id or null if no results are found.
*/
public abstract getAssignmentById(id: string): Promise<Assignment|null>;

/**
* Get all assignments with a specific class id.
*
* @param classId The id of the class.
* @returns A promise that resolves to an array of all assignments with the given class id.
*/
public abstract getAssignmentsByClassId(classId: string): Promise<Assignment[]>;

/**
* Get all assignments with a specific learning path id.
*
* @param learningPathId The id of the learning path.
* @returns A promise that resolves to an array of all assignments with the given learning path id.
*/
public abstract getAssignmentsByLearningPathId(learningPathId: string): Promise<Assignment[]>;

/**
* Delete an assignment by its id.
*
* @param id The id of the assignment to delete.
* @returns A promise that resolves when the assignment is deleted.
*/
public abstract deleteAssignmentById(id: string): Promise<void>;

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { IDatasourceAssignment } from "./datasourceAssignmentInterface";
import { IDatasourceClass } from "./datasourceClassInterface";
import { IDatasourceJoinRequest } from "./datasourceJoinRequestInterface";
import { IDatasourceTeacher } from "./datasourceTeacherInterface";
Expand Down Expand Up @@ -25,4 +26,10 @@ export interface IDatasource {
*/
getDatasourceJoinRequest(): Promise<IDatasourceJoinRequest>;

/**
* Retrieves the data source for assignments;
* @returns A promise that resolves to an instance of `IDatasourceAssignment`.
*/
getDatasourceAssignment(): Promise<IDatasourceAssignment>;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { EntityNotFoundError } from "../../../../../config/error";
import { Assignment } from "../../../../../core/entities/assignment";
import { AssignmentTypeORM } from "../../data_models/assignmentTypeorm";
import { ClassTypeORM } from "../../data_models/classTypeorm";
import { TeacherTypeORM } from "../../data_models/teacherTypeorm";
import { IDatasourceAssignment } from "../datasourceAssignmentInterface";

export class DatasourceAssignmentTypeORM extends IDatasourceAssignment {

public async createAssignment(newAssignment: Assignment, teacherId: string): Promise<Assignment> {
// Check if the class exists
const classModel: ClassTypeORM | null = await this.datasource
.getRepository(ClassTypeORM)
.findOne({ where: { id: newAssignment.classId } });

if (!classModel) {
throw new EntityNotFoundError(`Class with id ${newAssignment.classId} not found`);
}

// Class exists, check if teacher exists
const teacherModel: TeacherTypeORM | null = await this.datasource
.getRepository(TeacherTypeORM)
.findOne({ where: { id: teacherId } });

if(!teacherModel) {
throw new EntityNotFoundError(`Teacher with id ${teacherId} not found`);
}

// Class exists and teacher exist: insert assignment into the database
const assignmentModel: AssignmentTypeORM = this.datasource
.getRepository(AssignmentTypeORM)
.create({
class: { id: newAssignment.classId },
learning_path_id: newAssignment.learningPathId,
start: newAssignment.startDate,
deadline: newAssignment.deadline,
extra_instructions: newAssignment.extraInstructions
});

await this.datasource.getRepository(AssignmentTypeORM).save(assignmentModel);

return assignmentModel.toAssignmentEntity();
}

public async getAssignmentById(id: string): Promise<Assignment|null> {
const assignmentModel: AssignmentTypeORM | null = await this.datasource
.getRepository(AssignmentTypeORM)
.findOne({
where: { id: id },
relations: ["class"]
});

if (assignmentModel !== null) {
return assignmentModel.toAssignmentEntity();
}
return null;
}

public async getAssignmentsByClassId(classId: string): Promise<Assignment[]> {
const assignmentModels: AssignmentTypeORM[] = await this.datasource
.getRepository(AssignmentTypeORM)
.find({
where: { class: { id: classId } },
relations: ["class"]
});

return assignmentModels.map((assignmentModel: AssignmentTypeORM) => assignmentModel.toAssignmentEntity());
}

public async getAssignmentsByLearningPathId(learningPathId: string): Promise<Assignment[]> {
const assignmentModels: AssignmentTypeORM[] = await this.datasource
.getRepository(AssignmentTypeORM)
.find({
where: { learning_path_id: learningPathId },
relations: ["class"]
});

return assignmentModels.map((assignmentModel: AssignmentTypeORM) => assignmentModel.toAssignmentEntity());
}

public async deleteAssignmentById(id: string): Promise<void> {
await this.datasource.getRepository(AssignmentTypeORM).delete(id);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { IDatasourceClass } from "../datasourceClassInterface";
import { DatasourceClassTypeORM } from "./datasourceClassTypeORM";
import { DatasourceJoinRequestTypeORM } from "./datasourceJoinRequestTypeORM";
import { IDatasourceJoinRequest } from "../datasourceJoinRequestInterface";
import { IDatasourceAssignment } from "../datasourceAssignmentInterface";
import { DatasourceAssignmentTypeORM } from "./datasourceAssignmentTypeORM";

export class DatasourceTypeORM implements IDatasource {

Expand Down Expand Up @@ -41,5 +43,9 @@ export class DatasourceTypeORM implements IDatasource {
public async getDatasourceJoinRequest(): Promise<IDatasourceJoinRequest> {
return new DatasourceJoinRequestTypeORM(await DatasourceTypeORM.datasourcePromise);
}

public async getDatasourceAssignment(): Promise<IDatasourceAssignment> {
return new DatasourceAssignmentTypeORM(await DatasourceTypeORM.datasourcePromise);
}

}
Loading