Skip to content
This repository
branch: master
Federico Cargnelutti October 03, 2011
executable file 157 lines (137 sloc) 4.607 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
<?php
class UsersController extends Controller
{
    /**
* @route GET /?method=users
* @route GET /users
*
* @param Request $request
* @return Response|View
*/
    public function indexAction($request)
    {
        // serve HTML, JSON and XML
        $request->acceptContentTypes(array('html', 'json', 'xml'));
        
        if ('html' == $request->getContentType()) {
            $response = new View();
            $response->setLayout('main');
        } else {
            $response = new Response();
        }
        
        $response->users = $this->getModel('User')->findAll();
        return $response;
    }
    
    /**
* @route GET /?method=users.show&id=1
* @route GET /?method=users.show&id=matt
* @route GET /users/1
* @route GET /users/matt
*
* @param Request $request
* @return Response|View
* @throws Exception
*/
    public function showAction($request)
    {
        // serve HTML, JSON and XML
        $request->acceptContentTypes(array('html', 'json', 'xml'));
        
        $model = $this->getModel('User');
        $id = $request->getParam('id');
        $user = is_numeric($id) ? $model->find($id) : $model->findBy(array('username'=>$id));
        if (! $user) {
            throw new Exception('User not found', Response::NOT_FOUND);
        }
        
        if ('html' == $request->getContentType()) {
            $response = new View();
            $response->setLayout('main');
        } else {
            $response = new Response();
            $response->setEtagHeader(md5('/users/' . $user->id));
        }
        
        $response->user = $user;
        return $response;
    }

    /**
* @route POST /?method=users.create&format=json
* @route POST /users/create.json
*
* @param Request $request
* @return Response
* @throws Exception
*/
    public function createAction($request)
    {
        $request->acceptContentTypes(array('json'));
        if ('POST' != $request->getMethod()) {
            throw new Exception('HTTP method not allowed', Response::NOT_ALLOWED);
        }
        
        try {
            $user = new User(array(
                'name' => $request->getPost('name'),
                'username' => $request->getPost('username'),
                'email' => $request->getPost('email'),
                'gender' => $request->getPost('gender')
            ));
        } catch (ValidationException $e) {
            throw new Exception($e->getMessage(), Response::OK);
        }
        
        $id = $this->getModel('User')->save($user);
        if (! is_numeric($id)) {
            throw new Exception('An error occurred while creating user', Response::OK);
        }
        
        $response = new Response();
        $response->setCode(Response::CREATED);
        $response->setEtagHeader(md5('/users/' . $id));
        
        return $response;
    }

    /**
* @route POST /?method=users.update&id=1&format=json
* @route POST /users/1/update.json
*
* @param Request $request
* @return Response
* @throws Exception
*/
    public function updateAction($request)
    {
        $request->acceptContentTypes(array('json'));
        if ('POST' != $request->getMethod()) {
            throw new Exception('HTTP method not supported', Response::NOT_ALLOWED);
        }
        
        $id = $request->getParam('id');
        
        $model = $this->getModel('User');
        $user = $model->find($id);
        if (! $user) {
            throw new Exception('User not found', Response::NOT_FOUND);
        }
        
        try {
            $user->username = $request->getPost('username');
        } catch (ValidationException $e) {
            throw new Exception($e->getMessage(), Response::OK);
        }
        $model->save($user);
        
        // return 200 OK
        return new Response();
    }
    
    /**
* @route GET /?method=users.destroy&id=1&format=json
* @route GET /users/1/destroy.json
*
* @param Request $request
* @return Response
* @throws Exception
*/
    public function destroyAction($request)
    {
        $request->acceptContentTypes(array('json'));
        
        $id = $request->getParam('id');
        $model = $this->getModel('User');
        $user = $model->find($id);
        if (! $user) {
            throw new Exception('User not found', Response::NOT_FOUND);
        }
        $model->delete($user->id);
        
        // return 200 OK
        return new Response();
    }
}
Something went wrong with that request. Please try again.