From a70098e142e541a5edb987e810167dd26e74589a Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Wed, 8 Jul 2015 22:05:57 +0000 Subject: [PATCH] Improvements, adding a city is working, need more AngularJS support --- appinfo/database.xml | 6 ++++++ appinfo/info.xml | 2 +- appinfo/routes.php | 6 +++--- controller/citycontroller.php | 18 ++++++++++++++++++ css/style.css | 30 ++++++++++++++++++++++++++++++ db/citymapper.php | 18 ++++++++++++++++++ js/public/app.js | 28 +++++++++++++++++++++++++--- templates/main.php | 14 +++++++------- 8 files changed, 108 insertions(+), 14 deletions(-) diff --git a/appinfo/database.xml b/appinfo/database.xml index 2a0c1e3..1489b6b 100644 --- a/appinfo/database.xml +++ b/appinfo/database.xml @@ -21,6 +21,12 @@ true 255 + + user_id + text + true + 255 + diff --git a/appinfo/info.xml b/appinfo/info.xml index 19f9093..8d6e5c9 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -5,7 +5,7 @@ Watch the weather directly on your ownCloud AGPL Loic Blot - 0.0.1 + 0.0.3 7 170501 diff --git a/appinfo/routes.php b/appinfo/routes.php index 514e850..15cb2d8 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -16,10 +16,10 @@ $application = new Application(); $application->registerRoutes($this, array('routes' => array( 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#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#update', 'url' => '/city/update', 'verb' => 'POST'), + + array('name' => 'weather#get', 'url' => '/weather/get', 'verb' => 'GET'), ))); ?> diff --git a/controller/citycontroller.php b/controller/citycontroller.php index f09a161..e274cae 100644 --- a/controller/citycontroller.php +++ b/controller/citycontroller.php @@ -37,5 +37,23 @@ class CityController extends Controller { public function index () { 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()); + } }; ?> diff --git a/css/style.css b/css/style.css index da8d719..b27232c 100644 --- a/css/style.css +++ b/css/style.css @@ -57,3 +57,33 @@ position: absolute; 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; +} diff --git a/db/citymapper.php b/db/citymapper.php index f75eb61..b7d864c 100644 --- a/db/citymapper.php +++ b/db/citymapper.php @@ -19,5 +19,23 @@ class CityMapper extends Mapper { public function __construct (IDb $db) { 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; + } }; ?> diff --git a/js/public/app.js b/js/public/app.js index 7eacdf6..871b2cf 100644 --- a/js/public/app.js +++ b/js/public/app.js @@ -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/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) { return typeof obj === undefined || obj === undefined; @@ -26,14 +26,15 @@ app.controller('WeatherController', ['$scope', '$interval', '$timeout', '$compil $scope.userId = ''; $scope.cities = []; $scope.selectedCityId = 0; - $scope.showCreateCity = false; + $scope.showAddCity = false; + $scope.addCityError = ''; $timeout(function () { $scope.loadCities(); }); $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) { if (!undef(data['cities'])) { $scope.cities = data['cities'] @@ -47,5 +48,26 @@ app.controller('WeatherController', ['$scope', '$interval', '$timeout', '$compil $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; + }); + }; } ]); diff --git a/templates/main.php b/templates/main.php index 9954577..2a31497 100644 --- a/templates/main.php +++ b/templates/main.php @@ -12,16 +12,16 @@
  • - Add a city... -
    -

    Create city

    + Add a city... +
    +

    Add city


    -

    Title

    - {{ createCityError }} +

    City name

    + {{ addCityError }}
    - - + +