Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. const express = require('express');
  2. const router = express.Router();
  3. const mongoose = require('mongoose');
  4. const multer = require('multer');
  5. const fs = require('fs');
  6. const cors = require('cors');
  7. const csv = require('fast-csv');
  8.  
  9. const {Card,validate} = require('../models/card');
  10.  
  11. ////////////////////////////////////////
  12. ////////////////////////////////////////
  13. ///////// upload scanned cards//////////
  14. ////////////////////////////////////////
  15. ////////////////////////////////////////
  16. const storage = multer.diskStorage({
  17. destination: function(req, file,cb){
  18. cb(null, './csv/')
  19. } ,
  20. filename: function(req,file,cb){
  21. cb(null, file.originalname);
  22. }
  23. });
  24. ////////////////////////////////////////
  25. ////////////////////////////////////////
  26. /////////////////cors///////////////////
  27. ////////////////////////////////////////
  28. ////////////////////////////////////////
  29.  
  30.  
  31.  
  32.  
  33. ////////////////////////////////////////
  34. ////////////////////////////////////////
  35. ///////// filter uploaded cards/////////
  36. ////////////////////////////////////////
  37. ////////////////////////////////////////
  38.  
  39.  
  40. const fileFilter = (req,file,cb) => {
  41. console.log("oofie");
  42. if(file.mimetype === 'text/csv' || file.mimetype === 'application/vnd.ms-excel' ){
  43. console.log("oof");
  44. cb(null,true);
  45. }else{
  46. cb(null,false);
  47. }
  48.  
  49. }
  50.  
  51. const upload = multer({
  52. storage: storage,
  53. fileFilter: fileFilter, limits: { /* stuff */ }
  54. });
  55.  
  56.  
  57.  
  58.  
  59.  
  60. ////////////////////////////////////////
  61. ////////////////////////////////////////
  62. ////////////// Routes //////////////////
  63. ////////////////////////////////////////
  64. ////////////////////////////////////////
  65.  
  66.  
  67.  
  68.  
  69.  
  70. //////////////////////////////////////////
  71. //////////////////////////////////////////
  72.  
  73. router.get('/',upload.single('cardcsv'), async (req, res) => {
  74. if(typeof req.file === 'undefined'){return res.status(400).send("no file")}
  75. //console.log(req.file)
  76. var stream = fs.createReadStream(`./csv/${req.file.originalname}`);
  77. var gatherIds = [];
  78. csv
  79. .fromStream(stream)
  80. .on("data", function(data){
  81. //console.log(data[6])
  82. if(parseInt(data[6])){
  83. //console.log(data);
  84. gatherIds.push({
  85. name: data[1],
  86. condi: data[2],
  87. set:data[4],
  88. quant:data[6]})
  89. }
  90. })
  91. .on("end", async function(){
  92. console.log("done");
  93. //console.log(gatherIds[0].quant);
  94. console.log(gatherIds);
  95. let boi = await FindMatches(gatherIds);
  96. //console.log(boi);
  97. res.send(JSON.stringify(boi));
  98. });
  99.  
  100. });
  101.  
  102. //////////////////////////////////////////
  103. //////////////////////////////////////////
  104.  
  105. router.post('/', async (req, res) => {
  106. const { error } = validate(req.body);
  107. // console.log(req.body)
  108. if (error) return res.status(400).send(error.details[0].message);
  109.  
  110. let card = new Card({
  111.  
  112. name: req.body.name,
  113. condi: req.body.condi,
  114. set: req.body.set,
  115. url: req.body.url
  116. });
  117. card = await card.save();
  118.  
  119. res.send(card);
  120. });
  121.  
  122. //////////////////////////////////////////
  123. //////////////////////////////////////////
  124.  
  125. router.delete('/:id', async (req, res) => {
  126. const image = await Image.findByIdAndRemove(req.params.id);
  127.  
  128. if (!image) return res.status(404).send('The image with the given ID was not found.');
  129.  
  130. res.send(image);
  131. });
  132.  
  133.  
  134. module.exports = router;
  135.  
  136. async function FindMatches(gatherIds){
  137. var matches = 0;
  138. var mCards = [];
  139. for(var i = 0 ; i < gatherIds.length; i++){
  140. if(i === gatherIds.length){return mCards};
  141.  
  142.  
  143. for(var d = 0 ; d < parseInt(gatherIds[i].quant); d++){
  144. console.log("D: "+ d + " i: " + i)
  145.  
  146.  
  147. var card = await Card.findOne({name: gatherIds[i].name,set: gatherIds[i].set,condi:gatherIds[i].condi}, function(err, cardd)
  148. {
  149.  
  150. if (err)
  151. {
  152.  
  153. return err;
  154. }
  155. if(cardd === null){
  156. mCards.push(err + " " +"No card in system for:" + gatherIds[i].name + " " + gatherIds[i].set + " " + gatherIds[i].condi)
  157. }
  158.  
  159.  
  160.  
  161.  
  162. })
  163. console.log(gatherIds[i]);
  164. if(card !== null){
  165. mCards.push(card.url);
  166. var del = await Card.findByIdAndRemove(card._id);
  167. };
  168.  
  169.  
  170.  
  171. }
  172. }
  173. console.log(mCards)
  174. return mCards;
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement