-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.js
54 lines (46 loc) · 1.66 KB
/
router.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
const router = require('express').Router();
const { newUser, userLogin } = require('./controllers/userController');
const { addNewCategory, getAllCategories } = require('./controllers/categoryController');
const { addNewMenuItem, editMenuItem, getAllMenuItems } = require('./controllers/menuItemController');
const { userValidate, validateLogin } = require('./validation/validationControl');
const { cloudinary } = require('./utils/cloudinary.utils');
//USERS
// post new user - subscribe
router.post('/user/subscribe', userValidate, newUser);
router.post('/user/login', validateLogin, userLogin);
// MENU ITEMS
// CATEGORIES
router.post('/category', addNewCategory);
router.get('/categories/:id', getAllCategories);
// ITEMS AKA PRODUCTS
router.post('/item', addNewMenuItem);
router.put('/item/:id', editMenuItem);
router.get('/items/:id', getAllMenuItems);
//IMAGES
// TODO Add controllers for these endpoints
//image upload
router.post('/image/upload', async (req, res) => {
try {
const image = req.body.data;
const itemId = req.body.itemId;
const uploadedResponse = await cloudinary.uploader.upload(image, {upload_preset: 'dimome_test'});
res.send(uploadedResponse).status(201);
} catch (error) {
console.error(error);
res.status(500).json({message: 'image didnt send!'});
}
})
//image download
router.get('/image', async (req, res) => {
try {
const { resources } = await cloudinary.search.expression(
'folder:test-images')
.execute();
const publicIds = resources.map( file => file.public_id);
res.send(publicIds).status(201);
} catch (error) {
console.error(error);
res.status(500);
}
});
module.exports = router;