Skip to content

Commit 4bed0be

Browse files
committed
feat(api): basic user service
1 parent 0ca26e5 commit 4bed0be

15 files changed

+105
-56
lines changed

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,16 @@ Run the project in development environment
2020
npm run prod
2121

2222
Run the project for production. Isomorphic full power.
23+
24+
### Database
25+
26+
The api is powered by the PostgreSQL database server.
27+
It is required if you want to make local testing.
28+
Following steps are only relevant to an Archlinux configuration. For any other systems, you'll have to figure things out.
29+
30+
pacman -S postgresql
31+
initdb --locale en_US.UTF-8 -E UTF8 -D '/var/lib/postgres/data'
32+
systemctl start postgresql.service
33+
createdb keycode-dev
34+
35+
You may have to add yourself in the postgres group.

api/controllers/user.controller.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
'use strict';
22

3+
import { UserService } from '../services';
4+
35
export default {
46

57
create: (req, res) => {
6-
res.status('200').send('created.');
8+
UserService.create(req.body.name, req.body.email)
9+
.then(function (user) {
10+
res.status('200').send(user);
11+
})
12+
.catch(function (err) {
13+
res.status(400).send({ message: err.message });
14+
});
715
}
816

917
}

api/data/index.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
import q from 'q';
4+
5+
import config from '../../config';
6+
7+
let pgp = require('pg-promise')({ promiseLib: q });
8+
let db = pgp(config.postgres);
9+
10+
import Text from './text.table';
11+
import User from './user.table';
12+
13+
export default {
14+
15+
db,
16+
17+
init: () => {
18+
return q.all([
19+
db.query(User),
20+
db.query(Text)
21+
]);
22+
}
23+
24+
}

api/data/text.table.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
export default `
4+
CREATE TABLE IF NOT EXISTS texts (
5+
6+
id SERIAL PRIMARY KEY,
7+
8+
data VARCHAR(1000) NOT NULL,
9+
author INTEGER REFERENCES users (id),
10+
11+
votes INTEGER,
12+
approved BOOLEAN DEFAULT false,
13+
14+
rating INTEGER,
15+
ratingcount INTEGER
16+
17+
);
18+
`;

api/data/user.table.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
export default `
4+
CREATE TABLE IF NOT EXISTS users (
5+
6+
id SERIAL PRIMARY KEY,
7+
8+
name VARCHAR(42) NOT NULL,
9+
email VARCHAR(42) UNIQUE,
10+
11+
lognup VARCHAR(60),
12+
lognupat DATE,
13+
14+
banned BOOLEAN,
15+
admin BOOLEAN
16+
17+
);
18+
`;

api/models/index.js

-9
This file was deleted.

api/models/text.model.js

-20
This file was deleted.

api/models/user.model.js

-18
This file was deleted.

api/services/text.service.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
'use strict';
22

3-
import { Text } from '../models';
4-
53
export default {
64

75
create: () => {

api/services/user.service.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
'use strict';
22

3-
import { User } from '../models';
3+
import q from 'q';
4+
5+
import { db } from '../data';
46

57
export default {
68

7-
create: () => {
9+
create: (name, email) => {
10+
11+
if (!name || !email) {
12+
return q.reject(new Error('Missing data'));
13+
}
14+
15+
return db.one('SELECT * FROM users WHERE email = $1', email)
16+
.then(function (user) {
17+
18+
if (user) { return user; }
19+
20+
return db.query(`
21+
INSERT INTO users(name, email) VALUES ($1, $2)
22+
`, [name, email]);
23+
});
824

925
}
1026

config/dev.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
export default {
44

5-
mongo: 'mongodb://zavatta:[email protected].com:55822/keycode'
5+
postgres: 'postgres://opzbsbhu:[email protected].com:5432/opzbsbhu'
66

77
}

config/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ const env = process.env.NODE_ENV || 'dev';
66

77
export default _.merge({
88

9-
env,
9+
env
1010

1111
}, require('./' + env));

config/prod.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
export default {
44

5-
mongo: 'mongodb://localhost/keycode'
5+
postgres: 'postgres://xzhsmpgo:[email protected]:5432/xzhsmpgo'
66

77
}

config/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
export default {
44

5-
mongo: 'mongodb://localhost/keycode-test'
5+
postgres: 'postgres://@localhost/keycode-test'
66

77
}

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"lodash": "^3.10.1",
2626
"node-uuid": "^1.4.3",
2727
"nodemailer": "^1.5.0",
28+
"pg-promise": "^1.10.7",
2829
"react": "^0.13.0",
2930
"serialize-javascript": "^1.0.0",
3031
"serve-favicon": "^2.1.6"

0 commit comments

Comments
 (0)