unit ServerMethodsUnit1;
interface
uses
  System.SysUtils, System.Classes, Datasnap.DSServer, Datasnap.DSAuth,
  Data.DBXJSON;

type
  TEchoStringCallback = class(TDBXCallback)
    function Execute(const Arg: TJSONValue): TJSONValue; override;
  end;

  TMyServerMethods = class(TDSServerModule)
  private
    { Private declarations }
  public
    { Public declarations }
    function EchoString(Value: string): string;
    [TRoleAuth('admin')]
    function ReverseString(Value: string): string;
    function ServerTime: TDateTime; // new

    function EchoStringEx(Value: string; callback: TDBXcallback): string;
  end;

implementation
uses
  System.StrUtils,
  Vcl.Dialogs;

{$R *.dfm}

function TMyServerMethods.EchoString(Value: string): string;
begin
  Result := Value
end;

function TMyServerMethods.EchoStringEx(Value: string;
  callback: TDBXcallback): string;
var
  msg: TJSONObject;
  pair: TJSONPair;
  response: TJSONValue;
begin
  Result := Value;

  msg := TJSONObject.Create;
  pair := TJSONPair.Create('ECHO', Value);
  msg.AddPair(pair);
  try
    response := callback.Execute(msg)
  finally
    response.Free
  end
end;

function TMyServerMethods.ReverseString(Value: string): string;
begin
  Result := System.StrUtils.ReverseString(Value)
end;

function TMyServerMethods.ServerTime: TDateTime;
begin
  Result := Now
end;

{ TEchoStringCallback }

function TEchoStringCallback.Execute(const Arg: TJSONValue): TJSONValue;
var
  Data: TJSONValue;
begin
  Data := TJSONValue(Arg.Clone);
  ShowMessage('Callback: ' + TJSONObject(Data).Get(0).JSonValue.value);
  Result := Data
end;

end.

