Skip to content

Commit

Permalink
search for matched campgrounds from query search done
Browse files Browse the repository at this point in the history
  • Loading branch information
bmarieay committed Feb 24, 2022
1 parent a4c4caa commit 05e9cba
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 14 deletions.
24 changes: 12 additions & 12 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
# YelpCamp United States
<hr>
This is my first full stack CRUD project that allows users to view, post, edit, delete, and leave reviews on the campgrounds.
<br><br>
<b>Features that I added/will be added on top of Colt Steele's Web Development Bootcamp: </b>
<br>

1.**[NPS API](https://www.nps.gov/subjects/developer/guides.htm)** - I integrated real campgrounds in the United States using an API.<br>
2. **☀️light and 🌙dark mode** - I used JS DOM Manipulation, localStorage, and Cookies to implement a theme feature<br>
3. **🌳My campgrounds** - This feature allows users to show the campgrounds they uploaded to YelpCamp! <br>
4. **📄pagination feature** - I also used DOM Manipulation, cookies, and Mongoose to implement pagination. This augments the performance by loading only few datas the the user wants rather than loading a whole single resource.<br>
5. 🔍**Search Feature** ➡️ <i>Coming soon!</i>
1. **☀️light and 🌙dark mode** - I used JS DOM Manipulation, localStorage, and Cookies to implement a theme feature<br>
2. **🌳My campgrounds** - This feature allows users to show the campgrounds they uploaded to YelpCamp! <br>
3. **📄pagination feature** - I also used DOM Manipulation, cookies, and Mongoose to implement pagination. This augments the performance by loading only few datas the the user wants rather than loading a whole single resource.<br>
4. 🔍**Search Feature** ➡️ <i>Coming soon!</i>


<hr>

## 🔨Stacks
YelpCamp is built with **MEN** (*Mongo, Express, and Node*) stack.
<hr>


## 👀 Previews
#### Login
Expand Down Expand Up @@ -57,7 +56,7 @@ YelpCamp is built with **MEN** (*Mongo, Express, and Node*) stack.
#### Reviews
<img src="assets/imgs/review.png" width="100%" alt="Home page">

<hr>


## 🧰Tools

Expand All @@ -69,7 +68,7 @@ YelpCamp is built with **MEN** (*Mongo, Express, and Node*) stack.
5. #### Embedded Javascript
6. #### Axios
7. #### Joi
<hr>


## 💻 To run on your local machine:
### Prerequisties:
Expand Down Expand Up @@ -101,6 +100,7 @@ YelpCamp is built with **MEN** (*Mongo, Express, and Node*) stack.
- API_KEY=[value]
- OWNER_ID=[value]
6. Open a new terminal and type `mongod`. The project will not start until this is opened ⚠️
7. On the previous terminal, type
`node app.js` or `nodemon app.js` (*recommended*)
8. Once you see "Database connected", go to your preferred browser, then go to *localhost:3000*
7. Now to seeds your database, on the previous terminal, type `node seeds/index.js`. (After getting a response of done on your terminal, wait for 3-8 seconds to let all the campgrounds' images get uploaded to cloudinary).
8. Hit CTRL+C to exit the current process.
9. Then type `node app.js` or `nodemon app.js` (*recommended*)
10. Once you see "Database connected", go to your preferred browser, then go to *localhost:3000*
69 changes: 67 additions & 2 deletions controllers/campgrounds.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,32 @@ const mapBoxToken = process.env.MAPBOX_TOKEN;
const axios = require("axios");
const key = process.env.API_KEY;
const geocoder = mbxGeocoding({ accessToken: mapBoxToken });
//TODO: AFTER NEED TO REFACTOR ASYNCS TO MIDDLEWARE
mbxGeocoding({ accessToken: mapBoxToken });
const config = {
params:
{
api_key : key
}
};

async function upload(images, camp){
for(let i =0; i< images.length; i++){
try {
//store the result after upload and insert in camp images
const res = await cloudinary.uploader.upload_large(images[i], {folder: 'YelpCamp'});
camp.images.push({url: res.secure_url, filename: res.original_filename});
} catch (e) {
if(i === 0){
camp.success = 'fail';
}
}
}
}
//TODO: MAKE A MIDDLEWARE FOR RENDERING INDEX
module.exports.index = async (req, res) => {
const result = {};
result.results = [];
const allCampgrounds = await Campground.find({});
result.allItemsFetched = allCampgrounds.map( camp => camp).length;
const max = Math.ceil(result.allItemsFetched / 20.0);
Expand Down Expand Up @@ -55,8 +71,57 @@ module.exports.index = async (req, res) => {
// res.send(result);
}
//user searched for something
const queried = await axios.get(`https://developer.nps.gov/api/v1/campgrounds?limit=20&q=${q}`, config);
res.send(queried.data.data);
const queried = await axios.get(`https://developer.nps.gov/api/v1/campgrounds?limit=3&q=${q}`, config);
let matchedCampground;
const campPromises = queried.data.data.map(async function(camp) {
console.log("BEFORE SEARCHING");
matchedCampground = await Campground.find({title: camp.name});
console.log("AFTER SEARCHING")
result.results.push(matchedCampground);
console.log("AFTER PUSH");
console.log(result.results);
});
await Promise.all(campPromises);
// queried.data.data.forEach( async (camp, i) => {
// console.log("BEFORE SEARCHING", i)
// matchedCampground = await Campground.find({title: camp.name});
// console.log("AFTER SEARCHING", i)
// result.results.push(matchedCampground);
// console.log("AFTER PUSH", i);
// console.log(result.results);
// // if(camp.images[0] && camp.name){
// // const price = Math.floor(Math.random() * 20) + 10;
// // const campground = new Campground({
// // //do reverse lookup here!!!!!from the coordinates if no address available
// // location: camp.addresses[0] ?
// // `${camp.addresses[0].line1} ${camp.addresses[0].city} ${camp.addresses[0].stateCode}`:
// // await reverseGeo([Number.parseFloat( camp.longitude, 10), Number.parseFloat( camp.latitude, 10)]),

// // title: camp.name,

// // description: camp.description,
// // //assign a random price if there is no cost
// // price: camp.fees[0] ? camp.fees[0].cost : price,

// // geometry: {
// // type: 'Point',
// // coordinates: [
// // camp.longitude,
// // camp.latitude
// // ]
// // }
// // })
// // await upload(camp.images.map(img => img.url), campground);
// // await User.findByIdAndUpdate(mainAuth, {$push:{campgrounds: campground}});
// // //add cloud uploader for below. use only and save if campground not yet in database otherwise just show it
// // if(campground.success !== 'fail') {
// // await campground.save();
// // }
// // result.results.push()
// // }
// })
console.log("OUTSIDE",result.results);
res.send(result);


}
Expand Down

0 comments on commit 05e9cba

Please sign in to comment.