Add city delete and interface improvements

master
Loic Blot 2015-07-09 19:05:43 +00:00
parent a70098e142
commit cc92ebc7b4
5 changed files with 108 additions and 2 deletions

View File

@ -38,6 +38,15 @@ class CityController extends Controller {
return new TemplateResponse($this->appName, 'main');
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function getAll() {
$cities = $this->mapper->getAll($this->userId);
return new JSONResponse(array("cities" => $cities, "userid" => $this->userId));
}
/**
* @NoAdminRequired
* @NoCSRFRequired
@ -48,6 +57,7 @@ class CityController extends Controller {
}
// @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
if ($id = $this->mapper->create($this->userId, $name)) {
return new JSONResponse(array("id" => $id));
@ -55,5 +65,24 @@ class CityController extends Controller {
return new JSONResponse(array());
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function delete ($id) {
if (!$id || !is_numeric($id)) {
return new JSONResponse(array(), Http::STATUS_BAD_REQUEST);
}
$city = $this->mapper->load($id);
if ($city['user_id'] != $this->userId) {
return new JSONResponse(array(), 403);
}
$this->mapper->delete($id);
return new JSONResponse(array("deleted" => true));
}
};
?>

View File

@ -43,6 +43,21 @@
color: #333;
}
#city-list-left li .icon-delete {
width: 30px;
height: 18px;
top: 0;
right: 0;
position: absolute;
cursor: pointer;
height: 100%;
display: none;
}
#city-list-left li:hover .icon-delete {
display: inline;
}
#city-right {
padding: 15px;
height: 100%;

View File

@ -20,6 +20,31 @@ class CityMapper extends Mapper {
parent::__construct($db, 'weather_city');
}
public function load ($id) {
$sql = 'SELECT id, name, user_id FROM ' .
'*PREFIX*weather_city WHERE id = ?';
$query = \OCP\DB::prepare($sql);
$result = $query->execute(array($id));
if ($row = $result->fetchRow()) {
return $row;
}
return null;
}
public function getAll ($userId) {
$sql = 'SELECT id, name FROM ' .
'*PREFIX*weather_city WHERE user_id = ?';
$query = \OCP\DB::prepare($sql);
$result = $query->execute(array($userId));
$cities = array();
while ($row = $result->fetchRow()) {
$cities[] = $row;
}
return $cities;
}
public function create ($userId, $name) {
\OCP\DB::beginTransaction();
$query = \OCP\DB::prepare('INSERT INTO *PREFIX*weather_city ' .
@ -37,5 +62,13 @@ class CityMapper extends Mapper {
}
return null;
}
public function delete ($id) {
\OCP\DB::beginTransaction();
$query = \OCP\DB::prepare('DELETE FROM *PREFIX*weather_city ' .
'WHERE id = ?');
$query->execute(array($id));
\OCP\DB::commit();
}
};
?>

View File

@ -58,7 +58,7 @@ app.controller('WeatherController', ['$scope', '$interval', '$timeout', '$compil
$http.post(OC.generateUrl('/apps/weather/city/add'), {'name': city.name}).
success(function (data, status, headers, config) {
if (data != null && !undef(data['id'])) {
$scope.boards.push({"name": city.name, "id": data['id']})
$scope.cities.push({"name": city.name, "id": data['id']})
$scope.showAddCity = false;
}
else {
@ -69,5 +69,34 @@ app.controller('WeatherController', ['$scope', '$interval', '$timeout', '$compil
$scope.addCityError = g_error500;
});
};
$scope.deleteCity = function(city) {
if (undef(city)) {
alert(g_error500);
return;
}
$http.post(OC.generateUrl('/apps/weather/city/delete'), {'id': city.id}).
success(function (data, status, headers, config) {
if (data != null && !undef(data['deleted'])) {
for (var i = 0; i < $scope.cities.length; i++) {
if ($scope.cities[i].id === city.id) {
$scope.cities.splice(i, 1);
// If current city is the removed city, close it
if ($scope.selectedCityId === city.id) {
$scope.selectedCityId = 0;
}
return;
}
}
}
else {
alert('Failed to remove city. Please contact your administrator');
}
}).
fail(function (data, status, headers, config) {
alert(g_error500);
});
};
}
]);

View File

@ -7,7 +7,7 @@
<div class="ng-scope" id="app" ng-app="Weather" ng-controller="WeatherController">
<div id="city-list-left">
<ul class="city-list">
<li class=city-list-item" ng-repeat="city in citys" class="{{ city.id == selectedCityId ? 'selected' : ''}}">
<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>
<div class="icon-delete svn delete action" ng-click="deleteCity(city);"></div>
</li>