Friday, August 28, 2015

web api response plain text formatter

1)Nuget to install WebAPIContrib text formatter, 


initialized it in the Application_Start/WebApiConfig.cs

at end add below line:
  config.Formatters.Add(new PlainTextFormatter());
2) In the controller add "text/plain" parameter.
 public HttpResponseMessage GetPlainText()
{
  return ControllerContext.Request.CreateResponse(HttpStatusCode.OK, "Test data", "text/plain");
}

Tuesday, August 18, 2015

mongodb geo query sample

Please make sure you typecast the lat-longs with float. Or else mongo extension will treat it as a string (surprise, surprise) and query will return assuming empty condition (this took forever me to find).
EDIT: Have a look at Cakephp mongo driver source for more ways to use geo queries.https://github.com/ichikaway/cakephp-mongodb/blob/master/samples/controllers/geos_controller.php



https://github.com/ichikaway/cakephp-mongodb/blob/cake2.2/samples/Controller/GeosController.php


/**
* GeoController class
*
* @uses AppController
* @package mongodb
* @subpackage mongodb.samples.controllers
*/
class GeosController extends AppController {
/**
* name property
*
* @var string 'Geo'
* @access public
*/
public $name = 'Geos';
/**
* index method
*
* @return void
* @access public
*/
public function index($type = null, $lat = null, $long = null, $opt1 = null, $opt2 = null) {
$params = array(
'limit' => 35,
'page' => 1,
);
if(!empty($type) && !empty($lat) && !empty($long)) {
$lat = floatval($lat);
$long = floatval($long);
$opt1 = floatval($opt1);
$opt2 = floatval($opt2);
switch($type) {
case('near'):
if(!empty($opt1)){
$cond = array('loc' => array('$near' => array($lat, $long), '$maxDistance' => $opt1));
} else {
$cond = array('loc' => array('$near' => array($lat, $long)));
}
break;
case('box'):
$lowerLeft = array($lat, $long);
$upperRight = array($opt1, $opt2);
$cond = array('loc' => array('$within' => array('$box' => array($lowerLeft, $upperRight))));
break;
case('circle'):
$center = array($lat, $long);
$radius = $opt1;
$cond = array('loc' => array('$within' => array('$center' => array($center, $radius))));
break;
}
$params['conditions'] = $cond;
} else {
$params['order'] = array('_id' => -1);
}
$results = $this->Geo->find('all', $params);
$this->set(compact('results'));
}
/**
* add method
*
* @return void
* @access public
*/
public function add() {
if (!empty($this->data)) {
$this->Geo->create();
if ($this->Geo->save($this->data)) {
$this->flash(__('Geo saved.', true), array('action' => 'index'));
} else {
}
}
}
/**
* delete method
*
* @param mixed $id null
* @return void
* @access public
*/
public function delete($id = null) {
if (!$id) {
$this->flash(__('Invalid Geo', true), array('action' => 'index'));
}
if ($this->Geo->delete($id)) {
$this->flash(__('Geo deleted', true), array('action' => 'index'));
} else {
$this->flash(__('Geo deleted Fail', true), array('action' => 'index'));
}
}
}