-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
74 lines (65 loc) · 1.76 KB
/
utils.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
const mongoose = require('mongoose');
module.exports.connectDB = () => {
const db = mongoose.connect(process.env.MONGODB_URI, { useNewUrlParser: true });
const RestaurantSchema = new mongoose.Schema({
name: { type: String, required: true },
placeId: String,
rating: { type: Number, default: 0, min: 0, max: 5 },
priceLevel: String,
phoneNumber: String,
location: {
type: {
type: String,
enum: ['Point'],
required: true
},
coordinates: {
type: [Number],
required: true
},
address: String,
},
occasions: [{ type: 'ObjectId', ref: 'Tag' }],
tags: [{ type: 'ObjectId', ref: 'Tag' }],
openingHours: [String],
reviewCount: Number,
photoUrls: [String],
});
const TagSchema = new mongoose.Schema({
text: { type: String, required: true },
});
const restaurant = mongoose.model('Restaurant', RestaurantSchema);
const tag = mongoose.model('Tag', TagSchema);
return { restaurant, tag };
};
module.exports.paginateResults = ({
after: cursor,
pageSize = 20,
results,
}) => {
if (pageSize < 1) return [];
if (!cursor) return results.slice(0, pageSize);
const cursorIndex = results.findIndex(
item => item.id === cursor
);
return cursorIndex >= 0
? cursorIndex === results.length - 1
? []
: results.slice(
cursorIndex + 1,
Math.min(results.length, cursorIndex + 1 + pageSize),
)
: results.slice(0, pageSize);
};
module.exports.formatPrice = (price) => {
if (price === 1)
return ["0-100"];
else if (price === 2)
return ["100-200"];
else if (price === 3)
return ["200-300", "100-300"];
else if (price === 4)
return ["300↑", "300-400", "200-400", "100-400"];
else
return "";
}