Skip to content

Commit

Permalink
project frontend to backend connect api with test
Browse files Browse the repository at this point in the history
  • Loading branch information
ZHallen122 committed Oct 24, 2024
1 parent 40b4158 commit b89e3b7
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 0 deletions.
129 changes: 129 additions & 0 deletions frontend/src/app/api/project/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { gql } from '@apollo/client';
import client from '../../../utils/apolloClient';

// Define the queries and mutations

// Fetch user projects
export const GET_USER_PROJECTS = gql`
query GetUserProjects {
getUserProjects {
id
projectName
path
projectPackages {
id
content
}
}
}
`;

export const getUserProjects = async (): Promise<any> => {
try {
const response = await client.query({
query: GET_USER_PROJECTS,
});
return response.data.getUserProjects;
} catch (error) {
console.error('Error fetching user projects:', error);
throw error;
}
};

// Fetch project details
export const GET_PROJECT_DETAILS = gql`
query GetProjectDetails($projectId: String!) {
getProjectDetails(projectId: $projectId) {
id
projectName
path
projectPackages {
id
content
}
}
}
`;

export const getProjectDetails = async (projectId: string): Promise<any> => {
try {
const response = await client.query({
query: GET_PROJECT_DETAILS,
variables: { projectId },
});
return response.data.getProjectDetails;
} catch (error) {
console.error('Error fetching project details:', error);
throw error;
}
};

// Upsert project (Create or Update)
export const UPSERT_PROJECT = gql`
mutation UpsertProject($upsertProjectInput: UpsertProjectInput!) {
upsertProject(upsertProjectInput: $upsertProjectInput) {
id
projectName
path
projectPackages {
id
content
}
}
}
`;

export const upsertProject = async (upsertProjectInput: any): Promise<any> => {
try {
const response = await client.mutate({
mutation: UPSERT_PROJECT,
variables: {
upsertProjectInput,
},
});
return response.data.upsertProject;
} catch (error) {
console.error('Error creating/updating project:', error);
throw error;
}
};

// Delete project
export const DELETE_PROJECT = gql`
mutation DeleteProject($projectId: String!) {
deleteProject(projectId: $projectId)
}
`;

export const deleteProject = async (projectId: string): Promise<boolean> => {
try {
const response = await client.mutate({
mutation: DELETE_PROJECT,
variables: { projectId },
});
return response.data.deleteProject;
} catch (error) {
console.error('Error deleting project:', error);
throw error;
}
};

// Remove package from project
export const REMOVE_PACKAGE_FROM_PROJECT = gql`
mutation RemovePackageFromProject($projectId: String!, $packageId: String!) {
removePackageFromProject(projectId: $projectId, packageId: $packageId)
}
`;

export const removePackageFromProject = async (projectId: string, packageId: string): Promise<boolean> => {
try {
const response = await client.mutate({
mutation: REMOVE_PACKAGE_FROM_PROJECT,
variables: { projectId, packageId },
});
return response.data.removePackageFromProject;
} catch (error) {
console.error('Error removing package from project:', error);
throw error;
}
};
40 changes: 40 additions & 0 deletions frontend/src/test/projectApi.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { getUserProjects, getProjectDetails, deleteProject, upsertProject, removePackageFromProject } from '../app/api/project/route';

describe('Project API', () => {
let projectId: string;
let packageId: string;

it('should upsert a project', async () => {
const upsertProjectInput = {
projectName: 'Test Project',
projectPackages: ['Package 1', 'Package 2'],
};
const project = await upsertProject(upsertProjectInput);
expect(project).toHaveProperty('id');
projectId = project.id;
console.log('Project id is: ' + projectId)
packageId = project.projectPackages[0].id;
});

it('should get user projects', async () => {
const projects = await getUserProjects();
expect(Array.isArray(projects)).toBe(true);
expect(projects.length).toBeGreaterThan(0);
});

it('should get project details', async () => {
const projectDetails = await getProjectDetails(projectId);
expect(projectDetails).toHaveProperty('id', projectId);
});

it('should remove a package from project', async () => {
const removed = await removePackageFromProject(projectId, packageId);
expect(removed).toBe(true);
});

it('should delete a project', async () => {
const deleted = await deleteProject(projectId);
expect(deleted).toBe(true);
});
});

0 comments on commit b89e3b7

Please sign in to comment.