Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Venkat knex #69

Merged
merged 4 commits into from
Apr 3, 2020
Merged

Venkat knex #69

merged 4 commits into from
Apr 3, 2020

Conversation

anandvenkat4
Copy link
Contributor

Knex style queries.

Copy link
Owner

@lakesare lakesare left a comment

Choose a reason for hiding this comment

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

That's exactly what we want 👍
Don't forget to delete the CourseModel.select.countAllPublic method as we no longer need it.


var amountOfAllCourses;
var subquery = knex('problem').count('id').whereRaw('problem.course_id = course.id');
Copy link
Owner

Choose a reason for hiding this comment

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

It's ES6, let's use const for all vars that do not change, and let for all vars that do get reassigned.
Fyi, - const and let are preferable, because they let us have {} scope instead of function(){} scope, - and that lets us use them easily within loops e.g..

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I knew I had to change this, but sent it for the functional check. Thats a good learning.

.where('if_public', 'true')
.andWhere(2, "<=", subquery);
}

Copy link
Owner

Choose a reason for hiding this comment

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

Looks nice!
We can improve it by factoring out this piece of code:

knex('course').count('id', {as: 'amount_of_public_courses'})
    .where('if_public', 'true')
    .andWhere(2, "<=", subquery); 

E.g. we can do (please check if this actually works though :-)):

const courseCategoryId = request.query.courseCategoryId;
let amountPromise = knex('course').count('id', {as: 'amount_of_public_courses'})
    .where('if_public', 'true')
    .andWhere(2, "<=", subquery); 

if (courseCategoryId) {
  amountPromise = amountPromise.andWhere('course_category_id', courseCategoryId)
}

const amountOfAllCourses = await amountPromise;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will check and revert. Also, will change the 'allPublic' so that we can completely get rid of older syntax and then will commit the changes. Thank you for reviewing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is done in the new commit.

@anandvenkat4
Copy link
Contributor Author

That's exactly what we want 👍
Don't forget to delete the CourseModel.select.countAllPublic method as we no longer need it.

Done.

amountofAllCoursesQuery = amountofAllCoursesQuery.andWhere('course_category_id', courseCategoryId);
}

const amountOfAllCourses = await amountofAllCoursesQuery;
Copy link
Owner

Choose a reason for hiding this comment

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

Does this actually work locally? Do we not need to access 'amount_of_public_courses' somehow?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, my bad. I did not push the latest change. Done now. Please check.

@@ -48,7 +49,8 @@ router.get('/public', catchAsync(async (request, response) => {
amountofAllCoursesQuery = amountofAllCoursesQuery.andWhere('course_category_id', courseCategoryId);
}

const amountOfAllCourses = await amountofAllCoursesQuery;
let amountOfAllCourses = await amountofAllCoursesQuery;
amountOfAllCourses = amountOfAllCourses[0].amountOfPublicCourses;
Copy link
Owner

Choose a reason for hiding this comment

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

👍Lgtm
I'll merge this now, but for the future, - we can do const amountOfAllCourses = (await amountofAllCoursesQuery)[0].amountOfPublicCourses; whenever it looks better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure. Thanks :)

@lakesare lakesare merged commit 97743c4 into lakesare:master Apr 3, 2020
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.

2 participants