Delete Step

This commit is contained in:
Takeshi Kimata
2024-03-09 14:10:53 +09:00
parent 62df98db89
commit 0481e91277
5 changed files with 130 additions and 3 deletions

View File

@@ -51,6 +51,10 @@ app.use("/cases", casesIndexRoute);
app.use("/cases", casesNewRoute); app.use("/cases", casesNewRoute);
app.use("/cases", casesEditRoute); app.use("/cases", casesEditRoute);
// "/steps"
const stepsDeleteRoute = require("./routes/steps/delete")(sequelize);
app.use("/steps", stepsDeleteRoute);
// "/runs" // "/runs"
const runsIndexRoute = require("./routes/runs/index")(sequelize); const runsIndexRoute = require("./routes/runs/index")(sequelize);
const runsNewRoute = require("./routes/runs/new")(sequelize); const runsNewRoute = require("./routes/runs/new")(sequelize);

View File

@@ -0,0 +1,31 @@
function defineCaseStep(sequelize, DataTypes) {
const CaseStep = sequelize.define("CaseStep", {
caseId: {
type: DataTypes.INTEGER,
allowNull: false,
},
stepId: {
type: DataTypes.INTEGER,
allowNull: false,
},
stepNo: {
type: DataTypes.INTEGER,
allowNull: false,
},
});
CaseStep.associate = (models) => {
CaseStep.belongsTo(models.Case, {
foreignKey: "caseId",
onDelete: "CASCADE",
});
CaseStep.belongsTo(models.Step, {
foreignKey: "stepId",
onDelete: "CASCADE",
});
};
return CaseStep;
}
module.exports = defineCaseStep;

View File

@@ -0,0 +1,51 @@
const express = require("express");
const router = express.Router();
const defineStep = require("../../models/steps");
const defineCaseStep = require("../../models/caseSteps");
const { DataTypes, Op } = require("sequelize");
module.exports = function (sequelize) {
const Step = defineStep(sequelize, DataTypes);
const CaseStep = defineCaseStep(sequelize, DataTypes);
router.delete("/:stepId", async (req, res) => {
const stepId = req.params.stepId;
const caseId = req.query.parentCaseId;
try {
const step = await Step.findByPk(stepId);
if (!step) {
return res.status(404).send("Step not found");
}
// Get caseStep to be deleted.
const deletingCaseStep = await CaseStep.findOne({
where: {
StepId: stepId,
},
});
// Decrease stepNo for all caseSteps with greater than the caseStep to be deleted.
await CaseStep.update(
{ stepNo: sequelize.literal("stepNo - 1") },
{
where: {
CaseId: caseId,
stepNo: {
[Op.gt]: deletingCaseStep.stepNo,
},
},
}
);
await step.destroy();
res.status(204).send();
} catch (error) {
console.error(error);
res.status(500).send("Internal Server Error");
}
});
return router;
};

View File

@@ -83,6 +83,30 @@ async function fetchCase(url: string) {
} }
} }
/**
* delete step
*/
async function fetchDeleteStep(stepId: number, parentCaseId: number) {
const fetchOptions = {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
};
const url = `${apiServer}/steps/${stepId}?parentCaseId=${parentCaseId}`;
try {
const response = await fetch(url, fetchOptions);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
} catch (error) {
console.error("Error deleting project:", error);
throw error;
}
}
/** /**
* Update folder * Update folder
*/ */
@@ -120,11 +144,20 @@ export default function Page({
const [isUpdating, setIsUpdating] = useState<boolean>(false); const [isUpdating, setIsUpdating] = useState<boolean>(false);
const url = `${apiServer}/cases?caseId=${params.caseId}`; const url = `${apiServer}/cases?caseId=${params.caseId}`;
const onDeleteClick = async (stepId: number) => {
await fetchDeleteStep(stepId, params.caseId);
const updatedSteps = testCase.Steps.filter(step => step.id !== stepId);
setTestCase({
...testCase,
Steps: updatedSteps
});
};
useEffect(() => { useEffect(() => {
async function fetchDataEffect() { async function fetchDataEffect() {
try { try {
const data = await fetchCase(url); const data = await fetchCase(url);
console.log(data);
setTestCase(data); setTestCase(data);
} catch (error) { } catch (error) {
console.error("Error in effect:", error.message); console.error("Error in effect:", error.message);
@@ -288,7 +321,7 @@ export default function Page({
}); });
}} }}
onStepPlus={() => {}} onStepPlus={() => {}}
onStepDelete={() => {}} onStepDelete={onDeleteClick}
/> />
</div> </div>
)} )}

View File

@@ -15,9 +15,17 @@ export default function StepsEditor({
onStepPlus, onStepPlus,
onStepDelete, onStepDelete,
}: Props) { }: Props) {
// sort steps by junction table's column
const sortedSteps = steps.slice().sort((a, b) => {
const stepNoA = a.caseSteps.stepNo;
const stepNoB = b.caseSteps.stepNo;
return stepNoA - stepNoB;
});
return ( return (
<> <>
{steps.map((step, index) => ( {sortedSteps.map((step, index) => (
<div key={index} className="flex"> <div key={index} className="flex">
<Textarea <Textarea
size="sm" size="sm"