API Platform filter entity data - symfony

I just start to use Api platform and immediately stuck with problem how to filter data.
I have entity User and i want to filter data that are present in response ( JSON API format)
{
"links": {
"self": "/api/users"
},
"meta": {
"totalItems": 2,
"itemsPerPage": 30,
"currentPage": 1
},
"data": [
{
"id": "/api/users/1",
"type": "User",
"attributes": {
"_id": 1,
"username": "jonhdoe",
"isActive": true,
"address": null
}
},
{
"id": "/api/users/3",
"type": "User",
"attributes": {
"_id": 3,
"username": "test",
"isActive": true,
"address": null
}
}
]
}
so I want to remove e.g. User with id 3, but not use filters sent via request. I just want to set filter that will be always run when someone go to /api/users.
I look to api-platform extensions but this will be applied on each request e.g. /api/trucks. So at end I just want to get something like
{
"links": {
"self": "/api/users"
},
"meta": {
"totalItems": 1,
"itemsPerPage": 30,
"currentPage": 1
},
"data": [
{
"id": "/api/users/1",
"type": "User",
"attributes": {
"_id": 1,
"username": "jonhdoe",
"isActive": true,
"address": null
}
}
]
}

As you pointed out, extensions are the way to go.
The applyToCollection method gets a $resourceClass parameter containing the current resource class.
So you can apply the WHERE clause only for a specific class in this method like:
public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null)
{
if (User::class === $resourceClass) {
$queryBuilder->doSomething();
}
// Do nothing for other classes
}

Related

Not able to filter out required property in Azure TSI Gen1 Get Events API response

I am using the below request body to fetch only the required property values.
"searchSpan": {
"from": {
"dateTime": "2021-11-20T00:00:00.000Z"
},
"to": {
"dateTime": "2021-11-20T23:00:00.000Z"
}
},
"predicateString": "[Params.Name] = 'power'",
"take": 100
}
}
The URL is like below:
https://12345678a-bcde-3e91-blah-2292933292aa.env.timeseries.azure.com/events?api-version=2016-12-12
Despite specifying the required property the response returns all properties as if it has not seen the predicate string. What might I be doing wrong?
{
"warnings": [],
"events": [
{
"schema": {
"rid": 0,
"$esn": "my-event-hub",
"properties": [
{
"name": "mytimestamp",
"type": "DateTime"
},
{
"name": "Params.Name",
"type": "String"
},
{
"name": "Params.Value",
"type": "Double"
}
]
},
"$ts": "2021-11-20T10:01:50Z",
"values": [
"2021-11-20T10:01:50Z",
"energy",
60
]
},
{
"schemaRid": 0,
"$ts": "2021-11-20T10:01:50Z",
"values": [
"2021-11-20T10:01:50Z",
"power",
10
]
},
{
"schemaRid": 0,
"$ts": "2021-11-20T10:01:50Z",
"values": [
"2021-11-20T10:01:50Z",
"strength",
200
]
},
]
}
Edit
I'm getting "Properties count error" in the TSI overview page. This might quite be the root cause but I don't know for sure
"For Time Series Insights environment ABC: You have used all 641/600 properties in your environment".

Graph API search by metadata / column value

I'm developping an application using MS Graph API on a sharepoint site.
I'm trying to build the URL that allow me to find a drive item in a library with a colum param.
Actually I built this get URL to retrieve the drive items in the library:
https://graph.microsoft.com/v1.0/sites/{mainSite}:/sites/{subsite}:/lists/{libname}/items
I obtain a JSON with all files.
{
"#odata.context": "https://graph.microsoft.com/v1.0/$metadata#Collection(microsoft.graph.list)('myLib')/items",
"value": [
...
{
"#odata.etag": "\"7773c0de-bed1-425b-9065-2310c600757b,6\"",
"createdDateTime": "2019-11-21T09:19:57Z",
"eTag": "\"7773c0de-bed1-425b-9065-2310c600757b,6\"",
"id": "24",
"lastModifiedDateTime": "2020-05-27T09:09:05Z",
"webUrl": "https://xxxx.sharepoint.com/sites/SUBSITE/myLib/Cartouche.docx",
"createdBy": {
"user": {
"email": "xxx#xxx.fr",
"id": "5fe24d57-1357-4fef-9cae-bfd79b0bf831",
"displayName": "David Vera"
}
},
"lastModifiedBy": {
"user": {
"email": "dvera#dsidiff.fr",
"id": "5fe24d57-1357-4fef-9cae-bfd79b0bf831",
"displayName": "David Vera"
}
},
"parentReference": {
"id": "1836034d-8a35-4488-995e-a7fe9cd30b4c",
"siteId": "xxx.sharepoint.com,82a2327c-e2cf-4d30-b529-31c3bcdcda5e,fbi3zrs9-b07a-4777-945b-3b42c5c876d"
},
"contentType": {
"id": "0x010100E4AD49D3A8873645A2803D49D2A4BF60"
}
},
...
]
}
I tried to use filter :
https://graph.microsoft.com/v1.0/sites/{mainSite}:/sites/{subsite}:/lists/{libname}/items?expand=fields(select=CODE_ANNONCEUR,val)&filter=fields/val eq 'myself'
It returns me :
{
"error": {
"code": "-1, Microsoft.SharePoint.Client.InvalidClientQueryException",
"message": "The expression « fields (select ») is not valid.",
"innerError": {
"request-id": "c3edc510-2d63-4f21-86cd-a377fb02d324",
"date": "2020-05-27T09:44:23"
}
}
}
I solved the issue this way :
https://graph.microsoft.com/v1.0/sites/xxx.sharepoint.com:/sites/yyy:/lists/zzz/items?expand=fields/CODE_ANNONCEUR&filter=fields/CODE_ANNONCEUR eq 'stringExpected'

Is there any way we can only validate field name not value in Pact json

The example below is the response. In this I just want to validate field not field value from the body.
When I do pact verify:
{
"provider": {
"name": "provider"
},
"consumer": {
"name": "consumer"
},
"interactions": [
{
"description": "A valid data read request",
"request": {
"method": "GET",
"path": "/v1/users",
"query": {
"user": [
"1"
]
}
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"body": {
"name": "xyz",
"work": "gen",
"age": "29",
}
},
"providerStates": [
{
"name": "user exists state"
}
]
}
],
"metadata": {
"pactSpecification": {
"version": "3.0.0"
},
"pact-jvm": {
"version": "4.0.4"
}
}
}
Only i need to validate field such as name, age and work not field value.
got solution we can achieve this using
final DslPart body = new PactDslJsonBody()
.stringType("name", "xyz")
.stringType("work", "gen")
.integerType("age", 29)
or for only optional
DslPart body = new PactDslJsonBody()
.eachKeyLike("name", PactDslJsonRootValue.stringType("xyz"))
.eachKeyLike("work", PactDslJsonRootValue.stringType("gen"))
.eachKeyLike("age", PactDslJsonRootValue.integerType(29))
.asBody()
and set this body to PactDslWithProvider body and we can have matching rule generated in the json so that when we verify only the field type and field name

Structure Firebase data to return array of objects

What is the best way to structure firebase database so that querying just one endpoint i.e admin.database().ref('categories').once() returns me a json of the form
{
"categories": [
{
"-ky1": {
"name": "Profile",
"id": "-ky1",
"sections": [{
"-ky1a": {
"name": "section1",
"id": "-ky1a",
"-ky1a1": {
"name": "field1",
"id": "-ky1a1"
}
},
"-ky1b": {
"name": "section2",
"id": "-ky1b",
"-ky1b1": {
"name": "fieldA",
"id": "-ky1b1"
}
}
}]
}
},
{
"-ky2": {
...
}
}
]
}
where ideally, I get back an array of nested objects.

JSON.NET - Getting nested values

I have a JSON something like this:
{
"key": "Target",
"value": {
"__type": "Entity:http://schemas.microsoft.com/xrm/2011/Contracts",
"Attributes": [
{
"key": "prioritycode",
"value": {
"__type": "OptionSetValue:http://schemas.microsoft.com/xrm/2011/Contracts",
"Value": 1
}
},
{
"key": "completeinternalreview",
"value": false
},
{
"key": "stepname",
"value": "10-Lead"
},
{
"key": "createdby",
"value": {
"__type": "EntityReference:http://schemas.microsoft.com/xrm/2011/Contracts",
"Id": "ca2ead0c-8786-e511-80f9-3863bb347b18",
"KeyAttributes": [],
"LogicalName": "systemuser",
"Name": null,
"RowVersion": null
}
}
]
}
}
How do the toke for the key/values by searching for the value of the key?
Eg I want to get the key value pair 'completeinternalreview'
Assuming you have a C# class like this to represent that attributes object from your JSON:
public class MyValue
{
[JsonProperty("Attributes")]
public List<KeyValuePair<string, object>> Attributes { get; set; }
}
you can simply deserialize the string:
var result = JsonConvert.DeserializeObject<KeyValuePair<string, MyValue>>(jsonString);
and then find the correct key-value pair with:
var kvp = result.Value.Attributes.Find(a => a.Value == "completeinternalreview");

Resources