create runs table
This commit is contained in:
@@ -35,6 +35,12 @@ const foldersNewRoute = require('./routes/folders/new')(sequelize);
|
||||
app.use('/folders', foldersIndexRoute);
|
||||
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;
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Server is running on port ${PORT}`);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
/** @type {import('sequelize-cli').Migration} */
|
||||
module.exports = {
|
||||
up: async (queryInterface, Sequelize) => {
|
||||
await queryInterface.createTable('Projects', {
|
||||
|
||||
52
backend/migrations/20240212082050-create-runs.js
Normal file
52
backend/migrations/20240212082050-create-runs.js
Normal 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
28
backend/models/runs.js
Normal 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;
|
||||
30
backend/routes/runs/index.js
Normal file
30
backend/routes/runs/index.js
Normal 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;
|
||||
};
|
||||
33
backend/routes/runs/new.js
Normal file
33
backend/routes/runs/new.js
Normal 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;
|
||||
};
|
||||
@@ -44,6 +44,37 @@ module.exports = {
|
||||
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) => {
|
||||
|
||||
Reference in New Issue
Block a user