Advertisement
TeamFocus-Matija

UserController

Jul 17th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.66 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Controllers;
  4.  
  5. use Exceptions\RestParameterException;
  6. use Models\User;
  7.  
  8. class UserController extends ControllerBase
  9. {
  10.  
  11.     public function getUserById()
  12.     {
  13.         $response = $this->response;
  14.         try {
  15.             $id = $this->getJsonParameterFromPost('user_id', 'int', true);
  16.  
  17.             $user = User::findFirst(
  18.                 [
  19.                     "id = :id:",
  20.                     "bind" => [
  21.                         "id" => $id
  22.                     ]
  23.                 ]
  24.             );
  25.  
  26.             if ($user) {
  27.                 $response->setStatusCode(200, "OK");
  28.                 $response->setJsonContent($user);
  29.             } else {
  30.                 $response->setStatusCode(404, "Not found");
  31.                 $response->setJsonContent(array('status' => 'ERROR', 'messages' => "User you are looking for doesn't exist"));
  32.             }
  33.         } catch (\Exception $e) {
  34.             $response = $this->getExceptionResponse($e);
  35.         } finally {
  36.             return $response;
  37.         }
  38.     }
  39.  
  40.     public function getUsersByLink(){
  41.         $response = $this->response;
  42.         try {
  43.             $link = $this->getJsonParameterFromPost('link', 'string', true);
  44.  
  45.             $users = User::find(
  46.                 [
  47.                     "link = :link:",
  48.                     "bind" => [
  49.                         "link" => $link
  50.                     ]
  51.                 ]
  52.             );
  53.  
  54.             if (sizeof($users)>0) {
  55.                 $response->setStatusCode(200, "OK");
  56.                 $response->setJsonContent($users);
  57.             } else {
  58.                 $response->setStatusCode(404, "Not found");
  59.                 $response->setJsonContent(array('status' => 'ERROR', 'messages' => "Users you are looking for with that link don't exist"));
  60.             }
  61.         } catch (\Exception $e) {
  62.             $response = $this->getExceptionResponse($e);
  63.         } finally {
  64.             return $response;
  65.         }
  66.     }
  67.  
  68.     public function registerNewUser()
  69.     {
  70.         $response = $this->response;
  71.         try {
  72.             $name = $this->getJsonParameterFromPost('name', 'string', true);
  73.             $university_id = $this->getJsonParameterFromPost('university_id', 'int', true);
  74.             $link = $this->getJsonParameterFromPost('link', 'string', true);
  75.  
  76.             $user = User::findFirst(
  77.                 [
  78.                     "name = :name: AND link=:link:",
  79.                     "bind" => [
  80.                         "name" => $name,
  81.                         "link" => $link
  82.                     ]
  83.                 ]
  84.             );
  85.  
  86.             if ($user) {
  87.                 $response->setStatusCode(409, "Conflict");
  88.                 $response->setJsonContent(array('status' => 'ERROR', 'messages' => 'Such user already exists!'));
  89.                 return;
  90.             }
  91.  
  92.             $user = new User();
  93.             $user->name = $name;
  94.             $user->link = $link;
  95.             $user->university_id = $university_id;
  96.  
  97.             if ($user->save()) {
  98.                 $response->setStatusCode(201, "Created");
  99.                 $response->setJsonContent(array(
  100.                     'status' => 'SUCCESS',
  101.                     'messages' => 'Successfully registered a new user!',
  102.                     'user'=>$user));
  103.             } else {
  104.                 $response->setStatusCode(500, "Unexpected error");
  105.                 $response->setJsonContent(array('status' => 'ERROR', 'messages' => 'Something went wrong, user not registered!'));
  106.             }
  107.         } catch (\Exception $e) {
  108.             $response = $this->getExceptionResponse($e);
  109.         } finally {
  110.             return $response;
  111.         }
  112.     }
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement