Advertisement
Guest User

Untitled

a guest
Sep 29th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.18 KB | None | 0 0
  1. var mysql = require('mysql');
  2. var util = require('util');
  3. var Product = require('./product');
  4. var television = Product.television;
  5. var monitor = Product.monitor;
  6. var desktop = Product.desktop;
  7. var tablet = Product.tablet;
  8. var laptop = Product.laptop;
  9.  
  10. var db = mysql.createConnection({
  11. host: "soen343-mysql.cutkgxnrwyh2.us-east-1.rds.amazonaws.com",
  12. user: "soen343team",
  13. password: "spiderman",
  14. database: "electronics"
  15. });
  16.  
  17. db.connect(function(err) {
  18. if (err) throw err;;
  19. });
  20.  
  21.  
  22. function Inventory(){
  23.  
  24. this.products = [];
  25.  
  26. this.viewProducts=function(products, req, res){
  27. var items = [];
  28.  
  29. return new Promise(function(resolve, reject) {
  30. products.forEach(function(product, i){
  31. var sql = "SELECT * from " + product.type + " WHERE model =" + product.model;
  32. db.query(sql, function(error, results, fields){
  33. if(error) return error.code;
  34. switch (product.type) {
  35. case 'television':
  36. var televisionItem = new television(results[0].id,results[0].model,results.weight,results[0].price,results[0].brand,results[0].dimensions);
  37. items.push(televisionItem);
  38. break;
  39. case 'monitor':
  40. var monitorItem = new monitor(results[0].id,results[0].model,results[0].weight,results[0].price,results[0].brand,results[0].size);
  41. items.push(monitorItem);
  42. break;
  43.  
  44. case 'desktop':
  45. var desktopItem = new desktop(results[0].id,results[0].model,results[0].weight,results[0].price,results[0].brand,results[0].processorType,results[0].cpuCores,results[0].ram,results[0].hardDriveSize,results[0].dimensions);
  46. items.push(desktopItem);
  47. break;
  48.  
  49. case 'tablet':
  50. var tabletItem = new tablet(results[0].id,results[0].model,results[0].weight,results[0].price,results[0].brand,results[0].processorType,results[0].cpuCores,results[0].ram,results[0].hardDriveSize,results[0].dimensions,results[0].batteryInfo,results[0].operatingSystem,results[0].camera);
  51. items.push(tabletItem);
  52. break;
  53.  
  54. case 'laptop':
  55. var laptopItem = new laptop(results[0].id,results[0].model,results[0].weight,results[0].price,results[0].brand,results[0].processorType,results[0].cpuCores,results[0].ram,results[0].hardDriveSize,results[0].size,results[0].batteryInfo,results[0].operatingSystem,results[0].camera,results[0].touchScreen)
  56. items.push(laptopItem);
  57. break;
  58.  
  59. default:
  60. console.log('error');
  61. }
  62. console.log(i)
  63. if(i == (products.length - 1)){
  64. resolve(items);
  65. }
  66. })
  67. })
  68. })
  69. }
  70. this.addProduct=function(req,res){
  71. if(req.body.type == "desktop"){
  72. var d1 = new desktop(req.body.id , req.body.model , req.body.weight, req.body.price, req.body.brand, req.body.processorType, req.body.cpuCores, req.body.ram, req.body.hardDriveSize, req.body.dimensions);
  73. var dataArray = ["desktop", d1.model, d1.weight, d1.price, d1.brand, d1.processorType, d1.cpuCores, d1.ram, d1.hardDriveSize, d1.dimensions];
  74. addToDb(dataArray);
  75. }
  76. else if(req.body.type == "television"){
  77. var d1 = new television(req.body.id , req.body.model , req.body.weight, req.body.price, req.body.brand, req.body.dimensions);
  78. var dataArray = ["television", d1.model, d1.weight, d1.price, d1.brand, d1.dimensions];
  79. addToDb(dataArray);
  80. }
  81. else if(req.body.type == "monitor"){
  82. var d1 = new monitor(req.body.id , req.body.model , req.body.weight, req.body.price, req.body.brand, req.body.size);
  83. var dataArray = ["monitor", d1.model, d1.weight, d1.price, d1.brand, d1.size];
  84. addToDb(dataArray);
  85. }
  86. else if(req.body.type == "tablet"){
  87. var d1 = new tablet(req.body.id , req.body.model , req.body.weight, req.body.price, req.body.brand, req.body.processorType, req.body.cpuCores, req.body.ram, req.body.hardDriveSize, req.body.dimensions, req.body.batteryInfo, req.body.operatingSystem, req.body.cameraInfo);
  88. var dataArray = ["tablet", d1.model, d1.weight, d1.price, d1.brand, d1.processorType, d1.cpuCores, d1.ram, d1.hardDriveSize, d1.dimensions, d1.batteryInfo, d1.operatingSystem, d1.cameraInfo];
  89. addToDb(dataArray);
  90. }
  91. else if(req.body.type == "laptop"){
  92. var d1 = new laptop(req.body.id , req.body.model , req.body.weight, req.body.price, req.body.brand, req.body.processorType, req.body.cpuCores, req.body.ram, req.body.hardDriveSize, req.body.size, req.body.batteryInfo, req.body.operatingSystem, req.body.camera, req.body.touchScreen);
  93. var dataArray = ["laptop", d1.model, d1.weight, d1.price, d1.brand, d1.processorType, d1.cpuCores, d1.ram, d1.hardDriveSize, d1.size, d1.batteryInfo, d1.operatingSystem, d1.camera, d1.touchScreen];
  94. addToDb(dataArray);
  95. }
  96.  
  97. function addToDb(a){
  98.  
  99. console.log("Connected!");
  100. //this inserts the model number as well as the type into the inventory table
  101. var sql2 = "INSERT INTO inventory (model,type) VALUES ('"+a[1]+"','"+a[0]+"')";
  102. //this inserts the rest of the product info into the correct table corresponding to the type of product
  103. switch(a[0]){
  104. case 'television':
  105. var sql = "INSERT INTO "+a[0]+" (model, weight, price, brand, dimensions) VALUES ('"+a[1]+"','"+a[2]+"','"+a[3]+"','"+a[4]+"','"+a[5]+"')";
  106. break;
  107. case 'monitor':
  108. var sql = "INSERT INTO "+a[0]+" (model, weight, price, brand, size) VALUES ('"+a[1]+"','"+a[2]+"','"+a[3]+"','"+a[4]+"','"+a[5]+"')";
  109. break;
  110. case 'desktop':
  111. var sql = "INSERT INTO "+a[0]+" (model, weight, price, brand, processorType, cpuCores, ram, hardDriveSize,dimensions) VALUES ('"+a[1]+"','"+a[2]+"','"+a[3]+"','"+a[4]+"','"+a[5]+"','"+a[6]+"','"+a[7]+"','"+a[8]+"','"+a[9]+"')";
  112. break;
  113. case 'tablet':
  114. var sql = "INSERT INTO "+a[0]+" (model, weight, price, brand, processorType, cpuCores, ram, hardDriveSize,dimensions,batteryInfo,operatingSystem,cameraInfo) VALUES ('"+a[1]+"','"
  115. +a[2]+"','"+a[3]+"','"+a[4]+"','"+a[5]+"','"+a[6]+"','"+a[7]+"','"+a[8]+"','"+a[9]+"','"+a[10]+"','"+a[11]+"','"+a[12]+"')";
  116. break;
  117. case 'laptop':
  118. var sql = "INSERT INTO "+a[0]+" (model, weight, price, brand, processorType, cpuCores, ram, hardDriveSize,size,batteryInfo,operatingSystem,camera,touchScreen) VALUES ('"+a[1]+"','"
  119. +a[2]+"','"+a[3]+"','"+a[4]+"','"+a[5]+"','"+a[6]+"','"+a[7]+"','"+a[8]+"','"+a[9]+"','"+a[10]+"','"+a[11]+"','"+a[12]+"','"+a[13]+"')";
  120. break;
  121. default:
  122. return 'error';
  123.  
  124.  
  125. }
  126. db.query(sql, function (err, result) {
  127. if (err);
  128. console.log("1 record inserted");
  129. });
  130.  
  131. db.query(sql2, function (err, result) {
  132. if (err);
  133. console.log("1 record inserted");
  134. });
  135.  
  136. }
  137. }
  138.  
  139.  
  140. }
  141.  
  142. module.exports = Inventory;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement