Skip to content

Commit dd72c8d

Browse files
committed
changes in code
1 parent f92f296 commit dd72c8d

File tree

4 files changed

+370
-45
lines changed

4 files changed

+370
-45
lines changed

abc

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

0 commit comments

Comments
 (0)