From 05e9cbad5b5d642cc14afee39f35735d1adfd5b0 Mon Sep 17 00:00:00 2001 From: bmarieay Date: Thu, 24 Feb 2022 09:25:07 -0800 Subject: [PATCH] search for matched campgrounds from query search done --- ReadMe.md | 24 ++++++------- controllers/campgrounds.js | 69 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 79 insertions(+), 14 deletions(-) diff --git a/ReadMe.md b/ReadMe.md index 0cfc2af..37fa1b2 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -1,21 +1,20 @@ # YelpCamp United States -
This is my first full stack CRUD project that allows users to view, post, edit, delete, and leave reviews on the campgrounds.

Features that I added/will be added on top of Colt Steele's Web Development Bootcamp:
1.**[NPS API](https://www.nps.gov/subjects/developer/guides.htm)** - I integrated real campgrounds in the United States using an API.
-2. **☀️light and 🌙dark mode** - I used JS DOM Manipulation, localStorage, and Cookies to implement a theme feature
-3. **🌳My campgrounds** - This feature allows users to show the campgrounds they uploaded to YelpCamp!
-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.
-5. 🔍**Search Feature** ➡️ Coming soon! +1. **☀️light and 🌙dark mode** - I used JS DOM Manipulation, localStorage, and Cookies to implement a theme feature
+2. **🌳My campgrounds** - This feature allows users to show the campgrounds they uploaded to YelpCamp!
+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.
+4. 🔍**Search Feature** ➡️ Coming soon! + -
## 🔨Stacks YelpCamp is built with **MEN** (*Mongo, Express, and Node*) stack. -
+ ## 👀 Previews #### Login @@ -57,7 +56,7 @@ YelpCamp is built with **MEN** (*Mongo, Express, and Node*) stack. #### Reviews Home page -
+ ## 🧰Tools @@ -69,7 +68,7 @@ YelpCamp is built with **MEN** (*Mongo, Express, and Node*) stack. 5. #### Embedded Javascript 6. #### Axios 7. #### Joi -
+ ## 💻 To run on your local machine: ### Prerequisties: @@ -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* diff --git a/controllers/campgrounds.js b/controllers/campgrounds.js index 81c1074..11ced64 100644 --- a/controllers/campgrounds.js +++ b/controllers/campgrounds.js @@ -6,6 +6,7 @@ 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: @@ -13,9 +14,24 @@ const config = { 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); @@ -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); }