Skip to content

Commit

Permalink
feat(api): use sequelize
Browse files Browse the repository at this point in the history
  • Loading branch information
balthazar committed Oct 8, 2015
1 parent 4211782 commit 5da7860
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 96 deletions.
14 changes: 0 additions & 14 deletions api/data/index.js

This file was deleted.

20 changes: 0 additions & 20 deletions api/data/reset.js

This file was deleted.

18 changes: 0 additions & 18 deletions api/data/text.table.js

This file was deleted.

20 changes: 0 additions & 20 deletions api/data/user.table.js

This file was deleted.

7 changes: 7 additions & 0 deletions api/models/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

import Sequelize from 'sequelize';

import config from '../../config';

export default new Sequelize(config.postgres);
22 changes: 22 additions & 0 deletions api/models/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

import q from 'q';
import Text from './text.model';
import User from './user.model';
import Language from './language.model';

q.all([
User.sync(),
Language.sync(),
])
.then(() => { return Text.sync(); })
.then(() => { console.log('Success'); })
.catch(err => { console.log(err); });

export default {

User,
Text,
Language

};
12 changes: 12 additions & 0 deletions api/models/language.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

import Sequelize from 'sequelize';

import db from './db';

export default db.define('language', {

name: { type: Sequelize.STRING },
img: { type: Sequelize.STRING }

});
23 changes: 23 additions & 0 deletions api/models/text.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

import Sequelize from 'sequelize';

import db from './db';
import { User, Language } from './';

export default db.define('text', {

data: { type: Sequelize.STRING(1000) },
author: { type: Sequelize.INTEGER, model: User },
language: { type: Sequelize.INTEGER, model: Language },

upvotes: { type: Sequelize.INTEGER, defaultValue: 0 },
downvotes: { type: Sequelize.INTEGER, defaultValue: 0 },
flags: { type: Sequelize.INTEGER, defaultValue: 0 },

rating: { type: Sequelize.FLOAT, defaultValue: 0 },
ratingCount: { type: Sequelize.INTEGER, defaultValue: 0 },

used: { type: Sequelize.INTEGER, defaultValue: 0 }

});
18 changes: 18 additions & 0 deletions api/models/user.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

import Sequelize from 'sequelize';

import db from './db';

export default db.define('user', {

name: { type: Sequelize.STRING },
avatar: { type: Sequelize.STRING },
email: { type: Sequelize.STRING },

banned: { type: Sequelize.BOOLEAN, defaultValue: false },
admin: { type: Sequelize.BOOLEAN, defaultValue: false },

githubId: { type: Sequelize.BIGINT }

});
28 changes: 5 additions & 23 deletions api/services/user.service.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,18 @@
'use strict';

import { db } from '../data';
import { User } from '../models';

exports.getById = id => {
return db.one('SELECT * FROM users WHERE id = $1', id);
return User.findById(id);
};

exports.getByGithub = id => {
return db.one('SELECT * FROM users WHERE githubId = $1', id);
return User.findOne({ where: { githubId: id }});
};

exports.updateOrCreate = (id, name, avatar) => {
exports.updateOrCreate = (githubId, name, avatar) => {

return db.oneOrNone('SELECT * FROM users WHERE githubId = $1', id)
.then(user => {
if (user) {
return db.none(`
UPDATE users SET name = $1, avatar = $2 WHERE id = $3;
`, [name, avatar, user.id])
.then(() => {
return { ...user, name, avatar };
});
}

return db.none(`
INSERT INTO users(name, avatar, githubId) VALUES ($1, $2, $3);
`, [name, avatar, id]);
})
.then(user => {
if (user) { return user; }
return exports.getByGithub(id);
})
return User.upsert({ name, avatar, githubId }, { fields: ['name', 'avatar'] })
.then(user => {
if (user.banned) { throw new Error('You are banned from KeyCode.'); }
return user;
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@
"nodemailer": "^1.5.0",
"passport": "^0.3.0",
"passport-github": "^1.0.0",
"pg-promise": "^1.10.7",
"pg": "^4.4.2",
"pg-hstore": "^2.3.2",
"q": "^1.4.1",
"react": "^0.13.0",
"sequelize": "^3.11.0",
"serialize-javascript": "^1.0.0",
"serve-favicon": "^2.1.6",
"superagent": "^1.4.0"
Expand Down

0 comments on commit 5da7860

Please sign in to comment.