How to make a CRUD via REST

2

Introduction

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

I've seen a lot of videos and read many tutorials about Delphi's REST Client.

I was even able 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 listing or a search for a web service, they never do something like "Create a record and then list" or "Search for a record and update it".

Doubt

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

I would use only one RESTClient / RESTRequest / RESTResponse / ETC. And from there, perform the operations by code, changing the RESTRequest / ETC properties according to the need / screen, and do all the treatments at hand.

OU

I would have multiple RESTRequests / ETC, for every operation I might need (and who knows put them inside a DM).

Well, with this, I would be able to do the LiveBindings and do the data treatments without much suffering.

OU

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

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

    
asked by anonymous 05.05.2014 / 00:57

1 answer

1

Well, I ended up finding the answer.

The correct one would be to create several RESTRequest / ETC depending on your need.

If the component you are moving is using LiveBindings, create a specific RESTRequest / ETC for it, because then it becomes much easier to reload a list for example.

And for operations like "Add / Delete / ETC" that do not need to use anything specific on the screens, use a common RESTRequest / ETC for these operations, and modify the url code and specific parameters for the operations. / p>

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 name of the group.)

Note that this is my second RESTRequest, the first one does LiveBinding with a listView that loads all registered groups.

Then I create an event at the save button click.

I set the RootElement of my JSON.

I set the URL of my webservice.

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

Add the parameters that will be sent.

Execute a request (this is where the registry is added)

Then I display the return in a Memo.

In this case, I just need to re-run RESTRequest1 to reload the list of groups (which I have not done yet).

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

Thank you.

    
05.05.2014 / 17:06