delphimvcframework/samples/renders/RenderSampleControllerU.pas

302 lines
7.8 KiB
ObjectPascal
Raw Normal View History

2014-07-04 17:52:17 +02:00
unit RenderSampleControllerU;
2013-11-10 01:03:53 +01:00
interface
uses
MVCFramework, MVCFramework.Commons, ObjectsMappers, System.JSON;
2013-11-10 01:03:53 +01:00
type
[MVCPath('/')]
TRenderSampleController = class(TMVCController)
public
2016-11-27 23:17:20 +01:00
[MVCHTTPMethod([httpGET])]
[MVCPath('/customers/($id)')]
[MVCProduces('text/plain')]
procedure GetPerson_AsText(const id: Integer);
[MVCHTTPMethod([httpGET])]
2013-11-10 01:03:53 +01:00
[MVCPath('/customers')]
2016-11-27 23:17:20 +01:00
[MVCProduces('application/json')]
2013-11-14 02:09:22 +01:00
procedure GetCustomers_AsDataSet(CTX: TWebContext);
2013-11-10 01:03:53 +01:00
2016-11-27 23:17:20 +01:00
[MVCHTTPMethod([httpGET])]
[MVCPath('/multi')]
[MVCProduces('application/json')]
procedure GetCustomersAndCountry_AsDataSet;
[MVCHTTPMethod([httpGET])]
2014-04-01 00:02:31 +02:00
[MVCPath('/people')]
[MVCProduces('application/json')]
2016-11-27 23:17:20 +01:00
procedure GetPeople_AsObjectList;
2014-04-01 00:02:31 +02:00
2016-11-27 23:17:20 +01:00
[MVCHTTPMethod([httpGET])]
2014-05-30 11:29:58 +02:00
[MVCPath('/skilledpeople')]
2015-06-29 14:36:54 +02:00
[MVCProduces('application/json')]
2016-11-27 23:17:20 +01:00
procedure GetProgrammersAndPhilosophersAsObjectList;
2014-05-30 11:29:58 +02:00
2016-11-27 23:17:20 +01:00
[MVCHTTPMethod([httpGET])]
2016-02-26 19:21:57 +01:00
[MVCPath('/customers/view/($id).html')]
[MVCProduces('text/html', 'UTF-8')]
2016-11-27 23:17:20 +01:00
procedure GetPerson_AsHTMLView;
2016-02-26 19:21:57 +01:00
2016-11-27 23:17:20 +01:00
[MVCHTTPMethod([httpGET])]
2014-04-01 00:02:31 +02:00
[MVCPath('/customers/($id).html')]
[MVCProduces('text/html', 'UTF-8')]
procedure GetPerson_AsHTML(CTX: TWebContext);
2016-11-27 23:17:20 +01:00
[MVCHTTPMethod([httpGET])]
2014-07-04 17:52:17 +02:00
[MVCPath('/customers/unicode/($id).html')]
[MVCProduces('text/html', 'UTF-8')]
procedure GetUnicodeText_AsHTML(CTX: TWebContext);
2016-11-27 23:17:20 +01:00
[MVCHTTPMethod([httpGET])]
2013-11-11 01:11:09 +01:00
[MVCPath('/customers/($id)')]
[MVCProduces('application/json')]
2016-11-27 23:17:20 +01:00
procedure GetCustomerByID_AsTObject(const id: Integer);
2013-11-10 01:03:53 +01:00
2016-11-27 23:17:20 +01:00
[MVCHTTPMethod([httpGET])]
2014-03-06 14:20:57 +01:00
[MVCPath('/files/customers.json')]
2013-11-11 19:32:20 +01:00
[MVCProduces('application/json')]
2016-11-27 23:17:20 +01:00
procedure GetPersonJSON;
2014-04-01 00:02:31 +02:00
2016-11-27 23:17:20 +01:00
[MVCHTTPMethod([httpGET])]
2014-04-01 00:02:31 +02:00
[MVCPath('/files/customers.png')]
[MVCProduces('image/png')]
2016-11-27 23:17:20 +01:00
procedure GetPersonPhoto;
2014-04-01 00:02:31 +02:00
2016-11-27 23:17:20 +01:00
[MVCHTTPMethod([httpGET])]
2015-06-29 14:36:54 +02:00
[MVCPath('/images/customers/($id)')]
procedure GetPersonPhotoAsStream(CTX: TWebContext);
2016-11-27 23:17:20 +01:00
[MVCHTTPMethod([httpGET])]
[MVCPath('/exception')]
procedure RaiseException;
2013-11-10 01:03:53 +01:00
end;
implementation
uses
2016-02-26 19:21:57 +01:00
System.SysUtils, BusinessObjectsU, Data.DBXJSON, WebModuleU,
Generics.Collections,
2016-11-27 23:17:20 +01:00
System.Classes, MyDataModuleU;
2013-11-10 01:03:53 +01:00
{ TRoutingSampleController }
2014-07-04 17:52:17 +02:00
procedure TRenderSampleController.GetUnicodeText_AsHTML(CTX: TWebContext);
var
s: string;
begin
s := '<html><body>';
s := s + '什么是Unicode(统一码)? in Simplified Chinese <br>';
s := s + 'Što je Unicode? in Croatian <br>';
s := s + 'Co je Unicode? in Czech';
s := s + '</body></html>';
Render(s);
end;
2016-11-27 23:17:20 +01:00
procedure TRenderSampleController.RaiseException;
var
a: Integer;
begin
a := 0;
Render(IntToStr(10 div a));
end;
procedure TRenderSampleController.GetCustomerByID_AsTObject(const id: Integer);
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
2016-11-27 23:17:20 +01:00
if id = 7 then // just a sample
Render(HTTP_STATUS.NotFound, 'Customer Not Found')
2013-11-11 01:11:09 +01:00
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;
2016-11-27 23:17:20 +01:00
procedure TRenderSampleController.GetCustomersAndCountry_AsDataSet;
var
lDM: TMyDataModule;
lJObj: TJSONObject;
begin
lDM := TMyDataModule.Create(nil);
try
lDM.qryCustomers.Open;
lDM.qryCountry.Open;
lJObj := TJSONObject.Create;
try
lJObj.AddPair('customers', lDM.qryCustomers.AsJSONArray);
lJObj.AddPair('countries', lDM.qryCountry.AsJSONArray);
Render(lJObj);
except // avoid memory leaks
lJObj.Free;
raise;
end;
finally
lDM.Free;
end;
end;
2013-11-14 02:09:22 +01:00
procedure TRenderSampleController.GetCustomers_AsDataSet(CTX: TWebContext);
2013-11-10 01:03:53 +01:00
var
2016-11-27 23:17:20 +01:00
lDM: TMyDataModule;
2013-11-10 01:03:53 +01:00
begin
2016-11-27 23:17:20 +01:00
lDM := TMyDataModule.Create(nil);
try
lDM.qryCustomers.Open;
Render(lDM.qryCustomers);
finally
lDM.Free;
end;
2013-11-10 01:03:53 +01:00
end;
2014-04-01 00:02:31 +02:00
procedure TRenderSampleController.GetPerson_AsHTML(CTX: TWebContext);
2013-11-11 19:32:20 +01:00
begin
2016-11-27 23:17:20 +01:00
ResponseStream
.Append('<html><body><ul>')
.Append('<li>FirstName: Daniele</li>')
.Append('<li>LastName: Teti')
2016-02-26 19:21:57 +01:00
.AppendFormat('<li>DOB: %s</li>', [ISODateToString(EncodeDate(1975, 5, 2))])
2016-11-27 23:17:20 +01:00
.Append('<li>Married: yes</li>')
.Append('</ul></body></html>');
2016-11-18 14:09:54 +01:00
RenderResponseStream;
2016-02-26 19:21:57 +01:00
end;
2016-11-27 23:17:20 +01:00
procedure TRenderSampleController.GetPerson_AsHTMLView;
2016-02-26 19:21:57 +01:00
var
Cust: TCustomer;
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';
PushObjectToView('customer', Cust);
2016-02-27 09:58:54 +01:00
LoadView(['header', 'customer', 'footer']);
2016-11-18 14:09:54 +01:00
RenderResponseStream;
2016-02-27 09:58:54 +01:00
{ If you need more flexibility, you can use GetRenderedView to compose your
output using small views.
Here's an example:
ContentType := TMVCMediaType.TEXT_HTML;
Render(GetRenderedView(['header', 'customer','footer']));
}
2013-11-11 19:32:20 +01:00
end;
2016-11-27 23:17:20 +01:00
procedure TRenderSampleController.GetPerson_AsText(const id: Integer);
2014-04-01 00:02:31 +02:00
begin
2016-11-27 23:17:20 +01:00
ResponseStream
.AppendLine('ID : ' + id.ToString)
.AppendLine('FirstName : Daniele')
.AppendLine('LastName : Teti')
.AppendLine('DOB : ' + DateToStr(EncodeDate(1979, 5, 2)))
.AppendLine('Married : yes');
2016-11-18 14:09:54 +01:00
RenderResponseStream;
2014-04-01 00:02:31 +02:00
end;
2016-02-26 19:21:57 +01:00
procedure TRenderSampleController.GetProgrammersAndPhilosophersAsObjectList
2016-11-27 23:17:20 +01:00
;
2014-05-30 11:29:58 +02:00
var
List: TObjectList<TPerson>;
p: TProgrammer;
ph: TPhilosopher;
begin
List := TObjectList<TPerson>.Create(True);
p := TProgrammer.Create;
p.Married := True;
p.FirstName := 'Peter';
p.LastName := 'Parker';
p.Skills := 'Delphi, JavaScript, Python, C++';
List.Add(p);
ph := TPhilosopher.Create;
p.Married := False;
ph.FirstName := 'Bruce';
ph.LastName := 'Banner';
ph.Mentors := 'Abbagnano, Algarotti, Cavalieri, Pareyson';
List.Add(ph);
p := TProgrammer.Create;
p.Married := False;
p.FirstName := 'Sue';
p.LastName := 'Storm';
p.Skills := 'Delphi, JavaScript';
List.Add(p);
Render<TPerson>(List);
end;
2016-11-27 23:17:20 +01:00
procedure TRenderSampleController.GetPeople_AsObjectList;
2014-04-01 00:02:31 +02:00
var
2014-05-30 11:29:58 +02:00
p: TPerson;
2014-04-01 00:02:31 +02:00
People: TObjectList<TPerson>;
begin
People := TObjectList<TPerson>.Create(True);
{$REGION 'Fake data'}
2014-05-30 11:29:58 +02:00
p := TPerson.Create;
p.FirstName := 'Daniele';
p.LastName := 'Teti';
p.DOB := EncodeDate(1979, 11, 4);
p.Married := True;
People.Add(p);
p := TPerson.Create;
p.FirstName := 'John';
p.LastName := 'Doe';
p.DOB := EncodeDate(1879, 10, 2);
p.Married := False;
People.Add(p);
p := TPerson.Create;
p.FirstName := 'Jane';
p.LastName := 'Doe';
p.DOB := EncodeDate(1883, 1, 5);
p.Married := True;
People.Add(p);
2014-04-01 00:02:31 +02:00
{$ENDREGION}
Render<TPerson>(People);
// or if you want to be more opne to future extension
// RenderListAsProperty<TPerson>('people', People);
end;
2016-11-27 23:17:20 +01:00
procedure TRenderSampleController.GetPersonJSON;
2013-11-11 19:32:20 +01:00
var
2014-05-30 11:29:58 +02:00
p: TJSONObject;
2013-11-11 19:32:20 +01:00
begin
2014-05-30 11:29:58 +02:00
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);
2013-11-11 19:32:20 +01:00
end;
2016-11-27 23:17:20 +01:00
procedure TRenderSampleController.GetPersonPhoto;
2014-04-01 00:02:31 +02:00
begin
2014-04-01 19:36:05 +02:00
// ContentType := 'image/jpeg';
2016-11-27 23:17:20 +01:00
SendFile('..\..\_\customer.png');
2014-04-01 00:02:31 +02:00
end;
2015-06-29 14:36:54 +02:00
procedure TRenderSampleController.GetPersonPhotoAsStream(CTX: TWebContext);
var
LPhoto: TFileStream;
begin
2016-11-27 23:17:20 +01:00
LPhoto := TFileStream.Create('..\..\_\customer.png',
2016-02-26 19:21:57 +01:00
fmOpenRead or fmShareDenyWrite);
2015-06-29 14:36:54 +02:00
ContentType := 'image/png'; // you can also use MVCProduces attribute
// LPhoto is a plain TStream descendant, so it can be rendered as usual
Render(LPhoto, True);
end;
2013-11-10 01:03:53 +01:00
end.