-
Notifications
You must be signed in to change notification settings - Fork 3
Add project frontend api with test and fix backend project api #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Sma1lboy
merged 7 commits into
main
from
add--project-frontend-api-with-test-and-fix-backend-project
Oct 26, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
dc5c1bf
fix user id should be number
ZHallen122 071d737
update
ZHallen122 40b4158
jest auto test and apolloclient setup
ZHallen122 b89e3b7
project frontend to backend connect api with test
ZHallen122 6b8c307
[autofix.ci] apply automated fixes
autofix-ci[bot] 273ff2b
userId string to number
ZHallen122 cb906ef
Merge branch 'add--project-frontend-api-with-test-and-fix-backend-pro…
ZHallen122 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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> => { | ||
| 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need using nextjs' backend api, lets use graphql directly, create a graphql routing api