Skip to content

Commit

Permalink
Mfancher/system settings routes (#935)
Browse files Browse the repository at this point in the history
* Model and Migration

* Routes, Controllers, Middleware for instance settings
  • Loading branch information
FancMa01 authored Oct 31, 2024
1 parent 9f9d429 commit c23b463
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 2 deletions.
92 changes: 92 additions & 0 deletions Tombolo/server/controllers/instanceController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
const { where } = require("sequelize");
const Models = require("../models");
const instance_settings = Models.instance_settings;

// Get a single instance setting by name
const getInstanceSetting = async (req, res) => {
try {
const instance = await instance_settings.findOne({
where: { name: req.params.name },
});

if (!instance) {
return res.status(404).json({ message: "Instance setting not found" });
}
res.status(200).json(instance);
} catch (error) {
res.status(500).json({ message: error.message });
}
};

// Get all instance settings
const getAllInstanceSettings = async (req, res) => {
try {
const instances = await instance_settings.findAll();
if (!instances) {
return res.status(404).json({ message: "Instance settings not found" });
}
res.status(200).json(instances);
} catch (error) {
res.status(500).json({ message: error.message });
}
};

// Create a new instance setting
const createInstanceSetting = async (req, res) => {
try {
const newInstance = await instance_settings.create(req.body);

if (!newInstance) {
return res
.status(400)
.json({ message: "Failed to create instance setting" });
}
res.status(201).json(newInstance);
} catch (error) {
res.status(400).json({ message: error.message });
}
};

// Update an existing instance setting by ID
const updateInstanceSetting = async (req, res) => {
try {
const instance = await instance_settings.findOne({
where: { id: req.params.id },
});

if (!instance) {
return res.status(404).json({ message: "Instance setting not found" });
}

const updatedInstance = await instance.update(req.body);

res.status(200).json(updatedInstance);
} catch (error) {
res.status(400).json({ message: error.message });
}
};

// Delete an instance setting by ID
const deleteInstanceSetting = async (req, res) => {
try {
const instance = await instance_settings.findOne({
where: { id: req.params.id },
});
if (!instance) {
return res.status(404).json({ message: "Instance setting not found" });
}

await instance.destroy();
res.status(200).json({ message: "Instance setting deleted successfully" });
} catch (error) {
res.status(500).json({ message: error.message });
}
};

module.exports = {
getInstanceSetting,
getAllInstanceSettings,
createInstanceSetting,
updateInstanceSetting,
deleteInstanceSetting,
};
43 changes: 43 additions & 0 deletions Tombolo/server/middlewares/instanceMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const { body, param, validationResult } = require("express-validator");

const validateInstanceSetting = [
body("name").notEmpty().withMessage("Name is required"),
body("value").notEmpty().withMessage("Value is required"),
body("createdBy").notEmpty().withMessage("CreatedBy is required"),
(req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
next();
},
];

const validateInstanceId = [
param("id").isUUID(4).withMessage("Invalid instance setting id"),
(req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
next();
},
];

const validateInstanceUpdate = [
body("value").notEmpty().withMessage("Value is required"),
body("updatedBy").notEmpty().withMessage("UpdatedBy is required"),
(req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}
next();
},
];

module.exports = {
validateInstanceSetting,
validateInstanceId,
validateInstanceUpdate,
};
2 changes: 0 additions & 2 deletions Tombolo/server/models/instance_settings.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"use strict";

const { create } = require("lodash");

module.exports = (sequelize, DataTypes) => {
const instance_settings = sequelize.define(
"instance_settings",
Expand Down
44 changes: 44 additions & 0 deletions Tombolo/server/routes/instanceRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const express = require("express");

const {
validateInstanceId,
validateInstanceSetting,
validateInstanceUpdate,
} = require("../middlewares/instanceMiddleware");
const {
getInstanceSetting,
getAllInstanceSettings,
createInstanceSetting,
updateInstanceSetting,
deleteInstanceSetting,
} = require("../controllers/instanceController");
const role = require("../config/roleTypes");

const router = express.Router();

const { validateUserRole } = require("../middlewares/rbacMiddleware");

// Get a single instance setting by ID
router.get("/one/:name", getInstanceSetting);

// Get all instance settings
router.get("/all", getAllInstanceSettings);

// All routes below is accessible only by users with role "owner"
router.use(validateUserRole([role.OWNER]));

// Create a new instance setting
router.post("/", validateInstanceSetting, createInstanceSetting);

// Update an existing instance setting by ID
router.patch(
"/:id",
validateInstanceId,
validateInstanceUpdate,
updateInstanceSetting
);

// Delete an instance setting by ID
router.delete("/:id", validateInstanceId, deleteInstanceSetting);

module.exports = router;
2 changes: 2 additions & 0 deletions Tombolo/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ const sessions = require("./routes/sessionRoutes");
const cluster = require("./routes/clusterRoutes");
const roles = require("./routes/roleTypesRoute");
const status = require("./routes/statusRoutes");
const instanceSettings = require("./routes/instanceRoutes.js");

// Log all HTTP requests
app.use((req, res, next) => {
Expand Down Expand Up @@ -158,6 +159,7 @@ app.use("/api/monitorings", monitorings);
app.use("/api/asr", asr);
app.use("/api/directoryMonitoring", directoryMonitoring);
app.use("/api/roles", roles);
app.use("/api/instanceSettings", instanceSettings);

// Safety net for unhandled errors
app.use((err, req, res, next) => {
Expand Down

0 comments on commit c23b463

Please sign in to comment.