Guest User

Untitled

a guest
Aug 21st, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import {db} from "./database";
  2.  
  3. let Sequelize = require('sequelize');
  4.  
  5. import {PasswordController} from "./PasswordController";
  6. // import {User} from "../interfaces/UserInterface";
  7.  
  8. export class UserController {
  9.  
  10.     static findAll() {
  11.         return new Promise((res, rej) => {
  12.             db.users.findAll({
  13.                 attributes: ['id', 'nome', 'email']
  14.             }).then(users => {
  15.                 res(users);
  16.             }).catch(err => {
  17.                 rej(err);
  18.             });
  19.         });
  20.  
  21.     }
  22.  
  23.     static findAndCountAll(pagina, quantidade, busca) {
  24.         return new Promise((res, rej) => {
  25.             let registros = quantidade;
  26.             let inicio = 0;
  27.             if (pagina > 1) {
  28.                 inicio = (pagina - 1) * registros;
  29.             }
  30.  
  31.             let objConf = {
  32.                 attributes: ['id', 'nome', 'sobrenome', 'email', 'role', 'latitude', 'longitude', 'novidades', 'alertas'],
  33.                 offset: inicio,
  34.                 order: [['id', 'ASC']]
  35.             };
  36.             const Op = Sequelize.Op;
  37.  
  38.             if (busca !== "") {
  39.                 let objWhere = {
  40.                     [Op.or]: [
  41.                         {
  42.                             username: {
  43.                                 [Op.like]: '%' + busca + '%'
  44.                             }
  45.                         },
  46.                         {
  47.                             email: {
  48.                                 [Op.like]: '%' + busca + '%'
  49.                             }
  50.                         }
  51.                     ]
  52.                 };
  53.                 objConf['where'] = objWhere;
  54.             }
  55.             if (quantidade > 0) {
  56.                 objConf['limit'] = quantidade;
  57.             }
  58.  
  59.             db.users.findAndCountAll(objConf).then(users => {
  60.                 let objRetorno = users.rows;
  61.                 let usuarios = [];
  62.                 for (let usuario of objRetorno) {
  63.                     console.log(usuario);
  64.                     usuarios.push(usuario.dataValues);
  65.                 }
  66.                 let total = users.count + 1;
  67.                 let retornar = {total: total, usuarios: usuarios};
  68.                 res(retornar);
  69.             }).catch(err => {
  70.                 rej(err);
  71.             });
  72.         });
  73.  
  74.     }
  75.  
  76.     static findOne(data) {
  77.         return new Promise((res, rej) => {
  78.             db.users.findOne({where: {email: data.email}}).then(user => {
  79.                 if (user == null) {
  80.                     rej('Usuario nao encontrado');
  81.                 }
  82.                 res(user);
  83.             }).catch(err => {
  84.                 rej(err);
  85.             });
  86.         })
  87.     }
  88.  
  89.     static findMail(data) {
  90.         return new Promise((res, rej) => {
  91.             db.users.findOne({where: {email: data}}).then(user => {
  92.                 if (user == null) {
  93.                     //rej('Usuario nao encontrado');
  94.                     res("false");
  95.                 }
  96.                 res("true");
  97.             }).catch(err => {
  98.                 rej(err);
  99.             });
  100.         })
  101.     }
  102.  
  103.     static addUser(data, cb) {
  104.         PasswordController.criaHash(data.password, (hash, salt) => {
  105.             db.users.create({
  106.                 nome: data.nome,
  107.                 sobrenome: data.sobrenome,
  108.                 password: hash,
  109.                 role: data.role,
  110.                 salt: salt,
  111.                 email: data.email,
  112.                 latitude: data.latitude,
  113.                 longitude: data.longitude,
  114.                 novidades: data.novidades,
  115.                 alertas: data.alertas
  116.             }).then(() => {
  117.                 cb(true)
  118.             }).catch(() => {
  119.                 cb("nao inserido");
  120.             });
  121.         });
  122.     }
  123.  
  124.     static delUser(id, cb) {
  125.         db.users.destroy({
  126.             where: {
  127.                 id: id
  128.             }
  129.         }).then(msg => {
  130.             cb(null, msg); //1 deletado tipo, o callback devolve 2 parametros, o primeiro eh o erro e o segundo o resultado
  131.         }).catch(err => {
  132.             cb(err, null); //0 error
  133.         });
  134.     }
  135.  
  136.  
  137. }
  138. // exports.UserController = UserController;
  139. UserController.findAll().then((result) => {
  140.     let dict = [];
  141.     for(let index in result){
  142.         dict.push(JSON.stringify(result[index].dataValues));
  143.  
  144.     }
  145.     console.log(dict);
  146.  
  147. }).catch(erro => {
  148.     console.log(erro);
  149. };
  150.  
  151. //
  152. // let oi = new UserController();
  153.  
  154. // oi.delUser(2,(err, res) =>{
  155. //     if(err !== null){
  156. //         console.log(err);
  157. //     }else{
  158. //         console.log(res);
  159. //     }
  160. // });
  161. //
  162.  
  163. // for(let i=0; i<10; i++) {
  164. //     let teste = {
  165. //
  166. //         nome: 'david' + i,
  167. //         sobrenome: 'mota',
  168. //         password: 'senha' + i,
  169. //         role: 'user',
  170. //         email: 'oi@' + i + '.com',
  171. //             latitude: '0',
  172. //     longitude: '0',
  173. //     novidades: 1,
  174. //     alertas: 1
  175. //     };
  176. //
  177. //
  178. //     UserController.addUser(teste, (res) => {
  179. //         console.log(res);// coloca um breakpoint na linha e checa o valor de res ;)
  180. //     });
  181. // }
  182.  
  183. // let teste = {
  184. //     nome: 'david',
  185. //     sobrenome: 'mota',
  186. //     password: 'dave14',
  187. //     role: 'admin',
  188. //     email: 'david@mapinguari.com.br',
  189. //     latitude: '0',
  190. //     longitude: '0',
  191. //     novidades: 1,
  192. //     alertas: 1
  193. // };
  194. // UserController.addUser(teste,(res) => {
  195. //     console.log(res);// coloca um breakpoint na linha e checa o valor de res ;)
  196. // });
  197.  
  198.  
  199. // UserController.findAndCountAll().then((res) => {
  200. //     console.log(JSON.stringify(res));
  201. // }).catch((err) => {
  202. //     console.error(err);
  203. // });
  204. //     let pagina = parseInt(req.body.current);
  205. //     let quantidadeRegistros = parseInt(req.body.rowCount);
  206. //     let busca = req.body.searchPhrase;
  207. //     UserController.findAndCountAll(pagina, quantidadeRegistros, busca).then((data) => {
  208. //         res.send(JSON.stringify({
  209. //             current: pagina,
  210. //             rowCount: quantidadeRegistros, rows: data['usuarios'], total: data['total']
  211. //         }));
Add Comment
Please, Sign In to add comment