Passing Lists as a QueryString - should I use JSON - asp.net

Lets say I've got an online dating site, users can filter the user list based on various criteria, Height, Age, BodyType, Ethnic Origin....
I want to pass the criteria to the pager, via QueryString. Height and Age are easy as these are ranges, and I would use
MinHeight=3&MaxHeight=12&MinAge=21&MaxAge=30
However other Criteria like BodyType and Ethnic orgins are Lists of ForeignKey values e.g:
Ethnitity:2,3,5
What is the best way to pass these as a QueryString? Should I convert it to a Json string eg:
www.site.com?filterjson={\"minage\":0,\"maxage\":0,\"minheight\":0,\"maxheight\":0,\"bodytypelist\":[1,2,3],"ethnicitylist\":[2,3,4],\"eyecolorlist\":[],\"haircolorlist\":[],\"orientationlist\":[]}
Or is this not-valid/overkill/too complex?
Maybe something like this:
MinHeight=3&MaxHeight=12&bodytypes=1,2,3&
and parse the list values by splitting the ','?????
I don't know the ups and downs of all these ideas. So how would you pass a list of values in a querystring?

Using comma-separated values is the most pragmatic approach in my opinion. You can use this code to split values:
if (!string.IsNullOrEmpty(Request.QueryString["bodytypes"]))
{
string[] rgs = Request.QueryString["bodytypes"].Split(new char[] { ',' });
}

Both will work, though querystring is much easier to be 'hacked'. However if you have it well protected from malicious/unexpected values, I say it's fine.
Consuming data via querystring is relatively more straightforward than from JSON.

Related

Loop in Visual Query (or other solution)

Hello 2sxc / SIC fans!
A little help would be appreciated on that ...
I use JavaScript Kendo UI components to display data in a grid. These data are transcribed in Json in order to benefit from the connection on the data source of a hierarchical grid.
On the one side, I have an "employees" table with their attributes { id, lastName, firstName, email }. On the other hand, I have a list of training "events" { id, date, title, level }.
So, I would like to add the date and title of the last training received into the attributes of each person. So I would like the output Json to be after processing { id, lastName, firstName, email, dateLastTraining, titleLastTraining }.
One solution would be to re-manipulate the Json objects once I retrieve them in Razor but I would like to know if it was possible to do it directly at the source in the Visual Query.
I doubt this can be done because there should be a concept of loop (for each employee, filter the trainings according to the Id, sort them in order of date and take the Top1) and I think that there is no such possibility.
Am I wrong or would there be a solution to do that in Visual Query?
Thanks for your help !
Basically what you want is a kind of join-operation. ATM (2sxc 9.14) this isn't possible in visual query.

Retrofit POST with two multidimenional arrays, how to send?

I have this API and I need to send some data to it as follows:
www.abc.com/orders/new?items[][id]=1001&items[][quantity]=2
So I have to pass in these arrays
items[][id]=1001
items[][quantity]=2
If there are multiple items, it should look like this
items[][id]=1001 & items[][quantity]=2
& items[][id]=1002 & items[][quantity]=3
So this is the problem I am having because I am aware that #Query can accept arrays but the arrangement needs to be element of ids first and then element of quantities. I cannot have all ids first and then quantities which is what happens.
#POST("/orders/new")
void send(#Query("items[][id]") String...ids, #Query("items[][quantity") String...quantity)
Please note I omitted the callback for simplicity.
I have also tried with lists, whereby the entire list contains all the needed information in the correct arrangement but then came the problem of only having a single #Query annotation i.e. "items[][id]" and "items[][quantity]"
Use #QueryMap. It is like a HashMap. From retrofit documentation, we have this:
For complex query parameter combinations a Map can be used.
#GET("/group/{id}/users")
List<User> groupList(#QueryMap Map<String, String> options);
So, in your case, the key of the map will be the id, and the value will be the quantity.

Filter to Group URL on Visitors Flow

I have found a similar question earlier here:
Google Analytics Visitors Flow: grouping URLs?
However I'm confused because people suggest different way to write the Replace String, and either way I try it am not able to make it work.
So I have a ecommerce site with hundreds of different pages. The different parts of the website is:
http://example.com/sv/ (Root)
http://example.com/sv/category/1-name/
http://example.com/sv/product/1-name/
http://example.com/sv/designer-tool/1-name/
http://example.com/sv/checkout/
When I go to the visitors flow. I want to see the amount of people that go from example Root to Category, and from Category to Product, and from Product to Designer Tool, and from Designer Tool to Checkout. However now when I have so many different pages it becomes very difficult to follow the visitors flow, because the product pages are for example not grouped together.
So instead of above. I would like to remove the 1-name/ part in the end. And only see /sv/category/, /sv/product/, /sv/designer-tool/.
In the earlier post I understand you can use an advanced filter to do this. I have set the following settings:
Type: Search & Replace
Field: Request URI
Search String: ^/(category|product|designer-tool)(/\d*)(.*)
Replace String: /$A1$A3
I guess that my search string and my replace string is wrong. Any ideas?
EDIT: I updated my filter to the following:
Search String: ^/sv/(category|product|designer-tool)(/\d*)(.*)$
Replace String: /sv/\1/
Still testing and unsure if it's the correct way to set it up.
I was able to solve this by the Search String and the Replace String in my edit above.
So basically what I did was:
Create a secondary view/profile for your site. If you apply your filter to your one and only view/profile that means that you won't be able to see any detailed data about specific pages, because the filter removes/filter that.
Add an Advanced Filter with the following settings:
Type: Search & Replace
Field: Request URI
Search String: ^/sv/(category|product|designer-tool)(/\d*)(.*)$
Replace String: /sv/\1/
You need to wait 24h after creating your new profile/view before you can see any data in it.
So my confusion was regarding the Search and Replace String. The Search String is an regular expression for matching everything after your .tld. So for example http://www.example.com/sv/mypage/1-post/, the Search String will only search within /sv/mypage/1-post/.
The Replace String is what it should replace the whole Search String with. So in my case, I matched all URL's that had /sv/category/1-string/. I wanted only to keep the "category" part, so I replaced the whole string with /sv/category/ by inputting Replace String /sv/\1/
/sv/ means just what it says. \1 means that it should take the value of the first () of my Search String (In this case "category"). The ending / is just an ending slash.
All in all, it means that any URLs that looked like http://example.com/sv/category/1-string/ was changed to http://example.com/sv/category/. Meaning that I can now see data for all my categories as a group, instead of individual pages.

How does one convert string to number in JDOQL?

I have a JDOQL/DataNucleus storage layer which stores values that can have multiple primitive types in a varchar field. Some of them are numeric, and I need to compare (</>/...) them with numeric constants. How does one achieve that? I was trying to use e.g. (java.lang.)Long.parse on the field or value (e.g. java.lang.Long.parseLong(field) > java.lang.Long.parseLong(string_param)), supplying a parameter of type long against string field, etc. but it doesn't work. In fact, I very rarely get any errors, for various combinations it would return all values or no values for no easily discernible reasons.
Is there documentation for this?
Clarification: the field is of string type (actually a string collection from which I do a get). For some subset of values they may store ints, e.g. "3" string, and I need to do e.g. value >= 2 filters.
I tried using casts, but not much, they do produce errors, let me investigate some more
JDO has a well documented set of methods that are valid for use with JDOQL, upon which DataNucleus JDO adds some additional ones and allows users to add on support for others as per
http://www.datanucleus.org/products/accessplatform_3_3/jdo/jdoql.html#methods
then you also can use JDOQL casts (on the same page as that link).

Using / in query strings

I am trying to use query strings in ASP.NET. I have a requirement of the following format
http://localhost/website/1/?callback=?
Here 1 denotes the ID of the profile. This means some info from id=1 will be fetched through the string
If this would have been website/2/?callback=? , then the id would be 2. My questions is to how do I use this /id/ as a query string so it can be used to fetch the profile ID. This was my first preference to use /id/ format otherwise I could look into fetching using two ?'s
If the id =1, I want to fetch ID=1 particulars from DB on this page. http://localhost/website/1/?callback=?
In your case the ID is in the PATH, not the query string. You can access the path via Request.Path in an ASPX page. From there you would need to do some string parsing to get at the portion of the path where you expect the ID to be.
In your case I would probably use something like int.Parse(Request.Path.Split(new char[] {'/'}, StringSplitOptions.RemoveEmptyEntries)[1]), but please note that I've made that line pretty dense for brevity's sake. For starters, you should use int.TryParse() instead of int.Parse(). This code assumes that the ID will always be in the same place in the url. For example, it will work for "/website/2/" and "/user/2/", but not for "/website/somethingelse/2/".
UriTemplate might be a good choice for that sort of parsing. Not to mention, probably a bit more clear and explicit about what's happening.
Check out: http://msdn.microsoft.com/en-us/library/system.uritemplate(v=VS.90).aspx

Resources