diff --git a/backend/index.js b/backend/index.js index 5bf8d6d..153c5e0 100644 --- a/backend/index.js +++ b/backend/index.js @@ -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}`); diff --git a/backend/migrations/20240204090345-create-projects.js b/backend/migrations/20240204090345-create-projects.js index bfc7e30..ea41e19 100644 --- a/backend/migrations/20240204090345-create-projects.js +++ b/backend/migrations/20240204090345-create-projects.js @@ -1,5 +1,6 @@ 'use strict'; +/** @type {import('sequelize-cli').Migration} */ module.exports = { up: async (queryInterface, Sequelize) => { await queryInterface.createTable('Projects', { diff --git a/backend/migrations/20240212082050-create-runs.js b/backend/migrations/20240212082050-create-runs.js new file mode 100644 index 0000000..0de3cde --- /dev/null +++ b/backend/migrations/20240212082050-create-runs.js @@ -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'); + } +}; diff --git a/backend/models/runs.js b/backend/models/runs.js new file mode 100644 index 0000000..0180d74 --- /dev/null +++ b/backend/models/runs.js @@ -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; \ No newline at end of file diff --git a/backend/routes/runs/index.js b/backend/routes/runs/index.js new file mode 100644 index 0000000..22c1bf6 --- /dev/null +++ b/backend/routes/runs/index.js @@ -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; +}; \ No newline at end of file diff --git a/backend/routes/runs/new.js b/backend/routes/runs/new.js new file mode 100644 index 0000000..3b404c8 --- /dev/null +++ b/backend/routes/runs/new.js @@ -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; +}; diff --git a/backend/seeders/seed.js b/backend/seeders/seed.js index 427bc5e..7b51112 100644 --- a/backend/seeders/seed.js +++ b/backend/seeders/seed.js @@ -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) => {