pro118njsac/pro118njsac-back-b/authsec_node/Backend/listBuilder/controller.js

110 lines
2.9 KiB
JavaScript
Raw Permalink Normal View History

2024-09-16 05:00:58 +00:00
const { genSaltSync, hashSync, compareSync } = require("bcrypt")
const { sign } = require("jsonwebtoken")
const otpGenerator = require("otp-generator")
const nodemailer = require("nodemailer")
const bcrypt = require("bcrypt")
const { pool, db } = require("../config/database")
const fs = require("fs")
const path = require("path")
const { execArgv } = require("process")
const {
buildExtensionByFormCode,
} = require("../Functions/buildExtensionByFormCode")
const { exec } = require("child_process")
// const transporter = nodemailer.createTransport({
// host: "smtp.ethereal.email",
// port: 587,
// auth: {
// user: "maynard.skiles@ethereal.email",
// pass: "E36dmz2ceNJV5eFvC4",
// },
// })
const transporter = nodemailer.createTransport({
service: "gmail", // e.g., 'Gmail'
host: "smtp.gmail.com",
port: 587,
secure: false,
auth: {
user: "randomaff8@gmail.com",
pass: "fnnq tisa alvh uyre",
},
})
async function executeQuery(sql, values) {
const connection = await db.getConnection()
try {
const [rows] = await connection.execute(sql, values)
return rows
} finally {
connection.release()
}
}
const getAllUserNode = async (req, res) => {
try {
const sql = "SELECT name, email FROM users_node"
const result = await executeQuery(sql)
// Check if any data is fetched
if (result.length > 0) {
res.status(200).json({ users: result })
} else {
res.status(404).json({ message: "No users found" })
}
} catch (error) {
console.error("Error fetching users:", error)
res.status(500).json({ error: "Internal server error" })
}
}
const getByNameUserNode = async (req, res) => {
try {
const name = req.params.name
const sql = "SELECT name, email FROM users_node WHERE name = ?"
const result = await executeQuery(sql, [name])
// Check if any data is fetched
if (result.length > 0) {
res.status(200).json({ users: result })
} else {
res.status(404).json({ message: "No users found" })
}
} catch (error) {
console.error("Error fetching users:", error)
res.status(500).json({ error: "Internal server error" })
}
}
const addUserUserNode = async (req, res) => {
const body = req.body
const email = body.email
const password = body.password
const name = body.name
try {
const sql =
"INSERT INTO users_node (name, password, email) VALUES (?, ?, ?)"
const values = [name, password, email]
const result = await executeQuery(sql, values)
// Check if the user was successfully added
if (result.affectedRows > 0) {
res.status(200).json({ message: "User added successfully" })
} else {
res.status(400).json({ message: "Failed to add user" })
}
} catch (error) {
console.error("Error adding user:", error)
res.status(500).json({ error: "Internal server error" })
}
}
module.exports = {
getAllUserNode,
addUserUserNode,
getByNameUserNode,
}