User update stuff.
authorCameron Ball <cameron@getapproved.com.au>
Mon, 29 Dec 2014 09:03:53 +0000 (17:03 +0800)
committerCameron Ball <cameron@getapproved.com.au>
Mon, 29 Dec 2014 09:03:53 +0000 (17:03 +0800)
Controllers/UserController.php
Domain/Entities/IUser.php
Domain/Entities/User.php
config/Routes.php
public_html/index.php

index 62d150a..1504735 100644 (file)
@@ -2,12 +2,17 @@
 
 namespace Controllers;
 
+use Exception;
+use Domain\Exception\InvalidCountryException;
 use Controllers\IDivineController;
 use Services\Http\IHttpRequest;
 use Services\Http\IHttpResponse;
 use Services\IUserQuota;
+use Services\IStatusReporter;
+use Services\IUserSession;
 use DataAccess\IUserRepository;
 use Domain\Util;
+use Domain\VOs\Country;
 
 class UserController implements IDivineController
 {
@@ -15,21 +20,26 @@ class UserController implements IDivineController
     private $_response;
     private $_request;
     private $_userQuota;
+    private $_statusReporter;
+    private $_userSession;
     
     public function __construct(
         IHttpRequest $request,
         IHttpResponse $response,
         IUserRepository $userRepository,
-        IUserQuota $userQuota
+        IUserQuota $userQuota,
+        IStatusReporter $statusReporter,
+        IUserSession $userSession
     ) {
         $this->_request = $request;
         $this->_response = $response;
         $this->_userRepository = $userRepository;
         $this->_userQuota = $userQuota;
+        $this->_statusReporter = $statusReporter;
+        $this->_userSession = $userSession;
     }
     
     public function indexAction() {
-        ;
     }
     
     // list simfiles
@@ -44,12 +54,50 @@ class UserController implements IDivineController
             'displayName' => $user->getDisplayName(),
             'tags' => $user->getTags(),
             'country' => $user->getCountry()->getCountryName(),
-            'quota' => Util::bytesToHumanReadable($user->getQuota()),
-            'quotaRemaining' => Util::bytesToHumanReadable($this->_userQuota->getCurrentUserQuotaRemaining())
         );
+        
+        if($this->_userSession->getCurrentUser())
+        {
+            $returnArray['quota'] = Util::bytesToHumanReadable($user->getQuota());
+            $returnArray['quotaRemaining'] =  Util::bytesToHumanReadable($this->_userQuota->getCurrentUserQuotaRemaining());
+        }
 
         $this->_response->setHeader('Content-Type', 'application/json')
                         ->setBody(json_encode($returnArray))
                         ->sendResponse();
     }
+    
+    public function updateAction($facebookId)
+    {
+        if(!$user = $this->_userSession->getCurrentUser()) $this->_statusReporter->error('You must be authenticated update your account.');
+        $userUpdateData = $this->userFromJson($this->_request->getBody());
+        
+        try
+        {
+            if(isset($userUpdateData->displayName)) $user->setDisplayName($userUpdateData->displayName);
+            //TODO: Direct instantiation bad?
+            if(isset($userUpdateData->country)) $user->setCountry(new Country($userUpdateData->country));
+            $this->_userRepository->save($user);
+        } catch (Exception $e) {
+            if(strpos($e->getMessage(), 'Duplicate entry') !== false)
+            {
+                $this->_statusReporter->error('Sorry, that display name is already in use.');
+            }
+            
+            if($e instanceof InvalidCountryException)
+            {
+                $this->_statusReporter->error($userUpdateData->country . ' is not a valid country.');
+            }
+        }
+        
+        $this->_statusReporter->success();
+    }
+    
+    private function userFromJson($json)
+    {
+        $user = json_decode($json);
+        if(json_last_error() !== JSON_ERROR_NONE || !$user) $this->_statusReporter->error ('Malformed or missing JSON');
+        
+        return $user;
+    }
 }
index 01314bf..74d0c58 100644 (file)
@@ -2,6 +2,8 @@
 \r
 namespace Domain\Entities;\r
 \r
+use Domain\VOs\ICountry;\r
+\r
 interface IUser\r
 {\r
     public function setId($id);\r
@@ -14,4 +16,7 @@ interface IUser
     public function getFacebookId();\r
     public function setFacebookId($id);\r
     public function getQuota();\r
+    \r
+    public function setDisplayName($displayName);\r
+    public function setCountry(ICountry $country);\r
 }\r
index 627d682..167404d 100644 (file)
@@ -68,4 +68,14 @@ class User extends AbstractEntity implements IUser
     {\r
         return $this->_quota;\r
     }\r
+    \r
+    public function setDisplayName($displayName)\r
+    {\r
+        $this->_displayName = $displayName;\r
+    }\r
+    \r
+    public function setCountry(ICountry $country)\r
+    {\r
+        $this->_country = $country;\r
+    }\r
 }\r
index afa2366..cb9e903 100644 (file)
@@ -47,16 +47,10 @@ return [
     '/user/:facebookId' => [\r
         'controller' => 'User',\r
         'actions' => [\r
-            'GET' => 'getUser'\r
+            'GET' => 'getUser',\r
+            'POST' => 'update'\r
          ]\r
     ],\r
-    \r
-    '/user' => [\r
-        'controller' => 'User',\r
-        'actions' => [\r
-            'POST' => 'update',\r
-        ]\r
-    ],\r
 \r
     '/files/banner/:hash' => [\r
         'controller' => 'File',\r
index 511beaa..be2b695 100644 (file)
@@ -6,6 +6,7 @@ $config = require('../config/app.php');
 \r
 // Allow these origins to do cross domain JS.\r
 header("Access-Control-Allow-Origin: " . $config['allow-origin']);\r
+header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");\r
 \r
 // Nice exceptions\r
 if($config['mode'] == 'production')\r