I have recently started using nodejs as I think I'm going to move onto a new project at work (I am usually a Java programmer).
I am making a rest service api. Up until now I have had a route which defines my endpoints and then a controller function is called which persists my object and then returns the response.
I want to delegate my persistence logic to a service module to keep it out of my controller. For my more complex cases this is definitely necessary.
I am struggling to find how I can successfully refactor my code to use a service. I have not so far successfully called a service method returning a value from my controller which is then rendered.
I will provide my original modules of concern that are working, followed by my failed attempt to pull out logic into a service module that returns a value:
routes/index.js
const UserController = require('../controllers/userController');
const ProfileController = require('../controllers/profileController');
module.exports = (app) => {
app.get('/api', (req, res) => res.status(200).send({
message: 'Welcome to the Todos API!',
}));
app.get('/api/users', UserController.list);
app.post('/api/users', UserController.create);
app.get('/api/users/1/profile', function() {
console.log('got profile for user 1');
});
app.post('/api/users/1/profile', ProfileController.create);
};
models/user.js
'use strict';
module.exports = (sequelize, DataTypes) => {
var User = sequelize.define('User', {
username: DataTypes.STRING,
password: DataTypes.STRING,
email: DataTypes.STRING,
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
}
}
});
return User;
};
original (working) controllers/userController.js
const User = require('../models').User;
module.exports = {
create(req, res) {
return User
.create({
username: req.body.username,
password: req.body.password,
email: req.body.email,
})
.then(user => res.status(201).send(user))
.catch(error => res.status(400).send(error));
},
list(req, res) {
return User
.all()
.then(users => res.status(200).send(users))
.catch(error => res.status(400).send("couldnt get list: " + error));
},
};
Now I attempt to refactor and pull out the persistance code into a service:
modified controllers/userController.js
const UserService = require('../services/userService');
module.exports = {
create(req, res) {
return Promise.resolve(UserService.create)
.then(user => res.status(201).send(user))
.catch(error => res.status(400).send(error));
},
list(req, res) {
return User
.all()
.then(users => res.status(200).send(users))
.catch(error => res.status(400).send("couldnt get list: " + error));
},
};
new services/userService.js
const User = require('../models').User;
module.exports = {
create(req, res) {
return User
.create({
username: req.body.username,
password: req.body.password,
email: req.body.email,
})
},
list(req, res) {
return User
.all()
},
};
I am fairly sure that my problem is my lack of knowledge about how callbacks and promises work.
Any help and/or advice would be really welcomed. I am trying to get a good head start with my re-skilling for my job.
Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire