-
Notifications
You must be signed in to change notification settings - Fork 0
/
abc
260 lines (222 loc) · 7.68 KB
/
abc
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// const Tour = require('./../models/tourModel');
// exports.aliasTopTour = (req, res, next) => {
// req.query.limit = '5';
// req.query.sort = '-ratigsAverage,price';
// req.query.fields = 'name,price,ratingsAverage,summary,difficulty';
// next();
// };
// class APIFeatures {
// constructor(query, queryString) {
// this.query = query;
// this.queryString = queryString;
// }
// filter() {
// const qureyObj = { ...this.queryString };
// //this line of code tell us which we have to delete from the site..
// const excludeFields = ['page', 'sort', 'limit', 'fields'];
// //loopiing forEach element in queryObj to delte it from the site....
// excludeFields.forEach((el) => delete qureyObj[el]);
// let queryStr = JSON.stringify(qureyObj);
// queryStr = queryStr.replace(/\b(gte|gt|lte|lt)\b/g, (match) => `$${match}`);
// console.log(JSON.parse(queryStr));
// //For greating or equal to...
// //{duration: 5, difficulty: { $gte: 5}}
// //{ duration: { gte: '6' }, difficulty: '5' }
// // const query = Tour.find(qureyObj);
// // let query = Tour.find(JSON.parse(queryStr));
// this.query = this.query.find(JSON.parse(queryStr));
// return this;
// }
// //req.query is replace by this.queryString..
// //query is replace by this.query..
// sort() {
// if (this.queryString.sort) {
// const sortBy = this.queryString.sort.split(',').join('');
// console.log(sortBy);
// // query = query.sort(req.query.sort);
// this.query = this.query.sort(sortBy);
// } else {
// //if not sort is added then we must sort it by the time it is created at...
// this.query = this.query.sort('-createdAt');
// }
// return this;
// }
// limitFields() {
// if (this.queryString.fields) {
// const fields = this.queryString.fields.split(',').join(' ');
// this.query = this.query.select(fields);
// } else {
// this.query = this.query.select('-__v');
// }
// return this;
// }
// paginate() {
// const page = this.queryString.page * 1 || 1; // || is used to defining the default values...
// const limit = this.queryString.limit * 1 || 100;
// console.log(page);
// //so here basically what we are doing is seeing that skip
// //contain the page - 1 * limit value in which limit is by default set to 10..
// const skip = (page - 1) * limit;
// //page=2&limit=10
// this.query = this.query.skip(skip).limit(limit);
// return this;
// }
// }
// exports.getAllTours = async (req, res) => {
// try {
// //==> BUILD A QUERY..
// //1)Filtering
// //this line of code get all the queries from the site..
// // const qureyObj = { ...req.query };
// // //this line of code tell us which we have to delete from the site..
// // const excludeFields = ['page', 'sort', 'limit', 'fields'];
// // //loopiing forEach element in queryObj to delte it from the site....
// // excludeFields.forEach((el) => delete qureyObj[el]);
// // console.log(req.query);
// //==> simple method for filtering data using tradition mongoDb command for creating queries..
// // const tours = await Tour.find({
// // duration: 5,
// // difficulty: 'easy',
// // });
// //==> EXECUTE QUERY..
// // console.log(req.query);
// // //==> ADVANCED FILTERING...
// // //sorting the queryObj according to less than and greater than..
// // let queryStr = JSON.stringify(qureyObj);
// // queryStr = queryStr.replace(/\b(gte|gt|lte|lt)\b/g, (match) => `$${match}`);
// // console.log(JSON.parse(queryStr));
// // //For greating or equal to...
// // //{duration: 5, difficulty: { $gte: 5}}
// // //{ duration: { gte: '6' }, difficulty: '5' }
// // // const query = Tour.find(qureyObj);
// // let query = Tour.find(JSON.parse(queryStr));
// //2)Sorting...
// // if (req.query.sort) {
// // const sortBy = req.query.sort.split(',').join('');
// // console.log(sortBy);
// // // query = query.sort(req.query.sort);
// // query = query.sort(sortBy);
// // } else {
// // //if not sort is added then we must sort it by the time it is created at...
// // query = query.sort('-createdAt');
// // }
// //3)Field limiting...
// // if (req.query.fields) {
// // const fields = req.query.fields.split(',').join(' ');
// // query = query.select(fields);
// // } else {
// // query = query.select('-__v');
// // }
// //4)Pagination...
// // const page = req.query.page * 1 || 1; // || is used to defining the default values...
// // const limit = req.query.limit * 1 || 100;
// // console.log(page);
// // //so here basically what we are doing is seeing that skip
// // //contain the page - 1 * limit value in which limit is by default set to 10..
// // const skip = (page - 1) * limit;
// // //page=2&limit=10
// // query = query.skip(skip).limit(limit);
// // if (req.query.page) {
// // //used to counting the number of documents in the page...
// // const numTours = await Tour.countDocuments();
// // //if the skip is more than we must throw an error..
// // if (skip >= numTours) throw new Error('This page does not exits');
// // }
// //Excecute query...
// const feautres = new APIFeatures(Tour.find(), req.query)
// .filter()
// .sort()
// .limitFields()
// .paginate();
// const tours = await feautres.query;
// //using mongoose method..
// // const tours = await Tour.find()
// // .where('duration')
// // .equals(5)
// // .where('difficulty')
// // .equals('easy');
// res.status(200).json({
// status: 'success',
// results: tours.length,
// data: {
// tours,
// },
// });
// } catch (err) {
// res.status(404).json({
// status: 'fail',
// message: err,
// });
// }
// };
// //finding single tour we can use findById
// exports.getTour = async (req, res) => {
// try {
// const tour = await Tour.findById(req.params.id);
// //Tour.findOne({ _id: req.params.id})
// res.status(200).json({
// status: 'success',
// results: tour.length,
// data: {
// tour,
// },
// });
// } catch (err) {
// res.status(404).json({
// status: 'fail',
// message: err,
// });
// }
// };
// //.create method to create new tours..
// exports.createTour = async (req, res) => {
// try {
// const newTour = await Tour.create(req.body);
// res.status(201).json({
// status: 'success',
// data: {
// tour: newTour,
// },
// });
// } catch (err) {
// res.status(400).json({
// status: 'fail',
// message: 'Inavlid data sent...',
// });
// }
// };
// //findByIdAndUpdate to get by id and update... we pass, req.paramas.id, req.body,{new: true,}...
// //runValidators are used to validate the data..
// exports.updateTour = async (req, res) => {
// try {
// const tour = await Tour.findByIdAndUpdate(req.params.id, req.body, {
// new: true,
// runValidators: true,
// });
// res.status(200).json({
// status: 'success',
// data: {
// tour,
// },
// });
// } catch (err) {
// res.status(400).json({
// status: 'fail',
// message: err,
// });
// }
// };
// exports.deleteTour = async (req, res) => {
// try {
// await Tour.findByIdAndDelete(req.params.id, req.body);
// res.status(204).json({
// status: 'success',
// data: null,
// });
// } catch (err) {
// res.status(400).json({
// status: 'fail',
// message: err,
// });
// }
// };