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

Legend:

Unmodified
Added
Removed
  • 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                                ]
Note: See TracChangeset for help on using the changeset viewer.