delphimvcframework/samples/renders/RenderSampleControllerU.pas

97 lines
2.4 KiB
ObjectPascal
Raw Normal View History

2013-11-10 01:03:53 +01:00
unit RenderSampleControllerU;
interface
uses
MVCFramework, MVCFramework.Commons, ObjectsMappers;
type
[MVCPath('/')]
TRenderSampleController = class(TMVCController)
public
2013-11-14 02:09:22 +01:00
[MVCHTTPMethod([httpGet])]
[MVCPath('/customers/($id).html')]
2014-03-03 15:28:00 +01:00
{ this route require a request header ACCEPT: text/html }
2013-11-14 02:09:22 +01:00
[MVCConsumes('text/html')]
[MVCProduces('text/html', 'UTF-8')]
procedure GetPerson_AsText(CTX: TWebContext);
2013-11-10 01:03:53 +01:00
[MVCHTTPMethod([httpGet])]
[MVCPath('/customers')]
[MVCProduces('application/json')]
2013-11-14 02:09:22 +01:00
procedure GetCustomers_AsDataSet(CTX: TWebContext);
2013-11-10 01:03:53 +01:00
[MVCHTTPMethod([httpGet])]
2013-11-11 01:11:09 +01:00
[MVCPath('/customers/($id)')]
[MVCProduces('application/json')]
2013-11-14 02:09:22 +01:00
procedure GetCustomerByID_AsTObject(CTX: TWebContext);
2013-11-10 01:03:53 +01:00
2013-11-11 19:32:20 +01:00
[MVCHTTPMethod([httpGet])]
2013-11-14 02:09:22 +01:00
[MVCPath('/customers.json')]
2013-11-11 19:32:20 +01:00
[MVCProduces('application/json')]
procedure GetPersonJSON(CTX: TWebContext);
2013-11-10 01:03:53 +01:00
end;
implementation
uses
System.SysUtils, BusinessObjectsU, Data.DBXJSON, WebModuleU;
{ TRoutingSampleController }
2013-11-14 02:09:22 +01:00
procedure TRenderSampleController.GetCustomerByID_AsTObject(CTX: TWebContext);
2013-11-10 01:03:53 +01:00
var
2013-11-11 01:11:09 +01:00
Cust: TCustomer;
2013-11-10 01:03:53 +01:00
begin
2013-11-11 01:11:09 +01:00
if CTX.Request.ParamsAsInteger['id'] = 7 then
Render(404, 'Customer Not Found')
else
begin
Cust := TCustomer.Create;
Cust.Name := 'Daniele Teti Inc.';
Cust.ContactFirst := 'Daniele';
Cust.ContactLast := 'Teti';
Cust.AddressLine1 := 'Rome Street 12';
Cust.AddressLine2 := '00100';
Cust.City := 'ROME';
Render(Cust);
end;
2013-11-10 01:03:53 +01:00
end;
2013-11-14 02:09:22 +01:00
procedure TRenderSampleController.GetCustomers_AsDataSet(CTX: TWebContext);
2013-11-10 01:03:53 +01:00
var
wm: TWebModule1;
begin
wm := GetCurrentWebModule as TWebModule1;
wm.qryCustomers.Open;
Render(wm.qryCustomers);
end;
2013-11-14 02:09:22 +01:00
procedure TRenderSampleController.GetPerson_AsText(CTX: TWebContext);
2013-11-11 19:32:20 +01:00
begin
ResponseStream.
Append('<html><body><ul>').
Append('<li>FirstName: Daniele</li>').
Append('<li>LastName: Teti').
AppendFormat('<li>DOB: %s</li>', [ISODateToString(EncodeDate(1975, 5, 2))]).
Append('<li>Married: yes</li>').
Append('</ul></body></html>');
Render;
end;
procedure TRenderSampleController.GetPersonJSON(CTX: TWebContext);
var
P: TJSONObject;
begin
P := TJSONObject.Create;
P.AddPair('FirstName', 'Daniele');
P.AddPair('LastName', 'Teti');
P.AddPair('DOB', ISODateToString(EncodeDate(1975, 5, 2)));
P.AddPair('Married', TJSONTrue.Create);
Render(P);
end;
2013-11-10 01:03:53 +01:00
end.