123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- const db = require('../config/dbConfig');
- const url = require('url');
- const querystring = require('querystring');
- exports.getRules = (req, res) => {
- const query = 'SELECT * FROM rules WHERE deleted = 0 ORDER BY rule_id DESC';
- // Execute the query
- db.query(query, (error, results) => {
- if (error) {
- res.status(500).json({ error: 'Internal Server Error' });
- } else {
- // Convert the results to JSON
- const jsonResult = JSON.stringify(results);
- // Send the JSON response
- res.json({ rules: jsonResult });
- }
- });
- }
- exports.getRule = (req, res) => {
- const query = 'SELECT * FROM rules where rule_id = ?';
- const parsedUrl = url.parse(req.originalUrl);
- const queryParams = querystring.parse(parsedUrl.query);
- db.query(query, [req.query.ruleId], (error, results) => {
- if (error) {
- res.status(500).json({ error: 'Internal Server Error' });
- } else {
- // Convert the results to JSON
- const jsonResult = JSON.stringify(results);
- // Send the JSON response
- res.json({ rules: jsonResult });
- }
- });
- }
- exports.deleteRule = (req, res) => {
- const query = 'UPDATE rules SET deleted = 1 where rule_id = ?';
- console.log("ruld to delete" + req.body.ruleID)
- db.query(query, [req.body.ruleID], (error, results) => {
- if (error) {
- res.status(500).json({ error: 'Internal Server Error' });
- } else {
- const jsonResult = JSON.stringify(results);
- res.json({ rules: jsonResult });
- }
- });
- }
- exports.publishRule = async (req, res) => {
- const edit = req.body.edit;
- if (!edit) {
- const publishQuery = "INSERT INTO rules "
- + "(rule_id, time_stamp, name, lineage, summary, modules, creator_name, creator_url, deleted, list, icon, latest_version, email)"
- + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
- db.query(publishQuery, [req.body.ruleID, req.body.timestamp, req.body.name, req.body.lineage, req.body.summary, req.body.modules, req.body.creatorName, req.body.creatorUrl, 0, req.body.list, req.body.icon, req.body.latest_version, req.body.email], (err, results) => {
- if (err) {
- console.error('Error executing query:', err);
- res.status(500).json({ success: false, message: 'OTP Validated but issue while creating entry for rule ' });
- return;
- }
- res.status(200).json({ success: true, message: 'OTP is valid and your rule is now published' });
- })
- } else {
- const publishQueryVersionHistory = "INSERT INTO version_history "
- + "(rule_id, time_stamp, name, lineage, summary, modules, creator_name, creator_url, list, icon, rule_version)"
- + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
- const selectVersionHistory = "SELECT * FROM rules WHERE rule_id = ?"
- db.query(selectVersionHistory, [req.body.ruleID], (err, results) => {
- if (err) {
- console.error('Error executing query:', err);
- res.status(500).json({ success: false, message: 'Internal Server Error' });
- return;
- }
- console.log("rule =>" + req.body.ruleID)
- if (results.length > 0) {
- const versionHistoryData = results[0];
- // Execute the second query to insert data
- db.query(
- publishQueryVersionHistory,
- [
- versionHistoryData.rule_id,
- versionHistoryData.time_stamp,
- versionHistoryData.name,
- versionHistoryData.lineage,
- versionHistoryData.summary,
- versionHistoryData.modules,
- versionHistoryData.creator_name,
- versionHistoryData.creator_url,
- versionHistoryData.list,
- versionHistoryData.icon,
- req.body.latest_version - 1
- ],
- (err, insertResults) => {
- if (err) {
- console.log(err)
- console.error('Error executing insert query:', err);
- res.status(500).json({ success: false, message: 'Internal Server Error' });
- return;
- }
- const updateQuery = "UPDATE rules SET time_stamp = ?, name = ?, lineage = ?, summary = ?, modules = ?, creator_name = ?, creator_url = ?, version = ?, list = ?, icon = ?, latest_version = ? WHERE rule_id = ?;"
- db.query(updateQuery, [req.body.timestamp, req.body.name, req.body.lineage, req.body.summary, req.body.modules, req.body.creatorName, req.body.creatorUrl, req.body.version, req.body.list, req.body.icon, req.body.latest_version, req.body.ruleID], (err, results) => {
- if (err) {
- console.error('Error executing query:', err);
- res.status(500).json({ success: false, message: 'OTP Validated but issue while creating entry for rule ' });
- return;
- }
- res.status(200).json({ success: true, message: 'OTP is valid and your rule is now published' });
- });
- });
- }
- });
- }
- }
|