Rest Api slim3 php7 Apache2 mysql mongo-replica
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

76 lines
2.6 KiB

<?php
use Slim\Http\Request;
use Slim\Http\Response;
use GuzzleHttp\Client as LClient;
use Http\Adapter\Guzzle6\Client as LAdapter;
use Psr\Http\Message\ResponseInterface as LResponseInterface;
use GuzzleHttp\Exception\RequestException as LRequestException;
use GuzzleHttp\Psr7\Request as LRequest;
use Http\Client\Exception;
use Nahid\JsonQ\Jsonq;
use Anddye\Validation\Validator;
use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Validator as v;
use MongoDB\Client;
$postCallbackhelloworld = function ($request, $response, $id) use ($app, $server, $MongoClient) {
// ****************** get user info **********
$token = $server->getAccessTokenData(OAuth2\Request::createFromGlobals());
$USER=(string)$token['client_id'];
// ****************** get user info **********
// ****************** Get data **********
$allGetVars = $request->getQueryParams();
$jsonget=trim($allGetVars["getvars"]);
$jsonget1=json_decode($jsonget,true);
$start=$jsonget1['start'];
$end=$jsonget1['end'];
// ****************** Get data **********
// ********************* VALIDATION *********************
// see post helloword
// ********************* VALIDATION *********************
// ****************** make obj for validate data **********
try {
// ****************** mongo **********
// example pagination
$mongo = $MongoClient;
$mplaybook = $mongo->lab_db->tasks;
$regex=new MongoDB\BSON\Regex('^' . preg_quote("$USER") . '$');
$mstart=new MongoDB\BSON\Regex('^' . preg_quote("$start") . '$');
$mto=new MongoDB\BSON\Regex('^' . preg_quote("$end") . '$');
$query = array(
'user' => $regex,
'datefrom' => array('$gt' => $start, '$lte' => $end),
'dateto' => array('$gt' => $start, '$lte' => $end),
);
$match = array( '$match'=>$query);
$findPlaybks = $mplaybook->aggregate(array($match));
$c=0;
foreach($findPlaybks as $playbook)
{
$rrr[$c]['_id']=$jsonget['_id'];
$datefrom = $jsonget['datefrom'].' '.$jsonget['timefrom'];
$dateto = $jsonget['dateto'].' '.$jsonget['timeto'];
$rrr[$c]['start']=$datefrom;
$rrr[$c]['end']=$dateto;
$c++;
}
// ****************** mongo **********
// ****************** return data **********
$rr["data"]= $rrr;
$rr["query"]=$query;
$rr["start"]=$start;
$rr["error"][]="ok";
} catch(ValidationException $exception) {
$rr["error"][]=$exception->getMainMessage();
}
$response->getBody()->write(json_encode($rr));
return $response;
};
$app->get('/helloworld', $postCallbackhelloworld)->add($authMiddleware->withRequiredScope(['superuser', 'owner']));