Skip to content

Commit

Permalink
Merge pull request #40 from karudedios/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
karudedios committed Feb 15, 2017
2 parents 4029b69 + f9601b2 commit 8ec3ffa
Show file tree
Hide file tree
Showing 37 changed files with 563 additions and 172 deletions.
5 changes: 4 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
"angular": "^1.5.6",
"angular-ui-router": "^0.3.0",
"Materialize": "materialize#^0.97.6",
"angular-resource": "^1.5.6"
"angular-resource": "^1.5.6",
"angular-materialize": "^0.1.8",
"materialize-colorpicker": "*",
"prism": "^1.5.1"
}
}
1 change: 1 addition & 0 deletions features/auth/router/auth.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use strict';
const Router = require('express').Router;

module.exports = (User, strategy) =>
Expand Down
10 changes: 7 additions & 3 deletions features/auth/strategies/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,20 @@ module.exports = class LocalAuthenticationStrategy {
logIn(req, res, user);
})(req, res, next);

this.signup = (req, res) =>
this.signup = (req, res) => {
const createUser = new CreateUser(User);

new FindUser(User)
.findOne({ username: req.body.username })
.then(user => {
if (user) throw "Username exists";
return req.body;
})
.then(new CreateUser(User).create)
.then(createUser.create.bind(createUser))
.then(UserDto.new)
.then(logIn.bind(null, req, res))
.catch(err => res.status(400).send(err));
.catch(err => console.log(err) || res.status(400).send(err));
};
}

unauthenticated(req, res, next) {
Expand Down
10 changes: 7 additions & 3 deletions features/todo/model/todo.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

Expand All @@ -8,13 +7,13 @@ const TodoSchema = new Schema({
type: String,
required: true
},

desc: {
type: String,
required: false,
default: ''
},

color: {
type: String,
required: false,
Expand All @@ -25,6 +24,11 @@ const TodoSchema = new Schema({
},
message: '{VALUE} is not a valid HEX color'
}
},

owner: {
ref: 'User',
type: Schema.Types.ObjectId,
}
});

Expand Down
27 changes: 14 additions & 13 deletions features/todo/router/todo.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const _ = require('lodash');
const TodoDto = require('../model/dto');
const Router = require('express').Router;
const FindTodo = require('../services/findTodo');
Expand All @@ -6,29 +7,29 @@ const UpdateTodo = require('../services/updateTodo');
const DeleteTodo = require('../services/deleteTodo');
const PromisedResponse = require('../../../utils/promisedResponse');

module.exports = (Todo) =>
module.exports = (Todo, strategy) =>
new Router()
.get('/', PromisedResponse(() =>
.get('/', strategy.authenticated, PromisedResponse((req) =>
new FindTodo(Todo)
.find({ })
.find({ owner: req.user._id })
.then(TodoDto.newList)))
.get('/:id', PromisedResponse(req =>

.get('/:id', strategy.authenticated, PromisedResponse(req =>
new FindTodo(Todo)
.findOne({ _id: req.params.id })
.findOne({ _id: req.params.id, owner: req.user._id })
.then(TodoDto.new)))
.put('/:id', PromisedResponse(req =>

.put('/:id', strategy.authenticated, PromisedResponse(req =>
new UpdateTodo(Todo)
.update(req.params.id, req.body)
.then(TodoDto.new)))
.post('/', PromisedResponse(req =>

.post('/', strategy.authenticated, PromisedResponse(req =>
new CreateTodo(Todo)
.create(req.body)
.create(_.assign({ owner: req.user._id }, req.body))
.then(TodoDto.new)))
.delete('/:id', PromisedResponse(req =>

.delete('/:id', strategy.authenticated, PromisedResponse(req =>
new DeleteTodo(Todo)
.delete(req.params.id)
.then(TodoDto.new)));
6 changes: 4 additions & 2 deletions features/todo/services/createTodo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
const Q = require('q');
const Joi = require('joi');
const validateSchema = require('../../../utils/validateSchema');
const objectIdSchema = require('../../../utils/objectIdSchema');

const todoSchema = Joi.object().keys({
name: Joi.string().required().label('todo.name')
name: Joi.string().required().label('todo.name'),
owner: objectIdSchema.required().label('todo.owner')
}).label('todo');

module.exports = class CreateTodo {
constructor(Todo) {
Object.assign(this, { Todo });
}

create(todo) {
return Q.when(todo)
.then(validateSchema(todoSchema, todo))
Expand Down
5 changes: 3 additions & 2 deletions features/todo/services/findTodo.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@ const predicateSchema = Joi.object().keys({
_id: objectIdSchema.optional().label('todo._id'),
name: Joi.string().optional().label('todo.name'),
desc: Joi.string().optional().label('todo.desc'),
owner: objectIdSchema.optional().label('todo.owner'),
color: Joi.string().regex(/#[a-f0-9]{6}/i).optional().label('todo.color')
}).required().label('predicate');

module.exports = class FindTodo {
constructor(Todo) {
Object.assign(this, { Todo });
}

findOne(predicate) {
return Q.when()
.then(validateSchema(predicateSchema, predicate))
.then(() => {
return Q.ninvoke(this.Todo, 'findOne', predicate);
});
}

find(predicate) {
return Q.when()
.then(validateSchema(predicateSchema, predicate))
Expand Down
6 changes: 3 additions & 3 deletions features/user/router/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ const FindUser = require('../services/findUser');
const UpdateUser = require('../services/updateUser');
const PromisedResponse = require('../../../utils/promisedResponse');

module.exports = (User) =>
module.exports = (User, strategy) =>
new Router()
.get('/:id', PromisedResponse(req =>
.get('/:id', strategy.authenticated, PromisedResponse(req =>
new FindUser(User)
.findOne({ _id: req.params.id })
.then(UserDto.new)))

.put('/:id', PromisedResponse(req =>
.put('/:id', strategy.authenticated, PromisedResponse(req =>
new UpdateUser(User)
.update(req.params.id, req.body)
.then(UserDto.new)));
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"express-session": "^1.13.0",
"http": "0.0.0",
"joi": "^8.4.0",
"lodash": "^4.13.1",
"md5": "^2.1.0",
"mongoose": "^4.4.19",
"passport": "^0.3.2",
Expand Down
18 changes: 15 additions & 3 deletions public/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@

angular.module('todoApp',
[
'ui.router',
'ngResource'
]);
'ngResource',
'ui.materialize'
]).run(['$rootScope', 'Auth', '$state', AppRun]);

function AppRun($rootScope, Auth, $state) {
$rootScope.$on('$stateChangeStart', function (event, toState, toStateParams) {
Auth.me(function (doc) {
if(toState.name == 'login'){
$state.go('app.home')
}
}, function (err) {
$state.go('login')
})
})
}
5 changes: 3 additions & 2 deletions public/app.state.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function AppStateConfig($stateProvider, $urlRouterProvider){
views : {
'header' : {
templateUrl : 'layout/header.html',
controller : 'HeaderController'
controller : 'HeaderController as ctrl'
},
'content' : {
template : '<div class="container" ui-view></div>'
Expand All @@ -18,7 +18,8 @@ function AppStateConfig($stateProvider, $urlRouterProvider){

$urlRouterProvider.otherwise(function ($injector, $location) {
var $state = $injector.get('$state');

$state.go('app.home');
});


}
24 changes: 24 additions & 0 deletions public/factory/auth.factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
angular.module('todoApp')
.factory('Auth', ['$resource', AuthFactory]);

function AuthFactory($resource) {
return $resource('/auth', {},
{
signup: {
method : 'POST',
url : '/auth/signup'
},
me : {
method: 'GET',
url : '/auth/me'
},
signin : {
method: 'POST',
url : '/auth/signin',
},
signout: {
method: 'POST',
url: '/auth/signout'
}
});
}
20 changes: 0 additions & 20 deletions public/factory/todo.colors.js

This file was deleted.

6 changes: 6 additions & 0 deletions public/factory/user.factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
angular.module('todoApp')
.factory('User', ['$resource', User]);

function User($resource){
return $resource('/api/user/:id');
}
26 changes: 21 additions & 5 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
<meta charset="UTF-8">
<title>todo app</title>
<link href="../bower_components/Materialize/dist/css/materialize.min.css" rel="stylesheet"/>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link href="../bower_components/materialize-colorpicker/dist/css/materialize-colorpicker.min.css" rel="stylesheet"/>
<link href="../bower_components/prism/themes/prism.css" rel="stylesheet"/>

<link href="styles/todo.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

</head>
<body ng-app="todoApp">
Expand All @@ -17,25 +20,38 @@

</div>
<!-- bower dependencies -->
<script src="../bower_components/angular/angular.min.js"></script>
<script src="../bower_components/angular-ui-router/release/angular-ui-router.min.js"></script>
<script src="../bower_components/jquery/dist/jquery.min.js"></script>
<script src="../bower_components/angular/angular.min.js"></script>
<script src="../bower_components/Materialize/bin/materialize.js"></script>
<script type="text/javascript" src="../bower_components/angular-materialize/src/angular-materialize.js"></script>
<script src="../bower_components/angular-ui-router/release/angular-ui-router.min.js"></script>
<script src="../bower_components/angular-resource/angular-resource.min.js"></script>

<script src="../bower_components/materialize-colorpicker/dist/js/materialize-colorpicker.min.js"></script>
<script src="../bower_components/prism/prism.js"></script>

<!-- bower dependencies -->

<script src="app.js"></script>
<script src="app.state.js"></script>
<script src="utils.js"></script>

<script src="login/login.state.js"></script>
<script src="login/login.controller.js"></script>
<script src="login/directives/signup/login.signup.modal.directive.js"></script>
<script src="login/directives/password/login.password.confirmation.directive.js"></script>

<script src="layout/header.controller.js"></script>
<script src="sections/home/home.state.js"></script>
<script src="sections/todo/todo.state.js"></script>
<script src="sections/todo/todo.controller.js"></script>

<script src="sections/todo/directives/create/todo.create.modal.directive.js"></script>
<script src="sections/todo/directives/editcolor/todo.editcolor.modal.directive.js"></script>

<script src="sections/todo/directives/detail/todo.detail.modal.directive.js"></script>
<script src="factory/todo.factory.js"></script>
<script src="factory/todo.colors.js"></script>
<script src="factory/user.factory.js"></script>
<script src="factory/auth.factory.js"></script>

</body>
</html>
17 changes: 13 additions & 4 deletions public/layout/header.controller.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
angular.module('todoApp')
.controller('HeaderController', ['$scope', HeaderController]);
.controller('HeaderController', ['$scope', 'Auth', '$state', HeaderController]);

function HeaderController($scope){
function HeaderController($scope, Auth, $state){
var self = this;
self.showSideNav = showSideNav;

$(".button-collapse").sideNav();
self.logout = logout;

showSideNav();

function logout() {
Auth.signout({}, function(doc){
}, function(){
$state.go('login');
})
}

function showSideNav(){
$(".button-collapse").sideNav();
}
}
3 changes: 3 additions & 0 deletions public/layout/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
<li><a href="#/">Home</a></li>
<li><a href="#/todo">Tasks to Do</a></li>
</ul>
<ul class="right hide-on-med-and-down">
<li><a href="#/" ng-click="ctrl.logout()">Logout</a></li>
</ul>

<ul class="side-nav" id="mobile-demo">
<li><a href="#/">Home</a></li>
Expand Down
Loading

0 comments on commit 8ec3ffa

Please sign in to comment.