Skip to content

Commit

Permalink
Merge pull request #179 from SELab-2/services
Browse files Browse the repository at this point in the history
Services pr to make sure no further merge conflicts are happening
  • Loading branch information
DeLany123 authored Mar 27, 2024
2 parents ba55d2a + b81442b commit 0e5038c
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 31 deletions.
6 changes: 3 additions & 3 deletions frontend/src/assets/lang/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@
"helpers": {
"errors": {
"notFound": "Niet Gevonden",
"notFoundDetail": "Resource niet gevonden.",
"notFoundDetail": "Bron niet gevonden.",
"unauthorized": "Onbevoegd",
"unauthorizedDetail": "Je bent niet bevoegd om deze resource te bereiken.",
"unauthorizedDetail": "Je bent niet bevoegd om deze bron te bereiken.",
"server": "Server Fout",
"serverDetail": "Er vond een fout plaats op de server.",
"network": "Netwerk Fout",
"networkDetail": "Kan de server niet bereiken.",
"request": "Verzoek Fout",
"request": "Fout verzoek",
"requestDetail": "Een fout vond plaats tijdens het maken van het verzoek."
}
}
Expand Down
10 changes: 8 additions & 2 deletions frontend/src/composables/services/assistant.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Assistant} from '@/types/Assistant.ts';
import { Response } from '@/types/Response';
import {ref} from 'vue';
import {endpoints} from '@/config/endpoints.ts';
import { get, getList, create, delete_id } from '@/composables/services/helpers.ts';
import { get, getList, create, delete_id, delete_id_with_data } from '@/composables/services/helpers.ts';
import { useToast } from 'primevue/usetoast';
import {ComposerTranslation} from "vue-i18n";

Expand Down Expand Up @@ -32,6 +32,11 @@ export function useAssistant() {
create<Response>(endpoint, {assistant_id: assistant_id}, response, Response.fromJSON, toast, t);
}

async function assistantLeaveCourse(course_id: string, assistant_id: string, t: ComposerTranslation) {
const endpoint = endpoints.assistants.byCourse.replace('{course_id}', course_id);
delete_id_with_data<Response>(endpoint, {assistant_id: assistant_id}, response, Response.fromJSON, toast, t);
}

async function createAssistant(assistant_data: any, t: ComposerTranslation) {
const endpoint = endpoints.assistants.index;
create<Assistant>(endpoint, assistant_data, assistant, Assistant.fromJSON, toast, t);
Expand All @@ -54,6 +59,7 @@ export function useAssistant() {
createAssistant,
deleteAssistant,

assistantJoinCourse
assistantJoinCourse,
assistantLeaveCourse
};
}
17 changes: 14 additions & 3 deletions frontend/src/composables/services/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import axios, { AxiosError, AxiosResponse } from 'axios';
// import { useI18n } from 'vue-i18n';
import {Ref} from 'vue';
import {ComposerTranslation} from "vue-i18n";
const lifeTime = 3000;

// const { t } = useI18n();

export async function get<T>(endpoint: string, ref: Ref<T|null>, fromJson: (data: any) => T, toast:any, t: ComposerTranslation): Promise<void> {
await axios.get(endpoint).then((response: AxiosResponse) => {
ref.value = fromJson(response.data);
Expand Down Expand Up @@ -44,6 +41,20 @@ export async function delete_id<T>(endpoint: string, ref: Ref<T|null>, fromJson:
});
}

export async function delete_id_with_data<T>(endpoint: string, data: any, ref: Ref<T|null>, fromJson: (data: any) => T, toast:any, t: ComposerTranslation): Promise<void> {
const headers = {
// TODO change this to your token
Authorization: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzQyODQwMjY1LCJpYXQiOjE3MTEzMDQyNjUsImp0aSI6ImQwYTgxY2YxMzU5NTQ4OWQ4OGNiZDFmZmZiMGI0MmJhIiwidXNlcl9pZCI6IjAwMDIwMTI0NzAxMSJ9.izGK0MStcMiPkOAWs0wgWsYEs0_5S1WvsleWaIcttnk"
};
await axios.delete(endpoint,{ headers, data }).then((response: AxiosResponse) => {
ref.value = fromJson(response.data);
//toast.add({severity: "success", summary: "Success Message", detail: "Order submitted", life: lifeTime});
}).catch((error: AxiosError) => {
processError(error, toast, t);
console.error(error); // Log the error for debugging
});
}


export async function getList<T>(endpoint: string, ref: Ref<T[]|null>, fromJson: (data: any) => T, toast:any, t: ComposerTranslation): Promise<void> {
await axios.get(endpoint).then(response => {
Expand Down
18 changes: 12 additions & 6 deletions frontend/src/composables/services/students.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Student} from '@/types/Student';
import { Response } from '@/types/Response';
import {ref} from 'vue';
import {endpoints} from '@/config/endpoints.ts';
import { get, getList, create, delete_id} from '@/composables/services/helpers.ts';
import { get, getList, create, delete_id, delete_id_with_data } from '@/composables/services/helpers.ts';
import { useToast } from 'primevue/usetoast';
import {ComposerTranslation} from "vue-i18n";

Expand Down Expand Up @@ -36,17 +36,22 @@ export function useStudents() {
const endpoint = endpoints.students.byCourse.replace('{course_id}', course_id);
create<Response>(endpoint, {student_id: student_id}, response, Response.fromJSON, toast, t);
}
/*

async function studentLeaveCourse(course_id: string, student_id: string, t: ComposerTranslation) {
const endpoint = endpoints.students.byCourse.replace('{course_id}', course_id);
delete_id_with<Response>(endpoint, {student_id: student_id}, response, Response.fromJSON, toast, t);
}*/
delete_id_with_data<Response>(endpoint, {student_id: student_id}, response, Response.fromJSON, toast, t);
}

async function studentJoinGroup(group_id: string, student_id: string, t: ComposerTranslation) {
const endpoint = endpoints.students.byGroup.replace('{group_id}', group_id);
create<Response>(endpoint, {student_id: student_id}, response, Response.fromJSON, toast, t);
}

async function studentLeaveGroup(group_id: string, student_id: string, t: ComposerTranslation) {
const endpoint = endpoints.students.byGroup.replace('{group_id}', group_id);
delete_id_with_data<Response>(endpoint, {student_id: student_id}, response, Response.fromJSON, toast, t);
}

async function createStudent(student_data: any, t: ComposerTranslation) {
const endpoint = endpoints.students.index;
create<Student>(endpoint, student_data, student, Student.fromJSON, toast, t);
Expand All @@ -72,7 +77,8 @@ export function useStudents() {
deleteStudent,

studentJoinCourse,
// studentLeaveCourse,
studentJoinGroup
studentLeaveCourse,
studentJoinGroup,
studentLeaveGroup
};
}
10 changes: 8 additions & 2 deletions frontend/src/composables/services/teachers.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Teacher} from '@/types/Teacher.ts';
import { Response } from '@/types/Response';
import {ref} from 'vue';
import {endpoints} from '@/config/endpoints.ts';
import { get, getList, create, delete_id } from '@/composables/services/helpers.ts';
import { get, getList, create, delete_id, delete_id_with_data } from '@/composables/services/helpers.ts';
import { useToast } from 'primevue/usetoast';
import {ComposerTranslation} from "vue-i18n";

Expand Down Expand Up @@ -32,6 +32,11 @@ export function useTeacher() {
create<Response>(endpoint, {teacher_id: teacher_id}, response, Response.fromJSON, toast, t);
}

async function teacherLeaveCourse(course_id: string, teacher_id: string, t: ComposerTranslation) {
const endpoint = endpoints.teachers.byCourse.replace('{course_id}', course_id);
delete_id_with_data<Response>(endpoint, {teacher_id: teacher_id}, response, Response.fromJSON, toast, t);
}

async function createTeacher(teacher_data: any, t: ComposerTranslation) {
const endpoint = endpoints.teachers.index;
create<Teacher>(endpoint, teacher_data, teacher, Teacher.fromJSON, toast, t);
Expand All @@ -54,6 +59,7 @@ export function useTeacher() {
createTeacher,
deleteTeacher,

teacherJoinCourse
teacherJoinCourse,
teacherLeaveCourse
};
}
2 changes: 1 addition & 1 deletion frontend/src/types/Admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Faculty } from "./Faculty";

export class Admin {
constructor(
public id: number,
public id: string,
public last_login: Date |null,
public username: string,
public is_staff: boolean,
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/types/Assistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Faculty } from "./Faculty";

export class Assistant {
constructor(
public id: number,
public id: string,
public last_login: Date |null,
public username: string,
public is_staff: boolean,
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/types/Course.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export class Course {
constructor(
public id: number,
public id: string,
public name: string,
public description: string,
public academic_startyear: number
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/types/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Submission } from "./Submission";

export class Group {
constructor(
public id: number,
public id: string,
public score: number = -1,
public projects: Project[] = [],
public students: Student[] = [],
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/types/Projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Submission } from "./Submission";

export class Project {
constructor(
public id: number,
public id: string,
public name: string,
public description: string,
public visible: boolean,
Expand All @@ -19,15 +19,15 @@ export class Project {
public group_size: number,

public course: Course = new Course(
-1,
"-1",
"default",
"this is a default project given in the service because it isnt initiated",
0
), //TODO check
public structure_checks: Structure_check[] = [],
public extra_checks: Extra_check[] = [],
public groups: Group[] = [],
public submissions: Submission = new Submission(0,0,new Date(), false), //TODO check
public submissions: Submission = new Submission("0",0,new Date(), false), //TODO check
) {
}

Expand Down
4 changes: 2 additions & 2 deletions frontend/src/types/Structure_check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { Project } from "./Projects";

export class Structure_check {
constructor(
public id: number,
public id: string,
public name: string,
public obligated_extensions: File_extension[] = [],
public blocked_extensions: File_extension[] = [],
public project: Project = new Project(
0,
"0",
"default",
"this is a default project given in the service because it isnt initiated",
false,
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/types/Student.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Group } from "./Group";

export class Student {
constructor(
public id: number,
public id: string,
public last_login: Date |null,
public username: string,
public is_staff: boolean,
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/types/Submission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { Group } from "./Group";

export class Submission {
constructor(
public id: number,
public id: string,
public submission_number: number,
public submission_time: Date,
public structure_checks_passed: boolean,
public group: Group = new Group(0),
public group: Group = new Group("0"),
public files: File[] = [], //TODO check
public extra_checks_results : any[] = [], // TODO

Expand Down
4 changes: 2 additions & 2 deletions frontend/src/types/Teacher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Faculty } from "./Faculty";

export class Teacher {
constructor(
public id: number,
public last_login: Date |null,
public id: string,
public last_login: Date | null,
public username: string,
public is_staff: boolean,
public email: string,
Expand Down
10 changes: 9 additions & 1 deletion frontend/src/views/dashboard/DashboardView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const { t } = useI18n();
/* Service injection */
const { courses, getCoursesByStudent, createCourse, deleteCourse } = useCourses();
const { studentJoinCourse } = useStudents();
const { studentJoinCourse, studentLeaveCourse } = useStudents();
onMounted(async () => {
Expand Down Expand Up @@ -51,6 +51,13 @@ const handleDelete = () => {
deleteCourse(idValue.value, t);
};
// Function to handle form submission
const handleLeave = () => {
// Perform actions here, such as sending the input value to a backend API
console.log('Submitted value:', idValue.value);
studentLeaveCourse(idValue.value, "1");
};
// test code ^^^^
</script>
Expand All @@ -73,6 +80,7 @@ const handleDelete = () => {
<input type="text" v-model="idValue" placeholder="ID" />
<button @click="handleSubmit">join course with id</button>
<button @click="handleDelete">delete course with id</button>
<button @click="handleLeave">leave course with id</button>
<!--extra code to test -->

<!-- Course list body -->
Expand Down

0 comments on commit 0e5038c

Please sign in to comment.