Spaces:
Runtime error
Runtime error
Commit
·
dc65a06
1
Parent(s):
29c46e8
expose an api to update settings by ID
Browse files
src/controllers/setting.controller.ts
CHANGED
|
@@ -50,7 +50,7 @@ const getSettings = async (req: Request, res: Response) => {
|
|
| 50 |
const { sort_by, sort_order, page, limit } = req.query;
|
| 51 |
const filter = req.query.filter as Record<string, any>;
|
| 52 |
|
| 53 |
-
const allowedSortColumns = ['id', 'updated_by']
|
| 54 |
|
| 55 |
const whereClause = buildSettingsWhereClause(filter);
|
| 56 |
|
|
@@ -109,4 +109,36 @@ const getSettingById = async (req: Request, res: Response) => {
|
|
| 109 |
}
|
| 110 |
}
|
| 111 |
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
const { sort_by, sort_order, page, limit } = req.query;
|
| 51 |
const filter = req.query.filter as Record<string, any>;
|
| 52 |
|
| 53 |
+
const allowedSortColumns = ['id', 'updated_by', 'created_at'];
|
| 54 |
|
| 55 |
const whereClause = buildSettingsWhereClause(filter);
|
| 56 |
|
|
|
|
| 109 |
}
|
| 110 |
}
|
| 111 |
|
| 112 |
+
const updateSettingById = async (req: Request, res: Response) => {
|
| 113 |
+
try {
|
| 114 |
+
|
| 115 |
+
const { id } = req.params;
|
| 116 |
+
const {setting_key, setting_value} = req.body;
|
| 117 |
+
|
| 118 |
+
const setting = await Setting.findByPk(id);
|
| 119 |
+
|
| 120 |
+
if (!setting) {
|
| 121 |
+
return res.status(404).json({ error: 'Setting not found' });
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
if(setting_key){
|
| 125 |
+
setting.setting_key = setting_key;
|
| 126 |
+
}
|
| 127 |
+
if(setting_value){
|
| 128 |
+
setting.setting_value = setting_value;
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
// TODO: to change it once we have authentication in place
|
| 132 |
+
setting.updated_by = 1;
|
| 133 |
+
|
| 134 |
+
await setting.save();
|
| 135 |
+
|
| 136 |
+
return res.status(200).json(setting);
|
| 137 |
+
|
| 138 |
+
}catch (error) {
|
| 139 |
+
console.error('Error updating setting:', error);
|
| 140 |
+
return res.status(500).json({ error: 'Error updating setting' });
|
| 141 |
+
}
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
export { getSettings, getSettingById, updateSettingById };
|
src/routes/settings.routes.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
| 1 |
import express from 'express';
|
| 2 |
-
import { getSettings, getSettingById } from '../controllers/setting.controller';
|
| 3 |
|
| 4 |
const settingRouter = express.Router();
|
| 5 |
|
| 6 |
settingRouter.get('/', getSettings);
|
| 7 |
settingRouter.get('/:id', getSettingById);
|
|
|
|
|
|
|
| 8 |
export default settingRouter
|
|
|
|
| 1 |
import express from 'express';
|
| 2 |
+
import { getSettings, getSettingById, updateSettingById } from '../controllers/setting.controller';
|
| 3 |
|
| 4 |
const settingRouter = express.Router();
|
| 5 |
|
| 6 |
settingRouter.get('/', getSettings);
|
| 7 |
settingRouter.get('/:id', getSettingById);
|
| 8 |
+
settingRouter.put('/:id', updateSettingById);
|
| 9 |
+
|
| 10 |
export default settingRouter
|