Skip to content

Commit

Permalink
chore: update examples (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
zacharygolba authored Aug 13, 2016
1 parent d1fbddc commit e9df74a
Show file tree
Hide file tree
Showing 16 changed files with 71 additions and 48 deletions.
19 changes: 19 additions & 0 deletions examples/social-network/app/controllers/users.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
import { Controller } from 'lux-framework';

import User from 'app/models/user';

class UsersController extends Controller {
params = [
'name',
'email',
'password'
];

async login({
params: {
data: {
attributes: {
email,
password
}
}
}
}) {
const user = await User.findByEmail(email);

if (user) {
return user.authenticate(password);
}
}
}

export default UsersController;
10 changes: 5 additions & 5 deletions examples/social-network/app/models/action.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Model } from 'lux-framework';

import Comment from './comment';
import Notification from './notification';
import Post from './post';
import Reaction from './reaction';
import User from './user';
import Comment from 'app/models/comment';
import Notification from 'app/models/notification';
import Post from 'app/models/post';
import Reaction from 'app/models/reaction';
import User from 'app/models/user';

/* TODO: Add support for polymorphic relationship to a 'trackable'.
* https://github.com/postlight/lux/issues/75
Expand Down
2 changes: 1 addition & 1 deletion examples/social-network/app/models/comment.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Model } from 'lux-framework';

import track from '../utils/track';
import track from 'app/utils/track';

class Comment extends Model {
static belongsTo = {
Expand Down
2 changes: 1 addition & 1 deletion examples/social-network/app/models/post.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Model } from 'lux-framework';

import track from '../utils/track';
import track from 'app/utils/track';

class Post extends Model {
static belongsTo = {
Expand Down
2 changes: 1 addition & 1 deletion examples/social-network/app/models/reaction.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Model } from 'lux-framework';

import track from '../utils/track';
import track from 'app/utils/track';

class Reaction extends Model {
static belongsTo = {
Expand Down
35 changes: 13 additions & 22 deletions examples/social-network/app/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import {
generateSalt,
encryptPassword,
decryptPassword
} from '../utils/password';

const { assign } = Object;
} from 'app/utils/password';

class User extends Model {
static hasMany = {
Expand Down Expand Up @@ -46,39 +44,32 @@ class User extends Model {
if ((typeof id !== 'number') && password || dirtyAttributes.has('password')) {
const salt = generateSalt();

assign(user, {
Object.assign(user, {
password: encryptPassword(password, salt),
passwordSalt: salt
});
}
}
};

static scopes = {
findByEmail(email) {
return this.first().where({
email
});
}
};

static validates = {
password(password = '') {
return password.length >= 8;
}
};

static async authenticate(email, password) {
const user = await this.findOne({
where: {
email
}
});

if (user) {
const {
password: encrypted,
passwordSalt: salt
} = user;

if (password === decryptPassword(encrypted, salt)) {
return user;
}
}
authenticate(password) {
const { password: encrypted, passwordSalt: salt } = this;

return false;
return password === decryptPassword(encrypted, salt);
}
}

Expand Down
10 changes: 10 additions & 0 deletions examples/social-network/app/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export default function routes() {
action: 'show'
});

this.route('friendships', {
method: 'POST',
action: 'create'
});

this.route('notifications', {
method: 'GET',
action: 'index'
Expand All @@ -24,4 +29,9 @@ export default function routes() {
method: 'GET',
action: 'show'
});

this.route('users/login', {
method: 'POST',
action: 'login'
});
}
4 changes: 3 additions & 1 deletion examples/social-network/app/serializers/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { Serializer } from 'lux-framework';
class CommentsSerializer extends Serializer {
attributes = [
'edited',
'message'
'message',
'createdAt',
'updatedAt'
];

hasOne = [
Expand Down
8 changes: 5 additions & 3 deletions examples/social-network/app/serializers/friendships.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Serializer } from 'lux-framework';

class FriendshipsSerializer extends Serializer {
hasOne = [
'followee',
'follower'
attributes = [
'followerId',
'followeeId',
'createdAt',
'updatedAt'
];
}

Expand Down
4 changes: 3 additions & 1 deletion examples/social-network/app/serializers/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { Serializer } from 'lux-framework';
class NotificationsSerializer extends Serializer {
attributes = [
'unread',
'message'
'message',
'createdAt',
'updatedAt'
];

hasOne = [
Expand Down
4 changes: 3 additions & 1 deletion examples/social-network/app/serializers/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { Serializer } from 'lux-framework';
class PostsSerializer extends Serializer {
attributes = [
'body',
'title'
'title',
'createdAt',
'updatedAt'
];

hasOne = [
Expand Down
3 changes: 2 additions & 1 deletion examples/social-network/app/serializers/reactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Serializer } from 'lux-framework';

class ReactionsSerializer extends Serializer {
attributes = [
'type'
'type',
'createdAt'
];

hasOne = [
Expand Down
6 changes: 0 additions & 6 deletions examples/social-network/app/utils/password.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,3 @@ export function decryptPassword(hash, secret) {
decrypted += decipher.final('utf8');
return decrypted;
}

export default {
generateSalt,
encryptPassword,
decryptPassword
};
2 changes: 1 addition & 1 deletion examples/social-network/app/utils/track.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Action from '../models/action';
import Action from 'app/models/action';

export default async function track(trackable) {
if (trackable) {
Expand Down
4 changes: 2 additions & 2 deletions examples/social-network/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"author": "",
"license": "MIT",
"dependencies": {
"babel-core": "6.11.4",
"babel-core": "6.13.2",
"babel-preset-lux": "1.1.0",
"knex": "0.11.9",
"knex": "0.11.10",
"lux-framework": "1.0.0-rc.5",
"sqlite3": "3.1.4"
},
Expand Down
4 changes: 2 additions & 2 deletions examples/todo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"author": "",
"license": "MIT",
"dependencies": {
"babel-core": "6.11.4",
"babel-core": "6.13.2",
"babel-preset-lux": "1.1.0",
"knex": "0.11.9",
"knex": "0.11.10",
"lux-framework": "1.0.0-rc.5",
"sqlite3": "3.1.4"
},
Expand Down

0 comments on commit e9df74a

Please sign in to comment.