1
0
mirror of https://github.com/nerzhul/ocsms.git synced 2025-06-08 16:36:25 +00:00

Add replace function to purge all database before adding every record. Also add a delete before insert function when writing to DB, preventing SMS duplicates

This commit is contained in:
Loic Blot 2014-09-15 18:00:48 +02:00
parent 128167196a
commit ad1353c63c
3 changed files with 23 additions and 10 deletions

View File

@ -11,18 +11,11 @@
namespace OCA\OcSms\AppInfo;
/**
* Create your routes in here. The name is the lowercase name of the controller
* without the controller part, the stuff after the hash is the method.
* e.g. page#index -> PageController->index()
*
* The controller class has to be registered in the application.php file since
* it's instantiated in there
*/
$application = new Application();
$application->registerRoutes($this, array('routes' => array(
array('name' => 'sms#index', 'url' => '/', 'verb' => 'GET'),
array('name' => 'sms#push', 'url' => '/push', 'verb' => 'POST'),
array('name' => 'sms#replace', 'url' => '/replace', 'verb' => 'POST'),
array('name' => 'sms#retrieve_all_ids', 'url' => '/get/ids/all', 'verb' => 'GET'),
)));

View File

@ -59,6 +59,18 @@ class SmsController extends Controller {
return new JSONResponse(array("status" => true, "msg" => "OK"));
}
/**
* @NoAdminRequired
*/
public function replace($smsCount, $smsDatas) {
if ($this->checkPushStructure($smsCount, $smsDatas, true) === false) {
return new JSONResponse(array("status" => false, "msg" => $this->errorMsg));
}
$this->smsMapper->writeToDB($this->userId, $smsDatas, true);
return new JSONResponse(array("status" => true, "msg" => "OK"));
}
private function checkPushStructure ($smsCount, $smsDatas) {
if ($smsCount != count($smsDatas)) {
$this->errorMsg = "Error: sms count invalid";

View File

@ -35,10 +35,10 @@ class SmsMapper extends Mapper {
}
}
public function writeToDB ($userId, $smsList, $purgeBeforeInsert = false) {
public function writeToDB ($userId, $smsList, $purgeAllSmsBeforeInsert = false) {
\OCP\DB::beginTransaction();
if ($purgeBeforeInsert === true) {
if ($purgeAllSmsBeforeInsert === true) {
$query = \OC_DB::prepare('DELETE FROM *PREFIX*ocsms_smsdatas ' .
'WHERE user_id = ?');
$result = $query->execute(array($userId));
@ -50,6 +50,14 @@ class SmsMapper extends Mapper {
$sms["seen"] === "true" ? "1" : "0"
);
// Remove previous record
// @ TODO: only update the required fields, getAllIds can be useful
$query = \OC_DB::prepare('DELETE FROM *PREFIX*ocsms_smsdatas ' .
'WHERE user_id = ? AND sms_id = ?');
$result = $query->execute(array(
$userId, (int) $sms["_id"]
));
$query = \OC_DB::prepare('INSERT INTO *PREFIX*ocsms_smsdatas ' .
'(user_id, added, lastmodified, sms_flags, sms_date, sms_id,' .
'sms_address, sms_msg, sms_mailbox, sms_type) VALUES ' .