Post, Update and Delete methods

Hello Team;

I was looking at this https://www.youtube.com/watch?v=2SCvzw0L27o&t=1273s and wandering how could we use the FD and FDBatch (similar to what is been used in this sample) for HTTP POST/UPDATE/DELETE methods?

I was able to leverage the sample and build the httpget for what I'm looking for, but need to know how we could implement other methods?

Thanks,
Saeid.

You should create a method that receives parameters for the field values, and then execute the SQL statement using FDQuery inside the method implementation.

Hello Wagner;

Do we have any sample of defining the Put / Delete methods ...most of the demos for service call are based on Get and in my use case the client side is not a Delphi. I would like to create a generic services to do the Get and Put functions.

I had a good progress on Get based on the demo but Put / Update / Delete is not yet.

Hello Wagner;

Would you please review my resent question in respect to your respond and get back to me? I would like to see some real implementation for PUT & DELETE & UPDATE methods.

The end result is important; how we could do that (implement it) – using FireDAC or Aurelius is not important for me.

Thanks,

Iiit.ca

** This electronic message contains information from which may be legally privileged and confidential

comment:
I meant to see the real sample for httpPOST, httpDelete, etc.

Thank you for help in advance.

Hello Team; do you have any update on this subject for me?

I need a real sample which shown how we could use the [httppost] / delete / update methods, have a service running under windows and let us to add a record to database by invoking that method outside of Delphi.

Assume the client application is a PHP or C# and they want to call our generic API to add a record, deleted and update by JSON – of course as part of the JSON parameters we have ask them to send us user_name and pass_word so we could validated them against database before execute the request.

Thanks,

Iiit.ca

** This electronic message contains information from which may be legally privileged and confidential

All you need to do is create your service contract interface with the operations you want (PUT, POST, etc.) and the routes.

  [ServiceContract]
  [Route('customers')]
  ICustomerService = interface(IInvokable)
    ['{D2981778-B681-4D4B-B95E-9816B5C7F1D1}']
    [HttpPost, Route('')]
    procedure CreateCustomer(Customer: TCustomer);
    [HttpGet, Route('')]
    function GetCustomers: TList<TCustomer>;
    [HttpGet, Route('{Id}')]
    function GetCustomer(Id: Integer): TCustomer;
    [HttpPut, Route('{Id}')]
    procedure UpdateCustomer(Id: Integer; Customer: TCustomer);
    [HttpDelete, Route('{Id}')]
    procedure DeleteCustomer(Id: Integer);
  end;

Then create the service implementation for that interface with your custom code that will receive and return parameters. As you said, doesn't matter if you use Aurelius, FireDAC, or anything else, even non-database related. An example for GetCustomers:

function TCustomerService.GetCustomers: TList<TCustomer>;
var
  Customer: TCustomer;
begin
  Result := TList<TCustomer>.Create;
  TXDataOperationContext.Current.Handler.ManagedObjects.Add(Result);
  Query.SQL.Text := 'SELECT * FROM CUSTOMERS';
  Query.Open;
  while not Query.Eof do
  begin
    Customer := TCustomer.Create;
    TXDataOperationContext.Current.Handler.ManagedObjects.Add(Customer);
    Result.Add(Customer);
    Customer.Id := Query.FieldByName('ID').AsInteger;
    Customer.Name := Query.FieldByName('NAME').AsString;
    Customer.Country := Query.FieldByName('COUNTRY').AsString;
    Query.Next;
  end;
end;

In this case Query is just a shortcut for a TFDQuery component in a data module:

procedure TCustomerService.AfterConstruction;
begin
  inherited;
  DB := TDatabaseModule.Create(nil);
end;

function TCustomerService.GetQuery: TFDQuery;
begin
  Result := DB.FDQuery1;
end;

The full working sample project is attached here:
firedac-sql.zip (18.9 KB)

1 Like

Wagner; many many thanks. I will explore this and keep you posted if anything else came-up.

At the meantime; I've two Q:

  1. What would the best way to handle the varbinary (in DB world, blob) in methods?
  2. We want to use user/password as parameters when the service is called from client side (when client is not delphi) - considering the fact we would use "https" at production, is it good way to do that or there is other way you recommend based on your experience.

Hello Wagner;

Would you help me to use a proper type casting for loading the TBlob field(s) into table when posting the data?

Assume the table has this structure:

CREATE TABLE [dbo].[CUSTOMERS](

[ID] [int] NOT NULL,

[NAME] nvarchar NULL,

[COUNTRY] varchar NULL,

[RATE] [decimal](6, 4) NULL,

[CONTRY_CODE] varchar NULL,

[CREATEDDATE] varchar NULL,

[MAPIMAGE] varbinary NULL,

)

So the mapping class is something like this:

TCustomer = class

Id: Integer;

Name: string;

Country: string;

Rate:Double;

CountryCode:string;

CreatedDate:string;

MapImage:TBlob;

.

.

End;

I’ve to use a “varbinarry(max)” in my table data field, but not sure what type casing could be used to load them properly in insert?

Query.SQL.Text := 'INSERT INTO CUSTOMERS VALUES (:id, :name, :country, :rate, :code, :createddate, :blobmap)';

Query.ParamByName('id').AsInteger := Customer.Id;

Query.ParamByName('name').AsString := Customer.Name;

Query.ParamByName('country').AsString := Customer.Country;

Query.ParamByName('rate').AsFloat := Customer.Rate;

Query.ParamByName('code').AsString := Customer.CountryCode;

Query.ParamByName('createddate').AsString := Customer.CreatedDate;

Query.ParamByName('blobmap').LoadFromStream(aStream, ftBlob); // .AsAnsiString := Customer.Map.AsUnicodeString;

In JSON call, I could send an image data in base64 encoded data… but no when I decode and inserted to database / table is not a write values.

Thank you.

Saeid.

It could be good to create new different topics for different subjects.

You can read a TBlob using AsUnicodeString, AsBytes, or use SaveToSTream to save the content to a stream. Then you can use such data as you wish with your preferred database component - FireDAC, for example.

Thanks. I've tested that using the SaveToStream and LoadFromStream using blob type / although seems is working but the data saved in the field is not the same as original one passed through the httppost.

I will create a new request | topics for this to get more help.

SG.

A post was split to a new topic: Response codes

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.