mirror of
https://github.com/danieleteti/delphimvcframework.git
synced 2024-11-18 09:15:55 +01:00
0f347fc567
Small refactoring
78 lines
1.8 KiB
ObjectPascal
78 lines
1.8 KiB
ObjectPascal
unit RoutingSampleControllerU;
|
|
|
|
interface
|
|
|
|
uses
|
|
MVCFramework, MVCFramework.Commons, ObjectsMappers;
|
|
|
|
type
|
|
|
|
[MVCPath('/')]
|
|
TRoutingSampleController = class(TMVCController)
|
|
public
|
|
[MVCPath('/')]
|
|
procedure Index(CTX: TWebContext);
|
|
|
|
[MVCHTTPMethod([httpGet])]
|
|
[MVCPath('/search/($searchtext)/($page)')]
|
|
[MVCProduces('text/plain', 'UTF-8')]
|
|
[MVCConsumes('text/html')]
|
|
procedure SearchCustomers(CTX: TWebContext);
|
|
|
|
[MVCHTTPMethod([httpGet])]
|
|
[MVCPath('/people/($id)')]
|
|
[MVCProduces('application/json')]
|
|
procedure GetPerson(CTX: TWebContext);
|
|
|
|
end;
|
|
|
|
implementation
|
|
|
|
uses
|
|
System.SysUtils, BusinessObjectsU, Data.DBXJSON;
|
|
|
|
{ TRoutingSampleController }
|
|
|
|
procedure TRoutingSampleController.GetPerson(CTX: TWebContext);
|
|
var
|
|
P: TPerson;
|
|
IDPerson: Integer;
|
|
begin
|
|
IDPerson := CTX.Request.ParamsAsInteger['id'];
|
|
{
|
|
Use IDPerson to load the person from a database...
|
|
In this example, we're creating a fake person
|
|
}
|
|
P := TPerson.Create;
|
|
P.FirstName := 'Daniele';
|
|
P.LastName := 'Teti';
|
|
P.DOB := EncodeDate(1975, 5, 2);
|
|
P.Married := True;
|
|
Render(P);
|
|
end;
|
|
|
|
procedure TRoutingSampleController.Index(CTX: TWebContext);
|
|
begin
|
|
Render('This is the root path');
|
|
end;
|
|
|
|
procedure TRoutingSampleController.SearchCustomers(CTX: TWebContext);
|
|
var
|
|
search: string;
|
|
P: Integer;
|
|
orderby: string;
|
|
begin
|
|
search := CTX.Request.Params['searchtext'];
|
|
P := CTX.Request.ParamsAsInteger['page'];
|
|
orderby := '';
|
|
if CTX.Request.QueryStringParamExists('order') then
|
|
orderby := CTX.Request.QueryStringParam('order');
|
|
Render(Format(
|
|
'SEARCHTEXT: "%s"' + sLineBreak +
|
|
'PAGE: %d' + sLineBreak +
|
|
'ORDERBYFIELD: "%s"',
|
|
[search, P, orderby]));
|
|
end;
|
|
|
|
end.
|