Changeset e498070 for src/webservice


Ignore:
Timestamp:
Sep 9, 2015, 10:03:34 PM (11 years ago)
Author:
Joachim Langenbach <joachim.langenbach@…>
Branches:
master
Children:
915dc18
Parents:
d83a844
Message:
Location:
src/webservice
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/webservice/htdocs/SiWeather/Webservice/Setup.php

    rd83a844 re498070  
    251251                }
    252252
     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
    253278                // prepare stations table
    254279                $table = "stations";
    … …  
    257282                        $sql .= "  id INTEGER NOT NULL PRIMARY KEY,";
    258283                        $sql .= "  name TEXT NOT NULL,";
    259                         $sql .= "  city INTEGER,";
     284                        $sql .= "  city INTEGER NOT NULL,";
    260285                        $sql .= "  uuid TEXT NOT NULL,";
    261286                        $sql .= "  ownerMail TEXT NOT NULL,";
     287                        $sql .= "  type TEXT NOT NULL,";
     288                        $sql .= "  typeUrl TEXT,";
     289                        $sql .= "  status INTEGER NOT NULL,";
    262290                        $sql .= "  FOREIGN KEY(city) REFERENCES cities(id)";
     291                        $sql .= "  FOREIGN KEY(status) REFERENCES status(id)";
    263292                        $sql .= ")";
    264293                        if(!$this->databaseTableCreate($table, $sql, $errorMessage)){
  • src/webservice/htdocs/SiWeather/Webservice/WebserviceApi.php

    rd83a844 re498070  
    121121                        }
    122122
    123                         $expectedKeys = array("name", "city", "longitude", "latitude", "email", "type", "typeUrl");
     123                        $expectedKeys = array("name", "city", "longitude", "latitude", "email", "type");
    124124                        $missingKeys = array();
    125125                        foreach($expectedKeys as $key){
    … …  
    164164                                                "result" => "invalid email",
    165165                                                "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.",
    166179                                        ]
    167180                                );
    … …  
    237250                        $sqlStatement->bindValue(":location", "POINT(". $location->locationWkt() .")");
    238251                        $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){
    242254                                $cityId = $row[0];
    243                                 error_log($row[0]);
    244255                        }
    245256                        else{
    246257                                $sqlStatement = $db->prepare("INSERT INTO cities (name, location) VALUES(:name, GeomFromText(:location, 4326))");
    247258                                if(!$sqlStatement){
     259                                        error_log("Failed to add city to the database: ". $db->lastDatabaseErrorMessage());
    248260                                        $db->exec("ROLLBACK");
    249261                                        echo json_encode
    … …  
    273285                                $cityId = $db->lastInsertRowId();
    274286                        }
    275                         if($cityId < 0){
     287                        if(!is_numeric($cityId) || $cityId < 0){
    276288                                error_log("Failed to select city from the database: ". $db->lastDatabaseErrorMessage());
    277289                                $db->exec("ROLLBACK");
    … …  
    286298                        }
    287299
     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
    288335                        // insert station
    289336                        $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))");
    291338                        if(!$sqlStatement){
    292339                                $db->exec("ROLLBACK");
    … …  
    303350                        $sqlStatement->bindValue(":city", $cityId);
    304351                        $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);
    306356                        $sqlStatement->bindValue(":location", "POINT(". $location->locationWkt() .")");
    307357                        $sqlResult = $sqlStatement->execute();
    … …  
    368418                                [
    369419                                        "result" => "success",
     420                                        "message" => "Confirmation request is send to ". $json["email"] .".",
    370421                                        "uuid" => $uuid,
    371422                                ]
  • src/webservice/tests/StationsTest.php

    rd83a844 re498070  
    7777                // sending must failing register request, caused by unproven location
    7878                $response = $client->post("index.php/stations/register",
    79                         [ "json" => ["name" => "Cool Station", "longitude" => 23.2, "latitude" => 90, "city" => "Nice town", "email" => "test@example.org"]]
     79                        [ "json" => ["name" => "Cool Station", "longitude" => 23.2, "latitude" => 90, "city" => "Nice town", "type" => "testing", "email" => "test@example.org"]]
    8080                );
    8181                $this->assertEquals($response->getStatusCode(), 200, $response->getReasonPhrase());
    … …  
    101101
    102102                $response = $client->post("index.php/stations/register",
    103                         [ "json" => ["name" => "My Weather Station", "city" => "Clausthal-Zellerfeld", "longitude" => 10.28152, "latitude" => 51.8281, "email" => "test@example.org"]]
     103                        [ "json" => ["name" => "My Weather Station", "city" => "Clausthal-Zellerfeld", "longitude" => 10.28152, "latitude" => 51.8281, "type" => "testing", "email" => "test@example.org"]]
    104104                );
    105105                $this->assertEquals(200, $response->getStatusCode(), $response->getReasonPhrase());
    106106                $json = json_decode($response->getBody(), true);
    107107                $this->assertTrue(array_key_exists("result", $json));
    108                 $this->assertEquals("success", $json["result"]);
     108                $this->assertEquals("success", $json["result"], $json["message"]);
    109109                $this->assertTrue(array_key_exists("uuid", $json));
    110110                $this->assertTrue(!empty($json["uuid"]));
     111                $this->assertTrue(array_key_exists("message", $json));
     112                $this->assertTrue(!empty($json["message"]));
    111113
    112114                $stations = $this->query("index.php/stations");
Note: See TracChangeset for help on using the changeset viewer.