Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 47 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

#### Backend delpoyed at [Hive Stack Heroku](https://hive-stack.herokuapp.com/) <br>

[![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:
Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion api/middleware/locations/addIfDoesNotExist.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
2 changes: 2 additions & 0 deletions api/middleware/locations/findLocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down
55 changes: 36 additions & 19 deletions api/routes/reviewsRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down