delphimvcframework/samples/routing/RoutingSampleControllerU.pas

149 lines
3.8 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
[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')]
2021-04-06 19:22:28 +02:00
procedure SearchCustomers;
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
{ 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('/people2')]
[MVCProduces(TMVCMediaType.APPLICATION_JSON)]
[MVCConsumes(TMVCMediaType.APPLICATION_JSON)]
procedure CreatePerson2;
2013-11-09 10:32:54 +01:00
end;
implementation
uses
System.SysUtils, BusinessObjectsU, JsonDataObjects;
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>;
try
lPerson.Validate;
// SavePerson(lPerson);
finally
lPerson.Free;
end;
Render(HTTP_STATUS.Created, 'Person created');
2016-11-24 20:36:14 +01:00
end;
procedure TRoutingSampleController.CreatePerson2;
var
lJPerson: TJSONObject;
begin
lJPerson := StrToJSONObject(Context.Request.Body);
try
//SavePerson(lJPerson);
finally
lJPerson.Free;
end;
Render(HTTP_STATUS.Created, 'Person created JSON');
end;
2016-11-24 20:36:14 +01:00
procedure TRoutingSampleController.DeletePerson(const id: Integer);
begin
{ Here you should do something with id }
// RemovePerson(ID)
2021-04-06 19:22:28 +02:00
2014-04-01 00:02:31 +02:00
Render(204 { 'No content' } , 'Person deleted');
2021-04-06 19:22:28 +02:00
//Render204NoContent(); { Using the response shortcut methods }
2014-04-01 00:02:31 +02:00
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;
2021-04-06 19:22:28 +02:00
procedure TRoutingSampleController.SearchCustomers;
2013-11-09 10:32:54 +01:00
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
2021-04-06 19:22:28 +02:00
search := Context.Request.Params['searchtext'];
2014-05-22 01:06:35 +02:00
Page := 1;
2021-04-06 19:22:28 +02:00
if Context.Request.QueryStringParamExists('page') then
Page := StrToInt(Context.Request.QueryStringParam('page'));
2013-11-09 10:32:54 +01:00
orderby := '';
2021-04-06 19:22:28 +02:00
if Context.Request.QueryStringParamExists('order') then
orderby := Context.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]);
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.