Improved "renders" sample - added sensible defaults for MVCResponseBuilder

This commit is contained in:
Daniele Teti 2023-08-30 17:38:14 +02:00
parent f21bb1144b
commit 0775315682
3 changed files with 86 additions and 13 deletions

View File

@ -20,20 +20,11 @@ var
GPeople, GPeopleSmall: TObjectList<TPerson>;
function GetInterfacedPeopleList: TList<IPerson>;
var
lPerson: IPerson;
begin
Result := TList<IPerson>.Create;
lPerson := TInterfacedPerson.Create;
lPerson.Name := 'Daniele Teti';
lPerson.Age := 40;
lPerson.DOB := EncodeDate(1979, 11, 4);
Result.Add(lPerson);
lPerson := TInterfacedPerson.Create;
lPerson.Name := 'Peter Parker';
lPerson.Age := 35;
lPerson.DOB := EncodeDate(1984, 11, 4);
Result.Add(lPerson);
Result := TList<IPerson>.Create([
TInterfacedPerson.Create('Daniele Teti', 40, EncodeDate(1979, 11, 4)),
TInterfacedPerson.Create('Peter Parker', 35, EncodeDate(1984, 11, 4))
]);
end;
procedure PopulateList;

View File

@ -45,6 +45,11 @@ type
var AHandled: Boolean); override;
public
// Result BASED
[MVCHTTPMethod([httpGET])]
[MVCPath('/func/people/1')]
[MVCProduces('application/json')]
function GetPerson_AsFunction: TPerson;
[MVCHTTPMethod([httpGET])]
[MVCPath('/func/people')]
[MVCProduces('application/json')]
@ -54,6 +59,22 @@ type
[MVCPath('/func/customers/simple')]
function GetCustomers_AsDataSet_AsFunction: TDataSet;
[MVCHTTPMethod([httpGET])]
[MVCPath('/func/customers/($ID)')]
[MVCProduces('text/plain')]
function GetPerson_AsText_AsFunction(const ID: Integer): String;
// this action is polymorphic
[MVCHTTPMethod([httpGET])]
[MVCPath('/func/skilledpeople')]
[MVCProduces('application/json')]
function GetProgrammersAndPhilosophersAsObjectList_AsFunction: TObjectList<TPerson>;
[MVCHTTPMethod([httpGET])]
[MVCPath('/func/skilledpeople/withmvcresponse')]
[MVCProduces('application/json')]
function GetProgrammersAndPhilosophersAsObjectList_withmvcresponse_AsFunction: IMVCResponse;
// Render BASED
[MVCHTTPMethod([httpGET])]
@ -780,6 +801,11 @@ begin
Render(TNullablesTest.Create);
end;
function TRenderSampleController.GetPerson_AsFunction: TPerson;
begin
Result := TPerson.GetNew('Daniele','Teti', EncodeDate(1979,11,4), True);
end;
procedure TRenderSampleController.GetPerson_AsHTML;
begin
ResponseStream.Append('<html><body><ul>').Append('<li>FirstName: Daniele</li>')
@ -823,6 +849,23 @@ begin
RenderResponseStream;
end;
function TRenderSampleController.GetPerson_AsText_AsFunction(
const ID: Integer): String;
begin
var lSBldr := TStringBuilder.Create;
try
lSBldr
.AppendLine('ID : ' + ID.ToString)
.AppendLine('FirstName : Daniele')
.AppendLine('LastName : Teti')
.AppendLine('DOB : ' + DateToStr(EncodeDate(1979, 5, 2)))
.AppendLine('Married : yes');
Result := lSBldr.ToString;
finally
lSBldr.Free;
end;
end;
procedure TRenderSampleController.GetProgrammersAndPhilosophersAsObjectList;
var
List: TObjectList<TPerson>;
@ -851,6 +894,44 @@ begin
Render<TPerson>(List);
end;
function TRenderSampleController.GetProgrammersAndPhilosophersAsObjectList_AsFunction: TObjectList<TPerson>;
var
List: TObjectList<TPerson>;
p: TProgrammer;
ph: TPhilosopher;
begin
List := TObjectList<TPerson>.Create(True);
try
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);
except
List.Free;
raise;
end;
Result := List;
end;
function TRenderSampleController.GetProgrammersAndPhilosophersAsObjectList_withmvcresponse_AsFunction: IMVCResponse;
begin
Result := MVCResponseBuilder.Data(GetPeople_AsObjectList_AsFunction).Build;
end;
procedure TRenderSampleController.GetSimpleArrays;
begin
Render(TArrayTest.Create);

View File

@ -4659,6 +4659,7 @@ constructor TMVCResponseBuilder.Create;
begin
inherited;
fBuilt := False;
fStatusCode := HTTP_STATUS.OK;
end;
function TMVCResponseBuilder.Data(const Data: TObject): IMVCResponseBuilder;