Browse Source

Service Addition , Removal , Login Updation

Anand Ayyappan 1 year ago
parent
commit
719caf65a3
3 changed files with 68 additions and 31 deletions
  1. 33 14
      api/v1/addService/index.php
  2. 2 3
      api/v1/loginProvider/index.php
  3. 33 14
      api/v1/removeService/index.php

+ 33 - 14
api/v1/addService/index.php

@@ -1,19 +1,38 @@
 <?php
-require_once("../../../mdb/vendor/autoload.php");
+require_once("../vendor/autoload.php");
+
 use MongoDB\BSON\ObjectId;
 use MongoDB\Client;
+use Firebase\JWT\JWT;
+use Firebase\JWT\Key;
 
-// Requires the MongoDB PHP Driver
-// https://www.mongodb.com/docs/drivers/php/
-
-$client = new Client('mongodb://kpadmin:AnandAyyappan1981@127.0.0.1:27017/?authSource=kpmaster');
-$collection = $client->selectCollection('kpmaster', 'kpu');
-//$cursor = $collection->aggregate([['$geoNear' => ['near' => ['type' => 'Point', 'coordinates' => [76.93, 8.52]], 'spherical' => true, 'query' => ['srvs' => ['$all' => ['Painting']]], 'distanceField' => 'Dis', 'maxDistance' => 2000, 'includeLocs' => 'loc']], ['$project' => ['ID'=>'$oid', 'Name' => '$fn', 'Distance' => '$Dis', 'Location' => '$loc']]]);
-$updateResult = $collection->updateOne(
-   [ '_id' => new ObjectId('64b0f61b8b3742ad72109e80') ],
-   [ '$push' => [ 'srvs' => 'Running' ]]
-);
+$userInput = file_get_contents('php://input');
+$userInputData = json_decode($userInput, true);
+if ($userInputData !== null) {
+   $headers = explode(" ", trim($_SERVER["HTTP_AUTHORIZATION"]));
+   $key = '97eda3db6f898bfb1f821178d1c27064';
+   $jwt = $headers[1];
+   $decoded = JWT::decode($jwt, new Key($key, 'HS256'));
+   //print_r($decoded);
+   $client = new Client('mongodb://kpadmin:AnandAyyappan1981@127.0.0.1:27017/?authSource=kpmaster');
+   $collection = $client->selectCollection('kpmaster', 'kpu');
+   //$cursor = $collection->aggregate([['$geoNear' => ['near' => ['type' => 'Point', 'coordinates' => [76.93, 8.52]], 'spherical' => true, 'query' => ['srvs' => ['$all' => ['Painting']]], 'distanceField' => 'Dis', 'maxDistance' => 2000, 'includeLocs' => 'loc']], ['$project' => ['ID'=>'$oid', 'Name' => '$fn', 'Distance' => '$Dis', 'Location' => '$loc']]]);
+   $updateResult = $collection->updateOne(
+      ['_id' => new ObjectId($decoded->kpid)],
+      ['$addToSet' => ['srvs' => $userInputData["service"]]]
+   );
 
-printf("Matched %d document(s)\n", $updateResult->getMatchedCount());
-printf("Modified %d document(s)\n", $updateResult->getModifiedCount());
-?>
+   if ($updateResult->getModifiedCount() == 1) {
+      $message['status'] = true;
+      $message['statusMessage'] =  "Service Added Successfully";
+   } else {
+      $message['status'] = false;
+      $message['statusCode'] = 'KP005';
+      $message['statusMessage'] =  "Service Addition Failed";
+   }
+} else {
+   $message['status'] = false;
+   $message['statusCode'] = "KP001";
+   $message['statusMessage'] = "Data Not Received";
+}
+echo json_encode($message);

+ 2 - 3
api/v1/loginProvider/index.php

@@ -1,10 +1,9 @@
 <?php
 require_once("../vendor/autoload.php");
-
 use MongoDB\Client;
-use MongoDB\BSON\ObjectId;
+//use MongoDB\BSON\ObjectId;
 use Firebase\JWT\JWT;
-use Firebase\JWT\Key;
+//use Firebase\JWT\Key;
 //print_r($_REQUEST);
 $userInput = file_get_contents('php://input');
 $userInputData = json_decode($userInput, true);

+ 33 - 14
api/v1/removeService/index.php

@@ -1,19 +1,38 @@
 <?php
-require_once("../../../mdb/vendor/autoload.php");
+require_once("../vendor/autoload.php");
+
 use MongoDB\BSON\ObjectId;
 use MongoDB\Client;
+use Firebase\JWT\JWT;
+use Firebase\JWT\Key;
 
-// Requires the MongoDB PHP Driver
-// https://www.mongodb.com/docs/drivers/php/
-
-$client = new Client('mongodb://kpadmin:AnandAyyappan1981@127.0.0.1:27017/?authSource=kpmaster');
-$collection = $client->selectCollection('kpmaster', 'kpu');
-//$cursor = $collection->aggregate([['$geoNear' => ['near' => ['type' => 'Point', 'coordinates' => [76.93, 8.52]], 'spherical' => true, 'query' => ['srvs' => ['$all' => ['Painting']]], 'distanceField' => 'Dis', 'maxDistance' => 2000, 'includeLocs' => 'loc']], ['$project' => ['ID'=>'$oid', 'Name' => '$fn', 'Distance' => '$Dis', 'Location' => '$loc']]]);
-$updateResult = $collection->updateOne(
-   [ '_id' => new ObjectId('64b0f61b8b3742ad72109e80') ],
-   [ '$pull' => [ 'srvs' => 'Washing' ]]
-);
+$userInput = file_get_contents('php://input');
+$userInputData = json_decode($userInput, true);
+if ($userInputData !== null) {
+   $headers = explode(" ", trim($_SERVER["HTTP_AUTHORIZATION"]));
+   $key = '97eda3db6f898bfb1f821178d1c27064';
+   $jwt = $headers[1];
+   $decoded = JWT::decode($jwt, new Key($key, 'HS256'));
+   //print_r($decoded);
+   $client = new Client('mongodb://kpadmin:AnandAyyappan1981@127.0.0.1:27017/?authSource=kpmaster');
+   $collection = $client->selectCollection('kpmaster', 'kpu');
+   //$cursor = $collection->aggregate([['$geoNear' => ['near' => ['type' => 'Point', 'coordinates' => [76.93, 8.52]], 'spherical' => true, 'query' => ['srvs' => ['$all' => ['Painting']]], 'distanceField' => 'Dis', 'maxDistance' => 2000, 'includeLocs' => 'loc']], ['$project' => ['ID'=>'$oid', 'Name' => '$fn', 'Distance' => '$Dis', 'Location' => '$loc']]]);
+   $updateResult = $collection->updateOne(
+      ['_id' => new ObjectId($decoded->kpid)],
+      ['$pull' => ['srvs' => $userInputData["service"]]]
+   );
 
-printf("Matched %d document(s)\n", $updateResult->getMatchedCount());
-printf("Modified %d document(s)\n", $updateResult->getModifiedCount());
-?>
+   if ($updateResult->getModifiedCount() == 1) {
+      $message['status'] = true;
+      $message['statusMessage'] =  "Service Removed Successfully";
+   } else {
+      $message['status'] = false;
+      $message['statusCode'] = 'KP005';
+      $message['statusMessage'] =  "Service Removal Failed";
+   }
+} else {
+   $message['status'] = false;
+   $message['statusCode'] = "KP001";
+   $message['statusMessage'] = "Data Not Received";
+}
+echo json_encode($message);