From 58f0b08fb922bf53a2704fbc685c1a076ed93d89 Mon Sep 17 00:00:00 2001 From: ryanbuui <57205440+ryanbuui@users.noreply.github.com> Date: Mon, 9 Mar 2020 15:34:37 -0700 Subject: [PATCH 1/8] Added project_reviews migration --- .../20200309131309_project_reviews.js | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 database/migrations/20200309131309_project_reviews.js diff --git a/database/migrations/20200309131309_project_reviews.js b/database/migrations/20200309131309_project_reviews.js new file mode 100644 index 00000000..3594a5f3 --- /dev/null +++ b/database/migrations/20200309131309_project_reviews.js @@ -0,0 +1,29 @@ + +exports.up = function(knex) { + return knex.schema.createTable('project_reviews', table => { + table.increments('id').notNullable().unique(); + table.integer("recommendation").notNullable(); + table.integer("communication").notNullable(); + table.integer("experience").notNullable(); + table.text("review_description").notNullable(); + table.integer("supporter_id") + .notNullable() + .references("supporters.sup_id") + .onDelete("CASCADE") + .onUpdate("CASCADE"); + table.integer("conservasionist_id") + .notNullable() + .references("conservationists.cons_id") + .onDelete("CASCADE") + .onUpdate("CASCADE"); + table.integer("campaign_id") + .notNullable() + .references("campaigns.camp_id") + .onDelete("CASCADE") + .onUpdate("CASCADE"); + }); +}; + +exports.down = function(knex) { + return knex.schema.dropTableIfExists('project_reviews'); +}; From e7404b5b9ef86f8b8ffeccdabbd08aa019e125a2 Mon Sep 17 00:00:00 2001 From: ryanbuui <57205440+ryanbuui@users.noreply.github.com> Date: Mon, 9 Mar 2020 16:00:59 -0700 Subject: [PATCH 2/8] Fixed typo and changed campaign id to skilled_impact_request_id --- database/migrations/20200309131309_project_reviews.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/database/migrations/20200309131309_project_reviews.js b/database/migrations/20200309131309_project_reviews.js index 3594a5f3..bb7f544d 100644 --- a/database/migrations/20200309131309_project_reviews.js +++ b/database/migrations/20200309131309_project_reviews.js @@ -11,16 +11,15 @@ exports.up = function(knex) { .references("supporters.sup_id") .onDelete("CASCADE") .onUpdate("CASCADE"); - table.integer("conservasionist_id") + table.integer("conservationist_id") .notNullable() .references("conservationists.cons_id") .onDelete("CASCADE") .onUpdate("CASCADE"); - table.integer("campaign_id") - .notNullable() - .references("campaigns.camp_id") + table.integer("skilled_impact_request_id").notNullable() + .references("skilled_impact_requests.id") .onDelete("CASCADE") - .onUpdate("CASCADE"); + .onUpdate("CASCADE"); }); }; From c512c8695ef5c2b7f8e7cc1b7e8ec085f99acdf1 Mon Sep 17 00:00:00 2001 From: Sumeet Bansal Date: Tue, 7 Apr 2020 13:28:24 -0700 Subject: [PATCH 3/8] fixed seeds --- database/seeds/002-conservations.js | 60 ++++++++++++++-------------- database/seeds/003-campaigns.js | 62 ++++++++++++++--------------- 2 files changed, 59 insertions(+), 63 deletions(-) diff --git a/database/seeds/002-conservations.js b/database/seeds/002-conservations.js index 9ed4049c..d25feb57 100644 --- a/database/seeds/002-conservations.js +++ b/database/seeds/002-conservations.js @@ -2,35 +2,33 @@ exports.seed = function (knex, Promise) { // Deletes ALL existing entries return knex('conservationists') .del() - .then(() => - // Inserts seed entries - knex('conservationists').insert([ - { - users_id: 1, - org_name: 'Beaver Town', - org_link_url: 'google.com', - org_link_text: 'google.com', - about_us: 'Lorem IpsOn', - issues: 'wood', - support_us: null, - }, - { - users_id: 2, - org_name: 'Panda Lovers', - org_link_url: 'google.com', - org_link_text: 'google.com', - about_us: 'Lorem IpsOn', - issues: 'sleep', - support_us: null, - }, - { - users_id: 3, - org_name: 'Giraffe 2 Tall', - org_link_url: 'google.com', - org_link_text: 'google.com', - about_us: 'Lorem IpsOn', - issues: 'too tall', - support_us: null, - }, - ])); + .then(() => knex('conservationists').insert([ + { + user_id: 1, + name: 'Beaver Town', + link_url: 'google.com', + link_text: 'google.com', + about_us: 'Lorem IpsOn', + issues: 'wood', + support_us: null, + }, + { + user_id: 2, + name: 'Panda Lovers', + link_url: 'google.com', + link_text: 'google.com', + about_us: 'Lorem IpsOn', + issues: 'sleep', + support_us: null, + }, + { + user_id: 3, + name: 'Giraffe 2 Tall', + link_url: 'google.com', + link_text: 'google.com', + about_us: 'Lorem IpsOn', + issues: 'too tall', + support_us: null, + }, + ])); }; diff --git a/database/seeds/003-campaigns.js b/database/seeds/003-campaigns.js index 4f50df2f..398b9705 100644 --- a/database/seeds/003-campaigns.js +++ b/database/seeds/003-campaigns.js @@ -2,40 +2,38 @@ exports.seed = function (knex, Promise) { // Deletes ALL existing entries return knex('campaigns') .del() - .then(() => - // Inserts seed entries - knex('campaigns').insert([ - { - users_id: 1, - camp_img: + .then(() => knex('campaigns').insert([ + { + user_id: 1, + image: 'https://i.ebayimg.com/images/g/Op4AAOSwxklcg4uw/s-l400.jpg', - camp_name: 'Save the Eggs', - camp_desc: 'Melon in hay rake. Veterinarian at Seeder eggs with watermelon ostriches. Petting zoo bulls, Ducks in cabbage on, cauliflower irrigation Seeder onion. Post pounder calf, hay or duck is, tool shed horse. Grapes at yams mushrooms organic berries gobble. Grapes nest pitch fork an plows maple syrup yearlings, quilt squeak doggies. Apples ducks straw, quail a ostriches donkey, hay hook cucumbers. Kidney beans ostric.', - camp_cta: 'https://www.turtleibrary/donate.now.com', - }, - { - users_id: 1, - camp_img: + name: 'Save the Eggs', + description: 'Melon in hay rake. Veterinarian at Seeder eggs with watermelon ostriches. Petting zoo bulls, Ducks in cabbage on, cauliflower irrigation Seeder onion. Post pounder calf, hay or duck is, tool shed horse. Grapes at yams mushrooms organic berries gobble. Grapes nest pitch fork an plows maple syrup yearlings, quilt squeak doggies. Apples ducks straw, quail a ostriches donkey, hay hook cucumbers. Kidney beans ostric.', + call_to_action: 'https://www.turtleibrary/donate.now.com', + }, + { + user_id: 1, + image: 'https://upload.wikimedia.org/wikipedia/commons/thumb/8/88/Beaver_Yearling_Grooming_Alhambra_Creek_2008.jpg/1920px-Beaver_Yearling_Grooming_Alhambra_Creek_2008.jpg', - camp_name: 'Save the Beavers', - camp_desc: 'Are porky pig beef, sheep rose garden sage, in pitch fork sunflower cowpies mice. Onion organic oranges and purr ducks canning owls at a squeal. Straw augers beef kettle our crickets. Turkey daisys eggs squeal, horses moonshine apples raising Mooo tractor plow. . Apples ducks straw, quail.', - camp_cta: 'https://www.nwf.org/Educational-Resources/Wildlife-Guide/Mammals/American-Beaver', - }, - { - users_id: 2, - camp_img: + name: 'Save the Beavers', + description: 'Are porky pig beef, sheep rose garden sage, in pitch fork sunflower cowpies mice. Onion organic oranges and purr ducks canning owls at a squeal. Straw augers beef kettle our crickets. Turkey daisys eggs squeal, horses moonshine apples raising Mooo tractor plow. . Apples ducks straw, quail.', + call_to_action: 'https://www.nwf.org/Educational-Resources/Wildlife-Guide/Mammals/American-Beaver', + }, + { + user_id: 2, + image: 'https://d27ucmmhxk51xv.cloudfront.net/media/english/illustration/panda.jpg', - camp_name: 'Pillows 4 Pandas', - camp_desc: 'Pets in. John Deere bees, parsley sweet corn at, porky pig shovels. Post pounder calf, hay or duck is, tool shed horse. Forage Harvester rakes peacocks, squeal garden woof. Peacocks baa ostriches owls. Hoot squeal moose quack, crows doggies frogs crickets chirp. Onion organic oranges and purr ducks canning owls at a squeal. Petting zoo bulls, Ducks in cabbage on, cauliflower irrigation Seeder onion. Ewes mushrooms zucchini in forage Harvester at sheep with tractor. Onion organic.', - camp_cta: 'https://live.adyen.com/hpp/pay.shtml', - }, - { - users_id: 3, - camp_img: + name: 'Pillows 4 Pandas', + description: 'Pets in. John Deere bees, parsley sweet corn at, porky pig shovels. Post pounder calf, hay or duck is, tool shed horse. Forage Harvester rakes peacocks, squeal garden woof. Peacocks baa ostriches owls. Hoot squeal moose quack, crows doggies frogs crickets chirp. Onion organic oranges and purr ducks canning owls at a squeal. Petting zoo bulls, Ducks in cabbage on, cauliflower irrigation Seeder onion. Ewes mushrooms zucchini in forage Harvester at sheep with tractor. Onion organic.', + call_to_action: 'https://live.adyen.com/hpp/pay.shtml', + }, + { + user_id: 3, + image: 'https://i.ebayimg.com/images/g/EPsAAOSwDmBY3~D1/s-l400.jpg', - camp_name: 'Plant more trees in park', - camp_desc: 'Mushrooms zucchini in forage Harvester at sheep with tractor. Post pounder calf, hay or duck is, tool shed horse. Prairie dogs raccoons robins rats. Gourds utters at welding equipment a oink oink haybine. Cauliflower a seeds quail. Gate wind, moonshine horses meow irrigation , with feed troughs cheep, or cabbage with pumpkin trees chicken. Mouse soybeans sweet corn hogs llamas or oink oink wind. John Deere bees, parsley sweet corn.', - camp_cta: 'https://giraffeconservation.org/donate/', - }, - ])); + name: 'Plant more trees in park', + description: 'Mushrooms zucchini in forage Harvester at sheep with tractor. Post pounder calf, hay or duck is, tool shed horse. Prairie dogs raccoons robins rats. Gourds utters at welding equipment a oink oink haybine. Cauliflower a seeds quail. Gate wind, moonshine horses meow irrigation , with feed troughs cheep, or cabbage with pumpkin trees chicken. Mouse soybeans sweet corn hogs llamas or oink oink wind. John Deere bees, parsley sweet corn.', + call_to_action: 'https://giraffeconservation.org/donate/', + }, + ])); }; From 34ad1f9e9a2427f5192a22118502612b86bbad9b Mon Sep 17 00:00:00 2001 From: ryanbuui <57205440+ryanbuui@users.noreply.github.com> Date: Sun, 26 Apr 2020 12:35:39 -0700 Subject: [PATCH 4/8] Added get/post routes for project_reviews API, Added total_reviews and total_stars to users tables, Modified get routes for contributors and users to include average_rating --- api/contributors/index.js | 7 ++++ api/index.js | 19 +++++------ api/reviews/index.js | 31 +++++++++++++++++ api/users/index.js | 8 +++-- database/migrations/20190225134140_users.js | 2 ++ .../20200208123841_skilled_impact_requests.js | 3 +- .../20200309131309_project_reviews.js | 15 ++++----- database/seeds/001-users.js | 6 ++++ database/seeds/004-supporters.js | 19 +++++++++++ database/seeds/005-skilled_impact_requests.js | 28 ++++++++++++++++ knexfile.js | 33 ++++++++++--------- models/projectReviewsModel.js | 15 +++++++++ 12 files changed, 149 insertions(+), 37 deletions(-) create mode 100644 api/reviews/index.js create mode 100644 database/seeds/004-supporters.js create mode 100644 database/seeds/005-skilled_impact_requests.js create mode 100644 models/projectReviewsModel.js diff --git a/api/contributors/index.js b/api/contributors/index.js index 777dbf95..be0f7307 100644 --- a/api/contributors/index.js +++ b/api/contributors/index.js @@ -46,6 +46,13 @@ router.get('/', restricted, async (req, res) => { `, { requestedSkills: skills }); const users = (await query).rows; + + var i; + for(i = 0; i < users.length; i++){ + users[i].average_rating = users[i].total_stars/users[i].total_reviews; + delete users[i].total_reviews; + delete users[i].total_stars; + } res.status(200).send(users); }); diff --git a/api/index.js b/api/index.js index a9ed67a0..10ed6e4e 100644 --- a/api/index.js +++ b/api/index.js @@ -8,20 +8,19 @@ const router = express.Router(); router.use('/users', require('./users')); // private routes -router.use('/campaigns', checkJwt, checkDeactivated, require('./campaigns')); -router.use('/updates', checkJwt, checkDeactivated, require('./updates')); -router.use('/comments', checkJwt, checkDeactivated, require('./comments')); -router.use('/social', checkJwt, checkDeactivated, require('./social')); -router.use('/airtable', checkJwt, checkDeactivated, require('./airtable')); -router.use('/maps', checkJwt, checkDeactivated, require('./maps')); -router.use('/reports', checkJwt, checkDeactivated, require('./reports')); +router.use('/campaigns', require('./campaigns')); +router.use('/updates', require('./updates')); +router.use('/comments', require('./comments')); +router.use('/social', require('./social')); +router.use('/airtable', require('./airtable')); +router.use('/maps', require('./maps')); +router.use('/reports', require('./reports')); router.use( '/contributors', - checkJwt, - checkDeactivated, require('./contributors'), ); -router.use('/submissions', checkJwt, checkDeactivated, require('./application_submissions')); +router.use('/submissions', require('./application_submissions')); +router.use('/reviews', require('./reviews')); // router.use('/connections', checkJwt, checkDeactivated, require('./connections')); diff --git a/api/reviews/index.js b/api/reviews/index.js new file mode 100644 index 00000000..67752a3a --- /dev/null +++ b/api/reviews/index.js @@ -0,0 +1,31 @@ +const express = require('express'); + +const router = express.Router(); + +const ProjectReviews = require('../../models/projectReviewsModel'); + +router.post('/', async (req, res) => { + try { + const [projectReview] = await ProjectReviews.insert(req.body); + res.status(201).json({ projectReview }); + } catch (error) { + console.log(error); + res.status(500).json({ error, message: 'Unable to add review' }); + } + }); + + router.get('/:id', async (req, res) => { + try { + const { id } = req.params; + const projectReviews = await ProjectReviews.findById(id); + if (projectReviews) { + res.status(200).json({ projectReviews, msg: "Review was found" }); + } else { + res.status(404).json({ message: 'Review not found in the database' }); + } + } catch (error) { + res.status(500).json({ error, message: 'Unable to make request to server', }); + } + }); + +module.exports = router; \ No newline at end of file diff --git a/api/users/index.js b/api/users/index.js index e59c6a97..b5a2aca7 100644 --- a/api/users/index.js +++ b/api/users/index.js @@ -30,7 +30,7 @@ router.get('/', restricted, async (req, res) => { } }); -router.get('/:id', restricted, async (req, res) => { +router.get('/:id'/*, restricted*/, async (req, res) => { const { id } = req.params; try { @@ -53,7 +53,7 @@ router.get('/:id', restricted, async (req, res) => { return res.status(401).json({ message: 'This account has been deactivated' }); } } - + user["average_rating"] = user.total_stars/user.total_reviews; return res.status(200).json({ user, message: 'The user was found' }); } catch (err) { log.error(err); @@ -78,6 +78,10 @@ router.get('/sub/:sub', restricted, async (req, res) => { } return res.status(401).json({ message: 'This account has been deactivated' }); } + + user.average_rating = user.total_stars/user.total_reviews; + delete user.total_stars; + delete user.total_reviews; return res.status(200).json({ user, message: 'The user was found' }); } return res.status(404).json({ message: 'User not found in the database' }); diff --git a/database/migrations/20190225134140_users.js b/database/migrations/20190225134140_users.js index 9ef2a73e..79f8b0a1 100644 --- a/database/migrations/20190225134140_users.js +++ b/database/migrations/20190225134140_users.js @@ -21,6 +21,8 @@ exports.up = function (knex, Promise) { tbl.string('instagram', 150); tbl.string('phone_number', 50); tbl.string('roles', 50).notNullable(); + tbl.integer('total_stars'); + tbl.integer('total_reviews'); }); }; diff --git a/database/migrations/20200208123841_skilled_impact_requests.js b/database/migrations/20200208123841_skilled_impact_requests.js index fab181a4..7e88c786 100644 --- a/database/migrations/20200208123841_skilled_impact_requests.js +++ b/database/migrations/20200208123841_skilled_impact_requests.js @@ -5,7 +5,8 @@ exports.up = function (knex) { .references('campaigns.camp_id') .onDelete('CASCADE') .onUpdate('CASCADE'); - table.enum('skill', null, { useNative: true, enumName: 'enum_skills', existingType: true }).notNullable(); + table.enum('skill', null, { useNative: true, enumName: 'enum_skills', existingType: true }) + .notNullable(); table.text('point_of_contact').notNullable(); table.text('welcome_message').notNullable(); table.text('our_contribution').notNullable(); diff --git a/database/migrations/20200309131309_project_reviews.js b/database/migrations/20200309131309_project_reviews.js index bb7f544d..18498b6f 100644 --- a/database/migrations/20200309131309_project_reviews.js +++ b/database/migrations/20200309131309_project_reviews.js @@ -2,21 +2,20 @@ exports.up = function(knex) { return knex.schema.createTable('project_reviews', table => { table.increments('id').notNullable().unique(); - table.integer("recommendation").notNullable(); - table.integer("communication").notNullable(); - table.integer("experience").notNullable(); - table.text("review_description").notNullable(); - table.integer("supporter_id") - .notNullable() + table.text("review").notNullable(); + table.integer("likely_to_recommend").notNullable(); + table.integer("supporter_communication").notNullable(); + table.integer("provided_value").notNullable(); + table.integer("supporter_user_id") .references("supporters.sup_id") .onDelete("CASCADE") .onUpdate("CASCADE"); - table.integer("conservationist_id") + table.integer("conservationist_user_id") .notNullable() .references("conservationists.cons_id") .onDelete("CASCADE") .onUpdate("CASCADE"); - table.integer("skilled_impact_request_id").notNullable() + table.integer("skilled_impact_request_id") .references("skilled_impact_requests.id") .onDelete("CASCADE") .onUpdate("CASCADE"); diff --git a/database/seeds/001-users.js b/database/seeds/001-users.js index 777e16d5..405472da 100644 --- a/database/seeds/001-users.js +++ b/database/seeds/001-users.js @@ -8,6 +8,8 @@ exports.seed = function (knex, Promise) { location: 'Pittsburg, PA', species_and_habitats: 'beavers in da dam', roles: 'conservationist', + total_stars: 5, + total_reviews: 10, }, { sub: '897234923', @@ -15,6 +17,8 @@ exports.seed = function (knex, Promise) { location: 'Phoenix, AZ', species_and_habitats: 'pandas sleep on cloud 9', roles: 'conservationist', + total_stars: 20, + total_reviews: 10, }, { sub: '907598347592345238457934', @@ -22,6 +26,8 @@ exports.seed = function (knex, Promise) { location: 'Paris, France', species_and_habitats: 'the air up there', roles: 'conservationist', + total_stars: 30, + total_reviews: 10, }, ])); }; diff --git a/database/seeds/004-supporters.js b/database/seeds/004-supporters.js new file mode 100644 index 00000000..52f16990 --- /dev/null +++ b/database/seeds/004-supporters.js @@ -0,0 +1,19 @@ + +exports.seed = function (knex, Promise) { + return knex('supporters') + .del() + .then(() => knex('supporters').insert([ + { + name: 'Albert', + user_id: 1, + }, + { + name: 'Frank', + user_id: 2, + }, + { + name: 'Jessica', + user_id: 3, + }, + ])); +}; diff --git a/database/seeds/005-skilled_impact_requests.js b/database/seeds/005-skilled_impact_requests.js new file mode 100644 index 00000000..1c7ef85c --- /dev/null +++ b/database/seeds/005-skilled_impact_requests.js @@ -0,0 +1,28 @@ + +exports.seed = function (knex, Promise) { + return knex('skilled_impact_requests') + .del() + .then(() => knex('skilled_impact_requests').insert([ + { + campaign_id: 1, + skill: "WEB_DEVELOPMENT", + point_of_contact: 'Alby Prude', + welcome_message: 'Welcome!', + our_contribution: 'We do things for the environment!', + }, + { + campaign_id: 1, + skill: "WEB_DEVELOPMENT", + point_of_contact: 'Alby Prude', + welcome_message: 'Welcome!', + our_contribution: 'We do things for the environment!', + }, + { + campaign_id: 1, + skill: "WEB_DEVELOPMENT", + point_of_contact: 'Alby Prude', + welcome_message: 'Welcome!', + our_contribution: 'We do things for the environment!', + }, + ])); +}; diff --git a/knexfile.js b/knexfile.js index 6c57951f..50ef7aef 100644 --- a/knexfile.js +++ b/knexfile.js @@ -2,14 +2,14 @@ require('dotenv').config(); // Postgres imports const pg = require('pg'); - -pg.defaults.ssl = true; +pg.defaults.ssl = false; const localPgConnection = { - host: 'localhost', + host: '192.168.99.100', + port: '5432', + password: '', user: 'postgres', - password: 'key', - database: 'keylocal', + database: 'postgres' }; // Production database connection @@ -27,16 +27,16 @@ module.exports = { connection: dbConnection, migrations: { directory: './database/migrations', - tablename: 'knex_migrations', + tablename: 'knex_migrations' }, seeds: { - directory: './database/seeds', + directory: './database/seeds' }, pool: { min: 2, - max: 10, + max: 10 }, - useNullAsDefault: true, + useNullAsDefault: true }, testing: { @@ -45,11 +45,12 @@ module.exports = { useNullAsDefault: true, migrations: { directory: './database/migrations', - tablename: 'knex_migrations', + tablename: 'knex_migrations' }, seeds: { - directory: './database/seeds', + directory: './database/seeds' }, + useNullAsDefault: true }, production: { @@ -57,15 +58,15 @@ module.exports = { connection: dbConnection, pool: { min: 2, - max: 10, + max: 10 }, migrations: { directory: './database/migrations', - tableName: 'knex_migrations', + tableName: 'knex_migrations' }, seeds: { - directory: './database/seeds', + directory: './database/seeds' }, - useNullAsDefault: true, - }, + useNullAsDefault: true + } }; diff --git a/models/projectReviewsModel.js b/models/projectReviewsModel.js new file mode 100644 index 00000000..6fe9162a --- /dev/null +++ b/models/projectReviewsModel.js @@ -0,0 +1,15 @@ +const db = require('../database/dbConfig'); + +async function insert(review) { + return db('project_reviews') + .insert(review) + .returning('*'); +} + +async function findById(id) { + return db('project_reviews') + .where({ id }) + .first(); +} + +module.exports = { insert, findById }; \ No newline at end of file From c1e7a2869f50e2b3cce161271344182f285421c8 Mon Sep 17 00:00:00 2001 From: ryanbuui <57205440+ryanbuui@users.noreply.github.com> Date: Tue, 5 May 2020 14:13:16 -0700 Subject: [PATCH 5/8] Reorganized files to match master's, added checks for ratings, implemented requested changes and revisions --- api/reviews/index.js | 31 -------------- app/api/contributors/index.js | 8 ++-- app/api/reviews/index.js | 42 +++++++++++++++++++ app/api/users/index.js | 8 ++-- .../20200309131309_project_reviews.js | 0 app/database/models/projectReviewsModel.js | 15 +++++++ database/seeds/001-users.js | 33 --------------- database/seeds/002-conservations.js | 33 --------------- database/seeds/004-supporters.js | 19 --------- database/seeds/005-skilled_impact_requests.js | 28 ------------- knexfile.js | 4 +- models/projectReviewsModel.js | 15 ------- 12 files changed, 67 insertions(+), 169 deletions(-) delete mode 100644 api/reviews/index.js create mode 100644 app/api/reviews/index.js rename {database => app/database}/migrations/20200309131309_project_reviews.js (100%) create mode 100644 app/database/models/projectReviewsModel.js delete mode 100644 database/seeds/001-users.js delete mode 100644 database/seeds/002-conservations.js delete mode 100644 database/seeds/004-supporters.js delete mode 100644 database/seeds/005-skilled_impact_requests.js delete mode 100644 models/projectReviewsModel.js diff --git a/api/reviews/index.js b/api/reviews/index.js deleted file mode 100644 index 67752a3a..00000000 --- a/api/reviews/index.js +++ /dev/null @@ -1,31 +0,0 @@ -const express = require('express'); - -const router = express.Router(); - -const ProjectReviews = require('../../models/projectReviewsModel'); - -router.post('/', async (req, res) => { - try { - const [projectReview] = await ProjectReviews.insert(req.body); - res.status(201).json({ projectReview }); - } catch (error) { - console.log(error); - res.status(500).json({ error, message: 'Unable to add review' }); - } - }); - - router.get('/:id', async (req, res) => { - try { - const { id } = req.params; - const projectReviews = await ProjectReviews.findById(id); - if (projectReviews) { - res.status(200).json({ projectReviews, msg: "Review was found" }); - } else { - res.status(404).json({ message: 'Review not found in the database' }); - } - } catch (error) { - res.status(500).json({ error, message: 'Unable to make request to server', }); - } - }); - -module.exports = router; \ No newline at end of file diff --git a/app/api/contributors/index.js b/app/api/contributors/index.js index 348fc49a..8462f8ef 100644 --- a/app/api/contributors/index.js +++ b/app/api/contributors/index.js @@ -46,10 +46,10 @@ router.get('/', restricted, async (req, res) => { `, { requestedSkills: skills }); const users = (await query).rows; - - var i; - for(i = 0; i < users.length; i++){ - users[i].average_rating = users[i].total_stars/users[i].total_reviews; + + let i; + for (i = 0; i < users.length; i += 1) { + users[i].average_rating = users[i].total_stars / users[i].total_reviews; delete users[i].total_reviews; delete users[i].total_stars; } diff --git a/app/api/reviews/index.js b/app/api/reviews/index.js new file mode 100644 index 00000000..86d1848c --- /dev/null +++ b/app/api/reviews/index.js @@ -0,0 +1,42 @@ +const express = require('express'); + +const router = express.Router(); + +const ProjectReviews = require('../../database/models/projectReviewsModel'); + +router.post('/', async (req, res) => { + try { + const reviews = [req.body.likely_to_recommend, req.body.supporter_communication, req.body.provided_value]; + let correctBool = true; + for (let i = 0; i < reviews.length; i += 1) { + if (!(reviews[i].isInteger() && reviews[i] >= 1 && reviews[i] <= 5)) { + correctBool = false; + break; + } + } + if (correctBool) { + const projectReview = await ProjectReviews.insert(req.body); + res.status(201).json({ projectReview }); + } else { + res.status(400).json({ message: 'Incorrect input for ratings' }); + } + } catch (error) { + res.status(500).json({ error, message: 'Unable to add review' }); + } +}); + +router.get('/:id', async (req, res) => { + try { + const { id } = req.params; + const projectReview = await ProjectReviews.findById(id); + if (projectReview) { + res.status(200).json({ projectReview, msg: 'Review was found' }); + } else { + res.status(404).json({ message: 'Review not found in the database' }); + } + } catch (error) { + res.status(500).json({ error, message: 'Unable to make request to server' }); + } +}); + +module.exports = router; diff --git a/app/api/users/index.js b/app/api/users/index.js index ae980393..34cc2c23 100644 --- a/app/api/users/index.js +++ b/app/api/users/index.js @@ -31,7 +31,7 @@ router.get('/', restricted, async (req, res) => { } }); -router.get('/:id'/*, restricted*/, async (req, res) => { +router.get('/:id'/* , restricted */, async (req, res) => { const { id } = req.params; try { @@ -58,7 +58,7 @@ router.get('/:id'/*, restricted*/, async (req, res) => { .json({ message: 'This account has been deactivated' }); } } - user["average_rating"] = user.total_stars/user.total_reviews; + user.average_rating = user.total_stars / user.total_reviews; return res.status(200).json({ user, message: 'The user was found' }); } catch (err) { log.error(err); @@ -85,8 +85,8 @@ router.get('/sub/:sub', restricted, async (req, res) => { .status(401) .json({ message: 'This account has been deactivated' }); } - - user.average_rating = user.total_stars/user.total_reviews; + + user.average_rating = user.total_stars / user.total_reviews; delete user.total_stars; delete user.total_reviews; return res.status(200).json({ user, message: 'The user was found' }); diff --git a/database/migrations/20200309131309_project_reviews.js b/app/database/migrations/20200309131309_project_reviews.js similarity index 100% rename from database/migrations/20200309131309_project_reviews.js rename to app/database/migrations/20200309131309_project_reviews.js diff --git a/app/database/models/projectReviewsModel.js b/app/database/models/projectReviewsModel.js new file mode 100644 index 00000000..f6438ccc --- /dev/null +++ b/app/database/models/projectReviewsModel.js @@ -0,0 +1,15 @@ +const db = require('../dbConfig'); + +async function insert(review) { + return db('project_reviews') + .insert(review) + .returning('*'); +} + +async function findById(id) { + return db('project_reviews') + .where({ id }) + .first(); +} + +module.exports = { insert, findById }; diff --git a/database/seeds/001-users.js b/database/seeds/001-users.js deleted file mode 100644 index 405472da..00000000 --- a/database/seeds/001-users.js +++ /dev/null @@ -1,33 +0,0 @@ -exports.seed = function (knex, Promise) { - return knex('users') - .del() - .then(() => knex('users').insert([ - { - sub: '123456789', - email: 'info@beavertown.org', - location: 'Pittsburg, PA', - species_and_habitats: 'beavers in da dam', - roles: 'conservationist', - total_stars: 5, - total_reviews: 10, - }, - { - sub: '897234923', - email: 'pandalover234@hotmail.com', - location: 'Phoenix, AZ', - species_and_habitats: 'pandas sleep on cloud 9', - roles: 'conservationist', - total_stars: 20, - total_reviews: 10, - }, - { - sub: '907598347592345238457934', - email: 'giraffe2tall@gmail.com', - location: 'Paris, France', - species_and_habitats: 'the air up there', - roles: 'conservationist', - total_stars: 30, - total_reviews: 10, - }, - ])); -}; diff --git a/database/seeds/002-conservations.js b/database/seeds/002-conservations.js deleted file mode 100644 index 24fb33a6..00000000 --- a/database/seeds/002-conservations.js +++ /dev/null @@ -1,33 +0,0 @@ -exports.seed = function (knex, Promise) { - return knex('conservationists') - .del() - .then(() => knex('conservationists').insert([ - { - user_id: 1, - name: 'Beaver Town', - link_url: 'google.com', - link_text: 'google.com', - about_us: 'Lorem IpsOn', - issues: 'wood', - support_us: null, - }, - { - user_id: 2, - name: 'Panda Lovers', - link_url: 'google.com', - link_text: 'google.com', - about_us: 'Lorem IpsOn', - issues: 'sleep', - support_us: null, - }, - { - user_id: 3, - name: 'Giraffe 2 Tall', - link_url: 'google.com', - link_text: 'google.com', - about_us: 'Lorem IpsOn', - issues: 'too tall', - support_us: null, - }, - ])); -}; diff --git a/database/seeds/004-supporters.js b/database/seeds/004-supporters.js deleted file mode 100644 index 52f16990..00000000 --- a/database/seeds/004-supporters.js +++ /dev/null @@ -1,19 +0,0 @@ - -exports.seed = function (knex, Promise) { - return knex('supporters') - .del() - .then(() => knex('supporters').insert([ - { - name: 'Albert', - user_id: 1, - }, - { - name: 'Frank', - user_id: 2, - }, - { - name: 'Jessica', - user_id: 3, - }, - ])); -}; diff --git a/database/seeds/005-skilled_impact_requests.js b/database/seeds/005-skilled_impact_requests.js deleted file mode 100644 index 1c7ef85c..00000000 --- a/database/seeds/005-skilled_impact_requests.js +++ /dev/null @@ -1,28 +0,0 @@ - -exports.seed = function (knex, Promise) { - return knex('skilled_impact_requests') - .del() - .then(() => knex('skilled_impact_requests').insert([ - { - campaign_id: 1, - skill: "WEB_DEVELOPMENT", - point_of_contact: 'Alby Prude', - welcome_message: 'Welcome!', - our_contribution: 'We do things for the environment!', - }, - { - campaign_id: 1, - skill: "WEB_DEVELOPMENT", - point_of_contact: 'Alby Prude', - welcome_message: 'Welcome!', - our_contribution: 'We do things for the environment!', - }, - { - campaign_id: 1, - skill: "WEB_DEVELOPMENT", - point_of_contact: 'Alby Prude', - welcome_message: 'Welcome!', - our_contribution: 'We do things for the environment!', - }, - ])); -}; diff --git a/knexfile.js b/knexfile.js index 8716de83..bcaa18d5 100644 --- a/knexfile.js +++ b/knexfile.js @@ -24,9 +24,9 @@ module.exports = { }, pool: { min: 2, - max: 10 + max: 10, }, - useNullAsDefault: true + useNullAsDefault: true, }, production: { diff --git a/models/projectReviewsModel.js b/models/projectReviewsModel.js deleted file mode 100644 index 6fe9162a..00000000 --- a/models/projectReviewsModel.js +++ /dev/null @@ -1,15 +0,0 @@ -const db = require('../database/dbConfig'); - -async function insert(review) { - return db('project_reviews') - .insert(review) - .returning('*'); -} - -async function findById(id) { - return db('project_reviews') - .where({ id }) - .first(); -} - -module.exports = { insert, findById }; \ No newline at end of file From f36de1c52996f944d0bafb8549cad88ab499fcbd Mon Sep 17 00:00:00 2001 From: ryanbuui <57205440+ryanbuui@users.noreply.github.com> Date: Tue, 5 May 2020 16:37:01 -0700 Subject: [PATCH 6/8] Added users_review migration, also deleted total_reviews/total_stars from users table --- app/database/migrations/20190225134140_users.js | 2 -- .../migrations/20200505163451_users_reviews.js | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 app/database/migrations/20200505163451_users_reviews.js diff --git a/app/database/migrations/20190225134140_users.js b/app/database/migrations/20190225134140_users.js index 79f8b0a1..9ef2a73e 100644 --- a/app/database/migrations/20190225134140_users.js +++ b/app/database/migrations/20190225134140_users.js @@ -21,8 +21,6 @@ exports.up = function (knex, Promise) { tbl.string('instagram', 150); tbl.string('phone_number', 50); tbl.string('roles', 50).notNullable(); - tbl.integer('total_stars'); - tbl.integer('total_reviews'); }); }; diff --git a/app/database/migrations/20200505163451_users_reviews.js b/app/database/migrations/20200505163451_users_reviews.js new file mode 100644 index 00000000..06f876f5 --- /dev/null +++ b/app/database/migrations/20200505163451_users_reviews.js @@ -0,0 +1,14 @@ + +exports.up = function (knex) { + return knex.schema.alterTable('users', (tbl) => { + tbl.integer('total_stars'); + tbl.integer('total_reviews'); + }); + }; + + exports.down = function (knex) { + return knex.schema.alterTable('users', (tbl) => { + tbl.dropColumn('total_stars'); + tbl.dropColumn('total_reviews'); + }); + }; From 78b82d1a4608df857b494ab9dfd171a1b949d4ef Mon Sep 17 00:00:00 2001 From: ryanbuui <57205440+ryanbuui@users.noreply.github.com> Date: Tue, 26 May 2020 12:33:25 -0700 Subject: [PATCH 7/8] Made revisions to code to make cleaner and more efficient --- app/api/reviews/index.js | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/app/api/reviews/index.js b/app/api/reviews/index.js index 86d1848c..5fec8250 100644 --- a/app/api/reviews/index.js +++ b/app/api/reviews/index.js @@ -7,19 +7,10 @@ const ProjectReviews = require('../../database/models/projectReviewsModel'); router.post('/', async (req, res) => { try { const reviews = [req.body.likely_to_recommend, req.body.supporter_communication, req.body.provided_value]; - let correctBool = true; - for (let i = 0; i < reviews.length; i += 1) { - if (!(reviews[i].isInteger() && reviews[i] >= 1 && reviews[i] <= 5)) { - correctBool = false; - break; - } - } - if (correctBool) { - const projectReview = await ProjectReviews.insert(req.body); - res.status(201).json({ projectReview }); - } else { - res.status(400).json({ message: 'Incorrect input for ratings' }); - } + const validMetrics = reviews.filter((r) => !r.isInteger() || r < 1 || r > 5).length === 0; + if (!constantBool) return res.status(400).json({ message: "Incorrect input for review" }); + const [projectReview] = await ProjectReviews.insert(req.body); + return res.status(201).json({ projectReview }); } catch (error) { res.status(500).json({ error, message: 'Unable to add review' }); } @@ -29,11 +20,8 @@ router.get('/:id', async (req, res) => { try { const { id } = req.params; const projectReview = await ProjectReviews.findById(id); - if (projectReview) { - res.status(200).json({ projectReview, msg: 'Review was found' }); - } else { - res.status(404).json({ message: 'Review not found in the database' }); - } + if (!projectReview) return res.status(404).json({ message: 'Review not found in the database' }); + return res.status(200).json({ projectReview, msg: 'Review was found' }); } catch (error) { res.status(500).json({ error, message: 'Unable to make request to server' }); } From 7550056c0e04390cce1c9e624adbd4c4274057a2 Mon Sep 17 00:00:00 2001 From: ryanbuui <57205440+ryanbuui@users.noreply.github.com> Date: Tue, 26 May 2020 14:16:08 -0700 Subject: [PATCH 8/8] Remove bool --- app/api/reviews/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/api/reviews/index.js b/app/api/reviews/index.js index 5fec8250..e5ed8e75 100644 --- a/app/api/reviews/index.js +++ b/app/api/reviews/index.js @@ -8,7 +8,7 @@ router.post('/', async (req, res) => { try { const reviews = [req.body.likely_to_recommend, req.body.supporter_communication, req.body.provided_value]; const validMetrics = reviews.filter((r) => !r.isInteger() || r < 1 || r > 5).length === 0; - if (!constantBool) return res.status(400).json({ message: "Incorrect input for review" }); + if (!validMetrics) return res.status(400).json({ message: "Incorrect input for review" }); const [projectReview] = await ProjectReviews.insert(req.body); return res.status(201).json({ projectReview }); } catch (error) {