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
7 changes: 7 additions & 0 deletions app/api/contributors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ router.get('/', restricted, async (req, res) => {

const users = (await query).rows;

let i;
for (i = 0; i < users.length; i += 1) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move the let into the loop body here? Otherwise it doesn't really help with scoping.

for (let 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);
});

Expand Down
30 changes: 30 additions & 0 deletions app/api/reviews/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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];
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' });
}
});

router.get('/:id', async (req, res) => {
try {
const { id } = req.params;
const projectReview = await ProjectReviews.findById(id);
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' });
}
});

module.exports = router;
8 changes: 6 additions & 2 deletions app/api/users/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ router.get('/', restricted, async (req, res) => {
}
});

router.get('/:id', restricted, async (req, res) => {
router.get('/:id'/* , restricted */, async (req, res) => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you uncomment this please?

const { id } = req.params;

try {
Expand All @@ -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;
return res.status(200).json({ user, message: 'The user was found' });
} catch (err) {
log.error(err);
Expand All @@ -85,6 +85,10 @@ 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;
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' });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Comment on lines +8 to +9
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Nothing was really changed here but can you please revert? Nice to keep the git history clean on migrations.

table.text('point_of_contact').notNullable();
table.text('welcome_message').notNullable();
table.text('our_contribution').notNullable();
Expand Down
27 changes: 27 additions & 0 deletions app/database/migrations/20200309131309_project_reviews.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

exports.up = function(knex) {
return knex.schema.createTable('project_reviews', table => {
table.increments('id').notNullable().unique();
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_user_id")
.notNullable()
.references("conservationists.cons_id")
.onDelete("CASCADE")
.onUpdate("CASCADE");
table.integer("skilled_impact_request_id")
.references("skilled_impact_requests.id")
.onDelete("CASCADE")
.onUpdate("CASCADE");
});
};

exports.down = function(knex) {
return knex.schema.dropTableIfExists('project_reviews');
};
14 changes: 14 additions & 0 deletions app/database/migrations/20200505163451_users_reviews.js
Original file line number Diff line number Diff line change
@@ -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');
});
};
15 changes: 15 additions & 0 deletions app/database/models/projectReviewsModel.js
Original file line number Diff line number Diff line change
@@ -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 };