How to make a CRUD via REST

Asked

Viewed 3,282 times

2

Introducing

I need to access a web service in PHP via mobile application.

I have seen several videos and read many tutorials about Delphi’s REST Client.

I even managed to develop a prototype of my application.


Problem

But the problem is that in all the materials I’ve seen, they show examples with a single functionality. All example applications perform a web service listing or search, never do something like "Create a record and then list" or "Search a record and update it".


Doubt

I would like to know what would be the best practice to perform a "CRUD" for example...

I would only use a Restclient/Restrequest/Restresponse/ETC. And from that, carry out operations by code, changing the properties of Restrequest/ETC according to need/screen, and do all treatments at hand.

OR

I would have several Restrequests/ETC, for every operation I might need (and who knows how to put them inside a DM).

Because with this, I would be able to do the Livebindings and do the data processing without much suffering.

OR

Whatever the way it is done, both forms would be correct and could be merged.

Ex: Create a Trestrequest to list "products", another to list "customers" and another to perform Create/Update/Delete operations

  • 1

    Daniel, here we do not use "solved" in the title. The correct is to mark your answer as "correct".

  • That’s right, thank you for the correction. Tomorrow I will mark the answer as "correct".

1 answer

1


Well, I just found the answer.

The correct thing would be to create several Restrequest/ETC depending on your need.

If the component you are working with is using Livebindings, create a specific Restrequest/ETC for it, because it is much easier to reload a list for example.

And for operations like "Add/Delete/ETC" that don’t need to use anything specific on the screens, use a common Restrequest/ETC for these operations, and modify in the code the url and parameters specific to the operations.


EX:

procedure TForm1.savebtnCreateClick(Sender: TObject);
var
    LJson: TJSONObject;
begin
    RESTResponse2.RootElement := 'object';

    RESTRequest2.Resource := 'v1/groupProductService';

    RESTRequest2.Method := TRESTRequestMethod.rmPOST;
    RESTRequest2.Params.AddItem('nome_grupo', nome_grupoEdit.Text, TRESTRequestParameterKind.pkGETorPOST);

    RESTRequest2.Execute;

    LJson := RESTRequest2.Response.JSONValue as TJSONObject;
    Memo1.Lines.Text := LJson.ToString;
end;

In this case, I am creating a record in the database in my group table (The only parameter is the group name).

Note that this is my second Restrequest, the first it does Livebinding with a listview that loads all registered groups.

Then I create an event at the click of the save button.

I define the Rootelement of my JSON.

I set the URL of my webservice.

I define what type of request it will execute (POST/PUT/ETC).

I add the parameters that will be sent.

Run a request (this is where the record is added)

Then I display the return in a Memo.

In this case, I would just need to run Restrequest1 again to reload the group list (which I have not yet done).


I hope this can help someone, and if anything I’ve said is wrong, please correct me.

Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.