unit MainForm;

interface

uses
  System.SysUtils, System.Classes, WEBLib.Graphics, WEBLib.Forms,
  XData.Web.Client, XData.Web.Connection, Vcl.Controls, Vcl.StdCtrls,
  XData.Model.Classes,
  JS, Web,
  WEBLib.Dialogs,
  WEBLib.StdCtrls, Vcl.Imaging.pngimage, WEBLib.ExtCtrls, WEBLib.ComCtrls,
  WEBLib.Controls;

type
  TForm1 = class(TWebForm)
    XDataConnection1: TXDataWebConnection;
    cbEntitySets: TWebComboBox;
    WebLabel1: TWebLabel;
    XDataWebClient1: TXDataWebClient;
    WebPanel1: TWebPanel;
    WebLabel4: TWebLabel;
    WebImageControl1: TWebImageControl;
    WebPageControl1: TWebPageControl;
    WebPageControl1Sheet1: TWebTabSheet;
    WebPageControl1Sheet2: TWebTabSheet;
    WebPageControl1Sheet3: TWebTabSheet;
    WebLabel2: TWebLabel;
    edId: TWebEdit;
    btGet: TWebButton;
    GetMemo: TWebMemo;
    edQuery: TWebEdit;
    WebLabel5: TWebLabel;
    btList: TWebButton;
    ListMemo: TWebMemo;
    InfoLabel: TWebLabel;
    PostMemo: TWebMemo;
    btPost: TWebButton;
    WebTabSheet1: TWebTabSheet;
    PutMemo: TWebMemo;
    btPut: TWebButton;
    WebTabSheet2: TWebTabSheet;
    DeleteMemo: TWebMemo;
    btDelete: TWebButton;
    ServerLabel: TWebLabel;
    procedure WebFormCreate(Sender: TObject);
    procedure btGetClick(Sender: TObject);
    procedure btPutClick(Sender: TObject);
    procedure btPostClick(Sender: TObject);
    procedure btDeleteClick(Sender: TObject);
    procedure btListClick(Sender: TObject);
    procedure XDataWebClient1Load(Response: TXDataClientResponse);
    procedure XDataWebClient1Error(Error: TXDataClientError);
  private
    procedure InitForm; {$IFDEF PAS2JS}async;{$ENDIF}
    procedure InvokeGet; {$IFDEF PAS2JS}async;{$ENDIF}
    procedure InvokePut; {$IFDEF PAS2JS}async;{$ENDIF}
    procedure InvokePost; {$IFDEF PAS2JS}async;{$ENDIF}
    procedure InvokeDelete; {$IFDEF PAS2JS}async;{$ENDIF}
    procedure InvokeList; {$IFDEF PAS2JS}async;{$ENDIF}
    procedure ShowInfo(const Msg: string; Error: Boolean = False);
    function SelectedEntitySet: string;
    function SelectedId: Integer;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btDeleteClick(Sender: TObject);
begin
  InvokeDelete;
end;

procedure TForm1.btGetClick(Sender: TObject);
begin
  InvokeGet;
end;

procedure TForm1.btListClick(Sender: TObject);
begin
  InvokeList;
end;

function TForm1.SelectedEntitySet: string;
begin
  Result := cbEntitySets.Items[cbEntitySets.ItemIndex];
end;

function TForm1.SelectedId: Integer;
begin
  Result := StrToInt(edId.Text);
end;

procedure TForm1.ShowInfo(const Msg: string; Error: Boolean);
begin
  InfoLabel.Caption := Msg;
  if Error then
    InfoLabel.Font.Color := clRed
  else
    InfoLabel.Font.Color := clWindowText;
  InfoLabel.Visible := True;
end;

procedure TForm1.btPostClick(Sender: TObject);
begin
  InvokePost;
end;

procedure TForm1.btPutClick(Sender: TObject);
begin
  InvokePut;
end;

procedure TForm1.InitForm;
var
  Container: TXDataEntityContainer;
  I: Integer;
begin
  ServerLabel.Caption := Format('XData Server URL: %s', [XDataConnection1.URL]);
  cbEntitySets.Items.Clear;
  await(XDataConnection1.OpenAsync);
  Container  := XDataConnection1.Model.DefaultEntityContainer;
  for I := 0 to Container.EntitySets.Count - 1 do
    cbEntitySets.Items.Add(Container.EntitySets[I].Name);
  if cbEntitySets.Items.Count > 0 then
    cbEntitySets.ItemIndex := 0;
end;

procedure TForm1.InvokeDelete;
begin
  await(XDataWebClient1.DeleteAsync(SelectedEntitySet, TJSObject(TJSJSON.parse(DeleteMemo.Lines.Text))));
  ShowInfo(Format('Entity removed from %s collection.', [SelectedEntitySet]));
end;

procedure TForm1.InvokeGet;
var
  Response: TXDataClientResponse;
begin
  GetMemo.Lines.Text := '';
  Response := await(XDataWebClient1.GetAsync(SelectedEntitySet, SelectedId));
  GetMemo.Lines.Text := TJSJson.stringify(Response.Result)
end;

procedure TForm1.InvokeList;
var
  Response: TXDataClientResponse;
begin
  ListMemo.Lines.Text := '';
  Response := await(XDataWebClient1.ListAsync(SelectedEntitySet, edQuery.Text));
  ListMemo.Lines.Text := TJSJson.stringify(Response.Result);
end;

procedure TForm1.InvokePost;
begin
  await(XDataWebClient1.PostAsync(SelectedEntitySet, TJSObject(TJSJSON.parse(PostMemo.Lines.Text))));
  ShowInfo(Format('New entity created in %s collection.', [SelectedEntitySet]))
end;

procedure TForm1.InvokePut;
begin
  await(XDataWebClient1.PutAsync(SelectedEntitySet, TJSObject(TJSJSON.parse(PutMemo.Lines.Text))));
  ShowInfo(Format('Entity updated in %s collection.', [SelectedEntitySet]));
end;

procedure TForm1.WebFormCreate(Sender: TObject);
begin
  InitForm;
end;

procedure TForm1.XDataWebClient1Error(Error: TXDataClientError);
begin
  ShowInfo(
    Format('%s. RequestId: %s. Code: %s. Request Url: %s',
      [Error.ErrorMessage, Error.RequestId, Error.ErrorCode, Error.RequestUrl]),
    True
  );
end;

procedure TForm1.XDataWebClient1Load(Response: TXDataClientResponse);
begin
  InfoLabel.Visible := False;
end;

end.
