Created
August 27, 2015 19:38
-
-
Save kudarap/6b8fb0348c257a18e95b to your computer and use it in GitHub Desktop.
eden-json-rest module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php //--> | |
/* | |
CREATE TABLE IF NOT EXISTS `channel` ( | |
`id` int(11) NOT NULL AUTO_INCREMENT, | |
`user_id` int(11) NOT NULL, | |
`name` text NOT NULL, | |
`data` longtext NOT NULL, | |
`created_at` timestamp NULL DEFAULT NULL, | |
`updated_at` timestamp NULL DEFAULT NULL, | |
`deleted_at` timestamp NULL DEFAULT NULL, | |
PRIMARY KEY (`id`) | |
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; | |
*/ | |
namespace Modules; | |
use Resources\Channel as C; | |
class Channel | |
{ | |
/* Constants | |
--------------------------------------------*/ | |
/* Public Properties | |
--------------------------------------------*/ | |
/* Protected Properties | |
--------------------------------------------*/ | |
/* Public Methods | |
--------------------------------------------*/ | |
public static function push($name, $data) | |
{ | |
// validate | |
if(empty($name) || empty($data)) { | |
return Helper::error(array( | |
'msg' => 'required fields are required', | |
'fields' => array('name', 'data'))); | |
} | |
return C::create(array( | |
'user_id' => self::getUser(), | |
'name' => $name, | |
'data' => json_encode($data))); | |
} | |
public static function pull($name, $index = 0) | |
{ | |
// validate | |
if(empty($name)) { | |
return Helper::error(array( | |
'msg' => 'name field is required')); | |
} | |
// set index to 0 if null | |
if(empty($index)) { | |
$index = 0; | |
} | |
// build option and search | |
$result = C::find(array( | |
'filters' => array( | |
'name' => $name, | |
array('id > %d', $index)) | |
)); | |
// extract data on data field | |
// decode to data to json | |
foreach($result as $key => $value) { | |
$result[$key]['data'] = json_decode($value['data']); | |
$result[$key]['index'] = $value['id']; | |
unset($result[$key]['id']); | |
} | |
return $result; | |
} | |
/* Protected Methods | |
--------------------------------------------*/ | |
/* Private Methods | |
--------------------------------------------*/ | |
private static function getUser() | |
{ | |
// get user | |
if($user = Auth::getUser()) { | |
return $user['id']; | |
} | |
Helper::panic('User does not have Id'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment