Start add account endpoint

This commit is contained in:
rui hildt 2020-05-01 17:32:46 +02:00
parent 8465c4ecd6
commit 7e4d186446
9 changed files with 84 additions and 34 deletions

View File

@ -0,0 +1,18 @@
const db = require('../../data/dbConfig');
module.exports = {
addUser
};
function addUser(userData) {
db.one('INSERT INTO account VALUES("", ${username}, ${email}, ${password}, ${timezone}, ${earliest_time}, ${latest_time}) RETURNING *', userData)
.then(data => {
console.log(data);
return data;
})
.catch(error => {
console.log('ERROR:', error);
});
};

View File

@ -0,0 +1,19 @@
let express = require('express');
let router = express.Router();
let Account = require('../models/accountModel');
// Add a user
router.post('/', async (req, res) => {
const userData = {...req.body};
try {
const user = await Account.addUser(userData);
res.status(201).json(user);
} catch {
res.status(500).json({ message: "Failed to add new user" });
}
});
module.exports = router;

View File

@ -1,9 +0,0 @@
var express = require('express');
var router = express.Router();
// define the home page route
router.get('/', function (req, res) {
res.send('Accounts home page');
});
module.exports = router;

View File

@ -1,6 +1,7 @@
const express = require("express"); const express = require("express");
const accountsRoute = require('./routes/accountsRoute'); const accountsRoute = require('./routes/accountRoute');
const server = express(); const server = express();
server.use(express.json());
server.use('/api/accounts', accountsRoute); server.use('/api/accounts', accountsRoute);

5
data/dbConfig.js Normal file
View File

@ -0,0 +1,5 @@
const pgp = require('pg-promise')();
const db = pgp(process.env.DATABASE_URL);
module.exports = db;

View File

@ -1,5 +1,5 @@
## How to do a migration ## How to do a migration
- Create `.env` to the root folder - Create `.env` to the root folder
- Add the database url to the `.env` file: - Add the database url to the `.env` file:
`DATABASE_URL=postgres://db_user:password@server_url:PORT/db_name` `DATABASE_URL=postgres://username:password@host:port/database`
- Run `npm run migrate up` - Run `npm run migrate up`

View File

@ -1,9 +1,14 @@
/* eslint-disable camelcase */ const { PgLiteral } = require('node-pg-migrate');
exports.shorthands = { exports.shorthands = {
id: { type: 'serial', primaryKey: true }, id: { type: 'serial', primaryKey: true },
varchar: { type: 'varchar(128)' }, varchar: { type: 'varchar(128)' },
varchar_req: { type: 'varchar(128)', notNull: true } varchar_req: { type: 'varchar(128)', notNull: true },
created_at: {
type: 'timestamp',
notNull: true,
default: PgLiteral.create('CURRENT_TIMESTAMP'),
},
}; };
exports.up = (pgm) => { exports.up = (pgm) => {
@ -15,11 +20,7 @@ exports.up = (pgm) => {
timezone: 'varchar', timezone: 'varchar',
earliest_time: 'time', earliest_time: 'time',
latest_time: 'time', latest_time: 'time',
createdAt: { createdAt: 'created_at',
type: 'timestamp',
notNull: true,
default: pgm.func('current_timestamp'),
},
}); });
pgm.createTable('meeting', { pgm.createTable('meeting', {
@ -31,11 +32,7 @@ exports.up = (pgm) => {
duration: 'int', duration: 'int',
status: 'boolean', status: 'boolean',
password: 'varchar', password: 'varchar',
createdAt: { createdAt: 'created_at',
type: 'timestamp',
notNull: true,
default: pgm.func('current_timestamp'),
},
}); });
pgm.createTable('possible_date', { pgm.createTable('possible_date', {
@ -70,11 +67,7 @@ exports.up = (pgm) => {
host: 'boolean', host: 'boolean',
answered: 'boolean', answered: 'boolean',
timezone: 'varchar', timezone: 'varchar',
createdAt: { createdAt: 'created_at',
type: 'timestamp',
notNull: true,
default: pgm.func('current_timestamp'),
},
}); });
pgm.createTable('availibility', { pgm.createTable('availibility', {
@ -95,11 +88,7 @@ exports.up = (pgm) => {
start_time: { type: 'timestamp', notNull: true }, start_time: { type: 'timestamp', notNull: true },
end_time: { type: 'timestamp', notNull: true }, end_time: { type: 'timestamp', notNull: true },
timezone: 'varchar_req', timezone: 'varchar_req',
createdAt: { createdAt: 'created_at',
type: 'timestamp',
notNull: true,
default: pgm.func('current_timestamp'),
},
}); });
}; };

26
package-lock.json generated
View File

@ -56,6 +56,11 @@
"resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
"integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI="
}, },
"assert-options": {
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/assert-options/-/assert-options-0.6.1.tgz",
"integrity": "sha512-jH2pNULN0t3uFLb7Fh0SAuMo/Ei5yWiRirvLez2g+sd16d0xKl+DGdGkD6sqkrZTnCZK5lWRjUa4X3sxHQkg9g=="
},
"body-parser": { "body-parser": {
"version": "1.19.0", "version": "1.19.0",
"resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.19.0.tgz", "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.19.0.tgz",
@ -444,11 +449,27 @@
"resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz", "resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz",
"integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==" "integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw=="
}, },
"pg-minify": {
"version": "1.5.2",
"resolved": "https://registry.npmjs.org/pg-minify/-/pg-minify-1.5.2.tgz",
"integrity": "sha512-uZn/gXkGmO5JBdopxNLSpFMS1lXr+KJqynI8Di1Qyr8ZVXt67ruh+XNfzLMVdLzYv+MQRdNYQdVwWPSs0qM7xQ=="
},
"pg-pool": { "pg-pool": {
"version": "3.1.1", "version": "3.1.1",
"resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.1.1.tgz", "resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.1.1.tgz",
"integrity": "sha512-kYH6S0mcZF1TPg1F9boFee2JlCSm2oqnlR2Mz2Wgn1psQbEBNVeNTJCw2wCK48QsctwvGUzbxLMg/lYV6hL/3A==" "integrity": "sha512-kYH6S0mcZF1TPg1F9boFee2JlCSm2oqnlR2Mz2Wgn1psQbEBNVeNTJCw2wCK48QsctwvGUzbxLMg/lYV6hL/3A=="
}, },
"pg-promise": {
"version": "10.5.2",
"resolved": "https://registry.npmjs.org/pg-promise/-/pg-promise-10.5.2.tgz",
"integrity": "sha512-PsLE6SaPHoe7b5QJhoYeKeNcyTM4YYczTR0YwOKFtCVuLGejfA8nUHlaJEM7NUcORfidVxW7RPaXmY/aCf3Nqw==",
"requires": {
"assert-options": "0.6.1",
"pg": "8.0.3",
"pg-minify": "1.5.2",
"spex": "3.0.1"
}
},
"pg-protocol": { "pg-protocol": {
"version": "1.2.2", "version": "1.2.2",
"resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.2.2.tgz", "resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.2.2.tgz",
@ -600,6 +621,11 @@
"resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.1.tgz", "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.1.tgz",
"integrity": "sha512-JvdAWfbXeIGaZ9cILp38HntZSFSo3mWg6xGcJJsd+d4aRMOqauag1C63dJfDw7OaMYwEbHMOxEZ1lqVRYP2OAw==" "integrity": "sha512-JvdAWfbXeIGaZ9cILp38HntZSFSo3mWg6xGcJJsd+d4aRMOqauag1C63dJfDw7OaMYwEbHMOxEZ1lqVRYP2OAw=="
}, },
"spex": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/spex/-/spex-3.0.1.tgz",
"integrity": "sha512-priWZUrXBmVPHTOmtUeS7gZzCOUwRK87OHJw5K8bTC6MLOq93mQocx+vWccNyKPT2EY+goZvKGguGn2lx8TBDA=="
},
"split": { "split": {
"version": "1.0.1", "version": "1.0.1",
"resolved": "https://registry.npmjs.org/split/-/split-1.0.1.tgz", "resolved": "https://registry.npmjs.org/split/-/split-1.0.1.tgz",

View File

@ -18,6 +18,7 @@
"dotenv": "^8.2.0", "dotenv": "^8.2.0",
"express": "^4.17.1", "express": "^4.17.1",
"node-pg-migrate": "^4.7.0", "node-pg-migrate": "^4.7.0",
"pg": "^8.0.3" "pg": "^8.0.3",
"pg-promise": "^10.5.2"
} }
} }