B4J Question MiniORMS with handlers

Mashiane

Expert
Licensed User
Longtime User
Hi Fam

A couple of questions using MiniORMS with handlers.

This is based on this tutorial


In this code

B4X:
Sub Handle (req As ServletRequest, resp As ServletResponse)
    Request = req
    Response = resp
    Method = Request.Method.ToUpperCase
Dim FullElements() As String = WebApiUtils.GetUriElements(Request.RequestURI)
Elements = WebApiUtils.CropElements(FullElements, 3) ' 3 For Api handler
Select Method
Case "GET"
            ...
Case "POST"
            ...
Case "PUT"
            ...
Case "DELETE"
            ...
Case Else
Log("Unsupported method: " & Method)
            ReturnMethodNotAllow
Return
End Select
    ReturnBadRequest
End Sub

Q1. If for example my handler expects /records/users but a user enters /records how do I return an error message as the expected content does not meet the criteria?
Q2. For all METHOD calls, I'd like to (a) detect if there are some parameters provided. For example, on the GET call, I'd like to filter the records in my table based on the provided parameters. These could be to filter the records to return, sort them and perhaps limit the number of records returned?
Q3. For a DELETE call, if I pass parameters, how to I also detect that a parameter was specified and if its available I execute DELETE WHERE ? based on those parameters.
Q4. For a POST and a PUT, the record added/updated, how do I return the "id" field ONLY as JSON if the INSERT/UPDATE call was successful, and or when needed return the complete record including the new autoincrement id used.
Q5. In case of using own ID i.e. not autoincrement, these will be strings, how do I indicate the "id" type and also make the MiniORM use the specified id i provided?
 

aeric

Expert
Licensed User
Longtime User
Q1. If for example my handler expects /records/users but a user enters /records how do I return an error message as the expected content does not meet the criteria?

A1: You can check the length of URI elements.
B4X:
Dim FullElements() As String = WebApiUtils.GetUriElements(Request.RequestURI)
' Checking for URI elements in the format /records/{TableName}
Log(FullElements.Length)
If FullElements.Length < 3 Then
    ReturnBadRequest
    Return
End If
TableName = FullElements(2)
Elements = WebApiUtils.CropElements(FullElements, 3) ' 3 For Records Api handler
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Q2. For all METHOD calls, I'd like to (a) detect if there are some parameters provided. For example, on the GET call, I'd like to filter the records in my table based on the provided parameters. These could be to filter the records to return, sort them and perhaps limit the number of records returned?

A2: Let's take the following URL as example:

B4X:
Sub Handle (req As ServletRequest, resp As ServletResponse)
    Request = req
    Response = resp
    Method = Request.Method.ToUpperCase
    Dim parameters As Map = req.ParameterMap
    Log(parameters.Size)
    For Each key As String In parameters.Keys
        Dim values() As String = parameters.Get(key)
        For Each value As String In values
            Log($"${key}: ${value}"$)
        Next
    Next
End Sub

Logs:
B4X:
2
limit: true,10
limit: false
sort: name
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Eish, I keep forgetting that as programmers we start couting from zero... :D

I was like, we have 3 parameters, where does Aeric get 2 now.. ha ha ha...
No, there are indeed should be 3 parameters, this is not the idea of counting from 0.
We have 3 parameters but 2 are the same. Therefore it is 2 unique keys.
That is why the value is an array of String.
B4X:
Dim values() As String = parameters.Get(key)
You can hover your mouse pointer to see the documentation for req.ParameterMap.
 
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
No, there are indeed should be 3 parameters, this is not the idea of counting from 0.
We have 3 parameters but 2 are the same. Therefore it is 2 unique keys.
That is why the value is an array of String.
B4X:
Dim values() As String = parameters.Get(key)
You can hover your mouse pointer to see the documentation for req.ParameterMap.
I get that...

I was talking about

B4X:
Log(parameters.Size)
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
A couple of questions using MiniORMS with handlers.
I already answered 2 questions in this thread.

You see, the thread can be extended with discussions. Look at Q2.

Please open new question each (for Q3 to Q5) with specific or appropriate title.

Edit: So far, Q1 and Q2 are not related to MiniORM but questions more of related to Web API Server and B4J server.
 
Last edited:
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
Im understanding it bit by bit I guess

So the size will be 1, because the keys are the same value col

however one will have output like ...

B4X:
col: value1
col: value2
col: value3

Now I know there is another map called ParameterMap that works completely different than how I use maps.

You are right, its not counting from zero at all. Now I know.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Im understanding it bit by bit I guess

So the size will be 1, because the keys are the same value col

however one will have output like ...

B4X:
col: value1
col: value2
col: value3

Now I know there is another map called ParameterMap that works completely different than how I use maps.

You are right, its not counting from zero at all. Now I know.
In usual case, we may want to avoid repeated parameters with same key but the example is to demonstrate that B4J server has this feature.
As per your question 2, the size may not important. You only want to check whether the size is zero or not.
However, I think you need to understand how to make use of these parameters when working with more complex URLs.
 
Upvote 0

Mashiane

Expert
Licensed User
Longtime User
In usual case, we may want to avoid repeated parameters with same key but the example is to demonstrate that B4J server has this feature.
As per your question 2, the size may not important. You only want to check whether the size is zero or not.
However, I think you need to understand how to make use of these parameters when working with more complex URLs.
I think the other thing is whether the order of the parameters is maintained when getting them.

For example, if you want to execute a sort and you pass ?sort=firstname&sort=lastname&sort=age

You would want your select to be SELECT * FROM ... ORDER BY firstname,lastname,age

Does it maintain the order of the parameters where such are the same?
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I think the other thing is whether the order of the parameters is maintained when getting them.

For example, if you want to execute a sort and you pass ?sort=firstname&sort=lastname&sort=age

You would want your select to be SELECT * FROM ... ORDER BY firstname,lastname,age

Does it maintain the order of the parameters where such are the same?
I think the order is preserve but I can't guarantee.
Why not put something like this:
sort=firstname,lastname,age
 
Upvote 0
Top