From f7e69d81e4a81db861e3538bee9cf1520abb535d Mon Sep 17 00:00:00 2001 From: Cameron Ball Date: Mon, 29 Dec 2014 17:03:53 +0800 Subject: [PATCH] User update stuff. --- Controllers/UserController.php | 56 +++++++++++++++++++++++++++++++++++++++--- Domain/Entities/IUser.php | 5 ++++ Domain/Entities/User.php | 10 ++++++++ config/Routes.php | 10 ++------ public_html/index.php | 1 + 5 files changed, 70 insertions(+), 12 deletions(-) diff --git a/Controllers/UserController.php b/Controllers/UserController.php index 62d150a..1504735 100644 --- a/Controllers/UserController.php +++ b/Controllers/UserController.php @@ -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; + } } diff --git a/Domain/Entities/IUser.php b/Domain/Entities/IUser.php index 01314bf..74d0c58 100644 --- a/Domain/Entities/IUser.php +++ b/Domain/Entities/IUser.php @@ -2,6 +2,8 @@ namespace Domain\Entities; +use Domain\VOs\ICountry; + interface IUser { public function setId($id); @@ -14,4 +16,7 @@ interface IUser public function getFacebookId(); public function setFacebookId($id); public function getQuota(); + + public function setDisplayName($displayName); + public function setCountry(ICountry $country); } diff --git a/Domain/Entities/User.php b/Domain/Entities/User.php index 627d682..167404d 100644 --- a/Domain/Entities/User.php +++ b/Domain/Entities/User.php @@ -68,4 +68,14 @@ class User extends AbstractEntity implements IUser { return $this->_quota; } + + public function setDisplayName($displayName) + { + $this->_displayName = $displayName; + } + + public function setCountry(ICountry $country) + { + $this->_country = $country; + } } diff --git a/config/Routes.php b/config/Routes.php index afa2366..cb9e903 100644 --- a/config/Routes.php +++ b/config/Routes.php @@ -47,16 +47,10 @@ return [ '/user/:facebookId' => [ 'controller' => 'User', 'actions' => [ - 'GET' => 'getUser' + 'GET' => 'getUser', + 'POST' => 'update' ] ], - - '/user' => [ - 'controller' => 'User', - 'actions' => [ - 'POST' => 'update', - ] - ], '/files/banner/:hash' => [ 'controller' => 'File', diff --git a/public_html/index.php b/public_html/index.php index 511beaa..be2b695 100644 --- a/public_html/index.php +++ b/public_html/index.php @@ -6,6 +6,7 @@ $config = require('../config/app.php'); // Allow these origins to do cross domain JS. header("Access-Control-Allow-Origin: " . $config['allow-origin']); +header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); // Nice exceptions if($config['mode'] == 'production') -- 2.11.0