Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import {db} from "./database";
- let Sequelize = require('sequelize');
- import {PasswordController} from "./PasswordController";
- // import {User} from "../interfaces/UserInterface";
- export class UserController {
- static findAll() {
- return new Promise((res, rej) => {
- db.users.findAll({
- attributes: ['id', 'nome', 'email']
- }).then(users => {
- res(users);
- }).catch(err => {
- rej(err);
- });
- });
- }
- static findAndCountAll(pagina, quantidade, busca) {
- return new Promise((res, rej) => {
- let registros = quantidade;
- let inicio = 0;
- if (pagina > 1) {
- inicio = (pagina - 1) * registros;
- }
- let objConf = {
- attributes: ['id', 'nome', 'sobrenome', 'email', 'role', 'latitude', 'longitude', 'novidades', 'alertas'],
- offset: inicio,
- order: [['id', 'ASC']]
- };
- const Op = Sequelize.Op;
- if (busca !== "") {
- let objWhere = {
- [Op.or]: [
- {
- username: {
- [Op.like]: '%' + busca + '%'
- }
- },
- {
- email: {
- [Op.like]: '%' + busca + '%'
- }
- }
- ]
- };
- objConf['where'] = objWhere;
- }
- if (quantidade > 0) {
- objConf['limit'] = quantidade;
- }
- db.users.findAndCountAll(objConf).then(users => {
- let objRetorno = users.rows;
- let usuarios = [];
- for (let usuario of objRetorno) {
- console.log(usuario);
- usuarios.push(usuario.dataValues);
- }
- let total = users.count + 1;
- let retornar = {total: total, usuarios: usuarios};
- res(retornar);
- }).catch(err => {
- rej(err);
- });
- });
- }
- static findOne(data) {
- return new Promise((res, rej) => {
- db.users.findOne({where: {email: data.email}}).then(user => {
- if (user == null) {
- rej('Usuario nao encontrado');
- }
- res(user);
- }).catch(err => {
- rej(err);
- });
- })
- }
- static findMail(data) {
- return new Promise((res, rej) => {
- db.users.findOne({where: {email: data}}).then(user => {
- if (user == null) {
- //rej('Usuario nao encontrado');
- res("false");
- }
- res("true");
- }).catch(err => {
- rej(err);
- });
- })
- }
- static addUser(data, cb) {
- PasswordController.criaHash(data.password, (hash, salt) => {
- db.users.create({
- nome: data.nome,
- sobrenome: data.sobrenome,
- password: hash,
- role: data.role,
- salt: salt,
- email: data.email,
- latitude: data.latitude,
- longitude: data.longitude,
- novidades: data.novidades,
- alertas: data.alertas
- }).then(() => {
- cb(true)
- }).catch(() => {
- cb("nao inserido");
- });
- });
- }
- static delUser(id, cb) {
- db.users.destroy({
- where: {
- id: id
- }
- }).then(msg => {
- cb(null, msg); //1 deletado tipo, o callback devolve 2 parametros, o primeiro eh o erro e o segundo o resultado
- }).catch(err => {
- cb(err, null); //0 error
- });
- }
- }
- // exports.UserController = UserController;
- UserController.findAll().then((result) => {
- let dict = [];
- for(let index in result){
- dict.push(JSON.stringify(result[index].dataValues));
- }
- console.log(dict);
- }).catch(erro => {
- console.log(erro);
- };
- //
- // let oi = new UserController();
- // oi.delUser(2,(err, res) =>{
- // if(err !== null){
- // console.log(err);
- // }else{
- // console.log(res);
- // }
- // });
- //
- // for(let i=0; i<10; i++) {
- // let teste = {
- //
- // nome: 'david' + i,
- // sobrenome: 'mota',
- // password: 'senha' + i,
- // role: 'user',
- // email: 'oi@' + i + '.com',
- // latitude: '0',
- // longitude: '0',
- // novidades: 1,
- // alertas: 1
- // };
- //
- //
- // UserController.addUser(teste, (res) => {
- // console.log(res);// coloca um breakpoint na linha e checa o valor de res ;)
- // });
- // }
- // let teste = {
- // nome: 'david',
- // sobrenome: 'mota',
- // password: 'dave14',
- // role: 'admin',
- // email: 'david@mapinguari.com.br',
- // latitude: '0',
- // longitude: '0',
- // novidades: 1,
- // alertas: 1
- // };
- // UserController.addUser(teste,(res) => {
- // console.log(res);// coloca um breakpoint na linha e checa o valor de res ;)
- // });
- // UserController.findAndCountAll().then((res) => {
- // console.log(JSON.stringify(res));
- // }).catch((err) => {
- // console.error(err);
- // });
- // let pagina = parseInt(req.body.current);
- // let quantidadeRegistros = parseInt(req.body.rowCount);
- // let busca = req.body.searchPhrase;
- // UserController.findAndCountAll(pagina, quantidadeRegistros, busca).then((data) => {
- // res.send(JSON.stringify({
- // current: pagina,
- // rowCount: quantidadeRegistros, rows: data['usuarios'], total: data['total']
- // }));
Add Comment
Please, Sign In to add comment