Skip to content

Commit

Permalink
Seperate function
Browse files Browse the repository at this point in the history
  • Loading branch information
dungwinux committed Mar 7, 2019
1 parent 18fb521 commit 349a874
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/data/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,22 @@ async function updateUserPassword(user_id, old_pass, new_pass) {
/**
* Count number of submissions
*/
function countSubmissions(user_id) {
let query = {};
if (user_id) query = { user_id };
function countAllSubmissions() {
return new Promise((resolve, reject) => {
db.submissions.count(query, function(err, count) {
db.submissions.count({}, function(err, count) {
if (err) reject(err);
else resolve(count);
});
});
}

/**
* Count number of submissions from user_id
* @param {String} user_id User's ID
*/
function countUserSubmissions(user_id) {
return new Promise((resolve, reject) => {
db.submissions.count({ user_id }, function(err, count) {
if (err) reject(err);
else resolve(count);
});
Expand All @@ -274,7 +285,7 @@ function verifySubsQuery(page, size, count, maxSize) {
* @returns {Promise<Array<ReturnSubmission>>} Array of submission if success
*/
async function readAllSubmissions(page, size, count) {
const maxSize = await countSubmissions();
const maxSize = await countAllSubmissions();
({ page, size, count } = verifySubsQuery(page, size, count, maxSize));

return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -365,7 +376,7 @@ function readSubmission(sub_id) {
*/
async function readUserSubmission(user_id, page, size, count) {
const userData = await readUserByID(user_id);
const maxSize = await countSubmissions(user_id);
const maxSize = await countUserSubmissions(user_id);
({ page, size, count } = verifySubsQuery(page, size, count, maxSize));

return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -563,6 +574,8 @@ module.exports = {
readUserPassHash,
updateUserName,
updateUserPassword,
countAllSubmissions,
countUserSubmissions,
readAllSubmissions,
readSubmission,
readUserSubmission,
Expand Down

0 comments on commit 349a874

Please sign in to comment.