From dad88f85b68d8fb7900817b76f8a72ee9627cee6 Mon Sep 17 00:00:00 2001 From: Daniele Teti Date: Fri, 9 Dec 2022 09:58:55 +0100 Subject: [PATCH] https://github.com/danieleteti/delphimvcframework/issues/605 --- samples/activerecord_showcase/EntitiesU.pas | 4 +- .../async_client/MainClientFormU.pas | 36 ++--- .../WebModuleUnit1.pas | 22 +-- samples/serversentevents/SSEControllerU.pas | 127 +++++++++--------- samples/webcontextevents/MainControllerU.pas | 4 +- .../MVCFramework.Middleware.ActiveRecord.pas | 14 +- 6 files changed, 110 insertions(+), 97 deletions(-) diff --git a/samples/activerecord_showcase/EntitiesU.pas b/samples/activerecord_showcase/EntitiesU.pas index 7626a632..276ac556 100644 --- a/samples/activerecord_showcase/EntitiesU.pas +++ b/samples/activerecord_showcase/EntitiesU.pas @@ -594,7 +594,9 @@ function TCustomer.ToString: String; begin Result := ''; if PKIsNull then - Result := ''; + Result := '' + else + Result := fID.ValueOrDefault.ToString; Result := Format('[ID: %6s][CODE: %6s][CompanyName: %18s][City: %16s][Rating: %3d][Note: %s]',[ Result, fCode.ValueOrDefault, fCompanyName.ValueOrDefault, fCity, fRating.ValueOrDefault, fNote]); end; diff --git a/samples/jsonrpc_with_published_objects/async_client/MainClientFormU.pas b/samples/jsonrpc_with_published_objects/async_client/MainClientFormU.pas index f8151cba..e8101ffa 100644 --- a/samples/jsonrpc_with_published_objects/async_client/MainClientFormU.pas +++ b/samples/jsonrpc_with_published_objects/async_client/MainClientFormU.pas @@ -511,24 +511,24 @@ begin end, fGeneralErrorHandler); end; - procedure TMainForm.btnSubtractClick(Sender: TObject); - var - lReq: IJSONRPCRequest; - lExecutorAsync: IMVCJSONRPCExecutorAsync; - begin - lExecutorAsync := TMVCJSONRPCExecutor.Create('http://localhost:8080'); - lReq := TJSONRPCRequest.Create; - lReq.Method := 'subtract'; - lReq.RequestID := Random(1000); - lReq.Params.Add(StrToInt(edtValue1.Text)); - lReq.Params.Add(StrToInt(edtValue2.Text)); - lExecutorAsync - .ExecuteRequestAsync('/jsonrpc', lReq, - procedure(JSONRPCResp: IJSONRPCResponse) - begin - edtResult.Text := JSONRPCResp.Result.AsInteger.ToString; - end); - end; +procedure TMainForm.btnSubtractClick(Sender: TObject); +var + lReq: IJSONRPCRequest; + lExecutorAsync: IMVCJSONRPCExecutorAsync; +begin + lExecutorAsync := TMVCJSONRPCExecutor.Create('http://localhost:8080'); + lReq := TJSONRPCRequest.Create; + lReq.Method := 'subtract'; + lReq.RequestID := Random(1000); + lReq.Params.Add(StrToInt(edtValue1.Text)); + lReq.Params.Add(StrToInt(edtValue2.Text)); + lExecutorAsync + .ExecuteRequestAsync('/jsonrpc', lReq, + procedure(JSONRPCResp: IJSONRPCResponse) + begin + edtResult.Text := JSONRPCResp.Result.AsInteger.ToString; + end); +end; procedure TMainForm.btnSubtractWithNamedParamsClick(Sender: TObject); var diff --git a/samples/middleware_jwtblacklist/WebModuleUnit1.pas b/samples/middleware_jwtblacklist/WebModuleUnit1.pas index 394ba4db..e6062ae7 100644 --- a/samples/middleware_jwtblacklist/WebModuleUnit1.pas +++ b/samples/middleware_jwtblacklist/WebModuleUnit1.pas @@ -82,16 +82,18 @@ begin .AddController(TAdminController) .AddMiddleware( TMVCJWTAuthenticationMiddleware.Create( - TAuthenticationSample.Create, - lClaimsSetup, - 'mys3cr37', - '/login', - [ - TJWTCheckableClaim.ExpirationTime, - TJWTCheckableClaim.NotBefore, - TJWTCheckableClaim.IssuedAt - ], 300)) - .AddMiddleware(TMVCJWTBlackListMiddleware.Create(lOnAcceptToken, lOnNewJWTToBlackList)); + TAuthenticationSample.Create, + lClaimsSetup, + 'mys3cr37', + '/login', + [ + TJWTCheckableClaim.ExpirationTime, + TJWTCheckableClaim.NotBefore, + TJWTCheckableClaim.IssuedAt + ], 300)) + .AddMiddleware( + TMVCJWTBlackListMiddleware.Create(lOnAcceptToken, lOnNewJWTToBlackList) + ); end; end. diff --git a/samples/serversentevents/SSEControllerU.pas b/samples/serversentevents/SSEControllerU.pas index f9063410..ba5abed9 100644 --- a/samples/serversentevents/SSEControllerU.pas +++ b/samples/serversentevents/SSEControllerU.pas @@ -1,65 +1,66 @@ unit SSEControllerU; - -interface - -uses - MVCFramework, MVCFramework.Commons; - -type - - [MVCPath('/')] - TSSEController = class(TMVCController) - public - [MVCPath('/stocks')] - [MVCHTTPMethod([httpGET])] - [MVCProduces('text/event-stream')] - procedure Index; - - protected - procedure OnBeforeAction(Context: TWebContext; const AActionName: string; - var Handled: Boolean); override; - procedure OnAfterAction(Context: TWebContext; - const AActionName: string); override; - end; - -implementation - -uses - MVCFramework.Logger, System.SysUtils, StorageU; - -procedure TSSEController.Index; -var - lLastEventID: Integer; - lCurrentEventID: Integer; - lMessage: string; -begin - // wait a little bit - Sleep(1000 + Random(2000)); - - // retrieve the last id received by the client reading the request header. - lLastEventID := StrToIntDef(Context.Request.Headers[TMVCConstants.SSE_LAST_EVENT_ID], 0); - - // get the next message to send based on the last id already received by the client - lMessage := GetNextDataToSend(lLastEventID, lCurrentEventID); - - RenderSSE(lCurrentEventID.ToString, lMessage, 'stockupdate'); -end; - -procedure TSSEController.OnAfterAction(Context: TWebContext; - const AActionName: string); -begin - { Executed after each action } - inherited; -end; - -procedure TSSEController.OnBeforeAction(Context: TWebContext; - const AActionName: string; var Handled: Boolean); -begin - { Executed before each action - if handled is true (or an exception is raised) the actual - action will not be called } - inherited; -end; - + + +interface + +uses + MVCFramework, MVCFramework.Commons; + +type + + [MVCPath('/')] + TSSEController = class(TMVCController) + public + [MVCPath('/stocks')] + [MVCHTTPMethod([httpGET])] + [MVCProduces('text/event-stream')] + procedure Index; + + protected + procedure OnBeforeAction(Context: TWebContext; const AActionName: string; + var Handled: Boolean); override; + procedure OnAfterAction(Context: TWebContext; + const AActionName: string); override; + end; + +implementation + +uses + MVCFramework.Logger, System.SysUtils, StorageU; + +procedure TSSEController.Index; +var + lLastEventID: Integer; + lCurrentEventID: Integer; + lMessage: string; +begin + // wait a little bit + Sleep(1000 + Random(2000)); + + // retrieve the last id received by the client reading the request header. + lLastEventID := StrToIntDef(Context.Request.Headers[TMVCConstants.SSE_LAST_EVENT_ID], 0); + + // get the next message to send based on the last id already received by the client + lMessage := GetNextDataToSend(lLastEventID, lCurrentEventID); + + RenderSSE(lCurrentEventID.ToString, lMessage, 'stockupdate'); +end; + +procedure TSSEController.OnAfterAction(Context: TWebContext; + const AActionName: string); +begin + { Executed after each action } + inherited; +end; + +procedure TSSEController.OnBeforeAction(Context: TWebContext; + const AActionName: string; var Handled: Boolean); +begin + { Executed before each action + if handled is true (or an exception is raised) the actual + action will not be called } + inherited; +end; + end. - \ No newline at end of file + diff --git a/samples/webcontextevents/MainControllerU.pas b/samples/webcontextevents/MainControllerU.pas index c76c5fd9..245af764 100644 --- a/samples/webcontextevents/MainControllerU.pas +++ b/samples/webcontextevents/MainControllerU.pas @@ -33,7 +33,9 @@ end; procedure TMyController.DoSum(const a, b: Integer); begin - Render((Context.CustomIntfObject as ICalculator).DoCalc(a,b).ToString); + var lSvc := Context.CustomIntfObject as ICalculator; + + Render(lSvc.DoCalc(a,b).ToString); end; end. diff --git a/sources/MVCFramework.Middleware.ActiveRecord.pas b/sources/MVCFramework.Middleware.ActiveRecord.pas index fc47ef8e..dd8a5c91 100644 --- a/sources/MVCFramework.Middleware.ActiveRecord.pas +++ b/sources/MVCFramework.Middleware.ActiveRecord.pas @@ -71,6 +71,8 @@ type AContext: TWebContext; const AHandled: Boolean); public + constructor Create( + const DefaultConnectionDefName: string); overload; virtual; constructor Create( const DefaultConnectionDefName: string; const ConnectionDefFileName: string{ = 'FDConnectionDefs.ini'}); overload; virtual; @@ -96,10 +98,7 @@ var constructor TMVCActiveRecordMiddleware.Create(const DefaultConnectionDefName: string; const ConnectionDefFileName: string); begin - inherited Create; - fConnectionLoaded := False; - fDefaultConnectionDefName := DefaultConnectionDefName; - fConnectionDefFileName := ConnectionDefFileName; + Create(DefaultConnectionDefName, [], [], ConnectionDefFileName); end; constructor TMVCActiveRecordMiddleware.Create( @@ -107,6 +106,7 @@ constructor TMVCActiveRecordMiddleware.Create( const AdditionalARConnectionNames, AdditionalConnectionDefNames: TArray; const ConnectionDefFileName: string); begin + inherited Create; fConnectionLoaded := False; fDefaultConnectionDefName := DefaultConnectionDefName; fConnectionDefFileName := ConnectionDefFileName; @@ -114,6 +114,12 @@ begin fAdditionalConnectionDefNames := AdditionalConnectionDefNames; end; +constructor TMVCActiveRecordMiddleware.Create( + const DefaultConnectionDefName: string); +begin + Create(DefaultConnectionDefName, 'FDConnectionDefs.ini'); +end; + procedure TMVCActiveRecordMiddleware.EnsureConnection; var I: Integer;