unit XData.Module.Events;

interface

uses
  System.SysUtils,
  Aurelius.Criteria.Base,
  Aurelius.Engine.ObjectManager,
  Aurelius.Events.Manager, // remove this later when unifying TEvent<T>
  Sparkle.HttpServer.Request,
  Sparkle.HttpServer.Response,
  XData.Module.Base;

type
  TXDataModuleArgs = class
  strict private
    FHandler: TXDataBaseRequestHandler;
  public
    constructor Create(AHandler: TXDataBaseRequestHandler);
    property Handler: TXDataBaseRequestHandler read FHandler;
  end;

  TXDataEntityArgs = class(TXDataModuleArgs)
  strict private
    FEntity: TObject;
  public
    constructor Create(AHandler: TXDataBaseRequestHandler; AEntity: TObject);
    property Entity: TObject read FEntity;
  end;

  TEntityInsertingArgs = class(TXDataEntityArgs)
  end;
  TEntityInsertingProc = TProc<TEntityInsertingArgs>;

  TEntityInsertedArgs = class(TXDataEntityArgs)
  end;
  TEntityInsertedProc = TProc<TEntityInsertedArgs>;

  TEntityModifyingArgs = class(TXDataEntityArgs)
  end;
  TEntityModifyingProc = TProc<TEntityModifyingArgs>;

  TEntityModifiedArgs = class(TXDataEntityArgs)
  end;
  TEntityModifiedProc = TProc<TEntityModifiedArgs>;

  TEntityDeletingArgs = class(TXDataEntityArgs)
  end;
  TEntityDeletingProc = TProc<TEntityDeletingArgs>;

  TEntityDeletedArgs = class(TXDataEntityArgs)
  end;
  TEntityDeletedProc = TProc<TEntityDeletedArgs>;

  TEntityGetArgs = class(TXDataEntityArgs)
  end;
  TEntityGetProc = TProc<TEntityGetArgs>;

  TEntityListArgs = class(TXDataModuleArgs)
  private
    FCriteria: TCriteria;
  public
    constructor Create(AHandler: TXDataBaseRequestHandler; ACriteria: TCriteria);
    property Criteria: TCriteria read FCriteria;
  end;
  TEntityListProc = TProc<TEntityListArgs>;

  TModuleExceptionAction = (SendError, RaiseException, Ignore);
  TModuleExceptionArgs = class(TXDataModuleArgs)
  private
    FException: Exception;
    FAction: TModuleExceptionAction;
    FStatusCode: Integer;
    FErrorCode: string;
    FErrorMessage: string;
  public
    constructor Create(AHandler: TXDataBaseRequestHandler; const AException: Exception;
      AStatusCode: Integer; const AErrorCode, AErrorMessage: string; AAction: TModuleExceptionAction);
    property Exception: Exception read FException;
    property StatusCode: Integer read FStatusCode write FStatusCode;
    property ErrorCode: string read FErrorCode write FErrorCode;
    property ErrorMessage: string read FErrorMessage write FErrorMessage;
    property Action: TModuleExceptionAction read FAction write FAction;
  end;
  TModuleExceptionProc = TProc<TModuleExceptionArgs>;

  TManagerCreateArgs = class(TXDataModuleArgs)
  private
    FManager: TObjectManager;
  public
    constructor Create(AHandler: TXDataBaseRequestHandler; AManager: TObjectManager);
    property Manager: TObjectManager read FManager write FManager;
  end;
  TManagerCreateProc = TProc<TManagerCreateArgs>;

  TXDataModuleEvents = class
  private
    FOnEntityInserting: TEvent<TEntityInsertingProc>;
    FOnEntityInserted: TEvent<TEntityInsertedProc>;
    FOnEntityModifying: TEvent<TEntityModifyingProc>;
    FOnEntityModified: TEvent<TEntityModifiedProc>;
    FOnEntityDeleting: TEvent<TEntityDeletingProc>;
    FOnEntityDeleted: TEvent<TEntityDeletedProc>;
    FOnEntityGet: TEvent<TEntityGetProc>;
    FOnEntityList: TEvent<TEntityListProc>;
    FOnModuleException: TEvent<TModuleExceptionProc>;
    FOnManagerCreate: TEvent<TManagerCreateProc>;
  public
    property OnEntityInserting: TEvent<TEntityInsertingProc> read FOnEntityInserting;
    property OnEntityInserted: TEvent<TEntityInsertedProc> read FOnEntityInserted;
    property OnEntityModifying: TEvent<TEntityModifyingProc> read FOnEntityModifying;
    property OnEntityModified: TEvent<TEntityModifiedProc> read FOnEntityModified;
    property OnEntityDeleting: TEvent<TEntityDeletingProc> read FOnEntityDeleting;
    property OnEntityDeleted: TEvent<TEntityDeletedProc> read FOnEntityDeleted;
    property OnEntityGet: TEvent<TEntityGetProc> read FOnEntityGet;
    property OnEntityList: TEvent<TEntityListProc> read FOnEntityList;
    property OnModuleException: TEvent<TModuleExceptionProc> read FOnModuleException;
    property OnManagerCreate: TEvent<TManagerCreateProc> read FOnManagerCreate;
  end;

implementation

{ TXDataModuleArgs }

constructor TXDataModuleArgs.Create(AHandler: TXDataBaseRequestHandler);
begin
  FHandler := AHandler;
end;

{ TEntityListArgs }

constructor TEntityListArgs.Create(AHandler: TXDataBaseRequestHandler;
  ACriteria: TCriteria);
begin
  inherited Create(AHandler);
  FCriteria := ACriteria;
end;

{ TXDataEntityArgs }

constructor TXDataEntityArgs.Create(AHandler: TXDataBaseRequestHandler;
  AEntity: TObject);
begin
  inherited Create(AHandler);
  FEntity := AEntity;
end;

{ TModuleExceptionArgs }

constructor TModuleExceptionArgs.Create(AHandler: TXDataBaseRequestHandler;
  const AException: Exception; AStatusCode: Integer; const AErrorCode, AErrorMessage: string;
  AAction: TModuleExceptionAction);
begin
  inherited Create(AHandler);
  FException := AException;
  FStatusCode := AStatusCode;
  FErrorCode := AErrorCode;
  FErrorMessage := AErrorMessage;
  FAction := AAction;
end;
{ TManagerCreateArgs }

constructor TManagerCreateArgs.Create(AHandler: TXDataBaseRequestHandler;
  AManager: TObjectManager);
begin
  inherited Create(AHandler);
  FManager := AManager;
end;

end.
