Hi,

Ive been trying to learn the mean stack, originally i come from a PHP OOP 
background. I'm developing a restful api at the moment. I was hoping 
someone could validate what ive attempted so far.

1. Create a new user account
2. Assign a token to that account.


routes

module.exports = function(app){

    app.use('/api/account', require('./app/routes/account.route'));

};


account.route

'use strict';

var express    = require('express');
var router     = express.Router();
var controller = require('../controllers/account.controller');
controller     = new controller();

router.get('/:id', controller.show.bind(controller));
router.post('/', controller.store.bind(controller));


module.exports = router;


account.controller

'use strict';


var AccountRepository = require('../repositories/account.repository');


class AccountController {
    
    constructor() {
        this.accRepo = new AccountRepository();
    }
    
    show(req, res) {
        
    }

    store(req, res) {
        
        var user = {
            email: req.body.email,
            password: req.body.password
        };
        
        // Todo: Generate Token
        var token = {
            type: "email",
            value: "1234"
        };
        
        this.accRepo.createNewUser(user, token, (data) => {
            
            return res.status(data.status).json(data);
            
        });
        
    }
    
    
}

module.exports = AccountController;


account.repository

'use strict';

var User  = require('../models/user.model');
var Token = require('../models/token.model');

class AccountRepository {
    
    createNewUser(newUser, newToken, response) {
        
        User.create(newUser, function (err, user) {
            
            if (err) return response({
                success: false,
                status: 500,
                message: 'Failed trying to create account',
                error: err
            });
            
            Token.create(newToken, function (err, token) {
                
                if (err) return response({
                    success: false,
                    status: 500,
                    message: 'Failed trying to create token',
                    error: err
                });
                
                user.tokens.push(token);
                
                user.save((err, doc) => {
                    
                    if (err) return response({
                        success: false,
                        status: 500,
                        message: 'Failed trying to save user',
                        error: err
                    });
                    
                    if (doc) return response({
                        
                        success: true,
                        status: 201,
                        message: "Successful Created New User",
                        user: doc,
                        
                    })
                    
                });
                
            });
            
        })
    }
}

module.exports = AccountRepository;



Please tell me what you think of this approach in general AND I  would like 
to make improvements to my AccountRepository as that createNewUser function 
is quite big ( so many callbacks, even more when i add sending sms 
verification OR sending email verification) if you have feedback on this 
also that would be appreciated.


-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/22dc9003-c444-4e78-b202-2517acf0eb14%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to