Improvements, adding a city is working, need more AngularJS support

master
Loic Blot 2015-07-08 22:05:57 +00:00
parent c656d7eb50
commit a70098e142
8 changed files with 108 additions and 14 deletions

View File

@ -21,6 +21,12 @@
<notnull>true</notnull> <notnull>true</notnull>
<length>255</length> <length>255</length>
</field> </field>
<field>
<name>user_id</name>
<type>text</type>
<notnull>true</notnull>
<length>255</length>
</field>
</declaration> </declaration>
</table> </table>
</database> </database>

View File

@ -5,7 +5,7 @@
<description>Watch the weather directly on your ownCloud</description> <description>Watch the weather directly on your ownCloud</description>
<licence>AGPL</licence> <licence>AGPL</licence>
<author>Loic Blot</author> <author>Loic Blot</author>
<version>0.0.1</version> <version>0.0.3</version>
<requiremin>7</requiremin> <requiremin>7</requiremin>
<ocsid>170501</ocsid> <ocsid>170501</ocsid>

View File

@ -16,10 +16,10 @@ $application = new Application();
$application->registerRoutes($this, array('routes' => array( $application->registerRoutes($this, array('routes' => array(
array('name' => 'city#index', 'url' => '/', 'verb' => 'GET'), array('name' => 'city#index', 'url' => '/', 'verb' => 'GET'),
array('name' => 'city#get', 'url' => '/city/get', 'verb' => 'GET'),
array('name' => 'city#getall', 'url' => '/city/getall', 'verb' => 'GET'), array('name' => 'city#getall', 'url' => '/city/getall', 'verb' => 'GET'),
array('name' => 'city#create', 'url' => '/city/create', 'verb' => 'POST'), array('name' => 'city#add', 'url' => '/city/add', 'verb' => 'POST'),
array('name' => 'city#delete', 'url' => '/city/delete', 'verb' => 'POST'), array('name' => 'city#delete', 'url' => '/city/delete', 'verb' => 'POST'),
array('name' => 'city#update', 'url' => '/city/update', 'verb' => 'POST'),
array('name' => 'weather#get', 'url' => '/weather/get', 'verb' => 'GET'),
))); )));
?> ?>

View File

@ -37,5 +37,23 @@ class CityController extends Controller {
public function index () { public function index () {
return new TemplateResponse($this->appName, 'main'); return new TemplateResponse($this->appName, 'main');
} }
/**
* @NoAdminRequired
* @NoCSRFRequired
*/
public function add ($name) {
if (!$name) {
return new JSONResponse(array(), Http::STATUS_BAD_REQUEST);
}
// @TODO: check city is not already registered
if ($id = $this->mapper->create($this->userId, $name)) {
return new JSONResponse(array("id" => $id));
}
return new JSONResponse(array());
}
}; };
?> ?>

View File

@ -57,3 +57,33 @@
position: absolute; position: absolute;
align-items: flex-start; align-items: flex-start;
} }
.city-list hr {
border: 0;
height: 1px;
background: #BBB;
background-image: linear-gradient(to right, #CCC, #BBB, #CCC);
}
#create-city {
padding: 10px;
background-color: #EEE;
border: 1px solid #DDD;
}
#create-city h1 {
color: #444;
}
#create-city h1 {
text-align: center;
font-weight: bold;
}
#create-city h2 {
font-weight: bold;
}
.city-form-error {
color: red;
}

View File

@ -19,5 +19,23 @@ class CityMapper extends Mapper {
public function __construct (IDb $db) { public function __construct (IDb $db) {
parent::__construct($db, 'weather_city'); parent::__construct($db, 'weather_city');
} }
public function create ($userId, $name) {
\OCP\DB::beginTransaction();
$query = \OCP\DB::prepare('INSERT INTO *PREFIX*weather_city ' .
'(user_id, name) VALUES (?,?)');
$query->execute(array($userId, $name));
\OCP\DB::commit();
$sql = 'SELECT max(id) FROM ' .
'*PREFIX*weather_city WHERE user_id = ? and name = ?';
$query = \OCP\DB::prepare($sql);
$result = $query->execute(array($userId, $name));
if ($row = $result->fetchRow()) {
return $row['max'];
}
return null;
}
}; };
?> ?>

View File

@ -11,7 +11,7 @@
var app = angular.module('Weather', []); 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/ownboard/issues'; var g_error500 = 'Fatal Error: please check your owncloud.log and sent a bug report here: https://github.com/nerzhul/weather/issues';
function undef (obj) { function undef (obj) {
return typeof obj === undefined || obj === undefined; return typeof obj === undefined || obj === undefined;
@ -26,14 +26,15 @@ app.controller('WeatherController', ['$scope', '$interval', '$timeout', '$compil
$scope.userId = ''; $scope.userId = '';
$scope.cities = []; $scope.cities = [];
$scope.selectedCityId = 0; $scope.selectedCityId = 0;
$scope.showCreateCity = false; $scope.showAddCity = false;
$scope.addCityError = '';
$timeout(function () { $timeout(function () {
$scope.loadCities(); $scope.loadCities();
}); });
$scope.loadCities = function () { $scope.loadCities = function () {
$http.get(OC.generateUrl('/apps/ownboard/city/getall')). $http.get(OC.generateUrl('/apps/weather/city/getall')).
success(function (data, status, headers, config) { success(function (data, status, headers, config) {
if (!undef(data['cities'])) { if (!undef(data['cities'])) {
$scope.cities = data['cities'] $scope.cities = data['cities']
@ -47,5 +48,26 @@ app.controller('WeatherController', ['$scope', '$interval', '$timeout', '$compil
$scope.fatalError(); $scope.fatalError();
}); });
}; };
$scope.addCity = function(city) {
if (undef(city) || emptyStr(city.name)) {
$scope.addCityError = 'Empty city name !';
return;
}
$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.showAddCity = false;
}
else {
$scope.addCityError = 'Failed to add city. Please contact your administrator';
}
}).
fail(function (data, status, headers, config) {
$scope.addCityError = g_error500;
});
};
} }
]); ]);

View File

@ -12,16 +12,16 @@
<div class="icon-delete svn delete action" ng-click="deleteCity(city);"></div> <div class="icon-delete svn delete action" ng-click="deleteCity(city);"></div>
</li> </li>
<li> <li>
<a href="#" ng-click="showCreateCity = true;">Add a city...</a> <a href="#" ng-click="showAddCity = true;">Add a city...</a>
<div ng-show="showCreateCity == true" id="create-city"> <div ng-show="showAddCity == true" id="create-city">
<h1>Create city</h1> <h1>Add city</h1>
<hr> <hr>
<h2>Title</h2> <h2>City name</h2>
<span class="city-form-error" ng-show="createCityError != ''">{{ createCityError }}</span> <span class="city-form-error" ng-show="addCityError != ''">{{ addCityError }}</span>
<form novalidate> <form novalidate>
<input type="textbox" ng-model="city.name"/> <input type="textbox" ng-model="city.name"/>
<input type="submit" value="Add" ng-click="createCity(city);"/> <input type="submit" value="Add" ng-click="addCity(city);"/>
<input type="button" value="Cancel" ng-click="showCreateCity = false;"/> <input type="button" value="Cancel" ng-click="showAddCity = false;"/>
</form> </form>
</div> </div>
</li> </li>