maria maria - 3 months ago 25
Node.js Question

PHP convert functions to MongoDB

Im trying to convert some of my PHP functions to mongodb, but cant figure it out, could someone help me out?

PHP function applications:

function applications($gangId) {
$applications = $this->db->query("SELECT * FROM `gang_applications` where `status`='avaliable' and `gangid`='$gangId'");
return ($applications ? $applications : false);
}


my attempt on function applications:

gangshema.methods.applications(thisid) {
// some sort of callback?
db.gang_applications.find({status:avaliable, gangid: thisid}, function(err, cursor) {

if (cursor != 0) {
console.log(cursor);
}

});
}


PHP function application_done

function application_done($applicationId) {
$applications = $this->db->query("SELECT * FROM `gang_applications` where `id`='$applicationId'")->row();

return ($applications->status == 'avaliable' ? false : true);
}


my attempt on function application_done

gangshema.methods.application_done(applicationid) {
db.gang_applications.find({id:applicationid}, function(err,cursor) {
// return ($applications->status == 'avaliable' ? false : true);
});
}


but my main consern is a function called accept_applications. I have no clue on how to do this part, including calling other functions for their response.

function accept_application($userid,$applicationId) {
$box = 'failure';

if (empty($applicationId)) {
$message = "applicationId is empty";
} elseif ($this->application_done($applicationId)) {
$message = "Already registred!";

} else {
$application = $this->getApplication($applicationId);
$test = true;
if(!($test)) {
$message = "false test";
} else {
$this->db->query("UPDATE `gang_applications` SET `status`='accepted', `by`='$userid' where `id`='$applicationId'");
$this->gangs->add_member($application->userid,'gang','member',$application->gangid);
$message = "Accept!";
}

}
return $message;
}

Answer

Using this at the beginning of the Node.js script:

var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://localhost:27017/test';

First snippet:

function applications($gangId) {
    $applications = $this->db->query("SELECT * FROM `gang_applications` where `status`='avaliable' and `gangid`='$gangId'");
    return ($applications ? $applications : false);
}

becomes:

var findApplications = function(db, gangId) {
   var cursor = db.collection('gang_application').find({
     "status": "available" 
     "gangId": gangId
   });
   var results = [];
   cursor.each(function(err, doc) {
      if (err) console.log("Error: " + err);
      } else if (doc != null) {
         console.log("Null document.");
      } else {
         results.push(doc);
      }
   });
   return results;
};

Second snippet:

function application_done($applicationId) {
    $applications = $this->db->query("SELECT * FROM `gang_applications` where `id`='$applicationId'")->row();

    return ($applications->status == 'avaliable' ? false : true);
}

becomes:

function applications(gangId) {
  db.gang_application
}
var applicationsDone = function(db, applicationId) {
   var cursor = db.collection('gang_application').find({
     "id": applicationId
   });
   var results = [];
   cursor.each(function(err, doc) {
      if (err) {
        console.log("Error: " + err);
      } else if (doc != null) {
         console.log("Null document.");
      } else {
         results.push(doc.status);
      }
   });
   return results;
};

Call both as follows:

MongoClient.connect(url, function(err, db) {
  if (!db) {
    console.log("Database did not connect.");
  }
  else {
    findApplications(db, "102"); // Replace "102" with gangId
    applicationsDone(db, "104"); // Replace "104" with applicationId
  }
});

EDIT per comments, here's how to include a callback:

// Note extra `callback` parameter
var applicationsDone = function(db, applicationId, callback) {
   var cursor = db.collection('gang_application').findOne({
     "id": applicationId
   });
   cursor.each(function(err, doc) {
      if (err) {
        console.log("Error: " + err);
      } else if (doc != null) {
         console.log("Null document.");
      } else {
         return (doc.status == "available");
      }
   });
};

To call:

MongoClient.connect(url, function(err, db) {
  if (!db) {
    console.log("Database did not connect.");
  }
  else {
    var callback = function(doc) { console.log(doc.status); };
    applicationsDone(db, "104", callback);
  }
});

EDIT Third snippet:

function accept_application($userid, $applicationId) {
        $box = 'failure';

        if (empty($applicationId)) {
            $message = "applicationId is empty";
        } elseif ($this->application_done($applicationId)) {
            $message = "Already registred!";

        } else {
            $application = $this->getApplication($applicationId);
            $test = true;
            if(!($test)) {
                $message = "false test";
            } else {
                $this->db->query("UPDATE `gang_applications` SET `status`='accepted', `by`='$userid' where `id`='$applicationId'");
                $this->gangs->add_member($application->userid,'gang','member',$application->gangid);
                $message = "Accept!";
            }

        }
        return $message;
    }

becomes this. Note some changes to the functions above to get this to work, such as returning an array of documents from applications, and using .findOne() for applicationDone():

function acceptApplication(userId, applicationId) {
  if (!applicationId) return "No application ID";
  if (applicationDone(db, applicationId)) return "Application already submitted.";

  // Left out `if (!$test)`
  db.gang_applications.update({
    "id": applicationId,
    "userId": userId
  }, {
    "status": "accepted"
  }, upsert: false);

  //                    $this->gangs->add_member($application->userid,'gang','member',$application->gangid);

  return "Accepted!";
}