Changeset e498070 for src/webservice/htdocs/SiWeather/Webservice
- Timestamp:
- Sep 9, 2015, 10:03:34 PM (11 years ago)
- Branches:
- master
- Children:
- 915dc18
- Parents:
- d83a844
- Location:
- src/webservice/htdocs/SiWeather/Webservice
- Files:
-
- 2 edited
-
Setup.php (modified) (2 diffs)
-
WebserviceApi.php (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/webservice/htdocs/SiWeather/Webservice/Setup.php
rd83a844 re498070 251 251 } 252 252 253 // prepare status table 254 $table = "status"; 255 if(!$this->databaseTableExists($table)){ 256 $sql = "CREATE TABLE ". $table ." ("; 257 $sql .= " id INTEGER NOT NULL PRIMARY KEY,"; 258 $sql .= " name TEXT NOT NULL"; 259 $sql .= ")"; 260 if(!$this->databaseTableCreate($table, $sql, $errorMessage)){ 261 return false; 262 } 263 $status = array("confirming", "active", "deletionrequested"); 264 $sql = "INSERT INTO ". $table ." (name) VALUES(:name)"; 265 $sqlStatement = $this->_databaseHandler->prepare($sql); 266 foreach ($status as $name){ 267 $sqlStatement->clear(); 268 $sqlStatement->bindValue(":name", $name); 269 if(!$sqlStatement->execute()){ 270 $errorMessage = Translation::swGettext("Could not add status %s to the database", $name); 271 error_log($errorMessage); 272 return false; 273 } 274 $sqlStatement->reset(); 275 } 276 } 277 253 278 // prepare stations table 254 279 $table = "stations"; … … 257 282 $sql .= " id INTEGER NOT NULL PRIMARY KEY,"; 258 283 $sql .= " name TEXT NOT NULL,"; 259 $sql .= " city INTEGER ,";284 $sql .= " city INTEGER NOT NULL,"; 260 285 $sql .= " uuid TEXT NOT NULL,"; 261 286 $sql .= " ownerMail TEXT NOT NULL,"; 287 $sql .= " type TEXT NOT NULL,"; 288 $sql .= " typeUrl TEXT,"; 289 $sql .= " status INTEGER NOT NULL,"; 262 290 $sql .= " FOREIGN KEY(city) REFERENCES cities(id)"; 291 $sql .= " FOREIGN KEY(status) REFERENCES status(id)"; 263 292 $sql .= ")"; 264 293 if(!$this->databaseTableCreate($table, $sql, $errorMessage)){ -
src/webservice/htdocs/SiWeather/Webservice/WebserviceApi.php
rd83a844 re498070 121 121 } 122 122 123 $expectedKeys = array("name", "city", "longitude", "latitude", "email", "type" , "typeUrl");123 $expectedKeys = array("name", "city", "longitude", "latitude", "email", "type"); 124 124 $missingKeys = array(); 125 125 foreach($expectedKeys as $key){ … … 164 164 "result" => "invalid email", 165 165 "message" => "Your specified an invalid email address: ". $json["email"], 166 ] 167 ); 168 return; 169 } 170 if(!array_key_exists("typeUrl", $json)){ 171 $json["typeUrl"] = ""; 172 } 173 elseif(!empty($json["typeUrl"]) && !filter_var($json["typeUrl"], FILTER_VALIDATE_URL)){ 174 echo json_encode 175 ( 176 [ 177 "result" => "invalid type url", 178 "message" => "Your specified type url is invalid: ". $json["typeUrl"] ." Set it to an empty value or fix it.", 166 179 ] 167 180 ); … … 237 250 $sqlStatement->bindValue(":location", "POINT(". $location->locationWkt() .")"); 238 251 $result = $sqlStatement->execute(); 239 if($result->numColumns() && $result->columnType(0) != SQLITE3_NULL){ 240 error_log("not cool"); 241 $row = $result->fetchArray(); 252 $row = $result->fetchArray(); 253 if($row){ 242 254 $cityId = $row[0]; 243 error_log($row[0]);244 255 } 245 256 else{ 246 257 $sqlStatement = $db->prepare("INSERT INTO cities (name, location) VALUES(:name, GeomFromText(:location, 4326))"); 247 258 if(!$sqlStatement){ 259 error_log("Failed to add city to the database: ". $db->lastDatabaseErrorMessage()); 248 260 $db->exec("ROLLBACK"); 249 261 echo json_encode … … 273 285 $cityId = $db->lastInsertRowId(); 274 286 } 275 if( $cityId < 0){287 if(!is_numeric($cityId) || $cityId < 0){ 276 288 error_log("Failed to select city from the database: ". $db->lastDatabaseErrorMessage()); 277 289 $db->exec("ROLLBACK"); … … 286 298 } 287 299 300 // find out initial state 301 $statusId = -1; 302 $sqlStatement = $db->prepare("SELECT id FROM status WHERE name=:name"); 303 if(!$sqlStatement){ 304 $db->exec("ROLLBACK"); 305 error_log("Could not find initial state to register station (Prepare): ". $db->lastDatabaseErrorMessage()); 306 echo json_encode 307 ( 308 [ 309 "result" => "failed", 310 "message" => "Could not fetch status from database", 311 ] 312 ); 313 return; 314 } 315 $sqlStatement->bindValue(":name", "confirming"); 316 $result = $sqlStatement->execute(); 317 $row = $result->fetchArray(); 318 if($row){ 319 $statusId = $row[0]; 320 } 321 else{ 322 error_log("columns ". $result->numColumns() .", columnType: ". $result->columnType(0)); 323 $db->exec("ROLLBACK"); 324 error_log("Could not find initial state to register station (Execute): ". $db->lastDatabaseErrorMessage()); 325 echo json_encode 326 ( 327 [ 328 "result" => "failed", 329 "message" => "Could not fetch status from database (Status not found)", 330 ] 331 ); 332 return; 333 } 334 288 335 // insert station 289 336 $uuid = uuid_create(4); 290 $sqlStatement = $db->prepare("INSERT INTO stations (name, city, uuid, ownerMail, location) VALUES(:name, :city, :uuid, :ownerMail, GeomFromText(:location, 4326))");337 $sqlStatement = $db->prepare("INSERT INTO stations (name, city, uuid, ownerMail, type, typeUrl, status, location) VALUES(:name, :city, :uuid, :ownerMail, :type, :typeUrl, :status, GeomFromText(:location, 4326))"); 291 338 if(!$sqlStatement){ 292 339 $db->exec("ROLLBACK"); … … 303 350 $sqlStatement->bindValue(":city", $cityId); 304 351 $sqlStatement->bindValue(":uuid", $uuid); 305 $sqlStatement->bindValue(":ownerMail",$json["email"]); 352 $sqlStatement->bindValue(":ownerMail", $json["email"]); 353 $sqlStatement->bindValue(":type", $json["type"]); 354 $sqlStatement->bindValue(":typeUrl", $json["typeUrl"]); 355 $sqlStatement->bindValue(":status", $statusId); 306 356 $sqlStatement->bindValue(":location", "POINT(". $location->locationWkt() .")"); 307 357 $sqlResult = $sqlStatement->execute(); … … 368 418 [ 369 419 "result" => "success", 420 "message" => "Confirmation request is send to ". $json["email"] .".", 370 421 "uuid" => $uuid, 371 422 ]
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/siweather/chrome/site/your_project_logo.png)