create runs table

This commit is contained in:
Takeshi Kimata
2024-02-12 17:36:50 +09:00
parent 197a011c4c
commit b23922c8e5
7 changed files with 181 additions and 0 deletions

View File

@@ -35,6 +35,12 @@ const foldersNewRoute = require('./routes/folders/new')(sequelize);
app.use('/folders', foldersIndexRoute); app.use('/folders', foldersIndexRoute);
app.use('/folders', foldersNewRoute); app.use('/folders', foldersNewRoute);
// "/runs"
const runsIndexRoute = require('./routes/runs/index')(sequelize);
const runsNewRoute = require('./routes/runs/new')(sequelize);
app.use('/runs', runsIndexRoute);
app.use('/runs', runsNewRoute);
const PORT = process.env.PORT || 3001; const PORT = process.env.PORT || 3001;
app.listen(PORT, () => { app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`); console.log(`Server is running on port ${PORT}`);

View File

@@ -1,5 +1,6 @@
'use strict'; 'use strict';
/** @type {import('sequelize-cli').Migration} */
module.exports = { module.exports = {
up: async (queryInterface, Sequelize) => { up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('Projects', { await queryInterface.createTable('Projects', {

View File

@@ -0,0 +1,52 @@
'use strict';
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up (queryInterface, Sequelize) {
await queryInterface.createTable('runs', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
},
name: {
type: Sequelize.STRING,
allowNull: false,
},
configurations: {
type: Sequelize.STRING,
allowNull: true,
},
description: {
type: Sequelize.STRING,
allowNull: true,
},
state: {
type: Sequelize.INTEGER,
allowNull: true,
},
projectId: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'Projects',
key: 'id',
},
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
async down (queryInterface, Sequelize) {
await queryInterface.dropTable('runs');
}
};

28
backend/models/runs.js Normal file
View File

@@ -0,0 +1,28 @@
function defineRun(sequelize, DataTypes) {
const Run = sequelize.define('Run', {
name: {
type: DataTypes.STRING,
allowNull: false,
},
configurations: {
type: DataTypes.STRING,
allowNull: true,
},
description: {
type: DataTypes.STRING,
allowNull: true,
},
state: {
type: DataTypes.INTEGER,
allowNull: true,
},
projectId: {
type: DataTypes.INTEGER,
allowNull: false,
},
});
return Run;
}
module.exports = defineRun;

View File

@@ -0,0 +1,30 @@
const express = require("express");
const router = express.Router();
const defineRun = require("../../models/runs");
const { DataTypes } = require('sequelize');
module.exports = function(sequelize) {
const Run = defineRun(sequelize, DataTypes)
router.get("/", async (req, res) => {
const { projectId } = req.query;
if (!projectId) {
return res.status(400).json({ error: 'projectId is required' });
}
try {
const runs = await Run.findAll({
where: {
projectId: projectId
}
});
res.json(runs);
} catch (error) {
console.error(error);
res.status(500).send("Internal Server Error");
}
});
return router;
};

View File

@@ -0,0 +1,33 @@
const express = require("express");
const router = express.Router();
const defineRun = require("../../models/runs");
const { DataTypes } = require("sequelize");
module.exports = function (sequelize) {
const Run = defineRun(sequelize, DataTypes);
router.post("/", async (req, res) => {
try {
const { name, configurations, description, state, projectId } = req.body;
if (!name || !projectId) {
return res
.status(400)
.json({ error: "Name and projectId are required" });
}
const newRun = await Run.create({
name,
configurations,
description,
state,
projectId,
});
res.json(newRun);
} catch (error) {
res.status(500).json({ error: "Internal server error" });
}
});
return router;
};

View File

@@ -44,6 +44,37 @@ module.exports = {
updatedAt: new Date(), updatedAt: new Date(),
}, },
]); ]);
// Add runs table records
await queryInterface.bulkInsert('runs', [
{
name: 'Run 1',
projectId: 1,
configurations: null,
description: null,
state: 1,
createdAt: new Date(),
updatedAt: new Date(),
},
{
name: 'Run 2',
projectId: 1,
configurations: null,
description: null,
state: 1,
createdAt: new Date(),
updatedAt: new Date(),
},
{
name: 'Run 3',
projectId: 1,
configurations: null,
description: null,
state: 1,
createdAt: new Date(),
updatedAt: new Date(),
},
]);
}, },
down: async (queryInterface, Sequelize) => { down: async (queryInterface, Sequelize) => {