Skip to content

Commit

Permalink
Changed fropm procedure to functional actions
Browse files Browse the repository at this point in the history
  • Loading branch information
danieleteti committed Jan 23, 2024
1 parent 0ccf2b6 commit fada5f5
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions samples/serversideviews_mustache/WebSiteControllerU.pas
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ TWebSiteController = class(TMVCController)
protected
procedure OnBeforeAction(Context: TWebContext; const AActionNAme: string;
var Handled: Boolean); override;
procedure GeneratePeopleListAsCSV;
function GeneratePeopleListAsCSV: String;
public
[MVCPath('/people')]
[MVCHTTPMethods([httpGET])]
[MVCProduces(TMVCMediaType.TEXT_HTML)]
procedure PeopleList;
function PeopleList: String;

[MVCPath('/people')]
[MVCHTTPMethods([httpGET])]
[MVCProduces(TMVCMediaType.TEXT_CSV)]
// RESTful API, requires ACCEPT=text/csv
procedure ExportPeopleListAsCSV_API;
function ExportPeopleListAsCSV_API: String;

[MVCPath('/people/formats/csv')]
[MVCHTTPMethods([httpGET])]
// Route usable by the browser, doesn't requires ACCEPT=text/csv
procedure ExportPeopleListAsCSV;
function ExportPeopleListAsCSV: String;

[MVCPath('/people')]
[MVCHTTPMethods([httpPOST])]
Expand Down Expand Up @@ -59,7 +59,7 @@ TWebSiteController = class(TMVCController)
[MVCPath('/showcase')]
[MVCHTTPMethods([httpGET])]
[MVCProduces(TMVCMediaType.TEXT_HTML)]
procedure MustacheTemplateShowCase;
function MustacheTemplateShowCase: String;
end;

implementation
Expand Down Expand Up @@ -103,7 +103,7 @@ function TWebSiteController.EditPerson(guid: string): String;
lJItm.S['name'] := lItem;
lJItm.B['selected'] := TArray.BinarySearch<String>(lDevices, lItem, lIdx);
end;
Result := GetRenderedView(['header', 'editperson', 'footer'], lJObj);
Result := Page(['editperson'], lJObj);
finally
lJObj.Free;
end;
Expand All @@ -112,21 +112,21 @@ function TWebSiteController.EditPerson(guid: string): String;
end;
end;

procedure TWebSiteController.ExportPeopleListAsCSV;
function TWebSiteController.ExportPeopleListAsCSV: String;
begin
GeneratePeopleListAsCSV;
Result := GeneratePeopleListAsCSV;
// define the correct behaviour to download the csv inside the browser
ContentType := TMVCMediaType.TEXT_CSV;
Context.Response.CustomHeaders.Values['Content-Disposition'] :=
'attachment; filename=people.csv';
end;

procedure TWebSiteController.ExportPeopleListAsCSV_API;
function TWebSiteController.ExportPeopleListAsCSV_API: String;
begin
GeneratePeopleListAsCSV;
Result := GeneratePeopleListAsCSV;
end;

procedure TWebSiteController.GeneratePeopleListAsCSV;
function TWebSiteController.GeneratePeopleListAsCSV: String;
var
LDAL: IPeopleDAL;
lPeople: TPeople;
Expand All @@ -135,8 +135,7 @@ procedure TWebSiteController.GeneratePeopleListAsCSV;
lPeople := LDAL.GetPeople;
try
ViewData['people'] := lPeople;
LoadView(['people_header.csv', 'people_list.csv']);
RenderResponseStream; // rember to call RenderResponseStream!!!
Result := PageFragment(['people_header.csv', 'people_list.csv']);
finally
lPeople.Free;
end;
Expand All @@ -147,7 +146,7 @@ procedure TWebSiteController.Index;
Redirect('/people');
end;

procedure TWebSiteController.MustacheTemplateShowCase;
function TWebSiteController.MustacheTemplateShowCase: String;
var
LDAL: IPeopleDAL;
lPeople, lPeople2: TPeople;
Expand All @@ -164,8 +163,7 @@ procedure TWebSiteController.MustacheTemplateShowCase;
ViewData['people'] := lPeople;
ViewData['people2'] := lPeople2;
ViewData['myobj'] := lMyObj;
LoadView(['showcase']);
RenderResponseStream;
Result := Page(['showcase'], False);
finally
lMyObj.Free;
end;
Expand Down Expand Up @@ -194,7 +192,7 @@ function TWebSiteController.NewPerson: String;
lJItm.S['name'] := lItem;
lJItm.B['selected'] := False;
end;
Result := GetRenderedView(['header', 'editperson', 'footer'], lJObj);
Result := Page(['editperson'], lJObj);
finally
lJObj.Free;
end;
Expand All @@ -204,11 +202,13 @@ procedure TWebSiteController.OnBeforeAction(Context: TWebContext;
const AActionNAme: string; var Handled: Boolean);
begin
inherited;
SetPagesCommonHeaders(['header']);
SetPagesCommonFooters(['footer']);
ContentType := 'text/html';
Handled := False;
end;

procedure TWebSiteController.PeopleList;
function TWebSiteController.PeopleList: String;
var
LDAL: IPeopleDAL;
lPeople: TPeople;
Expand All @@ -217,8 +217,7 @@ procedure TWebSiteController.PeopleList;
lPeople := LDAL.GetPeople;
try
ViewData['people'] := lPeople;
LoadView(['header', 'people_list', 'footer']);
RenderResponseStream; // rember to call RenderResponseStream!!!
Result := Page(['people_list']);
finally
lPeople.Free;
end;
Expand Down

0 comments on commit fada5f5

Please sign in to comment.