diff --git a/appinfo/routes.php b/appinfo/routes.php index 9424a14..dede5b9 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -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'), ))); diff --git a/controller/smscontroller.php b/controller/smscontroller.php index a1ebdb9..6749ee9 100644 --- a/controller/smscontroller.php +++ b/controller/smscontroller.php @@ -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"; diff --git a/db/smsmapper.php b/db/smsmapper.php index c83b9ae..c81f1ca 100644 --- a/db/smsmapper.php +++ b/db/smsmapper.php @@ -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 ' .