Guest User

Untitled

a guest
Jan 2nd, 2019
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. const express = require('express');
  2. const bodyParser = require('body-parser');
  3. const sql = require('mssql');
  4. const app = express();
  5.  
  6. app.use(bodyParser.json());
  7.  
  8. app.use(function (req, res, next) {
  9. res.header("Access-Control-Allow-Origin", "*");
  10. res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
  11. res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, contentType,Content-Type, Accept, Authorization");
  12. next();
  13. });
  14.  
  15. const dbConfig = {
  16. user: "usr",
  17. password: "psswd",
  18. server: "server",
  19. database: "daDB"
  20. };
  21.  
  22. const executeQuery = function (res, query, parameters) {
  23. sql.connect(dbConfig, function (err) {
  24. if (err) {
  25. console.log(`You have an error: ${err}`);
  26. res.send(err);
  27. sql.close();
  28. }
  29. else {
  30. var request = new sql.Request();
  31.  
  32. if (parameters && parameters.length > 0) {
  33. parameters.forEach(function (p) {
  34. request.input(p.name, p.sqltype, p.value);
  35. });
  36. }
  37.  
  38.  
  39. request.query(query, function (err, result) {
  40. if (err) {
  41. console.log(`You have an error: ${err}`);
  42. res.send(err);
  43. sql.close();
  44. }
  45. else {
  46. res.send(result.recordset);
  47. sql.close();
  48. }
  49. });
  50. }
  51. });
  52. }
  53.  
  54. app.get("/api/InvoiceRequestApi", function (req, res) {
  55.  
  56. var query = "SELECT * FROM [InvoiceRequest];
  57. executeQuery(res, query);
  58. });
  59.  
  60. app.post("/api/InvoiceRequestApi", function (req, res) {
  61. const parameters = [
  62. { name: 'OrderId', sqltype: sql.VarChar, value: req.body.OrderId },
  63. { name: 'Mail', sqltype: sql.VarChar, value: req.body.Mail },
  64. { name: 'Name', sqltype: sql.VarChar, value: req.body.Name },
  65. { name: 'RFC', sqltype: sql.VarChar, value: req.body.RFC },
  66. { name: 'Adress', sqltype: sql.VarChar, value: req.body.Adress },
  67. { name: 'NoExt', sqltype: sql.VarChar, value: req.body.NoExt },
  68. { name: 'NoInt', sqltype: sql.VarChar, value: req.body.NoInt },
  69. { name: 'District', sqltype: sql.VarChar, value: req.body.District },
  70. { name: 'CP', sqltype: sql.VarChar, value: req.body.CP },
  71. { name: 'Municipality', sqltype: sql.VarChar, value: req.body.Municipality },
  72. { name: 'State', sqltype: sql.VarChar, value: req.body.State },
  73. { name: 'CreationDate', sqltype: sql.VarChar, value: req.body.CreationDate },
  74. { name: 'GeneratedInvoice', sqltype: sql.Bit, value: req.body.GeneratedInvoice },
  75. { name: 'InvoiceFile', sqltype: sql.VarChar, value: req.body.InvoiceFile },
  76. { name: 'FileXml', sqltype: sql.VarChar, value: req.body.FileXml },
  77. { name: 'StatusId', sqltype: sql.Int, value: req.body.StatusId }
  78. ];
  79.  
  80. var query = "INSERT INTO [InvoiceRequest] VALUES(@OrderId, @Mail, @Name, @RFC, @Adress, @NoExt, @NoInt, @District, @CP, @Municipality, @State, @CreationDate, @GeneratedInvoice, @InvoiceFile, @FileXml, @StatusId) SELECT SCOPE_IDENTITY() AS Id";
  81. executeQuery(res, query, parameters);
  82. });
  83.  
  84. const PORT = process.env.PORT || 8080
  85.  
  86. app.listen(PORT, () => {
  87. console.log(`App running on port: ${PORT}`)
  88. });
  89.  
  90. function sendMyData(){
  91. var invoiceData = {
  92. OrderId: $scope.theOrder,
  93. Mail: $scope.getMail,
  94. Name: $scope.getName,
  95. RFC: $scope.getTax,
  96. Adress: $scope.getStreet,
  97. NoExt: $scope.getExternal,
  98. NoInt: $scope.getInternal,
  99. District: $scope.getDistrict,
  100. CP: $scope.getPostalCode,
  101. Municipality: $scope.getMunicipality,
  102. State: $scope.getState,
  103. CreationDate: '2019-01-02',
  104. GeneratedInvoice: null,
  105. InvoiceFile: null,
  106. FileXml: null,
  107. StatusId: 1
  108. };
  109. //console.log(invoiceData);
  110. saveRequest.save(invoiceData).$promise
  111. .then(function(response){
  112. console.log(response);
  113. });
  114. }
  115.  
  116. function sendMyData(){
  117. var invoiceData = {
  118. OrderId: $scope.theOrder,
  119. Mail: $scope.getMail,
  120. Name: $scope.getName,
  121. RFC: $scope.getTax,
  122. Adress: $scope.getStreet,
  123. NoExt: $scope.getExternal,
  124. NoInt: $scope.getInternal,
  125. District: $scope.getDistrict,
  126. CP: $scope.getPostalCode,
  127. Municipality: $scope.getMunicipality,
  128. State: $scope.getState,
  129. CreationDate: '2019-01-02',
  130. GeneratedInvoice: null,
  131. InvoiceFile: null,
  132. FileXml: null,
  133. StatusId: 1
  134. };
  135. //console.log(invoiceData);
  136. saveRequest.save([invoiceData]).$promise
  137. .then(function(response){
  138. console.log(response);
  139. });
  140. }
Add Comment
Please, Sign In to add comment