Skip to content

Added project_reviews migration#181

Open
ryanbuui wants to merge 11 commits intomasterfrom
project_reviews
Open

Added project_reviews migration#181
ryanbuui wants to merge 11 commits intomasterfrom
project_reviews

Conversation

@ryanbuui
Copy link

@ryanbuui ryanbuui commented Mar 9, 2020

Description

Created a migration table called project_reviews containing:

  • id
  • 3 integers ("recommendation", "communication", "experience")
  • supporter id
  • conservationist id
  • campaign id

Closes #182.

@sumeet-bansal sumeet-bansal changed the base branch from master to development April 8, 2020 04:10
@sumeet-bansal sumeet-bansal changed the base branch from development to master April 25, 2020 19:33
…d total_stars to users tables, Modified get routes for contributors and users to include average_rating
@typokign typokign self-requested a review April 30, 2020 04:42
Copy link
Contributor

@typokign typokign left a comment

Choose a reason for hiding this comment

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

Great! Very clean, just a few suggestions below. Also, please run the linter, npm run lint:fix should auto-fix most lint issues for you.

Comment on lines 50 to 51
var i;
for(i = 0; i < users.length; i++){
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: There's no need to define i before the loop, and we prefer let/const to var. Try: for (let i = 0; i < users.length; i++) {

Comment on lines 24 to 25
tbl.integer('total_stars');
tbl.integer('total_reviews');
Copy link
Contributor

Choose a reason for hiding this comment

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

issue: We can't edit existing migrations, they're designed to only run once. Create a new migration with npx knex migrate:make and add these fields in there.

Comment on lines +8 to +9
table.enum('skill', null, { useNative: true, enumName: 'enum_skills', existingType: true })
.notNullable();
Copy link
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.

const users = (await query).rows;

let i;
for (i = 0; i < users.length; i += 1) {
Copy link
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++)

});

router.get('/:id', restricted, async (req, res) => {
router.get('/:id'/* , restricted */, async (req, res) => {
Copy link
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?

Comment on lines 10 to 16
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;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

You can simplify this a bit by using some functional programming concepts: map and reduce; map the reviews array to an array of booleans (true if the element is an integer between 1 and 5 inclusive) and reduce it using the and operator. Or maybe you could filter the reviews array by if the elements are not integers between 1 and 5 inclusive and then check length. No loop needed and you can make correctBool a constant.

const validMetrics = reviews
  .map((r) => r.isInteger() && r >= 1 && r <= 5)
  .reduce((a,r) => a && r);

const validMetrics = reviews.filter((r) => !r.isInteger() || r < 1 || r > 5).length === 0;

Comment on lines 17 to 22
if (correctBool) {
const projectReview = await ProjectReviews.insert(req.body);
res.status(201).json({ projectReview });
} else {
res.status(400).json({ message: 'Incorrect input for ratings' });
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Use a guard clause here.

if (!constantBool) return res.status(400).json({ message });

const projectReview = await ProjectReviews.insert(req.body);
return res.status(201).json({ projectReview });

Comment on lines 32 to 36
if (projectReview) {
res.status(200).json({ projectReview, msg: 'Review was found' });
} else {
res.status(404).json({ message: 'Review not found in the database' });
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Guard clause.

}
}
if (correctBool) {
const projectReview = await ProjectReviews.insert(req.body);
Copy link
Contributor

@sumeet-bansal sumeet-bansal May 5, 2020

Choose a reason for hiding this comment

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

This currently returns an array of inserted objects, even if you only pass in one parameter. You can use array destructuring to easily get the first element:

const [projectReview] = await ProjectReviews.insert(req.body);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

implement backend for skilled impact project reviews

3 participants