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,
  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);
    procedure XDataConnection1Connect(Sender: TObject);
  private
    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
  XDataWebClient1.Delete(SelectedEntitySet, TJSObject(TJSJSON.parse(DeleteMemo.Lines.Text)));
end;

procedure TForm1.btGetClick(Sender: TObject);
begin
  XDataWebClient1.Get(SelectedEntitySet, SelectedId);
end;

procedure TForm1.btListClick(Sender: TObject);
begin
  XDataWebClient1.List(SelectedEntitySet, edQuery.Text);
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
  XDataWebClient1.Post(SelectedEntitySet, TJSObject(TJSJSON.parse(PostMemo.Lines.Text)));
end;

procedure TForm1.btPutClick(Sender: TObject);
begin
  XDataWebClient1.Put(SelectedEntitySet, TJSObject(TJSJSON.parse(PutMemo.Lines.Text)));
end;

procedure TForm1.WebFormCreate(Sender: TObject);
begin
  ServerLabel.Caption := Format('XData Server URL: %s', [XDataConnection1.URL]);
  XDataConnection1.Connected := True;
end;

procedure TForm1.XDataConnection1Connect(Sender: TObject);
var
  Container: TXDataEntityContainer;
  I: Integer;
begin
  Container  := XDataConnection1.Model.DefaultEntityContainer;

  cbEntitySets.Items.Clear;
  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.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;
  if Response.RequestId = 'get' then
    GetMemo.Lines.Text := TJSJson.stringify(Response.Result)
  else
  if Response.RequestId = 'list' then
    ListMemo.Lines.Text := TJSJson.stringify(Response.Result)
  else
  if Response.RequestId = 'post' then
    ShowInfo(Format('New entity created in %s collection.', [SelectedEntitySet]))
  else
  if Response.RequestId = 'put' then
    ShowInfo(Format('Entity updated in %s collection.', [SelectedEntitySet]))
  else
  if Response.RequestId = 'delete' then
    ShowInfo(Format('Entity removed from %s collection.', [SelectedEntitySet]));
end;

end.
