Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. import cryptoJS = require ('crypto-js');
  2. import {MysqlError} from "mysql";
  3.  
  4. export default function(app, database) {
  5. /* @TODO: build this route, analog to app.post('/user in server.ts */
  6. app.post('/register', function(req, res){
  7. const firstName: string = req.body.firstName;
  8. const lastName: string = req.body.lastName;
  9. const email: string = req.body.email;
  10. const password: string = cryptoJS.SHA512(req.body.password).toString();
  11. const companyRegistrationNo: string = req.body.companyRegistrationNo;
  12.  
  13. if (firstName && lastName && email && password) {
  14. // Create database query and data
  15. const data: any = [
  16. email, firstName, lastName, password, companyRegistrationNo
  17. ];
  18. const selectQuery: string = 'SELECT * FROM nutzer WHERE email = ? OR company_registration_no = ?';
  19. database.query(selectQuery, data, (err: MysqlError, result: any) => {
  20. if (err) {
  21. // Query could not been executed
  22. res.status(200).send({
  23. message: 'user does not exist: ' + err,
  24. });
  25. } else {
  26. // The user already exists
  27. res.status(500).send({
  28. userId: result.insertId,
  29. message: 'user already exists',
  30. });
  31.  
  32. }
  33. return;
  34. });
  35. const query: string = 'INSERT INTO nutzer (email, first_name, last_name, password, companyRegistrationNo) ' +
  36. 'VALUES (?, ?, ?, ?, ?);';
  37. // Execute database query
  38. database.query(query, data, (err: MysqlError, result: any) => {
  39. if (err) {
  40. // Query could not been executed
  41. res.status(500).send({
  42. message: 'Database request failed: ' + err,
  43. });
  44. } else {
  45. // The user was created
  46. res.status(200).send({
  47. userId: result.insertId,
  48. message: 'Successfully created new user',
  49. });
  50. }
  51. });
  52. } else {
  53. res.status(400).send({
  54. message: 'Not all mandatory fields are filled in',
  55. });
  56. }
  57.  
  58.  
  59. }
  60. );
  61.  
  62. //other routes..
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement