delphimvcframework/samples/routing/RoutingSampleControllerU.pas

115 lines
3.0 KiB
ObjectPascal
Raw Normal View History

2013-11-09 10:32:54 +01:00
unit RoutingSampleControllerU;
interface
uses
2017-05-25 16:57:49 +02:00
MVCFramework, MVCFramework.Commons;
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;
2014-03-06 14:20:57 +01:00
2016-11-24 20:36:14 +01:00
{ This action requires that the ACCEPT header is text/plain to be invocated }
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
2016-11-24 20:36:14 +01:00
{ This action requires that the ACCEPT header is application/json to be invocated }
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')]
2016-11-24 20:36:14 +01:00
procedure GetPerson(const id: Integer);
2013-11-10 01:03:53 +01:00
2014-04-01 00:02:31 +02:00
[MVCHTTPMethod([httpDelete])]
[MVCPath('/people/($id)')]
2016-11-24 20:36:14 +01:00
procedure DeletePerson(const id: Integer);
{ To be invocated this action requires that:
- the CONTENT-TYPE header is application/json and
- that the ACCEPT header is application/json
}
[MVCHTTPMethod([httpPOST])]
[MVCPath('/people')]
[MVCProduces(TMVCMediaType.APPLICATION_JSON)]
[MVCConsumes(TMVCMediaType.APPLICATION_JSON)]
procedure CreatePerson;
2014-04-01 00:02:31 +02:00
2013-11-09 10:32:54 +01:00
end;
implementation
uses
2016-11-24 20:36:14 +01:00
System.SysUtils, BusinessObjectsU, Data.DBXJSON, System.JSON;
2013-11-09 10:32:54 +01:00
{ TRoutingSampleController }
2016-11-24 20:36:14 +01:00
procedure TRoutingSampleController.CreatePerson;
2014-04-01 00:02:31 +02:00
var
2016-11-27 23:17:20 +01:00
lPerson: TPerson;
2014-04-01 00:02:31 +02:00
begin
2016-11-27 23:17:20 +01:00
lPerson := Context.Request.BodyAs<TPerson>;
lPerson.Validate;
// SavePerson(lPerson);
Render(HTTP_STATUS.Created, 'Person created');
2016-11-24 20:36:14 +01:00
end;
procedure TRoutingSampleController.DeletePerson(const id: Integer);
begin
{ Here you should do something with id }
// RemovePerson(ID)
2014-04-01 00:02:31 +02:00
Render(204 { 'No content' } , 'Person deleted');
end;
2016-11-24 20:36:14 +01:00
procedure TRoutingSampleController.GetPerson(const id: Integer);
2013-11-10 01:03:53 +01:00
var
P: TPerson;
begin
{
2016-11-24 20:36:14 +01:00
Use ID to load the person from a database...
2013-11-10 01:03:53 +01:00
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;
2014-03-06 14:20:57 +01:00
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 := StrToInt(CTX.Request.QueryStringParam('page'));
2013-11-09 10:32:54 +01:00
orderby := '';
if CTX.Request.QueryStringParamExists('order') then
orderby := CTX.Request.QueryStringParam('order');
2016-11-24 20:36:14 +01:00
S := Format('SEARCHTEXT: "%s" - PAGE: %d - ORDER BY FIELD: "%s"',
2014-05-22 01:06:35 +02:00
[search, Page, orderby]);
2016-11-24 20:36:14 +01:00
ResponseStream.AppendLine(S).AppendLine(StringOfChar('*', 30))
.AppendLine('1. Daniele Teti').AppendLine('2. John Doe')
.AppendLine('3. Mark Rossi').AppendLine('4. Jack Verdi')
2014-04-01 00:02:31 +02:00
.AppendLine(StringOfChar('*', 30));
2016-11-27 23:17:20 +01:00
RenderResponseStream;
2013-11-09 10:32:54 +01:00
end;
end.