transaction non funzionante

This commit is contained in:
xfarrow 2023-10-12 15:41:38 +02:00
parent e36091bb05
commit 5c3196d687
2 changed files with 44 additions and 33 deletions

View File

@ -18,7 +18,6 @@ const port = 3000;
app.use(express.json()); app.use(express.json());
app.post('/blinkapi/register', api_controller.register_async); app.post('/blinkapi/register', api_controller.register_async);
app.post('blinkapi/login', api_controller.login);
// Start the server // Start the server
app.listen(port, () => { app.listen(port, () => {

View File

@ -1,15 +1,19 @@
const bcrypt = require('bcrypt'); const bcrypt = require('bcrypt');
const { Pool } = require('pg');
const crypto = require('crypto'); const crypto = require('crypto');
const pgp = require('pg-promise')();
const Pool = require('pg-pool');
const pool = new Pool({ const dbConfig = {
user: 'postgres', host: "localhost",
host: 'localhost', port: 5432,
database: 'Blink', database: "Blink",
password: 'postgres', user: "postgres",
port: 5432, password: "postgres"
max: 10, };
idleTimeoutMillis: 30000,
// Create a new connection pool
const db = pgp({
pool: new Pool(dbConfig),
}); });
function register(req, res){ function register(req, res){
@ -81,34 +85,43 @@ async function register_async(req, res){
// Generate activation link token // Generate activation link token
const activationLink = crypto.randomBytes(16).toString('hex'); const activationLink = crypto.randomBytes(16).toString('hex');
const hashPasswordPromise = bcrypt.hash(userData.password, 10); const hashPasswordPromise = bcrypt.hash(userData.password, 10);
var client;
try{ try{
client = await pool.connect(); const result = await db.tx(async (t) => {
const insertQuery = `
INSERT INTO "User" (display_name, date_of_birth, place_of_living, is_looking_for_job, email, password) // Inserting in "Person" table
VALUES ($1, $2, $3, $4, $5, $6) const userInsertQuery = `
RETURNING *`; INSERT INTO "Person" (email, password, display_name, date_of_birth, available, enabled, place_of_living)
const result = await client.query(insertQuery, [ VALUES ($1, $2, $3, $4, $5, $6)
userData.display_name, RETURNING id`;
userData.date_of_birth,
userData.place_of_living, const userResult = await t.one(userInsertQuery, [
userData.is_looking_for_job, userData.email,
userData.email, await hashPasswordPromise,
await hashPasswordPromise userData.display_name,
]); userData.date_of_birth,
res.status(200).json(result.rows[0]); userData.available,
false,
userData.place_of_living
]);
const activationLinkInsertQuery = `
INSERT INTO "ActivationLink" (user_id, activation_code)
VALUES ($1, $2)
RETURNING *`;
const activationLinkResult = await t.one(activationLinkInsertQuery, [
userResult.id,
activationLink,
]);
return res.status(200).json({ user: userResult, activationLink: activationLinkResult });
});
} }
catch (error){ catch (error){
console.error('Error inserting data:', error); console.error('Error inserting data:', error);
res.status(500).json("Internal server error"); res.status(500).json("Internal server error");
} }
finally {
if (client) {
client.release();
}
}
} }
function login(req, res){ function login(req, res){
@ -116,6 +129,5 @@ function login(req, res){
} }
module.exports = { module.exports = {
register_async, register_async
login
}; };