RAD Server CRUD Procedures – Part 3
In this RAD Server CRUD Procedures – Part 3 post we will show how to implement a FireMonkey (FMX) Client to call the RAD Server CRUD procedures and interact with the Employee Table.
In the part 2 post, we discussed modifying the default generated CRUD procedure implementations from the RAD Server Package Wizard, to show one possible way to implement Get, GetItem, Post, Put and Delete.
In the part 1 post, we discussed the the default generated CRUD procedure implementations from the RAD Server Package Wizard.
The Embarcadero RAD Server (also known as EMS – Enterprise Mobility Server) uses the industry standards of REST, and to transfer data to and from the RAD Server REST Service, JSON – JavaScript Object Notation, is typically used as the transfer encoding.
Due to the Embarcadero RAD Server supporting industry standard REST and JSON, we will use Delphi’s, C++ Builder and/or RAD Studio’s REST Client Library components (TRESTClient, TRESTRequest, and TRESTResponse ) to implement our EMS REST Client.
And recall from Post 2, we used the System.JSON Classes (TJSONArray and TJSONObject) on the back-end RAD Server to implement our CRUD procedures.
Employees REST Client
For our main REST Client UI form, we have all of the ten (10) columns (fields) from our Employee Table, mostly as TEdit controls for First Name, Last Name, etc., and TSpinBox for the Employee Number, Department Number, and Salary, and one TDateEdit for the HireDate:
The bottom panel has 10 TButtons. These buttons will perform the following functions from our RAD Server CRUD procedures:
READ: performs the REST HTTP GET, to return all records from the Employee table.
PREV: Displays the previous record.
NEXT: Displays the next record.
FIRST: Displays the first record.
LAST: Displays the last record.
CREATE: Starts the create mode, to allow the user to use the form to enter a new record.
COMMIT: The commit button gets enabled by the CREATE button, and this COMMIT button makes the REST HTTP POST call to the RAD Server to INSERT a new record into the Employee table.
EDIT: Starts the Edit mode for the UI form, similar to how Create enter a create mode, allow the user to edit a record.
UPDATE: The update button gets enabled by the EDIT button, and after we have made edits to the record, this Update button makes the REST HTTP PUT call to the RAD Server to UPDATE this existing record in the Employee table.
DELETE: This Delete button, let’s us delete the current EMP_NO from the Employee table, by making the REST HTTP DELETE call to the RAD Server to DELETE this EMP_NO from the Employee table.
With the COMMIT and UPDATE buttons, we do perform their REST calls, and then we perform another GET (READ) call, to get all of the records again. This was just done just for simplicity reasons.
This REST Client application also uses the REST Client Library components; RESTClient, RESTRequest and RESTResponse:
The Embarcadero REST Client Library is a framework for accessing REST-based web services, like the Embarcadero RAD (EMS) Server. The library is available for all platforms that are supported by Delphi and/or C++ Builder The REST Client Library framework focuses on JSON as the representation format.
Let’s now take a look at the implementation code for this Employees REST Client.
READ (GET)
The source code for the READ (Get) button is this:
// Make request.
RESTRequest.Execute;
//- Decode the data into a class global
if assigned(fJSONArray) then begin
fJSONArray.DisposeOf;
end;
fJSONArray := TJSONObject.ParseJSONValue(RESTResponse.Content) as TJSONArray;
//- set index.
fCurrentIndex := 0;
//- Load current resource to form.
JSONToForm;
end;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
procedure TfrmMain.btnReadClick(Sender: TObject); var Param: TRESTRequestParameter; begin //- Request data from ’employees’ end-point. RESTRequest.Resource := ’employees’; RESTRequest.Method := TRESTRequestMethod.rmGet; RESTRequest.Response := RESTResponse;
// Make request. RESTRequest.Execute;
//- Decode the data into a class global if assigned(fJSONArray) then begin fJSONArray.DisposeOf; end; fJSONArray := TJSONObject.ParseJSONValue(RESTResponse.Content) as TJSONArray;
//- set index. fCurrentIndex := 0;
//- Load current resource to form. JSONToForm; end; |
What we are doing in this code for the READ is:
1. We are creating a REST request.
2. The resource on the RAD Server that we want is ‘employees‘ . So we set the Resource property of the TRestRequest to ‘employees‘ ( RESTRequest.Resource := ’employees’; )
3. Next we set the Method property of the TRestRequest to Get ( RESTRequest.Method := TRESTRequestMethod.rmGet; ).
4. Then we set the Response component, to receive the data back from the request ( RESTRequest.Response := RESTResponse; )
5. The Request and Response components from the REST Client Library, are TRestRequest and TRestResponse, that we placed on the main UI form. At run-time we modify their properties as needed.
6. The other REST Client Library component is the REST Client (TRestClient), also added to the main UI form. The TRestCient represents your HTTP Client. So, what you do with these REST Client Library components is:
6a. Using the Object Inspector, for the TRestClient, set the BaseURL to the IP address and Port of your running RAD Server. In my case it’s http://127.0.0.1/8080 The loopback address on port 8080:
6b. for the TRestRequest, set the Resource to what you wish to use. In my case it’s the ‘employees‘ resource. And you also set its Response property to a Response component. In my case, to RESTResponse, to receive the data back from the server:
6c. If we look at the Content property on the TRestResponse, we see that it’s value is empty.
6d. Run the RAD Server Development Server we created from the Part 2 post:
6d. Right-click on the TRestResponse component and select Execute, we get a Response 200-OK :
This executed the request for the Employees data. So, now if you look at the response in the Content property of the TRestResponse, the content now has our Employee records, as a JSON Array:
So we used these REST Client Library visual components to create a connection to the RAD Server, and then in the code, at run-time, we modify the parameters, for the needed resource, HTTP Method, and correct Response.
7. Next, we execute the Request: ( RESTRequest.Execute; ).
8. Next, we decode the response we get back from the request. Now, recall, from the part 2 post, that the response will be a JSON Array containing all of the Employee table records, for the READ (Get) procedure: fJSONArray := TJSONObject.ParseJSONValue(RESTResponse.Content) as TJSONArray;
9. So we decode all the employee records into a TJSONArray which is at global scope:
private fJSONArray: TJSONArray; fCurrentIndex: int32; |
fJSONArray: TJSONArray, which is a private member of my class that is going to contain all of the employee data.
10. Next, we call this JSONToForm procedure:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
procedure TfrmMain.JSONToForm; var O: TJSONObject; begin ClearForm; if fJSONArray.Count>0 then begin O := fJSONArray.Items[fCurrentIndex] as TJSONObject; spedEmployeeNumber.Value := StrToInt( o.GetValue(‘EmployeeNumber’).ToString ); edFirstName.Text := o.GetValue(‘FirstName’).Value; edLastName.Text := o.GetValue(‘LastName’).Value; if assigned(o.GetValue(‘PhoneExt’)) then begin edtPhoneExt.Text := o.GetValue(‘PhoneExt’).Value; end; spedDepartmentNumber.Value := StrToFloat( o.GetValue(‘DepartmentNumber’).Value ); edtJobCode.Text := o.GetValue(‘JobCode’).Value; edtJobGrade.Text := o.GetValue(‘JobGrade’).Value; edtJobCountry.Text := o.GetValue(‘JobCountry’).Value; spedSalary.Text := o.GetValue(‘Salary’).Value; dtedHireDate.DateTime := StrToDateTime( o.GetValue(‘HireDate’).Value ); end; end; |
This JSONToForm procedure loops through the fJSONArray, and copies the values from that array to the various edit controls on the form. Another option would be to use Live Bindings to bind the data to the controls, but this JSONToForm procedure is a manual way of doing this.
Live Bindings was introduced in Delphi, C++ Builder, RAD Studio XE3 primarily as a way to link FireMonkey Controls (that don’t use the TDataSource model) to data, but also allow any object to be bound to and shown visually.
Testing FMX REST Client READ procedure
1. With the RAD Server running, start the Employees Client, click the READ button, you should see the Employee data displayed on the UI form, like this:
2. And on the Log on the RAD Development Server, you should see the Request for ’employees’ Resource, with the GET Endpoint, using the Get HTTP Method, like this:
NEXT button
To display the next record in the JSON array(fJSONArray), we implemented with this code:
procedure TfrmMain.btnNextClick(Sender: TObject); begin if fCurrentIndexpred(fJSONArray.Count) then begin inc(fCurrentIndex); end; JSONToForm; end |
Prev Button
To display the previous record in the JSON array (fJSONArray), we implemented with this code:
procedure TfrmMain.btnPrevClick(Sender: TObject); begin if fCurrentIndex>0 then begin dec(fCurrentIndex); end; JSONToForm; end; |
First Button
To display the first record in the JSON array (fJSONArray), we implemented with this code:
procedure TfrmMain.btnFirstClick(Sender: TObject); begin fCurrentIndex := 0; JSONToForm; end; |
Last Button
To display the last record in the JSON array (fJSONArray), we implemented with this code:
procedure TfrmMain.btnLastClick(Sender: TObject); begin if fJSONArray.Count>0 then begin fCurrentIndex := pred(fJSONArray.Count); end else begin fCurrentIndex := 0; end; JSONToForm; end; |
Create button
The Create button sets the UI into create mode, allowing the user to use the form to enter a new record. Clicking the Create button executes this code:
procedure TfrmMain.btnCreateClick(Sender: TObject); begin //- Clear the form and adjust text read-only and prompts. ClearForm; CreateMode; end; |
This OnClick Event for the Create button click code with the ClearForm procedure clears the form and adjusts the text read-only and prompts:
procedure TfrmMain.ClearForm; begin spedEmployeeNumber.Value := 0; edFirstName.Text := ”; edLastName.Text := ”; edtPhoneExt.Text := ”; spedDepartmentNumber.Value := 0; edtJobCode.Text := ”; edtJobGrade.Text := ”; edtJobCountry.Text := ”; spedSalary.Value := 0.0; dtedHireDate.DateTime := Now; end; |
And the CreateMode procedure, sets the columns Read Only values, default text prompts, font colors for the various edit fields, and disables all the button, except the Commit button, with this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
procedure TfrmMain.CreateMode; begin // Read Only edFirstName.ReadOnly := False; edLastName.ReadOnly := False; edtPhoneExt.ReadOnly := False; edtJobCode.ReadOnly := False; edtJobGrade.ReadOnly := False; spedSalary.ReadOnly := False; edtJobCountry.ReadOnly := False; dtedHireDate.ReadOnly := False; // Text Prompt edFirstName.TextPrompt := ‘First Name’; edLastName.TextPrompt := ‘Last Name’; edtPhoneExt.TextPrompt := ‘Phone Ext’; spedDepartmentNumber.TextPrompt := ‘Dept No’; spedDepartmentNumber.ReadOnly := False; edtJobCode.TextPrompt := ‘Job Code’; edtJobGrade.TextPrompt := ‘Job Grade’; edtJobCountry.TextPrompt := ‘Job Country’; spedSalary.TextPrompt := ‘0.0’; // Text Color edFirstName.TextSettings.FontColor := TAlphaColorRec.Red; edLastName.TextSettings.FontColor := TAlphaColorRec.Red; edtPhoneExt.TextSettings.FontColor := TAlphaColorRec.Red; spedDepartmentNumber.TextSettings.FontColor := TAlphaColorRec.Red; edtJobCode.TextSettings.FontColor := TAlphaColorRec.Red; edtJobGrade.TextSettings.FontColor := TAlphaColorRec.Red; edtJobCountry.TextSettings.FontColor := TAlphaColorRec.Red; spedSalary.TextSettings.FontColor := TAlphaColorRec.Red; dtedHireDate.TextSettings.FontColor := TAlphaColorRec.Red; // adjust buttons btnRead.Enabled := False; btnNext.Enabled := False; btnPrev.Enabled := False; btnLast.Enabled := False; btnFirst.Enabled := False; btnCreate.Enabled := False; btnCommit.Enabled := True;/strong> btnEdit.Enabled := False; btnUpdate.Enabled := False; btnDelete.Enabled := False; end; |
And displays the UI in create mode, like this:
The user can now enter a new employee record on this Create Form:
And click the COMMIT button, to POST (CREATE) the new Employee record. The OnClick Event for the COMMIT button executes this code:
//- Populate the JSON resource to post.
RESTRequest.Body.ClearBody;
with RESTRequest.Body.JSONWriter do begin
WriteStartObject;
WritePropertyName(‘FirstName’);
WriteValue(edFirstName.Text);
WritePropertyName(‘LastName’);
WriteValue(edLastName.Text);
WritePropertyName(‘PhoneExt’);
WriteValue(edtPhoneExt.Text);
WritePropertyName(‘DepartmentNumber’);
WriteValue(spedDepartmentNumber.Value);
WritePropertyName(‘JobCode’);
WriteValue(edtJobCode.Text);
WritePropertyName(‘JobGrade’);
WriteValue(edtJobGrade.Text);
WritePropertyName(‘JobCountry’);
WriteValue(edtJobCountry.Text);
WritePropertyName(‘Salary’);
WriteValue(spedSalary.Value);
WritePropertyName(‘HireDate’);
WriteValue(FormatDateTime(‘MM/DD/YYYY 00:00:00’,dtedHireDate.DateTime));
WriteEnd;
end;
// Adjust the form to receive the result.
BrowseMode;
// Execute the request
RESTRequest.Execute;
//- Re-read
btnReadClick(Sender);
end;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
procedure TfrmMain.btnCommitClick(Sender: TObject); begin //- Prepare to POST data to ’employees’ end-point. RESTRequest.Resource := ’employees’; RESTRequest.Method := TRESTRequestMethod.rmPOST; RESTRequest.Response := RESTResponse;/strong>
//- Populate the JSON resource to post. RESTRequest.Body.ClearBody; with RESTRequest.Body.JSONWriter do begin WriteStartObject; WritePropertyName(‘FirstName’); WriteValue(edFirstName.Text); WritePropertyName(‘LastName’); WriteValue(edLastName.Text); WritePropertyName(‘PhoneExt’); WriteValue(edtPhoneExt.Text); WritePropertyName(‘DepartmentNumber’); WriteValue(spedDepartmentNumber.Value); WritePropertyName(‘JobCode’); WriteValue(edtJobCode.Text); WritePropertyName(‘JobGrade’); WriteValue(edtJobGrade.Text); WritePropertyName(‘JobCountry’); WriteValue(edtJobCountry.Text); WritePropertyName(‘Salary’); WriteValue(spedSalary.Value); WritePropertyName(‘HireDate’); WriteValue(FormatDateTime(‘MM/DD/YYYY 00:00:00’,dtedHireDate.DateTime)); WriteEnd; end;
// Adjust the form to receive the result. BrowseMode;
// Execute the request RESTRequest.Execute;
//- Re-read btnReadClick(Sender);
end; |
The above btnCommitClick procedure performs these steps:
1. Resets the UI back to the first EMP_NO.
2. If you click the LAST button, we see that our new Jane Smith employee has been INSERTED into the Employee table with its auto-generated Employee Number = 148.
3. The COMMIT button makes the REST Client Library request:
RESTRequest.Resource := ’employees’; RESTRequest.Method := TRESTRequestMethod.rmPOST; RESTRequest.Response := RESTResponse; |
3a. We set the Resource property of the RESTRequest to our Resource Name on the RAD Server, which is ‘employees‘.
3b. Next we set the HTTP Method to POST, because we want to create a new record.
3c. Then we set the Response component, to receive the data back from the request ( RESTRequest.Response := RESTResponse; )
4. Next, we clear out the Body of the Request ( RESTRequest.Body.ClearBody; ),
5. And then we start adding as JSON data, all of the parameters that we want from the form:
with RESTRequest.Body.JSONWriter do begin WriteStartObject; WritePropertyName(‘FirstName’); WriteValue(edFirstName.Text); WritePropertyName(‘LastName’); WriteValue(edLastName.Text); |
6. We are adding these as PropertyName and PropertyValue, to write into a JSONWriter.
7. We then set the Form into BROWSE mode by called this BrowseMode procedure, that sets the edit controls on the form for read-only, text prompts, text color, and enables all the buttons, except the Commit and Update buttons.
8. After we are in Browse Mode, then after we execute the Request ( RESTRequest.Execute; ) we will be able to read the data, since the data will be in read-only mode.
9. Lastly, we execute the READ button ( btnReadClick(Sender); ) to just re-read the data from the database.
Update (Put) button
Next let’s look at the implementation of the Update (Put) button.
The Edit and Update buttons are implemented similar to the Create and Commit buttons we just discussed above.
1. Looking at this OnClick Event procedure code for the Update button:
//- Populate the JSON resource to post.
RESTRequest.Body.ClearBody;
with RESTRequest.Body.JSONWriter do begin
WriteStartObject;
WritePropertyName(‘FirstName’);
WriteValue(edFirstName.Text);
WritePropertyName(‘LastName’);
WriteValue(edLastName.Text);
WritePropertyName(‘PhoneExt’);
WriteValue(edtPhoneExt.Text);
WritePropertyName(‘DepartmentNumber’);
WriteValue(spedDepartmentNumber.Value);
WritePropertyName(‘JobCode’);
WriteValue(edtJobCode.Text);
WritePropertyName(‘JobGrade’);
WriteValue(edtJobGrade.Text);
WritePropertyName(‘JobCountry’);
WriteValue(edtJobCountry.Text);
WritePropertyName(‘Salary’);
WriteValue(spedSalary.Value);
WritePropertyName(‘HireDate’);
WriteValue(FormatDateTime(‘MM/DD/YYYY 00:00:00’,dtedHireDate.DateTime));
WriteEnd;
end;
// Adjust the form to recieve the result.
BrowseMode;
// Execute the request
RESTRequest.Execute;
//- Re-read
btnReadClick(Sender);
end;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
procedure TfrmMain.btnUpdateClick(Sender: TObject); begin //- Prepare to PUT data to ’employees’ end-point. RESTRequest.Resource := ’employees/’+spedEmployeeNumber.Text; RESTRequest.Method := TRESTRequestMethod.rmPUT; RESTRequest.Response := RESTResponse;
//- Populate the JSON resource to post. RESTRequest.Body.ClearBody; with RESTRequest.Body.JSONWriter do begin WriteStartObject; WritePropertyName(‘FirstName’); WriteValue(edFirstName.Text); WritePropertyName(‘LastName’); WriteValue(edLastName.Text); WritePropertyName(‘PhoneExt’); WriteValue(edtPhoneExt.Text); WritePropertyName(‘DepartmentNumber’); WriteValue(spedDepartmentNumber.Value); WritePropertyName(‘JobCode’); WriteValue(edtJobCode.Text); WritePropertyName(‘JobGrade’); WriteValue(edtJobGrade.Text); WritePropertyName(‘JobCountry’); WriteValue(edtJobCountry.Text); WritePropertyName(‘Salary’); WriteValue(spedSalary.Value); WritePropertyName(‘HireDate’); WriteValue(FormatDateTime(‘MM/DD/YYYY 00:00:00’,dtedHireDate.DateTime)); WriteEnd; end;
// Adjust the form to recieve the result. BrowseMode;
// Execute the request RESTRequest.Execute;
//- Re-read btnReadClick(Sender); end; |
2. We see we are also making a REST Client Library request:
//- Prepare to Update (PUT) data to ’employees’ end-point. RESTRequest.Resource := ’employees/’+spedEmployeeNumber.Text; RESTRequest.Method := TRESTRequestMethod.rmPUT; RESTRequest.Response := RESTResponse; |
2a. Where the Resource is ’employees’ with the specific EmployeeNumber we want to Update.
2b. Then for the HTTP Method we use PUT, since we will be UPDATEing this one specific record.
2c. And then we set the Response component, to receive the data back from the request ( RESTRequest.Response := RESTResponse; )
And the remainder of the implementation is the same as the Create button implementation, where we populate the JSON resource to post (update).
Testing Update (Put) Procedure
Let’s test the Update (Put) on the last Jane Smith, EmployeeNumber = 148 that we Created in the employee table:
1. With the RAD Development Server running.
2. Click the READ Button.
3. Click the LAST Button.
4. This should display our last Jane Smith, EmployeeNumber = 148 that we Created in the employee table:
5. Click the EDIT Button. The Edit Button implements this code allowing you to edit the Edit boxes:
procedure TfrmMain.btnEditClick(Sender: TObject); begin EditMode;/strong> end; |
Where the EditMode procedure is implemented with this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
procedure TfrmMain.EditMode; begin // Read Only edFirstName.ReadOnly := False; edLastName.ReadOnly := False; edtPhoneExt.ReadOnly := False; edtJobCode.ReadOnly := False; edtJobGrade.ReadOnly := False; edtJobCountry.ReadOnly := False; spedDepartmentNumber.ReadOnly := False; spedSalary.ReadOnly := False; dtedHireDate.ReadOnly := False; // Text Prompt edFirstName.TextPrompt := ‘First Name’; edLastName.TextPrompt := ‘Last Name’; edtPhoneExt.TextPrompt := ‘Phone Ext’; spedDepartmentNumber.TextPrompt := ‘Dept No’; edtJobCode.TextPrompt := ‘Job Code’; edtJobGrade.TextPrompt := ‘Job Grade’; edtJobCountry.TextPrompt := ‘Job Country’; spedSalary.TextPrompt := ‘Salary’; // Text Color edFirstName.TextSettings.FontColor := TAlphaColorRec.Blue; edLastName.TextSettings.FontColor := TAlphaColorRec.Blue; edtPhoneExt.TextSettings.FontColor := TAlphaColorRec.Blue; spedDepartmentNumber.TextSettings.FontColor := TAlphaColorRec.Blue; edtJobCode.TextSettings.FontColor := TAlphaColorRec.Blue; edtJobGrade.TextSettings.FontColor := TAlphaColorRec.Blue; edtJobCountry.TextSettings.FontColor := TAlphaColorRec.Blue; spedSalary.TextSettings.FontColor := TAlphaColorRec.Blue; dtedHireDate.TextSettings.FontColor := TAlphaColorRec.Blue; // adjust buttons btnRead.Enabled := False; btnNext.Enabled := False; btnPrev.Enabled := False; btnLast.Enabled := False; btnFirst.Enabled := False; btnCreate.Enabled := False; btnCommit.Enabled := False; btnEdit.Enabled := False; btnUpdate.Enabled := True; btnDelete.Enabled := False; end; |
6. This puts the UI form into EDIT mode, with the Update (Put) button enabled, allowing the user to modify this one Jane Smith, EmployeeNumber = 148 record:
7. Let’s say Jane get’s married and changes her last name from Smith to Doe. Change the LastName Edit box to Doe:
8. Click Update button.
9.We see that the RAD Server Log shows:
{“Thread”:5548,“Request”:{“Resource”:“employees”,“Endpoint”:“PutItem”,“Method”:“PUT”/strong>,“User”:“(blank)”,“Time”:“6/17/2021 4:28:24 PM”}} {“Thread”:12444,strong>“Request”:{“Resource”:“employees”,“Endpoint”:“Get”,“Method”:“GET”,/strong>“User”:“(blank)”,“Time”:“6/17/2021 4:28:24 PM”}} |
9a. The Request for the ’employees’ resource, calling the PutItem endpoint, using the HTTP Method for PUT.
9b. And this is followed by the RAD Server Request again for the ’employees’ resource, calling the Get endpoint, using the HTTP Method for Get. And this is for the Re-read of the data ( btnReadClick(Sender); )
10. If you now click the LAST button, we should see that the LastName for EmployeeNumber 148, has been Updated to ‘Doe’ from ‘Smith’:
Delete (Delete) Button
1. Lastly, looking at the implementation of the Delete button:
//- Add the employee number as a parameter to the request.
RESTREquest.Params.Clear;
with RESTRequest.Params.AddItem do begin
name := ‘EmployeeNumber’;
value := spedEmployeeNumber.Text;
end;
// Execute the request
RESTRequest.Execute;
//- Re-read
btnReadClick(Sender);
end;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
procedure TfrmMain.btnDeleteClick(Sender: TObject); var o: TJSONObject; begin //- Prepare to DELETE data from ’employees’ end-point. RESTRequest.Resource := ’employees/’+spedEmployeeNumber.Text; RESTRequest.Method := TRESTRequestMethod.rmDELETE; RESTRequest.Response := RESTResponse;
//- Add the employee number as a parameter to the request. RESTREquest.Params.Clear; with RESTRequest.Params.AddItem do begin name := ‘EmployeeNumber’; value := spedEmployeeNumber.Text; end;
// Execute the request RESTRequest.Execute;
//- Re-read btnReadClick(Sender); end; |
2. What we are doing in this Delete implementation is once again making a REST Client Library request:
//- Prepare to DELETE data from ’employees’ end-point. RESTRequest.Resource := ’employees/’+spedEmployeeNumber.Text; RESTRequest.Method := TRESTRequestMethod.rmDELETE; RESTRequest.Response := RESTResponse; |
2a. Where the Resource is the ’employees’ with the one specific EmployeeNumber we want to delete. For example, if we want to delete Employee Number 146, we set the Resource to ’employees/146′.
2b. Then for the HTTP Method we use Delete, since we will be DELETEing this one specific record from the employee table.
2c. And then we set the Response component, to receive the data back from the request ( RESTRequest.Response := RESTResponse; )
3. We then execute the Request ( RESTRequest.Execute; )
4. And finally we re-read the data back ( btnReadClick(Sender); )
Testing DELETE (Delete) Procedure
1. Let’s Delete the Last record, which should be the Jane Doe, EmployeeNumber = 148 record, that we just Updated.
2. With EmployeeNumber = 148 record displaying on the Employees Client, click the DELETE button.
3. On the RAD Server Log, we see
{“Thread”:3680,“Request”:{“Resource”:”employees”,“Endpoint”:”DeleteItem”,”Method”:”DELETE”,“User”:”(blank)”,”Time”:”6/18/2021 1:13:21 PM”}}
{“Thread”:11524,”Request”:{“Resource”:”employees”,”Endpoint”:”Get”,”Method”:”GET”,”User”:”(blank)”,”Time”:”6/18/2021 1:13:21 PM”}}
Telling us that we have made a Request for the ’employees’ resource on the RAD Server, called the DeleteItem endpoint, using the HTTP Method Delete.
The Delphi and C++ Builder project source code for this Employees REST Client can be downloaded here:
Delphi Employees REST Client
C++ Builder Employees REST Client
In conclusion, these three posts on RAD Server CRUD Procedures showed one way to build REST Services using RAD Server using the System.JSON classes and REST Clients using the REST Client Library. Consider using these sample Server and Client project application code as template code for implementing CRUD Procedures (CREATE, READ, UPDATE and DELETE) using RAD Server!