-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspaces.js
73 lines (67 loc) · 1.88 KB
/
spaces.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
const { Router } = require("express");
const CountrySpace = require("../models").countrySpace;
const Photos = require("../models").photos;
const RestaurantSpace = require("../models").restSpace;
const Reviews = require("../models").reviews;
const Categories = require("../models").category;
const User = require("../models").user;
const router = new Router();
router.get("/countries", async (req, res, next) => {
try {
const allCountries = await CountrySpace.findAll({
include: [{ model: Photos, include: { model: User, as: "user" } }],
});
console.log(allCountries);
res.send(allCountries);
} catch (e) {
console.log(e);
next();
}
});
router.get("/photos", async (req, res, next) => {
const limit = Math.min(req.query.limit || 12, 500);
const offset = req.query.offset || 0;
try {
const allPhotos = await Photos.findAndCountAll({
limit,
offset,
include: [
{ model: CountrySpace },
{ model: RestaurantSpace },
{ model: User, as: "user" },
],
order: [["createdAt", "DESC"]],
});
res.send({ images: allPhotos.rows, total: allPhotos.count });
} catch (e) {
console.log(e);
next();
}
});
router.get("/restaurants", async (req, res, next) => {
try {
const allRestaurants = await RestaurantSpace.findAll();
console.log(allRestaurants);
res.send(allRestaurants);
} catch (e) {
console.log(e);
}
});
router.get("/restaurants/:id", async (req, res, next) => {
try {
const { id } = req.params;
const allRestaurants = await RestaurantSpace.findByPk(id, {
include: [
Photos,
{ model: Reviews, include: User },
{ model: Categories, attributes: ["cuisine"] },
],
order: [[Reviews, "createdAt", "DESC"]],
});
console.log(allRestaurants);
res.send(allRestaurants);
} catch (e) {
console.log(e);
}
});
module.exports = router;