Prepare loading of weather view and check weather on an external API

master
Loic Blot 2015-07-09 19:35:17 +00:00
parent cc92ebc7b4
commit bf657ca4cc
4 changed files with 74 additions and 4 deletions

View File

@ -56,8 +56,19 @@ class CityController extends Controller {
return new JSONResponse(array(), Http::STATUS_BAD_REQUEST);
}
// @TODO: check city is not already registered
// @TODO: check there is an answer for the city http://api.openweathermap.org/data/2.5/forecast?q=Orsay&mode=xml
// Trim city name to remove unneeded spaces
$name = trim($name);
$cities = $this->mapper->getAll($this->userId);
for ($i = 0; $i < count($cities); $i++) {
if (strtolower($cities[$i]['name']) == strtolower($name)) {
return new JSONResponse(array(), 409);
}
}
if (!$this->getCityInformations($name)) {
return new JSONResponse(array(), Http::STATUS_NOT_FOUND);
}
if ($id = $this->mapper->create($this->userId, $name)) {
return new JSONResponse(array("id" => $id));
@ -84,5 +95,14 @@ class CityController extends Controller {
return new JSONResponse(array("deleted" => true));
}
private function getCityInformations ($name) {
$cityDatas = json_decode(file_get_contents("http://api.openweathermap.org/data/2.5/forecast?q=$name&mode=json"), true);
if ($cityDatas['cod'] != '200') {
return null;
}
return $cityDatas;
}
};
?>

View File

@ -102,3 +102,13 @@
.city-form-error {
color: red;
}
.city-load-error {
color: #FF3333;
position: absolute;
text-align: center;
top: 40%;
width: 100%;
padding: 20px;
font-size: 1.5em;
}

View File

@ -11,7 +11,7 @@
var app = angular.module('Weather', []);
var g_error500 = 'Fatal Error: please check your owncloud.log and sent a bug report here: https://github.com/nerzhul/weather/issues';
var g_error500 = 'Fatal Error: please check your owncloud.log and send a bug report here: https://github.com/nerzhul/weather/issues';
function undef (obj) {
return typeof obj === undefined || obj === undefined;
@ -29,6 +29,8 @@ app.controller('WeatherController', ['$scope', '$interval', '$timeout', '$compil
$scope.showAddCity = false;
$scope.addCityError = '';
$scope.cityLoadError = '';
$timeout(function () {
$scope.loadCities();
});
@ -49,6 +51,33 @@ app.controller('WeatherController', ['$scope', '$interval', '$timeout', '$compil
});
};
$scope.loadCity = function(city) {
if (undef(city) || emptyStr(city.name)) {
alert(g_error500);
return;
}
$http.get(OC.generateUrl('/apps/weather/weather/get'), {'name': city.name}).
success(function (data, status, headers, config) {
if (data != null && !undef(data['id'])) {
}
else {
$scope.cityLoadError = 'Failed to get city weather informations. Please contact your administrator';
}
}).
error(function (data, status, headers, config) {
if (status == 404) {
$scope.cityLoadError = "No city with this name found.";
}
else if (status == 500) {
$scope.cityLoadError = g_error500;
}
}).
fail(function (data, status, headers, config) {
$scope.cityLoadError = g_error500;
});
}
$scope.addCity = function(city) {
if (undef(city) || emptyStr(city.name)) {
$scope.addCityError = 'Empty city name !';
@ -65,6 +94,14 @@ app.controller('WeatherController', ['$scope', '$interval', '$timeout', '$compil
$scope.addCityError = 'Failed to add city. Please contact your administrator';
}
}).
error(function (data, status, headers, config) {
if (status == 404) {
$scope.addCityError = "No city with this name found.";
}
else if (status == 409) {
$scope.addCityError = "This city is already registered for your account.";
}
}).
fail(function (data, status, headers, config) {
$scope.addCityError = g_error500;
});

View File

@ -8,7 +8,7 @@
<div id="city-list-left">
<ul class="city-list">
<li class=city-list-item" ng-repeat="city in cities" class="{{ city.id == selectedCityId ? 'selected' : ''}}">
<a href="#" ng-click="loadCity(city.id);">{{ city.name }}</a>
<a href="#" ng-click="loadCity(city);">{{ city.name }}</a>
<div class="icon-delete svn delete action" ng-click="deleteCity(city);"></div>
</li>
<li>
@ -27,6 +27,9 @@
</li>
</ul>
</div>
<div id="city-right" ng-show="cityLoadError != ''">
<span class="city-load-error">{{ cityLoadError }}</span>
</div>
<div id="city-right" ng-show="selectedCityId != 0">
</div>
</div>