Add/Insert Step

This commit is contained in:
Takeshi Kimata
2024-03-11 20:48:24 +09:00
parent 35bb12827c
commit 966e4d7d1e
5 changed files with 100 additions and 10 deletions

View File

@@ -52,7 +52,9 @@ app.use("/cases", casesNewRoute);
app.use("/cases", casesEditRoute); app.use("/cases", casesEditRoute);
// "/steps" // "/steps"
const stepsNewRoute = require("./routes/steps/new")(sequelize);
const stepsDeleteRoute = require("./routes/steps/delete")(sequelize); const stepsDeleteRoute = require("./routes/steps/delete")(sequelize);
app.use("/steps", stepsNewRoute);
app.use("/steps", stepsDeleteRoute); app.use("/steps", stepsDeleteRoute);
// "/runs" // "/runs"

View File

@@ -10,11 +10,15 @@ module.exports = function (sequelize) {
router.delete("/:stepId", async (req, res) => { router.delete("/:stepId", async (req, res) => {
const stepId = req.params.stepId; const stepId = req.params.stepId;
// TODO The caseId should not be specified from the front end, but should be traced from stepId by association.
const caseId = req.query.parentCaseId; const caseId = req.query.parentCaseId;
const t = await sequelize.transaction();
try { try {
const step = await Step.findByPk(stepId); const step = await Step.findByPk(stepId);
if (!step) { if (!step) {
await t.rollback();
return res.status(404).send("Step not found"); return res.status(404).send("Step not found");
} }
@@ -23,6 +27,7 @@ module.exports = function (sequelize) {
where: { where: {
StepId: stepId, StepId: stepId,
}, },
transaction: t,
}); });
// Decrease stepNo for all caseSteps with greater than the caseStep to be deleted. // Decrease stepNo for all caseSteps with greater than the caseStep to be deleted.
@@ -35,14 +40,17 @@ module.exports = function (sequelize) {
[Op.gt]: deletingCaseStep.stepNo, [Op.gt]: deletingCaseStep.stepNo,
}, },
}, },
transaction: t,
} }
); );
await step.destroy(); await step.destroy({ transaction: t });
await t.commit();
res.status(204).send(); res.status(204).send();
} catch (error) { } catch (error) {
console.error(error); console.error(error);
await t.rollback();
res.status(500).send("Internal Server Error"); res.status(500).send("Internal Server Error");
} }
}); });

View File

@@ -0,0 +1,63 @@
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.post("/", async (req, res) => {
const newStepNo = req.query.newStepNo;
const caseId = req.query.parentCaseId;
const t = await sequelize.transaction();
try {
// Update existing stepNo for steps with stepNo greater than or equal to newStepNo
const maxStepNo = await CaseStep.max("stepNo", {
where: { caseId: caseId },
transaction: t,
});
if (maxStepNo >= newStepNo) {
await CaseStep.update(
{ stepNo: sequelize.literal("stepNo + 1") },
{
where: {
caseId: caseId,
stepNo: { [Op.gte]: newStepNo },
},
transaction: t,
}
);
}
const newStep = await Step.create(
{
step: "",
result: "",
},
{ transaction: t }
);
await CaseStep.create(
{
caseId: caseId,
stepId: newStep.id,
stepNo: newStepNo,
},
{ transaction: t }
);
await t.commit();
res.json(newStep);
} catch (error) {
console.error(error);
await t.rollback();
res.status(500).send("Internal Server Error");
}
});
return router;
};

View File

@@ -83,6 +83,30 @@ async function fetchCase(url: string) {
} }
} }
/**
* create step
*/
async function fetchCreateStep(newStepNo: number, parentCaseId: number) {
const fetchOptions = {
method: "POST",
headers: {
"Content-Type": "application/json",
},
};
const url = `${apiServer}/steps?newStepNo=${newStepNo}&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;
}
}
/** /**
* delete step * delete step
*/ */
@@ -146,7 +170,7 @@ export default function Page({
const url = `${apiServer}/cases?caseId=${params.caseId}`; const url = `${apiServer}/cases?caseId=${params.caseId}`;
const onPlusClick = async (newStepNo: number) => { const onPlusClick = async (newStepNo: number) => {
console.log(newStepNo); await fetchCreateStep(newStepNo, params.caseId);
}; };
const onDeleteClick = async (stepId: number) => { const onDeleteClick = async (stepId: number) => {
@@ -156,19 +180,12 @@ export default function Page({
return; return;
} }
const deletedStepNo = deletedStep.caseSteps.stepNo; const deletedStepNo = deletedStep.caseSteps.stepNo;
console.log(deletedStepNo)
// delete request // delete request
await fetchDeleteStep(stepId, params.caseId); await fetchDeleteStep(stepId, params.caseId);
// const updatedSteps = testCase.Steps.filter((step) => step.id !== stepId);
// setTestCase({
// ...testCase,
// Steps: updatedSteps,
// });
const updatedSteps = testCase.Steps.map(step => { const updatedSteps = testCase.Steps.map(step => {
if (step.caseSteps.stepNo > deletedStepNo) { if (step.caseSteps.stepNo > deletedStepNo) {
console.log("bigger", step)
return { return {
...step, ...step,
caseSteps: { caseSteps: {

View File

@@ -68,7 +68,7 @@ export default function StepsEditor({
isIconOnly isIconOnly
size="sm" size="sm"
className="bg-transparent rounded-full" className="bg-transparent rounded-full"
onPress={() => onStepPlus(step.caseSteps.stepNo)} onPress={() => onStepPlus(step.caseSteps.stepNo + 1)}
> >
<Plus size={16} /> <Plus size={16} />
</Button> </Button>