mirror of
https://github.com/xfarrow/blink
synced 2025-06-27 09:03:02 +02:00
Update api.js
This commit is contained in:
@@ -3,10 +3,13 @@
|
|||||||
** licensed under GPLv3
|
** licensed under GPLv3
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const app = express();
|
const app = express();
|
||||||
const port = 3000;
|
const port = 3000;
|
||||||
|
|
||||||
|
const { Client } = require('pg');
|
||||||
|
|
||||||
// Middleware which parses JSON for POST requests
|
// Middleware which parses JSON for POST requests
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
|
||||||
@@ -24,8 +27,50 @@ app.get('/api/items', (req, res) => {
|
|||||||
|
|
||||||
// POST - Register an account
|
// POST - Register an account
|
||||||
app.post('/api/register', (req, res) => {
|
app.post('/api/register', (req, res) => {
|
||||||
const User = req.body;
|
const userData = req.body;
|
||||||
res.status(200).json(User);
|
|
||||||
|
// Ensure that the required fields are present before proceeding
|
||||||
|
if (!userData.display_name || !userData.email) {
|
||||||
|
return res.status(400).json("Invalid request");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a PostgreSQL client
|
||||||
|
const client = new Client({
|
||||||
|
user: 'postgres',
|
||||||
|
host: 'localhost',
|
||||||
|
database: 'Blink',
|
||||||
|
password: 'postgres',
|
||||||
|
port: 5432, // Default PostgreSQL port
|
||||||
|
});
|
||||||
|
|
||||||
|
client.connect()
|
||||||
|
.then(() => {
|
||||||
|
// SQL query with placeholders for parameters
|
||||||
|
const insertQuery = `
|
||||||
|
INSERT INTO "User" (display_name, date_of_birth, place_of_living, is_looking_for_job, email)
|
||||||
|
VALUES ($1, $2, $3, $4, $5)
|
||||||
|
RETURNING *`; // Return the inserted row
|
||||||
|
|
||||||
|
return client.query(insertQuery, [
|
||||||
|
userData.display_name,
|
||||||
|
userData.date_of_birth,
|
||||||
|
userData.place_of_living,
|
||||||
|
userData.is_looking_for_job,
|
||||||
|
userData.email
|
||||||
|
]);
|
||||||
|
})
|
||||||
|
.then((result) => {
|
||||||
|
// Respond with the inserted user data
|
||||||
|
res.status(200).json(result.rows[0]);
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
console.error('Error inserting data:', error);
|
||||||
|
res.status(500).json("Internal server error");
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
// Close the database connection
|
||||||
|
client.end();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Start the server
|
// Start the server
|
||||||
|
Reference in New Issue
Block a user