Skip to content

Commit

Permalink
Solved App crashes when entering an existing email ID saved in `info_…
Browse files Browse the repository at this point in the history
…table`
  • Loading branch information
ygowthamr committed Oct 14, 2024
1 parent 0361282 commit 1fce91a
Show file tree
Hide file tree
Showing 2 changed files with 221 additions and 175 deletions.
148 changes: 91 additions & 57 deletions file_upload/form_db.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,96 @@
const express = require('express')
const mysql = require('mysql')
const db = require('../config/mysql_connection')
const {decodeAccessToken}=require('../login-system/token')
const express = require("express");
const mysql = require("mysql");
const db = require("../config/mysql_connection");
const { decodeAccessToken } = require("../login-system/token");
const app = express();
app.use(express.json());

const info=(req,res)=>{
const decodedtoken = decodeAccessToken(req.headers.authorization);
if (!decodedtoken || !decodedtoken.user) {
console.error('Invalid or missing user information in the token');
return res.status(401).send('Unauthorized');
}
const userid=decodedtoken.user;
console.log(userid)
const name=req.body.name.trim();
const email=req.body.email.trim();
const col_name=req.body.col_name.trim();
const state=req.body.state;
const course=req.body.course.trim();
const year=req.body.year;
const dept=req.body.dept.trim();
db.getConnection(async(err,connection)=>{
if(err) throw err;
const sql="INSERT INTO info_table VALUES (?,?,?,?,?,?,?)"
const sqlinsert=mysql.format(sql,[userid,name,email,col_name,state,year,course])
await connection.query(sqlinsert,async (err,result)=>{
if(err) throw err;
connection.release();
console.log("Data Saved");
res.sendStatus(200);
})
})
}

const check=(req,res)=>{
const decodedtoken = decodeAccessToken(req.headers.authorization);
if (!decodedtoken || !decodedtoken.user) {
console.error('Invalid or missing user information in the token');
return res.status(401).send('Unauthorized');
}
const userid=decodedtoken.user;
db.getConnection(async(err,connection)=>{
if(err) throw err;
const search="SELECT * FROM info_table where id=?"
const searchquery=mysql.format(search,[userid])
await connection.query(searchquery,async(err,result)=>{
if(err) throw err;
if(result.length!=0){
console.log('info checked')
res.sendStatus(201)
connection.release()
}
else{
connection.release()
}
})
})
}
const info = (req, res) => {
const decodedtoken = decodeAccessToken(req.headers.authorization);
if (!decodedtoken || !decodedtoken.user) {
console.error("Invalid or missing user information in the token");
return res.status(401).send("Unauthorized");
}

const userid = decodedtoken.user;

const name = req.body.name.trim();
const email = req.body.email.trim();
const col_name = req.body.col_name.trim();
const state = req.body.state;
const course = req.body.course.trim();
const year = req.body.year;
const dept = req.body.dept.trim();

db.getConnection(async (err, connection) => {
if (err) throw err;

// Check if the email already exists in the info_table
const emailCheckQuery =
"SELECT COUNT(*) AS count FROM info_table WHERE email = ?";
const emailCheckSql = mysql.format(emailCheckQuery, [email]);

await connection.query(emailCheckSql, async (err, results) => {
if (err) {
connection.release();
throw err;
}

// Check if the email already exists
if (results[0].count > 0) {
connection.release();
return res.sendStatus(400);
}

// Proceed to insert the data if the email does not exist
const sql = "INSERT INTO info_table VALUES (?,?,?,?,?,?,?)";
const sqlInsert = mysql.format(sql, [
userid,
name,
email,
col_name,
state,
year,
course,
]);

await connection.query(sqlInsert, async (err, result) => {
if (err) {
connection.release();
throw err;
}

connection.release();
console.log("Data Saved");
res.sendStatus(200);
});
});
});
};

const check = (req, res) => {
const decodedtoken = decodeAccessToken(req.headers.authorization);
if (!decodedtoken || !decodedtoken.user) {
console.error("Invalid or missing user information in the token");
return res.status(401).send("Unauthorized");
}
const userid = decodedtoken.user;
db.getConnection(async (err, connection) => {
if (err) throw err;
const search = "SELECT * FROM info_table where id=?";
const searchquery = mysql.format(search, [userid]);
await connection.query(searchquery, async (err, result) => {
if (err) throw err;
if (result.length != 0) {
console.log("info checked");
res.sendStatus(201);
connection.release();
} else {
connection.release();
}
});
});
};

// exporting info
module.exports={info,check};
module.exports = { info, check };
Loading

0 comments on commit 1fce91a

Please sign in to comment.