-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapiOrari.js
80 lines (63 loc) · 2.66 KB
/
apiOrari.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const router = require('express').Router();
const { route } = require('.');
var path = require('path');
var multer = require('multer');
const fs = require('fs'); // gestione filesystem
const jwtool = require("../libraries/JWTools");
const Resizer = require("../libraries/Resizer");
const orariTool = require("../libraries/orariTool");
// importo i miei modelli
const Menu = require('../model/Menu');
const Orari = require('../model/Orari');
const { NULL } = require('mysql/lib/protocol/constants/types');
const User = require('../model/User');
const { truncate } = require('fs');
router.post('/update', jwtool.autorization, async (req, res) => {
const orariObjInput = req.body;
// cerco l'orario dell'utente
var myOrario = await Orari.findOne({user_id: req.user_id});
if(!myOrario) {
const userTemp = await User.findOne({_id: req.user_id});
const newOrario = new Orari({
user_id: userTemp._id,
enable: false,
menu_id: userTemp.menu_id,
});
myOrario = await newOrario.save();
}
if( orariTool.checkOrari(orariObjInput) == true ) {
myOrario.orariObjString = JSON.stringify(orariObjInput);
myOrario.save();
} else {
return res.status(404).json({error: true, message: "Invalid Data!"});
}
return res.status(200).json({error:false, message: "OK"});
});
router.get('/enable', jwtool.autorization, async (req, res) => {
try {
var myOrario = await Orari.findOne({user_id: req.user_id});
if(!myOrario) { return res.status(404).json({error: true, message: "Resouce not found!"}); }
myOrario.enable = !myOrario.enable;
myOrario.save();
return res.status(200).json({error: false, message: "OK!", enable: myOrario.enable, object: myOrario});
} catch(e) {
return res.status(404).json( {error: true, message: "Error n.1!"} );
}
});
router.get('/info/:idOrari', async (req, res) => {
const idOrari = req.params.idOrari;
if(!idOrari) { return res.status(404).json({error: true, message: "Field required!"}); }
try {
var myOrario = await Orari.findOne({_id: idOrari});
if(!myOrario) { return res.status(404).json({error: true, message: "Resouce not found!"}); }
// info pubbliche
var orari = {
enable: myOrario.enable,
orariObjString: myOrario.orariObjString,
}
return res.status(200).json({error: false, message: "OK!", object: orari });
} catch(e) {
return res.status(404).json( {error: true, message: "Error n.1!"} );
}
});
module.exports = router;