Changeset 292 for Dev/branches/rest-dojo-ui/server/api.php
- Timestamp:
- 02/28/12 16:19:52 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Dev/branches/rest-dojo-ui/server/api.php
r275 r292 74 74 set_session_cookie($response, $user); 75 75 } else { 76 throw new ResponseException("No email and password provided.", Response::BADREQUEST);76 throw new ResponseException("No email and password provided.", Response::BADREQUEST); 77 77 } 78 78 $response->body = $user; … … 83 83 84 84 /** 85 * @uri /data/\w+ 85 * @uri /data/\w+(/\w+)? 86 86 */ 87 class Data CollectionResource extends Resource {88 89 function getType ($request) {87 class DataResource extends Resource { 88 89 function getTypeAndUid($request) { 90 90 $uri = get_clean_uri($request); 91 91 $path = explode('/', $uri); 92 $type = $path[2]; 93 return $type; 92 93 $info = array(); 94 $info['type'] = $path[2]; 95 if (isset($path[3])) { 96 $info['uid'] = $path[3]; 97 } 98 99 return $info; 94 100 } 95 101 … … 98 104 restore_session($response); 99 105 100 $type = $this->getType($request); 101 $objects = $type::get(array()); 102 103 $response->body = $objects; 106 $info = $this->getTypeAndUid($request); 107 if (isset($info['uid'])) { 108 $objects = $info['type']::get(array('uid' => $info['uid'])); 109 if (empty($objects)) { 110 throw new ResponseException("Object not found", Response::NOTFOUND); 111 } 112 $response->body = $objects[0]; 113 } else { 114 $objects = $info['type']::get(array()); 115 $response->body = $objects; 116 } 117 104 118 return $response; 105 119 } … … 107 121 function post($request) { 108 122 $response = new Response($request); 109 restore_session($response);110 111 $ type = $this->getType($request);123 $user = restore_session($response); 124 125 $info = $this->getTypeAndUid($request); 112 126 $onlyAdd = $request->ifNoneMatch('*'); 113 127 $onlyUpdate = $request->ifMatch('*'); 114 128 115 $object = FALSE; 116 if (isset($request->data->uid)) { 117 $objects = $type::get(array('uid' => $request->data->uid)); 129 $uid = null; 130 if (isset($info['uid'])) { 131 $uid = $info['uid']; 132 } else if (isset($request->data->uid)) { 133 $uid = $request->data->uid; 134 } 135 136 $object = null; 137 if ($uid) { 138 $objects = $info['type']::get(array('uid' => $uid)); 118 139 if (!empty($objects)) { 119 140 $object = $objects[0]; … … 126 147 127 148 if (!$object) { 128 $object = new $ type(null);149 $object = new $info['type']($uid); 129 150 $response->code = Response::CREATED; 130 151 } else { … … 134 155 $object->$key = $val; 135 156 } 136 if (!$object->save()) { 137 throw new ResponseException("Save failed", Response::INTERNALSERVERERROR); 138 } 139 140 $response->body = $object; 141 return $response; 142 } 143 144 function put($request) { 145 return $this->post($request); 146 } 147 148 } 149 150 /** 151 * @uri /data/\w+/\w+ 152 */ 153 class DataObjectResource extends Resource { 154 155 function getTypeAndUid($request) { 156 $uri = get_clean_uri($request); 157 $path = explode('/', $uri); 158 $type = $path[2]; 159 $uid = $path[3]; 160 return array('type' => $type, 'uid' => $uid); 161 } 162 163 function get($request) { 164 $response = new Response($request); 165 restore_session($response); 166 167 $info = $this->getTypeAndUid($request); 168 $objects = $info['type']::get(array('uid' => $info['uid'])); 169 if (empty($objects)) { 170 throw new ResponseException("Object not found", Response::NOTFOUND); 171 } 172 173 $response->body = $objects[0]; 174 return $response; 175 } 176 177 function post($request) { 178 $response = new Response($request); 179 restore_session($response); 180 181 $info = $this->getTypeAndUid($request); 182 $onlyAdd = $request->ifNoneMatch('*'); 183 $onlyUpdate = $request->ifMatch('*'); 184 185 $object = FALSE; 186 $objects = $info['type']::get(array('uid' => $info['uid'])); 187 if (!empty($objects)) { 188 $object = $objects[0]; 189 } 190 191 if (( $onlyUpdate && !$object ) || ( $onlyAdd && $object )) { 192 throw new ResponseException("Update/Create and existing object mismatch", Response::PRECONDITIONFAILED); 193 } 194 195 if (!$object) { 196 $object = new $info['type']($info->uid); 197 $response->code = Response::CREATED; 198 } else { 199 $response->code = Response::OK; 200 } 201 foreach ($request->data as $key => $val) { 202 $object->$key = $val; 157 if (isset($object->creator)) { 158 $object->creator = $user; 203 159 } 204 160 if (!$object->save()) {
Note: See TracChangeset
for help on using the changeset viewer.