Ignore:
Timestamp:
02/28/12 16:19:52 (13 years ago)
Author:
hendrikvanantwerpen
Message:

[Client] Added surveys list and survey details skeleton pages.
[Client] Changed method of passing parameters to pages. This still feels clumsy, because we're working against Dojo a bit with this.
[Server] Integrated REST resources for collections and objects, since they shared more than they differed.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Dev/branches/rest-dojo-ui/server/api.php

    r275 r292  
    7474            set_session_cookie($response, $user);
    7575        } else {
    76             throw new ResponseException("No email and password provided.",Response::BADREQUEST);
     76            throw new ResponseException("No email and password provided.", Response::BADREQUEST);
    7777        }
    7878        $response->body = $user;
     
    8383
    8484/**
    85  * @uri /data/\w+
     85 * @uri /data/\w+(/\w+)?
    8686 */
    87 class DataCollectionResource extends Resource {
    88 
    89     function getType($request) {
     87class DataResource extends Resource {
     88
     89    function getTypeAndUid($request) {
    9090        $uri = get_clean_uri($request);
    9191        $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;
    94100    }
    95101
     
    98104        restore_session($response);
    99105
    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
    104118        return $response;
    105119    }
     
    107121    function post($request) {
    108122        $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);
    112126        $onlyAdd = $request->ifNoneMatch('*');
    113127        $onlyUpdate = $request->ifMatch('*');
    114128
    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));
    118139            if (!empty($objects)) {
    119140                $object = $objects[0];
     
    126147
    127148        if (!$object) {
    128             $object = new $type(null);
     149            $object = new $info['type']($uid);
    129150            $response->code = Response::CREATED;
    130151        } else {
     
    134155            $object->$key = $val;
    135156        }
    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;
    203159        }
    204160        if (!$object->save()) {
Note: See TracChangeset for help on using the changeset viewer.