mirror of
https://github.com/Nezumi-2711/javascript-training.git
synced 2026-09-22 20:01:31 +00:00
Add method and optimized code
- Add some method in userService - Optimized code in registerController
This commit is contained in:
@@ -1,21 +1,21 @@
|
||||
export default class RegisterController {
|
||||
constructor(service, view) {
|
||||
this.view = view;
|
||||
this.registerView = view.registerView;
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
async controlRegister() {
|
||||
try {
|
||||
// Load spinner
|
||||
this.view.registerView.toogleLoaderSpinner();
|
||||
this.registerView.toogleLoaderSpinner();
|
||||
|
||||
// Get data from form
|
||||
const user = this.view.registerView.getDataFromForm();
|
||||
const user = this.registerView.getDataFromForm();
|
||||
|
||||
// Save user
|
||||
if (user) {
|
||||
// Check user exist
|
||||
const userExist = await this.service.userService.checkExistUserByEmail(
|
||||
const userExist = await this.service.userService.checkUserExist(
|
||||
user.email,
|
||||
);
|
||||
if (userExist) {
|
||||
@@ -23,24 +23,24 @@ export default class RegisterController {
|
||||
} else {
|
||||
await this.service.userService.saveUser(user);
|
||||
// Show popup success
|
||||
this.view.registerView.initRegisterSuccessPopup();
|
||||
this.view.registerView.tooglePopupForm();
|
||||
this.registerView.initRegisterSuccessPopup();
|
||||
this.registerView.tooglePopupForm();
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
// Show popup error
|
||||
this.view.registerView.initErrorPopup(error);
|
||||
this.view.registerView.tooglePopupForm();
|
||||
this.registerView.initErrorPopup(error);
|
||||
this.registerView.tooglePopupForm();
|
||||
}
|
||||
|
||||
// Close spinner
|
||||
this.view.registerView.toogleLoaderSpinner();
|
||||
this.registerView.toogleLoaderSpinner();
|
||||
}
|
||||
|
||||
init() {
|
||||
if (this.view.registerView.registerForm !== null) {
|
||||
this.view.registerView.addHandlerForm(this.controlRegister.bind(this));
|
||||
this.view.registerView.addHandlerInputFormChange();
|
||||
if (this.registerView.registerForm !== null) {
|
||||
this.registerView.addHandlerForm(this.controlRegister.bind(this));
|
||||
this.registerView.addHandlerInputFormChange();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,11 +23,11 @@ export default class UserService extends CommonService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Check user exist in database by email
|
||||
* Get user by email
|
||||
* @param {string} email Email need to be check
|
||||
* @returns {boolean} Return true if email exist and otherwise is false
|
||||
* @returns {Promise || number} Return id user when exist, otherwise will undefined
|
||||
*/
|
||||
async checkExistUserByEmail(email) {
|
||||
async getUserIdByEmail(email) {
|
||||
this.connectToDb();
|
||||
const existUser = FirebaseService.findKeyByPropery(
|
||||
this.path,
|
||||
@@ -38,4 +38,23 @@ export default class UserService extends CommonService {
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check user exist in database
|
||||
* @param {string} email Email to find user
|
||||
* @returns {boolean} Return true if find, otherwise return false
|
||||
*/
|
||||
async checkUserExist(email) {
|
||||
const userExist = await this.getUserIdByEmail(email);
|
||||
if (userExist) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
async getUserByEmail(email) {
|
||||
const id = await this.getUserIdByEmail(email);
|
||||
const user = await FirebaseService.getDataFromId(id, this.path);
|
||||
return new User(user);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user