How To Form Complex Where Filters In Loopback Using REST Style Syntax - rest

For consistency we need to use the REST URL style syntax (rather than JSON style syntax) when calling our Loopback apis. However, we are having difficulty structuring complex 'where' filters that include both 'and' and 'or' operators. There does not appear to be a way to group mixed conditional logic. For example, how would we create the equivalent of this SQL statement using REST syntax:
SELECT * FROM Customer WHERE (type='retail' OR type='nonprofit') AND terms='monthly'
For example, this does not work:
/api/customer?filter[where][or][0][type]=retail&filter[where][or][1][type]=nonprofit&filter[where][and][2][terms]=monthly

Try with something like this:
/api/customer?filter={
"where": {
"or": [
{"type":"retail"},
{"type":"nonprofit"},
"and": [{"terms":"monthly}]
]
}
}
Take a look to Where filter in "More complex example"

I am trying to work out a similar issue. Looks like you are pretty close, but I believe and is actually the top level comparison, and you are writing your query as if or is. So maybe..
/api/customer?filter[where][and][0][or][0][type]=retail&filter[where][and][0][or][1][type]=nonprofit&filter[where][and][1][terms]=monthly
I have no way to confirm this though as I do not have a functioning local environment with which to test it. Let us know if it works out. Thanks!
Update: Looks like this question has already been answered. https://github.com/strongloop/loopback/issues/3017 BLUF, it isn't possible in Loopback. Use the stringified JSON option.

Related

Is there another way way to replace paginate() with take()->get() where query param is present? (Laravel 9)

Usually I use paginate when I want the user to view a list (or a narrowed down list based on filters). Simple example below:
Thing::query()
->orderByDesc('created_at')
->paginate(40);
If I wanted the user to view a short list, like get the five newest models, I would create a separate api with a query like below:
Thing::query()
->orderByDesc('created_at')
->take(5)
->get();
I want to combine the two eloquent queries in such a way that it gets the paginated list by default, but will take 5 if the query param 'take=5' is present. I can do this the following way:
Thing::query()
->orderByDesc('created_at')
->when(
$request->query('take'),
fn ($query, $count) => $query->take((int)$count)->get(),
fn ($query) => $query->paginate(50)
);
The above works but has been described by a colleague as a little confusing, since the 3rd argument to when() is if the first argument is false (documentation) but that isn't immediately apparent when viewing the code. The "confusing" part might be subjective here but I would like to make sure my code is quickly understood by other devs as best as possible.
Does anyone know of a simpler/clearer or just another way to achieve this? In an ideal world the take()->get() would only exist in the when() method and paginate() would exist outside of it, but be overridden by the when() condition if true.
Note: I anticipate some people might say that they should remain as separate api's, however in my opinion the extra logic here is so simple that the gain in reduced code outweighs the gain in "do one thing well".

How to chain multiple assertThat statement in AssertJ

Here is an example:
assertThat(commentById.getId()).isNotNull();
assertThat(commentById.getContent()).isNotBlank();
assertThat(commentById.getAuthor()).isNotNull();
assertThat(commentById.getAuthor().getUsername()).isNotBlank();
assertThat(commentById.getAuthor().getAvatar()).isNotBlank();
assertThat(commentById.getAuthor().getId()).isNotNull();
Is there anyway to chain this into a single assertThat statement
Sorry for the unclear question. I mean, is there some fluent method calls to chain multiple assertThat statement together. Here is an example I can think of:
assertThat(commentById)
.isNotNull()
.and(Comment::getID).isNotNull()
.and(Comment::getContent).isNotBlank()
.and(Comment::getAuthor).is(author->{
author.isNotNull()
.and(User::getID).isNotNull()
.and(User::getAvatar).isNotBlank()
.and(User::getUsername).isNotBlank()
});
You can utilize satisfies method:
assertThat(commentById.getId()).isNotNull();
assertThat(commentById.getContent()).isNotBlank();
assertThat(commentById.getAuthor()).isNotNull().satisfies(author -> {
assertThat(author.getUsername()).isNotBlank();
assertThat(author.getAvatar()).isNotBlank();
assertThat(author.getId()).isNotNull();
});
This helps to eliminate repeating parts of code while testing nested structures.
If you want the commentById object itself to be tested by "one-liner", it is theoretically possible to apply same approach onto it (assertThat(commentById).satisfies(c -> {assertThat(c.getId()).isNotNull(); ...})), however I state it here only to literally answer your question, actually I don't see any benefit of such expression.
This is not possible at the moment, what is possible is to use extracting but that implies navigating from the current actual to the extracted one without being able to go back to the original actual.

Swift3 URL from URLComponents, how to add OR query items

Im constructing a URL from query items using URLComponents and I want to add some query items as OR conditions rather than AND. Im not sure what the proper terminology for this is. Anyway I would like the following, roughly
website.com/things?param1=thing&param2=thing|param3=thing|param4=thing
but appending query items i can only get
website.com/things?param1=thing&param2=thing&param3=thing&param4=thing
My goal is to check 3 different parameters for the term I pass in, and return any results that match from any of the 3. If I was constructing the url from a string, I could just use a pipe instead of ampersand (I think - please correct me if wrong), but Im using URLComponents and am not sure how to do this.
Perhaps Im going about this incorrectly. I dont have a ton of experience with this. If this is the wrong approach, please point me in the right direction. Im not sure how to word this question appropriately and that makes it hard to search for an answer
Im not sure what the proper terminology for this is
There is no terminology for it; it doesn't exist. What you're trying to do is nonstandard. There is no such thing as a query item OR condition. Standard separators are semicolon and ampersand, with ampersand used almost universally. You can't use a pipe to separate query items.
Thus, for example, if you paste website.com/things?param1=thing&param2=thing|param3=thing|param4=thing into the parser at http://www.freeformatter.com/url-parser-query-string-splitter.html, it doesn't know what to make of the pipes; it thinks that param2 must be thing|param3=thing|param4=thing.
Thus, URLComponents is not going to insert the pipe for you. Its goal and purpose is to make a valid URL, and you are attempting to make an invalid one.

Does Fiddler's QuickExec support logic operator?

I want to input the following command in QuickExec(e.g., using logic operator 'and')
=404 and ?/path/
Namely, the sessions which response
status equal to 404 and the keyword '/path/' appears in URL will be selected in Fiddler.
Does anyone know how to implement/support these operators in Fiddler?
Nope, logical operators aren't supported today. It's a fun idea though and the base case (=404 /partialpath) is something I'll probably build in soon.

Find string like 0-9 or plus or space or hypen in T-Sql?

At the moment I have it as
Example like '[0-9+ ]' OR Example like '%-%'
is it possible to have this in 1 like statement? or there is a good resource anywhere, where i can read up instead of having to ask possibly stupid questions?
Thanks
Actually you can combine all in the one LIKE operator as LIKE '%[0-9+ -]%'
Possible options of LIKE usage can be found here: http://technet.microsoft.com/en-us/library/ms179859.aspx