-
-
Notifications
You must be signed in to change notification settings - Fork 72
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
Venkat knex #69
Conversation
There was a problem hiding this 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.
backend/api/CourseApi/index.js
Outdated
|
||
var amountOfAllCourses; | ||
var subquery = knex('problem').count('id').whereRaw('problem.course_id = course.id'); |
There was a problem hiding this comment.
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..
There was a problem hiding this comment.
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.
backend/api/CourseApi/index.js
Outdated
.where('if_public', 'true') | ||
.andWhere(2, "<=", subquery); | ||
} | ||
|
There was a problem hiding this comment.
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;
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Done. |
backend/api/CourseApi/index.js
Outdated
amountofAllCoursesQuery = amountofAllCoursesQuery.andWhere('course_category_id', courseCategoryId); | ||
} | ||
|
||
const amountOfAllCourses = await amountofAllCoursesQuery; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Thanks :)
Knex style queries.