From a68cff82dc2704973b322728a7972699ea70b555 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Sat, 11 Jul 2015 16:55:00 +0000 Subject: [PATCH] Add forgotten file ! --- controller/weathercontroller.php | 55 ++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 controller/weathercontroller.php diff --git a/controller/weathercontroller.php b/controller/weathercontroller.php new file mode 100644 index 0000000..915251c --- /dev/null +++ b/controller/weathercontroller.php @@ -0,0 +1,55 @@ + + * @copyright Loic Blot 2015 + */ + +namespace OCA\Weather\Controller; + +use \OCP\IRequest; +use \OCP\AppFramework\Http\TemplateResponse; +use \OCP\AppFramework\Controller; +use \OCP\AppFramework\Http\JSONResponse; +use \OCP\AppFramework\Http; + +use \OCA\Weather\Db\CityMapper; + +class WeatherController extends Controller { + + private $userId; + private $mapper; + + public function __construct ($appName, IRequest $request, $userId, CityMapper $mapper) { + parent::__construct($appName, $request); + $this->userId = $userId; + $this->mapper = $mapper; + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + */ + public function get($name) { + $cityInfos = $this->getCityInformations($name); + if (!$cityInfos) { + return new JSONResponse(array(), Http::STATUS_NOT_FOUND); + } + return new JSONResponse($cityInfos); + } + + private function getCityInformations ($name) { + // @TODO setting for metric + $cityDatas = json_decode(file_get_contents("http://api.openweathermap.org/data/2.5/weather?q=$name&mode=json&units=metric"), true); + if ($cityDatas['cod'] != '200') { + return null; + } + + return $cityDatas; + } +}; +?>