-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add project frontend api with test and fix backend project api (#20)
This pr add project frontend api with test and fix backend project api (user id should be number) --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
161306c
commit 08ceb20
Showing
10 changed files
with
554 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
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> => { | ||
Check failure on line 76 in frontend/src/app/api/project/route.ts GitHub Actions / autofix
|
||
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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { | ||
ApolloClient, | ||
InMemoryCache, | ||
HttpLink, | ||
ApolloLink, | ||
concat, | ||
} from '@apollo/client'; | ||
|
||
const httpLink = new HttpLink({ | ||
// uri: process.env.NEXT_PUBLIC_API_BASE_URL, | ||
uri: 'http://localhost:8080/graphql', | ||
}); | ||
|
||
const authMiddleware = new ApolloLink((operation, forward) => { | ||
// Get the authentication token from local storage if it exists | ||
const token = localStorage.getItem('token'); | ||
// Use the setContext method to set the HTTP headers. | ||
if (token) { | ||
operation.setContext({ | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
}); | ||
} | ||
return forward(operation); | ||
}); | ||
|
||
const client = new ApolloClient({ | ||
link: concat(authMiddleware, httpLink), | ||
cache: new InMemoryCache(), | ||
}); | ||
|
||
export default client; |
Oops, something went wrong.