#1 2015-11-24 12:56:45

corchi72
Member
Registered: 2010-12-10
Posts: 232

what is the best way to return the data read from a service?

what is the best way to return the data read from a service,

I do not understand how can I return an array of objects (multiple records) in JSON format to be read by JS:

TSQLFile = class(TSQLRecordSigned)
  private
  ...
  public
    fName: RawUTF8;
    fModified: TTimeLog;
    fCreated: TTimeLog;
    fPicture: TSQLRawBlob;
    fKeyWords: RawUTF8;
    fLabel: RawUTF8;
    fImageIndex: Integer;
    fEnabled: Boolean;
    function CheckValues(reference: TSQLRecord): Boolean; virtual;
    procedure CopyFrom(ARec: TSQLRecord); // : TSQLRecord;
    procedure CopyTo(var ARec: TSQLRecord);
    property DisplayName: RawUTF8 read GetDisplayName;
    property Check: Boolean read fCheck write fCheck;
  published
    property Name: RawUTF8 read fName write fName;
    property Created: TTimeLog read fCreated write fCreated;
    property Modified: TTimeLog read fModified write fModified;
    property Picture: TSQLRawBlob read fPicture write fPicture;
    property KeyWords: RawUTF8 read fKeyWords write fKeyWords;
    property SignatureTime;
    property Signature;

   
  end;

  TSQLUserOrders = class(TSQLRecordMany)
  private
    fSource: TSQLUser;
    fDest: TSQLOrder;
  published
    property Source: TSQLUser read fSource;
    property Dest: TSQLOrder read fDest;

  end;
  
  
  TSQLUser = class(TSQLFile)
  private

    fOrders: TSQLUserOrders;
  published
    property Orders: TSQLUserOrders read fOrders;

  end;
  
  TSQLOrder = class(TSQLFile)
  private
    fOwner: RawUTF8;
    ....
	published

    property Users: TSQLUserOrders read fUsers;

  end;
  

 
 
  //Server Side
 

  
  TFileServer =   class(TSQLRestserverDB)
  private
  

  public
  
  published
  
    procedure LoadUserOrders(Ctxt: TSQLRestServerURIContext);
  end;
  
  
  ...
  
//this function returns all orders for a specific user 
procedure TFileServer.LoadUserOrders(Ctxt: TSQLRestServerURIContext);
var
 User:TSQLUser;
 fIds: TIDDynArray;
 aOrder:TSQLOrder;
 UserID: Integer;
 content: RawUTF8;
begin
  
  if not UrlDecodeNeedParameters(Ctxt.Parameters,'UserID') then exit;
  while Ctxt.Parameters<>nil do
  begin
      UrlDecodeInteger(Ctxt.Parameters,'UserID=',UserID,@Ctxt.Parameters);
  end;

  try
      User:=TSQLUser.Create;

      if Self.Retrieve(UserID,User) then
      begin

        User.Orders.DestGet( Self, User.ID, fIds);
        aOrder := TSQLOrder.CreateAndFillPrepare(Self, TInt64DynArray(fIds), CSVOrder);
        while aOrder.FillOne do
        begin
           content := ObjectToJSON(aOrder);
           
        end;

        Ctxt.Returns(content, HTML_SUCCESS, HEADER_CONTENT_TYPE + JSON_CONTENT_TYPE);

      end;
  finally
       User.Free;
  end;

end;

  
  

Thank corchi

Offline

Board footer

Powered by FluxBB