// 比赛管理 API
|
|
const GRAPHQL_ENDPOINT = 'http://localhost:8080/api/graphql';
|
|
// GraphQL 查询语句
|
const GET_ACTIVITIES_QUERY = `
|
query GetActivities($page: Int!, $size: Int!, $name: String) {
|
activities(page: $page, size: $size, name: $name) {
|
content {
|
id
|
name
|
description
|
signupDeadline
|
matchTime
|
address
|
playerMax
|
state
|
stateName
|
playerCount
|
createTime
|
ratingScheme {
|
id
|
name
|
}
|
}
|
totalElements
|
page
|
size
|
}
|
}
|
`;
|
|
const GET_ACTIVITY_QUERY = `
|
query GetActivity($id: ID!) {
|
activity(id: $id) {
|
id
|
pid
|
name
|
description
|
signupDeadline
|
matchTime
|
address
|
ratingSchemeId
|
playerMax
|
state
|
stateName
|
createTime
|
updateTime
|
ratingScheme {
|
id
|
name
|
totalScore
|
}
|
stages {
|
id
|
name
|
description
|
matchTime
|
address
|
ratingSchemeId
|
playerMax
|
state
|
stateName
|
ratingScheme {
|
id
|
name
|
}
|
}
|
}
|
}
|
`;
|
|
const GET_ALL_ACTIVITIES_QUERY = `
|
query GetAllActivities {
|
allActivities {
|
id
|
name
|
state
|
stateName
|
}
|
}
|
`;
|
|
const SAVE_ACTIVITY_MUTATION = `
|
mutation SaveActivity($input: ActivityInput!) {
|
saveActivity(input: $input) {
|
id
|
name
|
description
|
signupDeadline
|
matchTime
|
address
|
ratingSchemeId
|
playerMax
|
state
|
stateName
|
}
|
}
|
`;
|
|
const DELETE_ACTIVITY_MUTATION = `
|
mutation DeleteActivity($id: ID!) {
|
deleteActivity(id: $id)
|
}
|
`;
|
|
// API 函数
|
export const getActivities = async (page = 0, size = 10, name = '') => {
|
const response = await fetch(GRAPHQL_ENDPOINT, {
|
method: 'POST',
|
headers: {
|
'Content-Type': 'application/json',
|
},
|
body: JSON.stringify({
|
query: GET_ACTIVITIES_QUERY,
|
variables: { page, size, name }
|
})
|
});
|
|
const result = await response.json();
|
if (result.errors) {
|
throw new Error(result.errors[0].message);
|
}
|
return result.data.activities;
|
};
|
|
export const getActivity = async (id) => {
|
const response = await fetch(GRAPHQL_ENDPOINT, {
|
method: 'POST',
|
headers: {
|
'Content-Type': 'application/json',
|
},
|
body: JSON.stringify({
|
query: GET_ACTIVITY_QUERY,
|
variables: { id }
|
})
|
});
|
|
const result = await response.json();
|
if (result.errors) {
|
throw new Error(result.errors[0].message);
|
}
|
return result.data.activity;
|
};
|
|
export const getAllActivities = async () => {
|
const response = await fetch(GRAPHQL_ENDPOINT, {
|
method: 'POST',
|
headers: {
|
'Content-Type': 'application/json',
|
},
|
body: JSON.stringify({
|
query: GET_ALL_ACTIVITIES_QUERY
|
})
|
});
|
|
const result = await response.json();
|
if (result.errors) {
|
throw new Error(result.errors[0].message);
|
}
|
return result.data.allActivities;
|
};
|
|
export const saveActivity = async (activityData) => {
|
const response = await fetch(GRAPHQL_ENDPOINT, {
|
method: 'POST',
|
headers: {
|
'Content-Type': 'application/json',
|
},
|
body: JSON.stringify({
|
query: SAVE_ACTIVITY_MUTATION,
|
variables: { input: activityData }
|
})
|
});
|
|
const result = await response.json();
|
if (result.errors) {
|
throw new Error(result.errors[0].message);
|
}
|
return result.data.saveActivity;
|
};
|
|
export const deleteActivity = async (id) => {
|
const response = await fetch(GRAPHQL_ENDPOINT, {
|
method: 'POST',
|
headers: {
|
'Content-Type': 'application/json',
|
},
|
body: JSON.stringify({
|
query: DELETE_ACTIVITY_MUTATION,
|
variables: { id }
|
})
|
});
|
|
const result = await response.json();
|
if (result.errors) {
|
throw new Error(result.errors[0].message);
|
}
|
return result.data.deleteActivity;
|
};
|