I needed to implement some business logic on the server side during the process of fetching entities to ensure that the user had permission to the item being retrieved.
To accomplish this, my client-side breeze query code changed from something like:
var query = breeze.EntityQuery
.from(theUrl)
.expand("RelatedEntity")
.where(breeze.Predicate.create("id", "==", id));
to:
var query = breeze.EntityQuery
.from(theUrl)
.withParameters({ id: id })
.expand("RelatedEntity");
On the server side, my controller action changed from:
[HttpGet]
[BreezeQueryable]
public IQueryable<MyEntity> MyEntities()
{
return _uow.MyEntityRepo.All();
}
to something like:
[HttpGet]
[BreezeQueryable]
public IHttpActionResult FindById(int id)
{
var userId = HttpContext.Current.User.Identity.GetUserId();
var hasPermission = CheckPermission(id, userId); // some implementation ...
if (hasPermission) {
var myEntity = _uow.MyEntityRepo.GetById(id);
return Ok(myEntity)
} else {
return NotFound();
}
}
I can see the query come across the wire with the filter:
http://localhost:42789/breeze/MyEntity/FindById?$expand=RelatedEntity&id=1002
However, RelatedEntity
is undefined. When using the EntityQuery
, but not withParameters
, the related entity expands fine and is available in the result set.
Thank you