mirror of
https://github.com/Nezumi-2711/javascript-training.git
synced 2026-09-22 13:38:43 +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 {
|
export default class RegisterController {
|
||||||
constructor(service, view) {
|
constructor(service, view) {
|
||||||
this.view = view;
|
this.registerView = view.registerView;
|
||||||
this.service = service;
|
this.service = service;
|
||||||
}
|
}
|
||||||
|
|
||||||
async controlRegister() {
|
async controlRegister() {
|
||||||
try {
|
try {
|
||||||
// Load spinner
|
// Load spinner
|
||||||
this.view.registerView.toogleLoaderSpinner();
|
this.registerView.toogleLoaderSpinner();
|
||||||
|
|
||||||
// Get data from form
|
// Get data from form
|
||||||
const user = this.view.registerView.getDataFromForm();
|
const user = this.registerView.getDataFromForm();
|
||||||
|
|
||||||
// Save user
|
// Save user
|
||||||
if (user) {
|
if (user) {
|
||||||
// Check user exist
|
// Check user exist
|
||||||
const userExist = await this.service.userService.checkExistUserByEmail(
|
const userExist = await this.service.userService.checkUserExist(
|
||||||
user.email,
|
user.email,
|
||||||
);
|
);
|
||||||
if (userExist) {
|
if (userExist) {
|
||||||
@@ -23,24 +23,24 @@ export default class RegisterController {
|
|||||||
} else {
|
} else {
|
||||||
await this.service.userService.saveUser(user);
|
await this.service.userService.saveUser(user);
|
||||||
// Show popup success
|
// Show popup success
|
||||||
this.view.registerView.initRegisterSuccessPopup();
|
this.registerView.initRegisterSuccessPopup();
|
||||||
this.view.registerView.tooglePopupForm();
|
this.registerView.tooglePopupForm();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Show popup error
|
// Show popup error
|
||||||
this.view.registerView.initErrorPopup(error);
|
this.registerView.initErrorPopup(error);
|
||||||
this.view.registerView.tooglePopupForm();
|
this.registerView.tooglePopupForm();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close spinner
|
// Close spinner
|
||||||
this.view.registerView.toogleLoaderSpinner();
|
this.registerView.toogleLoaderSpinner();
|
||||||
}
|
}
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
if (this.view.registerView.registerForm !== null) {
|
if (this.registerView.registerForm !== null) {
|
||||||
this.view.registerView.addHandlerForm(this.controlRegister.bind(this));
|
this.registerView.addHandlerForm(this.controlRegister.bind(this));
|
||||||
this.view.registerView.addHandlerInputFormChange();
|
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
|
* @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();
|
this.connectToDb();
|
||||||
const existUser = FirebaseService.findKeyByPropery(
|
const existUser = FirebaseService.findKeyByPropery(
|
||||||
this.path,
|
this.path,
|
||||||
@@ -38,4 +38,23 @@ export default class UserService extends CommonService {
|
|||||||
|
|
||||||
return result;
|
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