Separate the Case get endpoint into cases/index and cases/show

This commit is contained in:
Takeshi Kimata
2024-03-20 20:19:12 +09:00
parent 8a2ca8c563
commit 742e0ac1f7
4 changed files with 50 additions and 28 deletions

View File

@@ -1,40 +1,16 @@
const express = require("express");
const router = express.Router();
const defineCase = require("../../models/cases");
const defineStep = require("../../models/steps");
const defineAttachment = require("../../models/attachments");
const { DataTypes } = require("sequelize");
module.exports = function (sequelize) {
const Case = defineCase(sequelize, DataTypes);
const Step = defineStep(sequelize, DataTypes);
const Attachment = defineAttachment(sequelize, DataTypes);
Case.belongsToMany(Step, { through: "caseSteps" });
Step.belongsToMany(Case, { through: "caseSteps" });
Case.belongsToMany(Attachment, { through: "caseAttachments" });
Attachment.belongsToMany(Case, { through: "caseAttachments" });
router.get("/", async (req, res) => {
const { caseId, folderId } = req.query;
const { folderId } = req.query;
if (!caseId && !folderId) {
return res.status(400).json({ error: "caseId or folderId is required" });
}
if (caseId) {
// Include steps if requested using caseId
const testcase = await Case.findByPk(caseId, {
include: [
{
model: Step,
through: { attributes: ["stepNo"] },
},
{
model: Attachment,
},
],
});
return res.json(testcase);
if (!folderId) {
return res.status(400).json({ error: "folderId is required" });
}
try {