Skip to content

Commit

Permalink
Merge branch 'development' into helper-error-translate
Browse files Browse the repository at this point in the history
  • Loading branch information
tyboro2002 authored Mar 27, 2024
2 parents f6efa3e + 97c58cb commit 6412b96
Show file tree
Hide file tree
Showing 15 changed files with 365 additions and 92 deletions.
23 changes: 17 additions & 6 deletions frontend/src/composables/services/admins.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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';
import {ComposerTranslation} from "vue-i18n";

Expand All @@ -10,22 +10,33 @@ export function useAdmin() {
const admin = ref<Admin|null>(null);
const toast = useToast();

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

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

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

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

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,31 +1,59 @@
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';
import {ComposerTranslation} from "vue-i18n";

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, t: ComposerTranslation) {
const endpoint = endpoints.assistants.retrieve.replace('{id}', id.toString());
async function getAssistantByID(id: string, t: ComposerTranslation) {
const endpoint = endpoints.assistants.retrieve.replace('{id}', id);
get<Assistant>(endpoint, assistant, Assistant.fromJSON, toast, t);
}

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

async function getAssistants(t: ComposerTranslation) {
const endpoint = endpoints.assistants.index;
getList<Assistant>(endpoint, assistants, Assistant.fromJSON, toast, t);
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, t: ComposerTranslation) {
const endpoint = endpoints.assistants.byCourse.replace('{course_id}', course_id);
create<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);
}

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

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,7 +1,7 @@
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';
import {ComposerTranslation} from "vue-i18n";

Expand All @@ -10,29 +10,48 @@ export function useCourses() {
const course = ref<Course|null>(null);
const toast = useToast();

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

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

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

async function createCourse(course_data: any, t: ComposerTranslation) {
const endpoint = endpoints.courses.index;
create<Course>(endpoint, course_data, course, Course.fromJSON, toast, t);
}

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

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



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';
import {ComposerTranslation} from "vue-i18n";

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

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

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

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

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,7 +1,7 @@
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';
import {ComposerTranslation} from "vue-i18n";

Expand All @@ -10,22 +10,33 @@ export function useGroup() {
const group = ref<Group|null>(null);
const toast = useToast();

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

async function getGroupsByProject(project_id: number, t: ComposerTranslation) {
const endpoint = endpoints.groups.byProject.replace('{project_id}', project_id.toString());
async function getGroupsByProject(project_id: string, t: ComposerTranslation) {
const endpoint = endpoints.groups.byProject.replace('{project_id}', project_id);
getList<Group>(endpoint, groups, Group.fromJSON, toast, t);
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, t: ComposerTranslation) {
const endpoint = endpoints.groups.byProject.replace('{group_id}', group_id);
create<Group>(endpoint, group_data, group, Group.fromJSON, toast, t);
}

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

return {
groups,
group,
getGroupByID,
getGroupsByProject
getGroupsByProject,

createGroup,
deleteGroup
};
}
33 changes: 30 additions & 3 deletions frontend/src/composables/services/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,39 @@ 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, t: ComposerTranslation): Promise<void> {
export async function create<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.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, t);
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, t: ComposerTranslation): 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, 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 => {
ref.value = response.data.map((data: T) => fromJson(data));
//toast.add({severity: "success", summary: "Success Message", detail: "Order submitted", life: lifeTime});
console.log(ref.value);
}
).catch((error: AxiosError) => {
processError(error, toast, t);
Expand Down Expand Up @@ -49,7 +76,7 @@ export async function getListMerged<T>(endpoints: string[], ref: Ref<T[]|null>,
ref.value = allData;
}

function processError(error: AxiosError, toast:any, t: ComposerTranslation){
export function processError(error: AxiosError, toast:any, t: ComposerTranslation){
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 6412b96

Please sign in to comment.