Skip to content

Commit

Permalink
Merge pull request #174 from SELab-2/services
Browse files Browse the repository at this point in the history
Services
  • Loading branch information
DeLany123 authored Mar 26, 2024
2 parents 8b8a16e + c7bdde9 commit 97c58cb
Show file tree
Hide file tree
Showing 15 changed files with 365 additions and 91 deletions.
24 changes: 18 additions & 6 deletions frontend/src/composables/services/admins.service.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
import {Admin} from '@/types/Admin.ts';
import {ref} from 'vue';
import {endpoints} from '@/config/endpoints.ts';
import { get, getList } from '@/composables/services/helpers.ts';
import { get, getList, create, delete_id } from '@/composables/services/helpers.ts';
import { useToast } from 'primevue/usetoast';

export function useAdmin() {
const admins = ref<Admin[]|null>(null);
const admin = ref<Admin|null>(null);
const toast = useToast();

async function getAdminByID(id: number) {
const endpoint = endpoints.admins.retrieve.replace('{id}', id.toString());
async function getAdminByID(id: string) {
const endpoint = endpoints.admins.retrieve.replace('{id}', id);
get<Admin>(endpoint, admin, Admin.fromJSON, toast);
console.log(admin)
}

async function getAdmins() {
const endpoint = endpoints.admins.index;
getList<Admin>(endpoint, admins, Admin.fromJSON, toast);
console.log(admins.value ? admins.value.map((admin, index) => `Admin ${index + 1}: ${JSON.stringify(admin)}`) : 'Admins is null');
}


async function createAdmin(admin_data: any) {
const endpoint = endpoints.admins.index;
create<Admin>(endpoint, admin_data, admin, Admin.fromJSON, toast);
}

async function deleteAdmin(id: string) {
const endpoint = endpoints.admins.retrieve.replace('{id}', id.toString());
delete_id<Admin>(endpoint, admin, Admin.fromJSON, toast);
}

return {
admins,
admin,
getAdminByID,
getAdmins
getAdmins,

createAdmin,
deleteAdmin
};
}
40 changes: 34 additions & 6 deletions frontend/src/composables/services/assistant.service.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,58 @@
import {Assistant} from '@/types/Assistant.ts';
import { Response } from '@/types/Response';
import {ref} from 'vue';
import {endpoints} from '@/config/endpoints.ts';
import { get, getList } from '@/composables/services/helpers.ts';
import { get, getList, create, delete_id } from '@/composables/services/helpers.ts';
import { useToast } from 'primevue/usetoast';

export function useAssistant() {
const assistants = ref<Assistant[]|null>(null);
const assistant = ref<Assistant|null>(null);
const response = ref<Response|null>(null);
const toast = useToast();

async function getAssistantByID(id: number) {
const endpoint = endpoints.assistants.retrieve.replace('{id}', id.toString());
async function getAssistantByID(id: string) {
const endpoint = endpoints.assistants.retrieve.replace('{id}', id);
get<Assistant>(endpoint, assistant, Assistant.fromJSON, toast);
}

async function getAssistantByCourse(course_id: string) {
const endpoint = endpoints.assistants.byCourse.replace('{course_id}', course_id);
get<Assistant>(endpoint, assistant, Assistant.fromJSON, toast);
console.log(assistant)
}

async function getAssistants() {
const endpoint = endpoints.assistants.index;
getList<Assistant>(endpoint, assistants, Assistant.fromJSON, toast);
console.log(assistants.value ? assistants.value.map((assistant, index) => `Assistant ${index + 1}: ${JSON.stringify(assistant)}`) : 'assistants is null');
}

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

async function createAssistant(assistant_data: any) {
const endpoint = endpoints.assistants.index;
create<Assistant>(endpoint, assistant_data, assistant, Assistant.fromJSON, toast);
}

async function deleteAssistant(id: string) {
const endpoint = endpoints.admins.retrieve.replace('{id}', id.toString());
delete_id<Assistant>(endpoint, assistant, Assistant.fromJSON, toast);
}

return {
assistants,
assistant,
response,

getAssistantByID,
getAssistants
getAssistantByCourse,
getAssistants,

createAssistant,
deleteAssistant,

assistantJoinCourse
};
}
37 changes: 28 additions & 9 deletions frontend/src/composables/services/courses.service.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,56 @@
import {Course} from '@/types/Course.ts';
import {ref} from 'vue';
import {endpoints} from '@/config/endpoints.ts';
import { get, getList } from '@/composables/services/helpers.ts';
import { get, getList, create, delete_id } from '@/composables/services/helpers.ts';
import { useToast } from 'primevue/usetoast';

export function useCourses() {
const courses = ref<Course[]|null>(null);
const course = ref<Course|null>(null);
const toast = useToast();

async function getCourseByID(id: number) {
const endpoint = endpoints.courses.retrieve.replace('{id}', id.toString());
async function getCourseByID(id: string) {
const endpoint = endpoints.courses.retrieve.replace('{id}', id);
get<Course>(endpoint, course, Course.fromJSON, toast);
console.log(course.value);
}

async function getCourses() {
const endpoint = endpoints.courses.index;
getList<Course>(endpoint, courses, Course.fromJSON, toast);
console.log(courses.value ? courses.value.map((course, index) => `Course ${index + 1}: ${JSON.stringify(course)}`) : 'Courses is null');
}

async function getCoursesByStudent(student_id: number) {
const endpoint = endpoints.courses.byStudent.replace('{student_id}', student_id.toString());
async function createCourse(course_data: any) {
const endpoint = endpoints.courses.index;
create<Course>(endpoint, course_data, course, Course.fromJSON, toast);
}

async function cloneCourse(course_id: string, clone_assistants: boolean) {
const endpoint = endpoints.courses.clone.replace('{course_id}', course_id);
create<Course>(endpoint, {clone_assistants: clone_assistants.toString() }, course, Course.fromJSON, toast);
}

async function deleteCourse(id: string) {
const endpoint = endpoints.courses.retrieve.replace('{id}', id);
delete_id<Course>(endpoint, course, Course.fromJSON, toast);
}

async function getCoursesByStudent(student_id: string) {
const endpoint = endpoints.courses.byStudent.replace('{student_id}', student_id);
getList<Course>(endpoint, courses, Course.fromJSON, toast);
console.log(courses.value ? courses.value.map((course, index) => `Course ${index + 1}: ${JSON.stringify(course)}`) : 'Courses is null');
}



return {
courses,
course,

getCourseByID,
getCourses,
getCoursesByStudent
getCoursesByStudent,

createCourse,
cloneCourse,
deleteCourse
};
}
19 changes: 15 additions & 4 deletions frontend/src/composables/services/faculties.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Faculty} from '@/types/Faculty.ts';
import {ref} from 'vue';
import {endpoints} from '@/config/endpoints.ts';
import { get, getList } from '@/composables/services/helpers.ts';
import { get, getList, create, delete_id } from '@/composables/services/helpers.ts';
import { useToast } from 'primevue/usetoast';

export function useFaculty() {
Expand All @@ -12,19 +12,30 @@ export function useFaculty() {
async function getFacultyByID(name: string) {
const endpoint = endpoints.faculties.retrieve.replace('{name}', name);
get<Faculty>(endpoint, faculty, Faculty.fromJSON, toast);
console.log(faculty)
}

async function getFacultys() {
const endpoint = endpoints.faculties.index;
getList<Faculty>(endpoint, faculties, Faculty.fromJSON, toast);
console.log(faculties.value ? faculties.value.map((faculty, index) => `Faculty ${index + 1}: ${JSON.stringify(faculty)}`) : 'Facultys is null');
}

async function createFaculty(faculty_data: any) {
const endpoint = endpoints.faculties.index;
create<Faculty>(endpoint, faculty_data, faculty, Faculty.fromJSON, toast);
}

async function deleteFaculty(id: string) {
const endpoint = endpoints.faculties.retrieve.replace('{id}', id);
delete_id<Faculty>(endpoint, faculty, Faculty.fromJSON, toast);
}

return {
faculties,
faculty,
getFacultyByID,
getFacultys
getFacultys,

createFaculty,
deleteFaculty
};
}
27 changes: 19 additions & 8 deletions frontend/src/composables/services/groups.service.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
import {Group} from '@/types/Group.ts';
import {ref} from 'vue';
import {endpoints} from '@/config/endpoints.ts';
import { get, getList } from '@/composables/services/helpers.ts';
import { get, getList, create, delete_id } from '@/composables/services/helpers.ts';
import { useToast } from 'primevue/usetoast';

export function useGroup() {
const groups = ref<Group[]|null>(null);
const group = ref<Group|null>(null);
const toast = useToast();

async function getGroupByID(id: number) {
const endpoint = endpoints.groups.retrieve.replace('{id}', id.toString());
async function getGroupByID(id: string) {
const endpoint = endpoints.groups.retrieve.replace('{id}', id);
get<Group>(endpoint, group, Group.fromJSON, toast);
console.log(group)
}

async function getGroupsByProject(project_id: number) {
const endpoint = endpoints.groups.byProject.replace('{project_id}', project_id.toString());
async function getGroupsByProject(project_id: string) {
const endpoint = endpoints.groups.byProject.replace('{project_id}', project_id);
getList<Group>(endpoint, groups, Group.fromJSON, toast);
console.log(groups.value ? groups.value.map((group, index) => `Group ${index + 1}: ${JSON.stringify(group)}`) : 'Groups is null');
}

async function createGroup(group_data: any, group_id: string) {
const endpoint = endpoints.groups.byProject.replace('{group_id}', group_id);
create<Group>(endpoint, group_data, group, Group.fromJSON, toast);
}

async function deleteGroup(id: string) {
const endpoint = endpoints.groups.retrieve.replace('{id}', id);
delete_id<Group>(endpoint, group, Group.fromJSON, toast);
}

return {
groups,
group,
getGroupByID,
getGroupsByProject
getGroupsByProject,

createGroup,
deleteGroup
};
}
32 changes: 30 additions & 2 deletions frontend/src/composables/services/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,36 @@ export async function get<T>(endpoint: string, ref: Ref<T|null>, fromJson: (data
});
}

export async function getList<T>(endpoint: string, ref: Ref<T[]|null>, fromJson: (data: any) => T, toast:any): Promise<void> {
export async function create<T>(endpoint: string, data:any, ref: Ref<T|null>, fromJson: (data: any) => T, toast:any): Promise<void> {
const headers = {
// TODO change this to your token
Authorization: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzQyODQwMjY1LCJpYXQiOjE3MTEzMDQyNjUsImp0aSI6ImQwYTgxY2YxMzU5NTQ4OWQ4OGNiZDFmZmZiMGI0MmJhIiwidXNlcl9pZCI6IjAwMDIwMTI0NzAxMSJ9.izGK0MStcMiPkOAWs0wgWsYEs0_5S1WvsleWaIcttnk"
};
await axios.post(endpoint, data, { headers }).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);
console.error(error); // Log the error for debugging
});
}

export async function delete_id<T>(endpoint: string, ref: Ref<T|null>, fromJson: (data: any) => T, toast:any): Promise<void> {
const headers = {
// TODO change this to your token
Authorization: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzQyODQwMjY1LCJpYXQiOjE3MTEzMDQyNjUsImp0aSI6ImQwYTgxY2YxMzU5NTQ4OWQ4OGNiZDFmZmZiMGI0MmJhIiwidXNlcl9pZCI6IjAwMDIwMTI0NzAxMSJ9.izGK0MStcMiPkOAWs0wgWsYEs0_5S1WvsleWaIcttnk"
};
await axios.delete(endpoint,{ headers }).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);
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): Promise<void> {
await axios.get(endpoint).then(response => {
ref.value = response.data.map((data: T) => fromJson(data));
//toast.add({severity: "success", summary: "Success Message", detail: "Order submitted", life: lifeTime});
Expand Down Expand Up @@ -45,7 +73,7 @@ export async function getListMerged<T>(endpoints: string[], ref: Ref<T[]|null>,
ref.value = allData;
}

function processError(error: AxiosError, toast:any){
export function processError(error: AxiosError, toast:any){
if (error.response) {
console.log(error.response.status);
// The request was made and the server responded with a status code
Expand Down
Loading

0 comments on commit 97c58cb

Please sign in to comment.