diff --git a/script/foursquareTestScript.js b/script/createFoursquareRatings.js similarity index 92% rename from script/foursquareTestScript.js rename to script/createFoursquareRatings.js index 74679bf..d776f7d 100644 --- a/script/foursquareTestScript.js +++ b/script/createFoursquareRatings.js @@ -6,16 +6,16 @@ const fs = require('fs') const iconUrl = 'https://image.flaticon.com/icons/svg/174/174850.svg' -async function foursquareCreate(createDbRestaurantObj) { +async function foursquareCreate(restaurantObj) { try { console.log('Creating Foursquare reviews...') const fsqData = JSON.parse( fs.readFileSync('script/finalFoursquareData.json', 'utf8') ) - const restaurantDbObj = await createDbRestaurantObj() + const bulkCreateArr = fsqData .filter(restaurant => { - if (restaurantDbObj[restaurant.name]) { + if (restaurantObj[restaurant.name]) { return true } }) @@ -23,7 +23,7 @@ async function foursquareCreate(createDbRestaurantObj) { return { source: 'Foursquare', rating: restaurant.rating, - restaurantId: restaurantDbObj[restaurant.name].id, + restaurantId: restaurantObj[restaurant.name].id, sourceLogo: iconUrl, reviewUrl: restaurant.reviewUrl } diff --git a/script/googleTestScript.js b/script/createGoogleRatings.js similarity index 89% rename from script/googleTestScript.js rename to script/createGoogleRatings.js index e32085b..8524d51 100644 --- a/script/googleTestScript.js +++ b/script/createGoogleRatings.js @@ -6,7 +6,7 @@ const utf8 = require('utf8') const iconUrl = 'https://image.flaticon.com/icons/svg/281/281781.svg' -async function googleCreate(createDbRestaurantObj) { +async function googleCreate(restaurantObj) { try { console.log('Creating Google reviews...') const googleData = JSON.parse( @@ -16,11 +16,9 @@ async function googleCreate(createDbRestaurantObj) { if (restaurant) return true }) - const restaurantDbObj = await createDbRestaurantObj() - const bulkCreateArr = newGoogleData .filter(restaurant => { - if (restaurantDbObj[restaurant.name]) { + if (restaurantObj[restaurant.name]) { return true } }) @@ -28,7 +26,7 @@ async function googleCreate(createDbRestaurantObj) { return { source: 'Google', rating: restaurant.rating, - restaurantId: restaurantDbObj[restaurant.name].id, + restaurantId: restaurantObj[restaurant.name].id, sourceLogo: iconUrl } }) diff --git a/script/openTableTestScript.js b/script/createOpentableLinks.js similarity index 82% rename from script/openTableTestScript.js rename to script/createOpentableLinks.js index 86f747e..61f7dba 100644 --- a/script/openTableTestScript.js +++ b/script/createOpentableLinks.js @@ -25,17 +25,16 @@ const getOpenTableArray = async () => { } } -const opentableCreate = async createDbRestaurantObj => { +const opentableCreate = async restaurantObj => { try { console.log('Creating OpenTable links...') - const restaurantDbObj = await createDbRestaurantObj() const opentableArray = await getOpenTableArray() const arrayForUpdate = opentableArray - .filter(restaurant => (restaurantDbObj[restaurant.name] ? true : false)) + .filter(restaurant => (restaurantObj[restaurant.name] ? true : false)) .map(restaurant => { return [ {opentableUrl: restaurant.reserve_url}, - {where: {id: restaurantDbObj[restaurant.name].id}} + {where: {id: restaurantObj[restaurant.name].id}} ] }) for (let i = 0; i < arrayForUpdate.length; i++) { diff --git a/script/yelpTestScript.js b/script/createYelpRestaurants.js similarity index 62% rename from script/yelpTestScript.js rename to script/createYelpRestaurants.js index 7c7ad4c..f8bb97c 100644 --- a/script/yelpTestScript.js +++ b/script/createYelpRestaurants.js @@ -21,7 +21,7 @@ async function getYelp(offset) { return data.businesses } -async function getRestaurants() { +async function getYelpRestaurants() { let restaurants = [] for (let i = 0; i < 20; i++) { @@ -32,7 +32,7 @@ async function getRestaurants() { } async function createYelpRestaurants() { - const restaurants = await getRestaurants() + const restaurants = await getYelpRestaurants() const restaurantsArr = restaurants.map(restaurant => { const location = restaurant.location.display_address.join(', ') @@ -49,19 +49,34 @@ async function createYelpRestaurants() { }) await Restaurant.bulkCreate(restaurantsArr) +} - const ratingsArr = restaurants.map((restaurant, idx) => { - let id = idx + 1 - return { - source: 'Yelp', - rating: restaurant.rating, - restaurantId: id, - sourceLogo: iconUrl, - reviewUrl: restaurant.url - } - }) - - await Review.bulkCreate(ratingsArr) +async function createYelpRatings(restaurantObj) { + try { + console.log('Creating Yelp reviews...') + const restaurants = await getYelpRestaurants() + let newObj = {} + const ratingsArr = restaurants + .filter(restaurant => { + if (restaurantObj[restaurant.name] && !newObj[restaurant.name]) { + newObj[restaurant.name] = true + return true + } + }) + .map(restaurant => { + return { + source: 'Yelp', + rating: restaurant.rating, + restaurantId: restaurantObj[restaurant.name].id, + sourceLogo: iconUrl, + reviewUrl: restaurant.url + } + }) + await Review.bulkCreate(ratingsArr) + console.log('Yelp reviews are done!') + } catch (error) { + console.error(error) + } } async function yelpCreate() { @@ -74,4 +89,4 @@ async function yelpCreate() { } } -module.exports = yelpCreate +module.exports = {yelpCreate, createYelpRatings} diff --git a/script/zomatoTestScript.js b/script/createZomatoRatings.js similarity index 91% rename from script/zomatoTestScript.js rename to script/createZomatoRatings.js index 1a445cb..f76bb91 100644 --- a/script/zomatoTestScript.js +++ b/script/createZomatoRatings.js @@ -66,21 +66,20 @@ const createZomatoRestaurantObj = async () => { } } -async function zomatoCreate(createDbRestaurantObj) { +async function zomatoCreate(restaurantObj) { try { console.log('Creating Zomato reviews...') let zomatoRestaurants = await createZomatoRestaurantObj() zomatoRestaurants = zomatoRestaurants.filter(restaurant => { if (restaurant) return true }) - const dbRestaurants = await createDbRestaurantObj() let reviewsForCreate = [] zomatoRestaurants.forEach(restaurant => { - if (dbRestaurants[restaurant.name]) { + if (restaurantObj[restaurant.name]) { reviewsForCreate.push({ source: 'Zomato', rating: restaurant.rating, - restaurantId: dbRestaurants[restaurant.name].id, + restaurantId: restaurantObj[restaurant.name].id, sourceLogo: iconUrl, reviewUrl: restaurant.reviewUrl }) diff --git a/script/seed.js b/script/seed.js index dbcb7f8..3a819d6 100644 --- a/script/seed.js +++ b/script/seed.js @@ -2,11 +2,11 @@ const db = require('../server/db') const {User} = require('../server/db/models') -const yelpCreate = require('./yelpTestScript') -const zomatoCreate = require('./zomatoTestScript') -const googleCreate = require('./googleTestScript') -const foursquareCreate = require('./foursquareTestScript') -const openTableCreate = require('./openTableTestScript') +const {yelpCreate, createYelpRatings} = require('./createYelpRestaurants') +const zomatoCreate = require('./createZomatoRatings') +const googleCreate = require('./createGoogleRatings') +const foursquareCreate = require('./createFoursquareRatings') +const openTableCreate = require('./createOpentableLinks') const createDbRestaurantObj = require('./helperFunctions') async function seed() { @@ -20,11 +20,18 @@ async function seed() { User.create({email: 'victor@email.com', password: '123'}) ]) console.log(`seeded ${users.length} users`) + //DB depends on Yelp to create restaurants before creating reviews await yelpCreate() - await zomatoCreate(createDbRestaurantObj) - await googleCreate(createDbRestaurantObj) - await foursquareCreate(createDbRestaurantObj) - await openTableCreate(createDbRestaurantObj) + //Creates object of existing restaurants to use in other create functions + const restaurantObj = await createDbRestaurantObj() + //Runs all async functions for creating reviews and adding OpenTable links in parallel + await Promise.all([ + createYelpRatings(restaurantObj), + zomatoCreate(restaurantObj), + googleCreate(restaurantObj), + foursquareCreate(restaurantObj), + openTableCreate(restaurantObj) + ]) console.log(`Seeded successfully`) } diff --git a/script/updateScript.js b/script/updateScript.js new file mode 100644 index 0000000..c199041 --- /dev/null +++ b/script/updateScript.js @@ -0,0 +1,28 @@ +const {createYelpRatings} = require('./createYelpRestaurants') +const createGoogleRatings = require('./createGoogleRatings') +const createZomatoRatings = require('./createZomatoRatings') +const createFoursquareRatings = require('./createFoursquareRatings') +const createRestaurantDbObj = require('./helperFunctions') +const db = require('../server/db') +const {Review} = require('../server/db/models') + +const updateRatings = async () => { + try { + console.log('Updating restaurant ratings...') + await Review.destroy({truncate: true, force: true}) + console.log('Existing reviews are deleted :(') + const restaurantObj = await createRestaurantDbObj() + await Promise.all([ + createYelpRatings(restaurantObj), + createGoogleRatings(restaurantObj), + createZomatoRatings(restaurantObj), + createFoursquareRatings(restaurantObj) + ]) + db.close() + console.log('Updating is done!') + } catch (error) { + console.error(error) + } +} + +updateRatings() diff --git a/server/api/restaurant.js b/server/api/restaurant.js index 041985b..66293e9 100644 --- a/server/api/restaurant.js +++ b/server/api/restaurant.js @@ -21,12 +21,3 @@ router.get('/:restaurantId', async (req, res, next) => { next(err) } }) - -router.post('/', async (req, res, next) => { - try { - const newRestaurant = await Restaurant.create(req.body) - res.json(newRestaurant) - } catch (err) { - next(err) - } -}) diff --git a/server/api/restaurants.spec.js b/server/api/restaurants.spec.js new file mode 100644 index 0000000..5245941 --- /dev/null +++ b/server/api/restaurants.spec.js @@ -0,0 +1,60 @@ +const {expect} = require('chai') +const request = require('supertest') +const db = require('../db') +const app = require('../index') +const Restaurant = db.model('restaurants') + +describe('Restaurant routes', () => { + beforeEach(() => { + return db.sync({force: true}) + }) + + const newRestaurant = { + name: 'test rest', + cuisineType: ['chinese'], + expenseRating: 2, + location: 'New York, NY', + latitude: 34.4, + longitude: 34.4 + } + + describe('Gets all restaurants', () => { + beforeEach(() => { + return Restaurant.create(newRestaurant) + }) + + it('GET /api/restaurant', async () => { + const res = await request(app) + .get('/api/restaurant') + .expect(200) + + expect(res.body).to.be.an('array') + expect(res.body[0].name).to.be.equal('test rest') + expect(res.body[0].cuisineType[0]).to.be.equal('chinese') + expect(res.body[0].expenseRating).to.be.equal(2) + expect(res.body[0].location).to.be.equal('New York, NY') + expect(res.body[0].latitude).to.be.equal(34.4) + expect(res.body[0].longitude).to.be.equal(34.4) + }) + }) + + describe('Gets a single restaurant', () => { + beforeEach(() => { + return Restaurant.create(newRestaurant) + }) + + it('GET api/restaurant/restaurantId', async () => { + const res = await request(app) + .get('/api/restaurant/1') + .expect(200) + + expect(res.body).to.be.an('object') + expect(res.body.name).to.be.equal('test rest') + expect(res.body.cuisineType[0]).to.be.equal('chinese') + expect(res.body.expenseRating).to.be.equal(2) + expect(res.body.location).to.be.equal('New York, NY') + expect(res.body.latitude).to.be.equal(34.4) + expect(res.body.longitude).to.be.equal(34.4) + }) + }) +}) diff --git a/server/api/review.js b/server/api/review.js index 23eb209..4779376 100644 --- a/server/api/review.js +++ b/server/api/review.js @@ -3,10 +3,10 @@ const {Review} = require('../db/models') module.exports = router router.get('/', async (req, res, next) => { - try { - const reviews = await Review.findAll() - res.json(reviews) - } catch (err) { - next(err) - } - }) + try { + const reviews = await Review.findAll() + res.json(reviews) + } catch (err) { + next(err) + } +}) diff --git a/server/api/review.spec.js b/server/api/review.spec.js new file mode 100644 index 0000000..5dc3fac --- /dev/null +++ b/server/api/review.spec.js @@ -0,0 +1,36 @@ +const {expect} = require('chai') +const request = require('supertest') +const db = require('../db') +const app = require('../index') +const Review = db.model('reviews') + +describe('Review routes', () => { + beforeEach(() => { + return db.sync({force: true}) + }) + + const newReview = { + source: 'Yelp', + sourceLogo: 'image!', + rating: 2.5, + reviewUrl: 'a review url' + } + + describe('Gets all reviews', () => { + beforeEach(() => { + return Review.create(newReview) + }) + + it('GET /api/review', async () => { + const res = await request(app) + .get('/api/review') + .expect(200) + + expect(res.body).to.be.an('array') + expect(res.body[0].source).to.be.equal('Yelp') + expect(res.body[0].sourceLogo).to.be.equal('image!') + expect(res.body[0].rating).to.be.equal(2.5) + expect(res.body[0].reviewUrl).to.be.equal('a review url') + }) + }) +}) diff --git a/server/api/users.js b/server/api/users.js index 702fdbb..0a3f517 100644 --- a/server/api/users.js +++ b/server/api/users.js @@ -2,16 +2,16 @@ const router = require('express').Router() const {User} = require('../db/models') module.exports = router -router.get('/', async (req, res, next) => { - try { - const users = await User.findAll({ - // explicitly select only the id and email fields - even though - // users' passwords are encrypted, it won't help if we just - // send everything to anyone who asks! - attributes: ['id', 'email'] - }) - res.json(users) - } catch (err) { - next(err) - } -}) +// router.get('/', async (req, res, next) => { +// try { +// const users = await User.findAll({ +// // explicitly select only the id and email fields - even though +// // users' passwords are encrypted, it won't help if we just +// // send everything to anyone who asks! +// attributes: ['id', 'email'] +// }) +// res.json(users) +// } catch (err) { +// next(err) +// } +// }) diff --git a/server/api/users.spec.js b/server/api/users.spec.js index 09f3529..8a16c9d 100644 --- a/server/api/users.spec.js +++ b/server/api/users.spec.js @@ -1,32 +1,32 @@ -/* global describe beforeEach it */ +// /* global describe beforeEach it */ -const {expect} = require('chai') -const request = require('supertest') -const db = require('../db') -const app = require('../index') -const User = db.model('user') +// const {expect} = require('chai') +// const request = require('supertest') +// const db = require('../db') +// const app = require('../index') +// const User = db.model('user') -describe('User routes', () => { - beforeEach(() => { - return db.sync({force: true}) - }) +// describe('User routes', () => { +// beforeEach(() => { +// return db.sync({force: true}) +// }) - describe('/api/users/', () => { - const codysEmail = 'cody@puppybook.com' +// describe('/api/users/', () => { +// const codysEmail = 'cody@puppybook.com' - beforeEach(() => { - return User.create({ - email: codysEmail - }) - }) +// beforeEach(() => { +// return User.create({ +// email: codysEmail +// }) +// }) - it('GET /api/users', async () => { - const res = await request(app) - .get('/api/users') - .expect(200) +// it('GET /api/users', async () => { +// const res = await request(app) +// .get('/api/users') +// .expect(200) - expect(res.body).to.be.an('array') - expect(res.body[0].email).to.be.equal(codysEmail) - }) - }) // end describe('/api/users') -}) // end describe('User routes') +// expect(res.body).to.be.an('array') +// expect(res.body[0].email).to.be.equal(codysEmail) +// }) +// }) // end describe('/api/users') +// }) // end describe('User routes') diff --git a/server/db/models/review.spec.js b/server/db/models/review.spec.js new file mode 100644 index 0000000..a4d4e56 --- /dev/null +++ b/server/db/models/review.spec.js @@ -0,0 +1,31 @@ +// /* global describe beforeEach it */ + +// const {expect} = require('chai') +// const db = require('../index') +// const Review = db.model('reviews') + +// describe('Review model', () => { +// beforeEach(() => { +// return db.sync({force: true}) +// }) + +// describe('Create review', () => { +// let newestReview + +// beforeEach(async () => { +// newestReview = await Review.create({ +// source: 'Yelp', +// sourceLogo: 'image!', +// rating: 2.5, +// reviewUrl: 'a review url' +// }) +// }) + +// console.log('newest review', newestReview) + +// expect(newestReview.source).to.be.equal('Yelp') +// expect(newestReview.sourceLogo).to.be.equal('image!') +// expect(newestReview.rating).to.be.equal(2.5) +// expect(newestReview.reviewUrl).to.be.equal('a review url') +// }) +// })