-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mfancher/system settings routes (#935)
* Model and Migration * Routes, Controllers, Middleware for instance settings
- Loading branch information
Showing
5 changed files
with
181 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters