Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const express = require('express');
- const router = express.Router();
- const mongoose = require('mongoose');
- const multer = require('multer');
- const fs = require('fs');
- const cors = require('cors');
- const csv = require('fast-csv');
- const {Card,validate} = require('../models/card');
- ////////////////////////////////////////
- ////////////////////////////////////////
- ///////// upload scanned cards//////////
- ////////////////////////////////////////
- ////////////////////////////////////////
- const storage = multer.diskStorage({
- destination: function(req, file,cb){
- cb(null, './csv/')
- } ,
- filename: function(req,file,cb){
- cb(null, file.originalname);
- }
- });
- ////////////////////////////////////////
- ////////////////////////////////////////
- /////////////////cors///////////////////
- ////////////////////////////////////////
- ////////////////////////////////////////
- ////////////////////////////////////////
- ////////////////////////////////////////
- ///////// filter uploaded cards/////////
- ////////////////////////////////////////
- ////////////////////////////////////////
- const fileFilter = (req,file,cb) => {
- console.log("oofie");
- if(file.mimetype === 'text/csv' || file.mimetype === 'application/vnd.ms-excel' ){
- console.log("oof");
- cb(null,true);
- }else{
- cb(null,false);
- }
- }
- const upload = multer({
- storage: storage,
- fileFilter: fileFilter, limits: { /* stuff */ }
- });
- ////////////////////////////////////////
- ////////////////////////////////////////
- ////////////// Routes //////////////////
- ////////////////////////////////////////
- ////////////////////////////////////////
- //////////////////////////////////////////
- //////////////////////////////////////////
- router.get('/',upload.single('cardcsv'), async (req, res) => {
- if(typeof req.file === 'undefined'){return res.status(400).send("no file")}
- //console.log(req.file)
- var stream = fs.createReadStream(`./csv/${req.file.originalname}`);
- var gatherIds = [];
- csv
- .fromStream(stream)
- .on("data", function(data){
- //console.log(data[6])
- if(parseInt(data[6])){
- //console.log(data);
- gatherIds.push({
- name: data[1],
- condi: data[2],
- set:data[4],
- quant:data[6]})
- }
- })
- .on("end", async function(){
- console.log("done");
- //console.log(gatherIds[0].quant);
- console.log(gatherIds);
- let boi = await FindMatches(gatherIds);
- //console.log(boi);
- res.send(JSON.stringify(boi));
- });
- });
- //////////////////////////////////////////
- //////////////////////////////////////////
- router.post('/', async (req, res) => {
- const { error } = validate(req.body);
- // console.log(req.body)
- if (error) return res.status(400).send(error.details[0].message);
- let card = new Card({
- name: req.body.name,
- condi: req.body.condi,
- set: req.body.set,
- url: req.body.url
- });
- card = await card.save();
- res.send(card);
- });
- //////////////////////////////////////////
- //////////////////////////////////////////
- router.delete('/:id', async (req, res) => {
- const image = await Image.findByIdAndRemove(req.params.id);
- if (!image) return res.status(404).send('The image with the given ID was not found.');
- res.send(image);
- });
- module.exports = router;
- async function FindMatches(gatherIds){
- var matches = 0;
- var mCards = [];
- for(var i = 0 ; i < gatherIds.length; i++){
- if(i === gatherIds.length){return mCards};
- for(var d = 0 ; d < parseInt(gatherIds[i].quant); d++){
- console.log("D: "+ d + " i: " + i)
- var card = await Card.findOne({name: gatherIds[i].name,set: gatherIds[i].set,condi:gatherIds[i].condi}, function(err, cardd)
- {
- if (err)
- {
- return err;
- }
- if(cardd === null){
- mCards.push(err + " " +"No card in system for:" + gatherIds[i].name + " " + gatherIds[i].set + " " + gatherIds[i].condi)
- }
- })
- console.log(gatherIds[i]);
- if(card !== null){
- mCards.push(card.url);
- var del = await Card.findByIdAndRemove(card._id);
- };
- }
- }
- console.log(mCards)
- return mCards;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement