Skip to content

Commit

Permalink
Delphi Tokyo Compat
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleteti committed May 2, 2024
1 parent d0c3414 commit 9b414b8
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 28 deletions.
28 changes: 7 additions & 21 deletions samples/articles_crud_server/Controllers.Articles.pas
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ constructor TArticlesController.Create(ArticlesService: IArticlesService);
function TArticlesController.CreateArticle(const Article: TArticle): IMVCResponse;
begin
fArticlesService.Add(Article);
Render201Created('/articles/' + Article.id.ToString, 'Article Created');
Result := CreatedResponse('/articles/' + Article.id.ToString, 'Article Created');
end;

function TArticlesController.CreateArticles(const ArticleList: TObjectList<TArticle>): IMVCResponse;
begin
fArticlesService.CreateArticles(ArticleList);
Result := MVCResponseBuilder.StatusCode(HTTP_STATUS.Created).Build;
Result := CreatedResponse('', 'Articles created');
end;

procedure TArticlesController.DeleteArticleByID(id: Integer);
Expand All @@ -93,43 +93,29 @@ procedure TArticlesController.DeleteArticleByID(id: Integer);

function TArticlesController.GetArticles: IMVCResponse;
begin
Result := MVCResponseBuilder
.StatusCode(HTTP_STATUS.OK)
.Body(fArticlesService.GetAll)
.Build;
Result := OKResponse(fArticlesService.GetAll);
end;

function TArticlesController.GetArticlesByDescription(const Search: String): IMVCResponse;
begin
Result := MVCResponseBuilder
.StatusCode(HTTP_STATUS.OK)
.Body(fArticlesService.GetArticles(Search))
.Build;
Result := OKResponse(fArticlesService.GetArticles(Search));
end;

function TArticlesController.UpdateArticleByID(const Article: TArticle; const id: Integer): IMVCResponse;
begin
Article.id := id;
fArticlesService.Update(Article);
Result := MVCResponseBuilder
.StatusCode(HTTP_STATUS.OK)
.Build;
Result := OKResponse;
end;

function TArticlesController.GetArticleByID(id: Integer): IMVCResponse;
begin
Result := MVCResponseBuilder
.StatusCode(HTTP_STATUS.OK)
.Body(fArticlesService.GetByID(id))
.Build;
Result := OKResponse(fArticlesService.GetByID(id));
end;

function TArticlesController.GetArticleMeta: IMVCResponse;
begin
Result := MVCResponseBuilder
.StatusCode(HTTP_STATUS.OK)
.Body(fArticlesService.GetMeta)
.Build;
Result := OKResponse(fArticlesService.GetMeta);
end;

end.
4 changes: 1 addition & 3 deletions samples/articles_crud_vcl_client/MainFormU.dfm
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ object MainForm: TMainForm
Height = 112
Align = alTop
TabOrder = 0
ExplicitWidth = 872
object DBNavigator1: TDBNavigator
AlignWithMargins = True
Left = 585
Expand All @@ -29,7 +28,6 @@ object MainForm: TMainForm
DataSource = dsrcArticles
Align = alRight
TabOrder = 3
ExplicitLeft = 581
end
object btnOpen: TButton
AlignWithMargins = True
Expand Down Expand Up @@ -72,7 +70,6 @@ object MainForm: TMainForm
Align = alBottom
BevelOuter = bvNone
TabOrder = 4
ExplicitWidth = 870
object Label1: TLabel
Left = 3
Top = 11
Expand Down Expand Up @@ -153,6 +150,7 @@ object MainForm: TMainForm
object dsArticles: TFDMemTable
AfterOpen = dsArticlesAfterOpen
BeforePost = dsArticlesBeforePost
AfterPost = dsArticlesAfterPost
BeforeDelete = dsArticlesBeforeDelete
BeforeRefresh = dsArticlesBeforeRefresh
FieldDefs = <>
Expand Down
15 changes: 11 additions & 4 deletions samples/articles_crud_vcl_client/MainFormU.pas
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ TMainForm = class(TForm)
procedure dsArticlesBeforeRowRequest(DataSet: TFDDataSet);
procedure btnRefreshRecordClick(Sender: TObject);
procedure btnFilterClick(Sender: TObject);
procedure dsArticlesAfterPost(DataSet: TDataSet);
private
fFilter: string;
fLoading: Boolean;
fRESTClient: IMVCRESTClient;
fBkmrk: TArray<Byte>;
{ Private declarations }
procedure ShowError(const AResponse: IMVCRESTResponse);
procedure SetFilter(const Value: string);
Expand Down Expand Up @@ -120,6 +122,14 @@ procedure TMainForm.dsArticlesAfterOpen(DataSet: TDataSet);
end;
end;

procedure TMainForm.dsArticlesAfterPost(DataSet: TDataSet);
begin
if DataSet.BookmarkValid(fBkmrk) then
begin
DataSet.GotoBookmark(fBkmrk);
end;
end;

procedure TMainForm.dsArticlesBeforeDelete(DataSet: TDataSet);
var
Res: IMVCRESTResponse;
Expand Down Expand Up @@ -147,11 +157,8 @@ procedure TMainForm.dsArticlesBeforePost(DataSet: TDataSet);
begin
ShowError(Res);
Abort;
end
else
begin
DataSet.Refresh;
end;
fBkmrk := DataSet.GetBookmark;
end;
end;

Expand Down
3 changes: 3 additions & 0 deletions sources/MVCFramework.RESTClient.Intf.pas
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,13 @@ interface
/// </summary>
function SetResponseCompletedProc(aResponseCompletedProc: TResponseCompletedProc): IMVCRESTClient;


{$IF Defined(SYDNEYORBETTER)}
/// <summary>
/// Executes while sending data
/// </summary>
function SetSendDataProc(aSendDataProc: TSendDataProc): IMVCRESTClient;
{$ENDIF}

///<summary>
/// Set the client certificate for the request</summary>
Expand Down
10 changes: 10 additions & 0 deletions sources/MVCFramework.RESTClient.pas
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ TMVCRESTClient = class(TInterfacedObject, IMVCRESTClient)
procedure DoBeforeRequest(aRequest: IHTTPRequest);
procedure DoRequestCompleted(aResponse: IHTTPResponse; var aHandled: Boolean);
procedure DoResponseCompleted(aMVCRESTResponse: IMVCRESTResponse);
{$IF defined(SYDNEYORBETTER)}
procedure DoOnSendDataEvent(const Sender: TObject; AContentLength, AWriteCount: Int64; var AAbort: Boolean);
{$ENDIF}
function GetBodyFormData: TMultipartFormData;
function ObjectIsList(aObject: TObject): Boolean;
function SerializeObject(aObject: TObject): string;
Expand Down Expand Up @@ -190,7 +192,9 @@ TMVCRESTClient = class(TInterfacedObject, IMVCRESTClient)
/// <summary>
/// Executes while sending data
/// </summary>
{$IF defined(SYDNEYORBETTER)}
function SetSendDataProc(aSendDataProc: TSendDataProc): IMVCRESTClient;
{$ENDIF}

///<summary>
/// Set the client certificate for the request</summary>
Expand Down Expand Up @@ -958,7 +962,9 @@ constructor TMVCRESTClient.Create;
fHTTPClient.OnValidateServerCertificate := DoValidateServerCertificate;
fHTTPClient.HandleRedirects := True;
fHTTPClient.MaxRedirects := TMVCRESTClientConsts.DEFAULT_MAX_REDIRECTS;
{$IF defined(SYDNEYORBETTER)}
fHTTPClient.OnSendData := DoOnSendDataEvent;
{$ENDIF}
{$IF defined(TOKYOORBETTER)}
fHTTPClient.SecureProtocols := CHTTPDefSecureProtocols;
{$ENDIF}
Expand Down Expand Up @@ -1520,6 +1526,7 @@ function TMVCRESTClient.ObjectIsList(aObject: TObject): Boolean;
Result := fRttiContext.GetType(aObject.ClassType).GetMethod('GetEnumerator') <> nil;
end;

{$IF defined(SYDNEYORBETTER)}
procedure TMVCRESTClient.DoOnSendDataEvent(const Sender: TObject; AContentLength,
AWriteCount: Int64; var AAbort: Boolean);
begin
Expand All @@ -1528,6 +1535,7 @@ procedure TMVCRESTClient.DoOnSendDataEvent(const Sender: TObject; AContentLength
fSendDataProc(AContentLength, AWriteCount, AAbort);
end;
end;
{$ENDIF}

function TMVCRESTClient.Options: IMVCRESTResponse;
begin
Expand Down Expand Up @@ -1896,11 +1904,13 @@ function TMVCRESTClient.SetResponseCompletedProc(aResponseCompletedProc: TRespon
fResponseCompletedProc := aResponseCompletedProc;
end;

{$IF Defined(SYDNEYORBETTER)}
function TMVCRESTClient.SetSendDataProc(
aSendDataProc: TSendDataProc): IMVCRESTClient;
begin
fSendDataProc := aSendDataProc;
end;
{$ENDIF}

function TMVCRESTClient.SetValidateServerCertificateProc(
aValidateCertificateProc: TValidateServerCertificateProc): IMVCRESTClient;
Expand Down

0 comments on commit 9b414b8

Please sign in to comment.