Files
pp-tcms/frontend/app/projects/[projectId]/runs/runsControl.ts
2024-04-21 12:28:30 +09:00

250 lines
5.6 KiB
TypeScript

import Config from "@/config/config";
const apiServer = Config.apiServer;
import { RunType, RunCaseInfoType } from "@/types/run";
async function fetchRun(runId: string) {
const url = `${apiServer}/runs/${runId}`;
try {
const response = await fetch(url, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error: any) {
console.error("Error fetching data:", error.message);
}
}
async function fetchRuns(projectId: string) {
const url = `${apiServer}/runs?projectId=${projectId}`;
try {
const response = await fetch(url, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error: any) {
console.error("Error fetching data:", error.message);
}
}
async function createRun(projectId: string) {
const newTestRun = {
name: "untitled run",
configurations: 0,
description: "",
state: 0,
projectId: projectId,
};
const fetchOptions = {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(newTestRun),
};
const url = `${apiServer}/runs`;
try {
const response = await fetch(url, fetchOptions);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error: any) {
console.error("Error creating new test run:", error);
throw error;
}
}
async function updateRun(updateTestRun: RunType) {
const fetchOptions = {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(updateTestRun),
};
const url = `${apiServer}/runs/${updateTestRun.id}`;
try {
const response = await fetch(url, fetchOptions);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error: any) {
console.error("Error updating run:", error);
throw error;
}
}
async function deleteRun(runId: number) {
const fetchOptions = {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
};
const url = `${apiServer}/runs/${runId}`;
try {
const response = await fetch(url, fetchOptions);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
} catch (error: any) {
console.error("Error deleting run:", error);
throw error;
}
}
async function fetchRunCases(runId: string) {
const url = `${apiServer}/runcases?runId=${runId}`;
try {
const response = await fetch(url, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error: any) {
console.error("Error fetching data:", error.message);
}
}
async function createRunCase(runId: string, caseId: number) {
const fetchOptions = {
method: "POST",
headers: {
"Content-Type": "application/json",
},
};
const url = `${apiServer}/runcases?runId=${runId}&caseId=${caseId}`;
try {
const response = await fetch(url, fetchOptions);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error: any) {
console.error("Error creating new runcase:", error);
throw error;
}
}
async function bulkCreateRunCases(runCaseInfo: RunCaseInfoType[]) {
const fetchOptions = {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(runCaseInfo)
};
const url = `${apiServer}/runcases/bulknew`;
try {
const response = await fetch(url, fetchOptions);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error: any) {
console.error("Error creating new runcase:", error);
throw error;
}
}
async function deleteRunCase(runId: string, caseId: number) {
const fetchOptions = {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
};
const url = `${apiServer}/runcases?runId=${runId}&caseId=${caseId}`;
try {
const response = await fetch(url, fetchOptions);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
} catch (error: any) {
console.error("Error deleting runcase:", error);
throw error;
}
}
async function bulkDeleteRunCases(runCaseInfo: RunCaseInfoType[]) {
const fetchOptions = {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(runCaseInfo)
};
const url = `${apiServer}/runcases/bulkdelete`;
try {
const response = await fetch(url, fetchOptions);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
} catch (error: any) {
console.error("Error deleting runcase:", error);
throw error;
}
}
export {
fetchRun,
fetchRuns,
createRun,
updateRun,
deleteRun,
fetchRunCases,
createRunCase,
bulkCreateRunCases,
deleteRunCase,
bulkDeleteRunCases,
};