humayuntariq98 / ExploreEZ

A Mongoose, Express and Node based travel application which is designed to revolutionize the way people plan, organize, and embark on their travel adventures.
MIT License
0 stars 3 forks source link

Glow #10

Open humayuntariq98 opened 1 year ago

humayuntariq98 commented 1 year ago

Grow 1 - If we update a destination, the image also gets updated through Google API

async function update(req, res) { try { const destinationData = { ...req.body }; const editedDestination = await Destination.findById(req.params.id); editedDestination.name = destinationData.name; editedDestination.favoriteSpots = destinationData.favoriteSpots; editedDestination.budget = destinationData.budget;

    // Fetch updated image from Google Places API
    const response = await axios.get(
        "https://maps.googleapis.com/maps/api/place/textsearch/json",
        {
            params: {
                query: editedDestination.name,
                key: process.env.GOOGLE_PLACES_API_KEY,
            },
        }
    );
    // Store api key inside env as google places API
    if (
        response.data.results &&
  response.data.results[0] &&
  response.data.results[0].photos
    ) {
        const photoReference = response.data.results[0].photos[0].photo_reference;
        const imageUrl = `https://maps.googleapis.com/maps/api/place/photo?maxwidth=800&photoreference=${photoReference}&key=${process.env.GOOGLE_PLACES_API_KEY}`;
        editedDestination.image = imageUrl;
    }

    await editedDestination.save();
    res.redirect("/destinations");
} catch (error) {
    handleError(res, "something went wrong", error);
}

}

Grow 2 - Being able to only update and remove our own reviews

async function removeReview(req, res) { try { const { destinationId, reviewId } = req.params; // Find the destination const destinationFound = await Destination.findOne({ "reviews._id": reviewId, "reviews.user": req.user._id, });

    // Find the index of the review within the reviews array
    const reviewIndex = destinationFound.reviews.findIndex(
        (review) => review._id.toString() === reviewId
    );

    // // Remove the review from the reviews array
    destinationFound.reviews.splice(reviewIndex, 1);

    // // Save the updated destination
    await destinationFound.save();
    res.redirect(`/destinations/${destinationId}`);
} catch (error) {
    handleError(res, "something went wrong", error);
}

}

krabecb commented 1 year ago

Great work with the logic here! 💯 Integrating Google's API within a week and also making sure to update a destination's image is an excellent achievement!

krabecb commented 1 year ago

Instructional Glow: README, styling, functionality, great work with location images/API! Code is organized.