Let's say our API has some functionality, that might be consumed identically from different endpoints/controllers/routes. For example, we might wanna perform a login procedure, from different places. Perhaps both post(/login) and post(/register) will eventually log the user in.
So it would make sense, to put this procedure in a different module. For instance, i have this function, in a services/UserLogin class:
/**
*
* @param {String} email
* @param {String} password
* @return {Promise<Object|false>} Resolves with a User object on success, false on failure.
*/
static login(email, password) {
return new Promise((resolve, reject) => {
db.query('SELECT * FROM `user` WHERE email = ? AND password = ?', [email, password], async function (error, results, fields) {
if (error) {
reject(error);
}
console.log(results.length)
if (results.length > 0) {
// const id = results[0].id/
const user = results[0];
resolve(user);
} else {
resolve(false);
}
});
})
}
So i named this class a "service". I'm wondering though, whether a service is the correct terminology here. I didn't manage to find a clear definition of this term, in the context of modern web applications. Is it possible, this UserLogin class should better be referred to as a "facade"?
I'm sure all of us have found ourselves wondering where to place such functions, and which terminology to use. Can someone shed some light on the issue?
Aucun commentaire:
Enregistrer un commentaire