delphimvcframework/samples/routingsample/RoutingSampleControllerU.pas

102 lines
2.4 KiB
ObjectPascal
Raw Normal View History

2013-11-09 10:32:54 +01:00
unit RoutingSampleControllerU;
interface
uses
2013-11-10 01:03:53 +01:00
MVCFramework, MVCFramework.Commons, ObjectsMappers;
2013-11-09 10:32:54 +01:00
type
[MVCPath('/')]
TRoutingSampleController = class(TMVCController)
public
2014-03-06 14:20:57 +01:00
[MVCPath('/')]
procedure Index(CTX: TWebContext);
2013-11-09 10:32:54 +01:00
[MVCHTTPMethod([httpGet])]
2014-05-22 01:06:35 +02:00
[MVCPath('/searches/($searchtext)')]
2013-11-12 01:23:50 +01:00
[MVCProduces('text/plain', 'UTF-8')]
2013-11-09 10:32:54 +01:00
procedure SearchCustomers(CTX: TWebContext);
2013-11-10 01:03:53 +01:00
[MVCHTTPMethod([httpGet])]
2014-05-22 01:06:35 +02:00
[MVCPath('/people/($id)')]
{ double MVCPath }
2014-04-01 00:02:31 +02:00
[MVCPath('/($id)')]
2013-11-10 01:03:53 +01:00
[MVCProduces('application/json')]
procedure GetPerson(CTX: TWebContext);
2014-04-01 00:02:31 +02:00
[MVCHTTPMethod([httpDelete])]
[MVCPath('/people/($id)')]
procedure DeletePerson(CTX: TWebContext);
2013-11-09 10:32:54 +01:00
end;
implementation
uses
2013-11-10 01:03:53 +01:00
System.SysUtils, BusinessObjectsU, Data.DBXJSON;
2013-11-09 10:32:54 +01:00
{ TRoutingSampleController }
2014-04-01 00:02:31 +02:00
procedure TRoutingSampleController.DeletePerson(CTX: TWebContext);
var
IDPerson: Integer;
begin
IDPerson := CTX.Request.ParamsAsInteger['id'];
// RemovePerson(IDPerson)
Render(204 { 'No content' } , 'Person deleted');
end;
2013-11-10 01:03:53 +01:00
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;
2014-03-06 14:20:57 +01:00
procedure TRoutingSampleController.Index(CTX: TWebContext);
begin
Render('This is the root path');
end;
2013-11-09 10:32:54 +01:00
procedure TRoutingSampleController.SearchCustomers(CTX: TWebContext);
var
search: string;
2014-05-22 01:06:35 +02:00
Page: Integer;
2013-11-09 10:32:54 +01:00
orderby: string;
2014-04-01 00:02:31 +02:00
S: string;
2013-11-09 10:32:54 +01:00
begin
search := CTX.Request.Params['searchtext'];
2014-05-22 01:06:35 +02:00
Page := 1;
if CTX.Request.QueryStringParamExists('page') then
Page := CTX.Request.QueryStringParam('page').ToInteger;
2013-11-09 10:32:54 +01:00
orderby := '';
if CTX.Request.QueryStringParamExists('order') then
orderby := CTX.Request.QueryStringParam('order');
2014-04-01 00:02:31 +02:00
S := Format(
'SEARCHTEXT: "%s" - PAGE: %d - ORDER BY FIELD: "%s"',
2014-05-22 01:06:35 +02:00
[search, Page, orderby]);
2014-04-01 00:02:31 +02:00
ResponseStream
.AppendLine(S)
.AppendLine(StringOfChar('*', 30))
.AppendLine('1. Daniele Teti')
.AppendLine('2. John Doe')
.AppendLine('3. Mark Rossi')
.AppendLine('4. Jack Verdi')
.AppendLine(StringOfChar('*', 30));
Render;
2013-11-09 10:32:54 +01:00
end;
end.