unit MainFormHeader;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,
  System.Actions, Vcl.ActnList, Vcl.ComCtrls, Vcl.ExtCtrls;

type
  TForm1 = class(TForm)
    PageControl1: TPageControl;
    Panel1: TPanel;
    btStartStop: TButton;
    TabSheet1: TTabSheet;
    Panel2: TPanel;
    ListBox1: TListBox;
    Button1: TButton;
    ActionList1: TActionList;
    acAddDatabase: TAction;
    acRemoveDatabase: TAction;
    acStartStop: TAction;
    Button3: TButton;
    procedure acStartStopExecute(Sender: TObject);
    procedure acStartStopUpdate(Sender: TObject);
    procedure acAddDatabaseExecute(Sender: TObject);
    procedure acRemoveDatabaseUpdate(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
  public
  end;

var
  Form1: TForm1;

implementation

uses
  ServerByHeader.Module;

{$R *.dfm}

procedure TForm1.acAddDatabaseExecute(Sender: TObject);
var
  DBName: string;
begin
  if not InputQuery('Add Database', 'Enter database name:', DBName) then Exit;
  if ListBox1.Items.IndexOf(DBName) >= 0 then
    raise Exception.Create('Database already exists.');
  if DBName = '' then
    raise Exception.Create('Database name cannot be empty');

  ServerByHeaderModule.AddTenant(DBName);
  ListBox1.Items.Add(DBName);
end;

procedure TForm1.acRemoveDatabaseUpdate(Sender: TObject);
begin
  acRemoveDatabase.Enabled := ListBox1.ItemIndex >= 0;
end;

procedure TForm1.acStartStopExecute(Sender: TObject);
begin
  if ServerByHeaderModule.IsRunning then
  begin
    ServerByHeaderModule.StopServer;
    btStartStop.Caption := 'Start server';
  end
  else
  begin
    ServerByHeaderModule.StartServer;
    btStartStop.Caption := 'Stop server';
  end;
end;

procedure TForm1.acStartStopUpdate(Sender: TObject);
begin
  if not ServerByHeaderModule.IsRunning then
    acStartStop.Caption := 'Start Server'
  else
    acStartStop.Caption := 'Stop Server';
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  ShowMessage(
    'This demo shows how to create a multi-tenant TMS XData Server.' + sLineBreak + sLineBreak +
    'The API has a single endpoint URL: ' + sLineBreak +
    'http://localhost:2001/tms/multitenant' + sLineBreak + sLineBreak +
    'Clients choose the database to access by passing the db name in HTTP request header database-name: ' + sLineBreak +
    'database-name: MyDBName'
  );
end;

end.
