Пример #1
0
 function execute($param, $post)
 {
     if (count($param) > 0) {
         if ($param[0] == "act") {
             $obj = json_decode($post["json"]);
             $drinker = GetByGuid("Drinker", $obj->Guid);
             if ($drinker instanceof Drinker) {
                 if ($obj->Action == "remove") {
                     $existingBeers = array();
                     foreach ($obj->Beers as $beer) {
                         $existingBeer = GetSingleByCondition("Beers", array("Guid" => $beer->Guid));
                         if ($existingBeer != null) {
                             $existingBeers[] = $existingBeer;
                         }
                     }
                     return ReturnBoolean(DeleteAll($existingBeers) && $this->refreshDrinkerProperties($drinker));
                 } else {
                     if ($obj->Action == "add") {
                         $newBeers = array();
                         foreach ($obj->Beers as $beer) {
                             $existingBeer = GetSingleByCondition("Beers", array("Guid" => $beer->Guid));
                             if ($existingBeer == null) {
                                 $newbeer = new Beer();
                                 $newbeer->Guid = $beer->Guid;
                                 $newbeer->DrinkTime = ConvertToDatabaseDateTime($beer->DrinkTime);
                                 $newbeer->DrinkerId = $drinker->Id;
                                 $newBeers[] = $newbeer;
                             }
                         }
                         return ReturnBoolean(InsertAll($newBeers) && $this->refreshDrinkerProperties($drinker));
                     } else {
                         if ($obj->Action == "sync") {
                             $beercount = $this->countExistingBeers($drinker);
                             if ($beercount !== $obj->ExpectedCount) {
                                 return ReturnBoolean(false);
                             }
                             $beers = GetAllByCondition("Beers", array("DrinkerId" => $drinker->Id), "DrinkTime DESC", " LIMIT " . count($obj->Beers));
                             for ($i = 0; $i < count($beers); $i++) {
                                 if ($beers[$i]->Guid != $obj->Beers[$i]->Guid) {
                                     return ReturnBoolean(false);
                                 }
                             }
                             return ReturnBoolean(true);
                         } else {
                             return ReturnError(LINK_INVALID);
                         }
                     }
                 }
             }
             return ReturnNotFound($param[0], "Drinker");
         } else {
             if (ValidateGuid($param[0])) {
                 $drinker = GetByGuid("Drinker", $param[0]);
                 if ($drinker != null && $drinker instanceof Drinker) {
                     /*
                     $limit = 0;
                     if (count($param) > 1 && is_numeric($param[1])) {
                         $beerCount = $this->countExistingBeers($drinker);
                         if ($beerCount > $param[1]) {
                             $limit = $beerCount - $param[0];
                         }
                     }
                     $limitstring = " LIMIT " . $limit;
                     if ($limit == 0)
                         $limitstring = "";
                     */
                     $beers = GetAllByCondition("Beers", array("DrinkerId" => $drinker->Id), " DrinkTime DESC ");
                     $resp = new BeerResponse();
                     foreach ($beers as $beer) {
                         $resp->Beers[] = new BeerEntity($beer);
                     }
                     return json_encode($resp);
                 } else {
                     return ReturnNotFound($param[0], "Drinker");
                 }
             }
         }
     }
     return ReturnError(LINK_INVALID);
 }
Пример #2
0
 function execute($param, $post)
 {
     try {
         if (count($param) > 0) {
             if ($param[0] == "act") {
                 $obj = json_decode($post["json"]);
                 if ($obj->Action == "delete") {
                     $guids = array();
                     foreach ($obj->Notes as $note) {
                         $guids[] = $note->Guid;
                     }
                     return ReturnBoolean($this->deleteNotes($obj->NoteTakerGuid, $obj->NoteCollectionGuid, $guids));
                 } else {
                     if ($obj->Action == "addorupdate") {
                         $newNotes = array();
                         $updateNotes = array();
                         $listId = $this->getNoteCollectionId($obj->NoteTakerGuid, $obj->NoteCollectionGuid);
                         if ($listId !== false) {
                             foreach ($obj->Notes as $note) {
                                 $existingNote = GetSingleByCondition("Notes", array("Guid" => $note->Guid, "NoteCollectionId" => $listId));
                                 if ($existingNote == null) {
                                     $newnote = new Note();
                                     $newnote->NoteCollectionId = $listId;
                                     $newnote->Guid = $note->Guid;
                                     $newnote->Content = $note->Content;
                                     $newnote->Description = $note->Description;
                                     $newnote->CreateTime = ConvertToDatabaseDateTime($note->CreateTime);
                                     $newnote->IsCompleted = $note->IsCompleted;
                                     $newNotes[] = $newnote;
                                 } else {
                                     $existingNote->Content = $note->Content;
                                     $existingNote->Description = $note->Description;
                                     $existingNote->CreateTime = ConvertToDatabaseDateTime($note->CreateTime);
                                     $existingNote->IsCompleted = $note->IsCompleted;
                                     $updateNotes[] = $existingNote;
                                 }
                             }
                             $res = InsertAll($newNotes);
                             $res &= UpdateAll($updateNotes);
                             return ReturnBoolean($res == 1);
                         }
                         return ReturnBoolean(false);
                     } else {
                         if ($obj->Action == "get") {
                             $listId = $this->getNoteCollectionId($obj->NoteTakerGuid, $obj->NoteCollectionGuid);
                             if ($listId !== false) {
                                 $notes = GetAllByCondition("Notes", array("NoteCollectionId" => $listId));
                                 $resp = new NoteResponse();
                                 foreach ($notes as $note) {
                                     $resp->Notes[] = new NoteEntity($note);
                                 }
                                 return ReturnJson($resp);
                             }
                             return ReturnBoolean(false);
                         } else {
                             return ReturnError(LINK_INVALID);
                         }
                     }
                 }
             }
         }
         return ReturnError(LINK_INVALID);
     } catch (\Exception $ex) {
         return ReturnError("Exception occured: " . $ex->getMessage());
     }
 }
 function execute($param, $post)
 {
     if (count($param) > 0) {
         if ($param[0] == "act") {
             $obj = json_decode($post["json"]);
             if ($obj->Action == "remove") {
                 $existingUserConnections = array();
                 foreach ($obj->UserConnections as $userConnection) {
                     $existingUserConnection = GetSingleByCondition("UserConnections", array("Guid" => $userConnection->Guid));
                     if ($existingUserConnection != null) {
                         $existingUserConnections[] = $existingUserConnection;
                     }
                 }
                 return ReturnBoolean(DeleteAll($existingUserConnections));
             } else {
                 if ($obj->Action == "add") {
                     $newUserConnections = array();
                     foreach ($obj->UserConnections as $userConnection) {
                         $existingUserConnection = GetSingleByCondition("UserConnections", array("Guid" => $userConnection->Guid));
                         if ($existingUserConnection == null) {
                             $newuserconnection = new NoteTaker();
                             $newuserconnection->Guid = $userConnection->Guid;
                             $newuserconnection->Color = $userConnection->Color;
                             $newuserconnection->Name = $userConnection->Name;
                             $newuserconnection->ConnectedUserGuid = $userConnection->ConnectedUserGuid;
                             $newuserconnection->UserGuid = $obj->Guid;
                             $newUserConnections[] = $newuserconnection;
                         }
                     }
                     return ReturnBoolean(InsertAll($newUserConnections));
                 } else {
                     if ($obj->Action == "addorupdate") {
                         $newUserConnections = array();
                         $updateUserConnections = array();
                         foreach ($obj->UserConnections as $userConnection) {
                             $existingUserConnection = GetSingleByCondition("UserConnections", array("Guid" => $userConnection->Guid));
                             if ($existingUserConnection == null) {
                                 $newuserconnection = new NoteTaker();
                                 $newuserconnection->Guid = $userConnection->Guid;
                                 $newuserconnection->Color = $userConnection->Color;
                                 $newuserconnection->Name = $userConnection->Name;
                                 $newuserconnection->ConnectedUserGuid = $userConnection->ConnectedUserGuid;
                                 $newuserconnection->UserGuid = $obj->Guid;
                                 $newUserConnections[] = $newuserconnection;
                             } else {
                                 $existingUserConnection->Color = $userConnection->Color;
                                 $existingUserConnection->Name = $userConnection->Name;
                                 $existingUserConnection->ConnectedUserGuid = $userConnection->ConnectedUserGuid;
                                 $existingUserConnection->UserGuid = $obj->Guid;
                                 $updateUserConnections[] = $existingUserConnection;
                             }
                         }
                         return ReturnBoolean(InsertAll($newUserConnections) && UpdateAll($updateUserConnections));
                     } else {
                         if ($obj->Action == "sync") {
                             $userConnectioncount = $this->countExistingUserConnections($obj);
                             if ($userConnectioncount !== $obj->ExpectedCount) {
                                 return ReturnBoolean(false);
                             }
                             $userConnections = GetAllByCondition("UserConnections", array("UserGuid" => $obj->Guid), "Name DESC", " LIMIT " . count($obj->UserConnections));
                             for ($i = 0; $i < count($userConnections); $i++) {
                                 if ($userConnections[$i]->Guid != $obj->UserConnections[$i]->Guid) {
                                     return ReturnBoolean(false);
                                 }
                             }
                             return ReturnBoolean(true);
                         } else {
                             return ReturnError(LINK_INVALID);
                         }
                     }
                 }
             }
         } else {
             if ($param[0] == "checkGUID" && isset($param[1])) {
                 $res = $this->checkForUniqueGuid($param[1]);
                 if ($res == 1) {
                     return ReturnBoolean(true);
                 }
                 return ReturnBoolean(false);
             } else {
                 if ($param[0] == "completeGUID" && isset($param[1])) {
                     $res = $this->getUniqueGuid($param[1]);
                     if ($res !== false) {
                         return ReturnJson(new GuidResponse($res));
                     }
                     return ReturnJson(new GuidResponse("00000000-0000-0000-0000-00000000000"));
                 } else {
                     if (ValidateGuid($param[0])) {
                         $userConnections = GetAllByCondition("UserConnections", array("UserGuid" => $param[0]), "Name DESC");
                         $resp = new UserConnectionResponse();
                         foreach ($userConnections as $userConnection) {
                             $resp->UserConnections[] = new NoteTakerEntity($userConnection);
                         }
                         return ReturnJson($resp);
                     }
                 }
             }
         }
     }
     return ReturnError(LINK_INVALID);
 }