unit Forms.Main;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.DB, Vcl.StdCtrls, Vcl.Mask,
  Vcl.DBCtrls, Aurelius.Bind.BaseDataset, Aurelius.Bind.Dataset, Vcl.Grids,
  Vcl.DBGrids, Vcl.ExtCtrls, Generics.Collections, XData.Client, Sparkle.Http.Client,
  TenantEntities;

type
  TfmMain = class(TForm)
    Label1: TLabel;
    cbTenant: TComboBox;
    DBNavigator1: TDBNavigator;
    DBGrid1: TDBGrid;
    AureliusDataset1: TAureliusDataset;
    DataSource1: TDataSource;
    AureliusDataset1Id: TIntegerField;
    AureliusDataset1Name: TStringField;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure AureliusDataset1ObjectInsert(Dataset: TDataSet; AObject: TObject);
    procedure AureliusDataset1ObjectRemove(Dataset: TDataSet; AObject: TObject);
    procedure AureliusDataset1ObjectUpdate(Dataset: TDataSet; AObject: TObject);
    procedure cbTenantClick(Sender: TObject);
    procedure cbTenantKeyPress(Sender: TObject; var Key: Char);
  private
    Client: TXDataClient;
    Products: TList<TProduct>;
    ToDestroy: TList<TObject>;
    procedure DoSendingRequest(Req: THttpRequest);
    procedure LoadTenants;
    procedure ReloadTenant;
  public
  end;

var
  fmMain: TfmMain;

implementation

{$R *.dfm}

procedure TfmMain.AureliusDataset1ObjectInsert(Dataset: TDataSet;
  AObject: TObject);
begin
  Client.Post(AObject);
  ToDestroy.Add(AObject);
end;

procedure TfmMain.AureliusDataset1ObjectRemove(Dataset: TDataSet;
  AObject: TObject);
begin
  Client.Delete(AObject);
end;

procedure TfmMain.AureliusDataset1ObjectUpdate(Dataset: TDataSet;
  AObject: TObject);
begin
  Client.Put(AObject);
end;

procedure TfmMain.cbTenantClick(Sender: TObject);
begin
  ReloadTenant;
end;

procedure TfmMain.cbTenantKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    cbTenant.ItemIndex := cbTenant.Items.IndexOf(cbTenant.Text);
    if cbTenant.ItemIndex = -1 then
      cbTenant.ItemIndex := cbTenant.Items.Add(cbTenant.Text);
    ReloadTenant;
  end;
end;

procedure TfmMain.DoSendingRequest(Req: THttpRequest);
begin
  if cbTenant.Text <> '' then
    Req.Headers.SetValue('database-name', cbTenant.Text);
end;

procedure TfmMain.FormCreate(Sender: TObject);
begin
  ReportMemoryLeaksOnShutdown := True;
  ToDestroy := TObjectList<TObject>.Create;
  Client := TXDataClient.Create;
  Client.Uri := 'http://localhost:2001/tms/multitenant';
  Client.HttpClient.OnSendingRequest := DoSendingRequest;
  LoadTenants;
end;

procedure TfmMain.FormDestroy(Sender: TObject);
begin
  AureliusDataset1.Close;
  FreeAndNil(Products);
  FreeAndNil(ToDestroy);
  Client.Free;
end;

procedure TfmMain.LoadTenants;
begin
  cbTenant.Items.BeginUpdate;
  try
    cbTenant.Items.Add('apple');
    cbTenant.Items.Add('google');
    cbTenant.Items.Add('microsoft');
    cbTenant.ItemIndex := 0;
  finally
    cbTenant.Items.EndUpdate;
  end;
  ReloadTenant;
end;

procedure TfmMain.ReloadTenant;
begin
  AureliusDataset1.Close;
  FreeAndNil(Products);
  ToDestroy.Clear;
  if cbTenant.Text = '' then Exit;
  Products := Client.List<TProduct>;
  AureliusDataset1.SetSourceList(Products);
  AureliusDataset1.Open;
end;

end.
