From 7670aa577188e701fb34a8a72f06d4e137fcc281 Mon Sep 17 00:00:00 2001 From: AceIsHuman Date: Tue, 28 Apr 2020 19:59:14 -0500 Subject: [PATCH 1/4] update to use req.body.googleId --- api/middleware/locations/addIfDoesNotExist.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/middleware/locations/addIfDoesNotExist.js b/api/middleware/locations/addIfDoesNotExist.js index 3cfba72..6060b19 100644 --- a/api/middleware/locations/addIfDoesNotExist.js +++ b/api/middleware/locations/addIfDoesNotExist.js @@ -12,7 +12,7 @@ module.exports = addIfDoesNotExist; async function addIfDoesNotExist(req, res, next) { if (!!res.locals.location) return next(); if (req.params.locationId.length < 10) return res.status(400).json({ message: "This location could not be found." }); - const [loc] = await LOCATIONS.addLocation({ googleId: req.params.locationId }); + const [loc] = await LOCATIONS.addLocation({ googleId: req.params.locationId || req.body.googleId }); res.locals.location = loc; next(); } \ No newline at end of file From ff11cbcad7af27cae9035f24c46c9cc9d884e642 Mon Sep 17 00:00:00 2001 From: AceIsHuman Date: Tue, 28 Apr 2020 19:59:46 -0500 Subject: [PATCH 2/4] update to use req.body.location_id --- api/middleware/locations/findLocation.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/api/middleware/locations/findLocation.js b/api/middleware/locations/findLocation.js index 0169c4d..dd17217 100644 --- a/api/middleware/locations/findLocation.js +++ b/api/middleware/locations/findLocation.js @@ -19,6 +19,8 @@ function findLocation(req, res, next) { let location = req.body; if (!!req.params.locationId) location = { id: req.params.locationId, googleId: req.params.locationId }; + // If req.body contains `location_id`, assign that value to `googleId` + if (!!req.body.location_id) req.body.googleId = req.body.location_id; const findBy = (type) => { if (location[type]) { From 01bd97944e042bd289c9fd7b13bbce934c7e452c Mon Sep 17 00:00:00 2001 From: AceIsHuman Date: Tue, 28 Apr 2020 20:02:39 -0500 Subject: [PATCH 3/4] update routes to use findLocation and addIfDoesNotExist middleware --- api/routes/reviewsRoute.js | 55 +++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 19 deletions(-) diff --git a/api/routes/reviewsRoute.js b/api/routes/reviewsRoute.js index 4c8df38..5999d13 100644 --- a/api/routes/reviewsRoute.js +++ b/api/routes/reviewsRoute.js @@ -7,6 +7,8 @@ const router = require("express").Router(); // MIDDLEWARE const requireBody = require("../middleware/requireBody"); +const findLocation = require("../middleware/locations/findLocation"); +const addIfDoesNotExist = require("../middleware/locations/addIfDoesNotExist"); // @route GET reviews/ // @desc Gets all the reviews in the database @@ -60,11 +62,10 @@ router.get("/:id/user", authenticate, async (req, res) => { // @desc Gets all reviews for location ID // @access currently Public, needs to be protected -router.get("/:id/location", async (req, res) => { +router.get("/:locationId/location", findLocation, async (req, res) => { + const location = res.locals.location; try { - const reviewLocation = await REVIEW_MODEL.getReviewsByLocation( - req.params.id - ); + const reviewLocation = await REVIEW_MODEL.getReviewsByLocation(location.id); console.log("rl", reviewLocation); if (reviewLocation) { res.status(200).json(reviewLocation); @@ -120,21 +121,20 @@ router.get("/:id/feature", async (req, res) => { .send({ message: "Location from this review is not found", error }); } } catch (err) { - res - .status(500) - .json({ - message: "Error fetching Review. Location May Not Have A Review. " - }); + res.status(500).json({ + message: "Error fetching Review. Location May Not Have A Review. " + }); } }); // @route Get reviews/:id/first // @desc Gets first posted review // @access currently Public, needs to be protected -router.get("/:id/first", async (req, res) => { +router.get("/:locationId/first", findLocation, async (req, res) => { + const location = res.locals.location; try { const featureReview = await REVIEW_MODEL.getFirstReviewByLocation( - req.params.id + location.id ); console.log("rl", featureReview); console.log("length", Object.keys(featureReview).length); @@ -154,15 +154,32 @@ router.get("/:id/first", async (req, res) => { // @route POST reviews/ // @desc Adds a new review // @access currently Public, needs to be protected -router.post("/", requireBody, async (req, res) => { - let review = req.body; - try { - const addedReview = await REVIEW_MODEL.add(review); - return res.status(201).json({ message: "New review added", addedReview }); - } catch (err) { - return res.status(500).json(err.message); +router.post( + "/", + authenticate, + requireBody, + findLocation, + addIfDoesNotExist, + async (req, res) => { + const review = { + rating: req.body.rating, + comments: req.body.comments, + internet_rating: req.body.internet_rating, + location_id: res.locals.location.id, + user_id: res.locals.decodedToken.userId, + upload_speed: req.body.upload_speed || null, + download_speed: req.body.download_speed || null, + secure_wifi: req.body.secure_wifi || null, + }; + + try { + const addedReview = await REVIEW_MODEL.add(review); + return res.status(201).json({ message: "New review added", addedReview }); + } catch (err) { + return res.status(500).json(err.message); + } } -}); +); // @route PUT reviews/ // @desc Edits a review From a08b4231bfe4201846d50498e19ff8c9968080a1 Mon Sep 17 00:00:00 2001 From: AceIsHuman Date: Wed, 29 Apr 2020 21:36:22 -0500 Subject: [PATCH 4/4] update-docs --- README.md | 60 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index f924fb7..7827903 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,9 @@ #### Backend delpoyed at [Hive Stack Heroku](https://hive-stack.herokuapp.com/)
+[![Maintainability](https://api.codeclimate.com/v1/badges/07dd8f429c840cfe6961/maintainability)](https://codeclimate.com/github/Lambda-School-Labs/where-to-code-be/maintainability) +[![Test Coverage](https://api.codeclimate.com/v1/badges/07dd8f429c840cfe6961/test_coverage)](https://codeclimate.com/github/Lambda-School-Labs/where-to-code-be/test_coverage) + ## Getting started To get the server running locally: @@ -11,6 +14,7 @@ To get the server running locally: - create **.env** - **DEV_SERVER** development database url - **JWT_SECRET** secret for jwtoken + - **GCP_KEY** API key for Google Cloud Platform - **TESTING_DATABASE** database url for testing environment - **knex migrate:latest** migrate tables for database - **knex seed:run** runs seeded testing data @@ -35,7 +39,7 @@ To get the server running locally: ## Endpoints -### Postman Docs: https://documenter.getpostman.com/view/9185503/SzS4RSns?version=latest +### Postman Docs: https://documenter.getpostman.com/view/9185503/SzS4RSns?version=latest > #### Authentication Routes @@ -72,14 +76,47 @@ To get the server running locally: ## Test User Accounts There are six test users seeded into the database: -| Email | Password | +| Email | Password | | --------------- | -------- | -| test1@gmail.com | test | -| test2@gmail.com | test2 | -| test3@gmail.com | test3 | -| test4@gmail.com | test4 | -| test5@gmail.com | test5 | -| test6@gmail.com | test6 | +| test1@gmail.com | test | +| test2@gmail.com | test2 | +| test3@gmail.com | test3 | +| test4@gmail.com | test4 | +| test5@gmail.com | test5 | +| test6@gmail.com | test6 | + +# Data Model + +#### BASIC USERS + +--- + +``` +{ + id: INTEGER + username: STRING + firstName: STRING + lastName: STRING + reviewCount: INTEGER + created_at: TIMESTAMP + updated_at: TIMESTAMP +} +``` + +#### LOCATIONS + +--- + +``` +{ + id: INTEGER, + googleId: STRING, // used when storing location from google + name: STRING, + address: STRING, + phone: STRING, + icon: STRING // url for image of location +} +``` ## Actions @@ -125,8 +162,7 @@ There are six test users seeded into the database: `formatAllLocationObjects(locationsList)` -> Returns all locations in array, and populate with details for any Google Places. -#### Middleware -___ +> ### Middleware `findLocation` -> Middleware that finds a location and adds it to res.locals.location @@ -142,6 +178,7 @@ create a .env file that includes the following: * JWT_SECRET - secret for jwtoken * TESTING_DATABASE - database url for testing environment * ENVIRONMENT - set to "development" until ready for "production" + * GCP_KEY - API token for Google Cloud Platform ## Contributing @@ -183,6 +220,3 @@ These contribution guidelines have been adapted from [this good-Contributing.md- ## Documentation See [Frontend Documentation](https://github.com/Lambda-School-Labs/where-to-code-fe/blob/master/README.md) for details on the fronend of our project. - -[![Maintainability](https://api.codeclimate.com/v1/badges/07dd8f429c840cfe6961/maintainability)](https://codeclimate.com/github/Lambda-School-Labs/where-to-code-be/maintainability) -[![Test Coverage](https://api.codeclimate.com/v1/badges/07dd8f429c840cfe6961/test_coverage)](https://codeclimate.com/github/Lambda-School-Labs/where-to-code-be/test_coverage)