unit Scaffolding.Types;

interface

uses
  System.SysUtils,
  Generics.Collections,
  XData.Model.Classes,
  XData.Module.Base;

type
  TScaffoldingEntityConfig = class;

  TScaffoldingConfig = class
  strict private
    FProjectName: string;
    FTemplatePath: string;
    FProjectDestinationBasePath: string;
    FProjectDestinationPath: string;
    FAuthUrl: string;
    FApiUrl: string;
    FModel: TXDataModel;
    FSelectedEntities: TList<TScaffoldingEntityConfig>;

    //temporary fields
    FLoginEnabled: Boolean;
  public
    constructor Create;
    destructor Destroy; override;
    property ProjectName: string read FProjectName write FProjectName;
    property TemplatePath: string read FTemplatePath write FTemplatePath;
    property ProjectDestinationPath: string read FProjectDestinationPath write FProjectDestinationPath;
    property ProjectDestinationBasePath: string read FProjectDestinationBasePath write FProjectDestinationBasePath;
    property AuthUrl: string read FAuthUrl write FAuthUrl;
    property ApiUrl: string read FApiUrl write FApiUrl;
    property Model: TXDataModel read FModel write FModel;
    property Entities: TList<TScaffoldingEntityConfig> read FSelectedEntities write FSelectedEntities;

    // temporary properties
    property LoginEnabled: Boolean read FLoginEnabled;
  end;

  TScaffoldingEntityConfig = class
  strict private
    FEntitySet: TXDataEntitySet;
    FPermissions: TEntitySetPermissions;
    procedure SetPermission(AInclude: Boolean; APermission: TEntitySetPermission);
  private
    function GetCanDelete: Boolean;
    function GetCanGet: Boolean;
    function GetCanInsert: Boolean;
    function GetCanList: Boolean;
    function GetCanModify: Boolean;
    procedure SetCanDelete(const Value: Boolean);
    procedure SetCanGet(const Value: Boolean);
    procedure SetCanInsert(const Value: Boolean);
    procedure SetCanList(const Value: Boolean);
    procedure SetCanModify(const Value: Boolean);
  public
    constructor Create(AEntitySet: TXDataEntitySet);
    property EntitySet: TXDataEntitySet read FEntitySet;
    property CanList: Boolean read GetCanList write SetCanList;
    property CanGet: Boolean read GetCanGet write SetCanGet;
    property CanInsert: Boolean read GetCanInsert write SetCanInsert;
    property CanModify: Boolean read GetCanModify write SetCanModify;
    property CanDelete: Boolean read GetCanDelete write SetCanDelete;
  end;

  EXDataScaffoldingException = class(Exception);

implementation

{ TScaffoldingConfig }

constructor TScaffoldingConfig.Create;
begin
  FSelectedEntities := TList<TScaffoldingEntityConfig>.Create;
  FLoginEnabled := False;
end;

destructor TScaffoldingConfig.Destroy;
begin
  FSelectedEntities.Free;
  inherited;
end;

{ TScaffoldingEntityConfig }

constructor TScaffoldingEntityConfig.Create(AEntitySet: TXDataEntitySet);
begin
  FEntitySet := AEntitySet;
  FPermissions := [TEntitySetPermission.List, TEntitySetPermission.Get,
                   TEntitySetPermission.Insert, TEntitySetPermission.Modify,
                   TEntitySetPermission.Delete];
end;

function TScaffoldingEntityConfig.GetCanDelete: Boolean;
begin
  Result := TEntitySetPermission.Delete in FPermissions;
end;

function TScaffoldingEntityConfig.GetCanGet: Boolean;
begin
  Result := TEntitySetPermission.Get in FPermissions;
end;

function TScaffoldingEntityConfig.GetCanInsert: Boolean;
begin
  Result := TEntitySetPermission.Insert in FPermissions;
end;

function TScaffoldingEntityConfig.GetCanList: Boolean;
begin
  Result := TEntitySetPermission.List in FPermissions;
end;

function TScaffoldingEntityConfig.GetCanModify: Boolean;
begin
  Result := TEntitySetPermission.Modify in FPermissions;
end;

procedure TScaffoldingEntityConfig.SetCanDelete(const Value: Boolean);
begin
  SetPermission(Value, TEntitySetPermission.Delete);
end;

procedure TScaffoldingEntityConfig.SetCanGet(const Value: Boolean);
begin
  SetPermission(Value, TEntitySetPermission.Get);
end;

procedure TScaffoldingEntityConfig.SetCanInsert(const Value: Boolean);
begin
  SetPermission(Value, TEntitySetPermission.Insert);
end;

procedure TScaffoldingEntityConfig.SetCanList(const Value: Boolean);
begin
  SetPermission(Value, TEntitySetPermission.List);
end;

procedure TScaffoldingEntityConfig.SetCanModify(const Value: Boolean);
begin
  SetPermission(Value, TEntitySetPermission.Modify);
end;

procedure TScaffoldingEntityConfig.SetPermission(AInclude: Boolean;
  APermission: TEntitySetPermission);
begin
  if AInclude then
    Include(FPermissions, APermission)
  else
    Exclude(FPermissions, APermission);
end;

end.
