2019-01-08 12:48:27 +01:00
|
|
|
unit WebSiteControllerU;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2023-10-06 12:30:40 +02:00
|
|
|
MVCFramework, System.Diagnostics, JsonDataObjects, MVCFramework.Commons, DAL,
|
|
|
|
System.Generics.Collections;
|
2019-01-08 12:48:27 +01:00
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
[MVCPath('/')]
|
|
|
|
TWebSiteController = class(TMVCController)
|
|
|
|
protected
|
|
|
|
procedure OnBeforeAction(Context: TWebContext; const AActionNAme: string;
|
|
|
|
var Handled: Boolean); override;
|
2024-01-23 17:20:15 +01:00
|
|
|
function GeneratePeopleListAsCSV: String;
|
2019-01-08 12:48:27 +01:00
|
|
|
public
|
|
|
|
[MVCPath('/people')]
|
|
|
|
[MVCHTTPMethods([httpGET])]
|
|
|
|
[MVCProduces(TMVCMediaType.TEXT_HTML)]
|
2024-01-23 17:20:15 +01:00
|
|
|
function PeopleList: String;
|
2019-01-08 12:48:27 +01:00
|
|
|
|
|
|
|
[MVCPath('/people')]
|
|
|
|
[MVCHTTPMethods([httpGET])]
|
|
|
|
[MVCProduces(TMVCMediaType.TEXT_CSV)]
|
|
|
|
// RESTful API, requires ACCEPT=text/csv
|
2024-01-23 17:20:15 +01:00
|
|
|
function ExportPeopleListAsCSV_API: String;
|
2019-01-08 12:48:27 +01:00
|
|
|
|
|
|
|
[MVCPath('/people/formats/csv')]
|
|
|
|
[MVCHTTPMethods([httpGET])]
|
|
|
|
// Route usable by the browser, doesn't requires ACCEPT=text/csv
|
2024-01-23 17:20:15 +01:00
|
|
|
function ExportPeopleListAsCSV: String;
|
2019-01-08 12:48:27 +01:00
|
|
|
|
|
|
|
[MVCPath('/people')]
|
|
|
|
[MVCHTTPMethods([httpPOST])]
|
|
|
|
[MVCConsumes(TMVCMediaType.APPLICATION_FORM_URLENCODED)]
|
2023-10-06 12:30:40 +02:00
|
|
|
procedure SavePerson(const [MVCFromBody] Person: TPerson);
|
2019-01-08 12:48:27 +01:00
|
|
|
|
|
|
|
[MVCPath('/deleteperson')]
|
|
|
|
[MVCHTTPMethods([httpPOST])]
|
|
|
|
[MVCConsumes(TMVCMediaType.APPLICATION_FORM_URLENCODED)]
|
2024-07-08 21:07:13 +02:00
|
|
|
procedure DeletePerson([MVCFromContentField('guid')] const GUID: String);
|
2019-01-08 12:48:27 +01:00
|
|
|
|
|
|
|
[MVCPath('/new')]
|
|
|
|
[MVCHTTPMethods([httpGET])]
|
|
|
|
[MVCProduces(TMVCMediaType.TEXT_HTML)]
|
2023-10-06 12:30:40 +02:00
|
|
|
function NewPerson: String;
|
2019-01-08 12:48:27 +01:00
|
|
|
|
|
|
|
[MVCPath('/edit/($guid)')]
|
|
|
|
[MVCHTTPMethods([httpGET])]
|
|
|
|
[MVCProduces(TMVCMediaType.TEXT_HTML)]
|
2023-10-06 12:30:40 +02:00
|
|
|
function EditPerson(guid: string): String;
|
2019-01-08 12:48:27 +01:00
|
|
|
|
|
|
|
[MVCPath('/')]
|
|
|
|
[MVCHTTPMethods([httpGET])]
|
|
|
|
[MVCProduces(TMVCMediaType.TEXT_HTML)]
|
|
|
|
procedure Index;
|
|
|
|
|
2020-11-05 17:14:39 +01:00
|
|
|
[MVCPath('/showcase')]
|
|
|
|
[MVCHTTPMethods([httpGET])]
|
|
|
|
[MVCProduces(TMVCMediaType.TEXT_HTML)]
|
2024-01-23 17:20:15 +01:00
|
|
|
function MustacheTemplateShowCase: String;
|
2024-03-08 16:17:30 +01:00
|
|
|
|
|
|
|
[MVCPath('/loadviewtest')]
|
|
|
|
[MVCHTTPMethods([httpGET])]
|
|
|
|
[MVCProduces(TMVCMediaType.TEXT_PLAIN)]
|
|
|
|
procedure LoadViewTest;
|
2019-01-08 12:48:27 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
{ TWebSiteController }
|
|
|
|
|
2024-03-08 16:17:30 +01:00
|
|
|
uses System.SysUtils, Web.HTTPApp, FireDAC.Stan.Intf, FireDAC.Stan.Option,
|
|
|
|
FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf, FireDAC.DApt.Intf, Data.DB,
|
|
|
|
FireDAC.Comp.DataSet, FireDAC.Comp.Client;
|
2019-01-08 12:48:27 +01:00
|
|
|
|
2024-07-08 21:07:13 +02:00
|
|
|
procedure TWebSiteController.DeletePerson(const GUID: String);
|
2019-01-08 12:48:27 +01:00
|
|
|
var
|
|
|
|
LDAL: IPeopleDAL;
|
|
|
|
begin
|
|
|
|
LDAL := TServicesFactory.GetPeopleDAL;
|
2024-07-08 21:07:13 +02:00
|
|
|
LDAL.DeleteByGUID(GUID);
|
2019-01-08 12:48:27 +01:00
|
|
|
Redirect('/people');
|
|
|
|
end;
|
|
|
|
|
2023-10-06 12:30:40 +02:00
|
|
|
function TWebSiteController.EditPerson(guid: string): String;
|
2019-01-08 12:48:27 +01:00
|
|
|
var
|
|
|
|
LDAL: IPeopleDAL;
|
|
|
|
lPerson: TPerson;
|
2023-10-06 12:30:40 +02:00
|
|
|
lDevices: TArray<String>;
|
|
|
|
lJDevices: TJSONArray;
|
|
|
|
lItem: string;
|
|
|
|
lIdx: Integer;
|
|
|
|
lJObj: TJsonObject;
|
2019-01-08 12:48:27 +01:00
|
|
|
begin
|
|
|
|
LDAL := TServicesFactory.GetPeopleDAL;
|
|
|
|
lPerson := LDAL.GetPersonByGUID(guid);
|
|
|
|
try
|
|
|
|
lDevices := LDAL.GetDevicesList;
|
2023-10-06 12:30:40 +02:00
|
|
|
ViewData['person'] := lPerson;
|
|
|
|
lJObj := TJsonObject.Create;
|
2019-01-08 12:48:27 +01:00
|
|
|
try
|
2023-10-06 12:30:40 +02:00
|
|
|
lJDevices := lJObj.A['devices'];
|
2019-01-08 12:48:27 +01:00
|
|
|
for lItem in lDevices do
|
|
|
|
begin
|
2023-10-06 12:30:40 +02:00
|
|
|
var lJItm := lJDevices.AddObject;
|
|
|
|
lJItm.S['name'] := lItem;
|
|
|
|
lJItm.B['selected'] := TArray.BinarySearch<String>(lDevices, lItem, lIdx);
|
2019-01-08 12:48:27 +01:00
|
|
|
end;
|
2024-01-23 17:20:15 +01:00
|
|
|
Result := Page(['editperson'], lJObj);
|
2019-01-08 12:48:27 +01:00
|
|
|
finally
|
2023-10-06 12:30:40 +02:00
|
|
|
lJObj.Free;
|
2019-01-08 12:48:27 +01:00
|
|
|
end;
|
|
|
|
finally
|
|
|
|
lPerson.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2024-01-23 17:20:15 +01:00
|
|
|
function TWebSiteController.ExportPeopleListAsCSV: String;
|
2019-01-08 12:48:27 +01:00
|
|
|
begin
|
2024-01-23 17:20:15 +01:00
|
|
|
Result := GeneratePeopleListAsCSV;
|
2019-01-08 12:48:27 +01:00
|
|
|
// define the correct behaviour to download the csv inside the browser
|
|
|
|
ContentType := TMVCMediaType.TEXT_CSV;
|
|
|
|
Context.Response.CustomHeaders.Values['Content-Disposition'] :=
|
|
|
|
'attachment; filename=people.csv';
|
|
|
|
end;
|
|
|
|
|
2024-01-23 17:20:15 +01:00
|
|
|
function TWebSiteController.ExportPeopleListAsCSV_API: String;
|
2019-01-08 12:48:27 +01:00
|
|
|
begin
|
2024-01-23 17:20:15 +01:00
|
|
|
Result := GeneratePeopleListAsCSV;
|
2019-01-08 12:48:27 +01:00
|
|
|
end;
|
|
|
|
|
2024-01-23 17:20:15 +01:00
|
|
|
function TWebSiteController.GeneratePeopleListAsCSV: String;
|
2019-01-08 12:48:27 +01:00
|
|
|
var
|
|
|
|
LDAL: IPeopleDAL;
|
|
|
|
lPeople: TPeople;
|
|
|
|
begin
|
|
|
|
LDAL := TServicesFactory.GetPeopleDAL;
|
|
|
|
lPeople := LDAL.GetPeople;
|
|
|
|
try
|
|
|
|
ViewData['people'] := lPeople;
|
2024-01-23 17:20:15 +01:00
|
|
|
Result := PageFragment(['people_header.csv', 'people_list.csv']);
|
2019-01-08 12:48:27 +01:00
|
|
|
finally
|
|
|
|
lPeople.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TWebSiteController.Index;
|
|
|
|
begin
|
|
|
|
Redirect('/people');
|
|
|
|
end;
|
|
|
|
|
2024-03-08 16:17:30 +01:00
|
|
|
procedure TWebSiteController.LoadViewTest;
|
|
|
|
var
|
|
|
|
lDS: TFDMemTable;
|
|
|
|
begin
|
|
|
|
lDS := TFDMemTable.Create(nil);
|
|
|
|
try
|
|
|
|
lDS.FieldDefs.Add('id', ftInteger);
|
|
|
|
lDS.FieldDefs.Add('first_name', ftString, 40);
|
|
|
|
lDS.FieldDefs.Add('last_name', ftString, 40);
|
|
|
|
lDS.FieldDefs.Add('age', ftInteger);
|
|
|
|
lDS.CreateDataSet;
|
|
|
|
lDS.AppendRecord([1,'Daniele','Teti',44]);
|
|
|
|
lDS.AppendRecord([2,'Bruce','Banner',54]);
|
|
|
|
lDS.AppendRecord([3,'Peter','Parker',34]);
|
|
|
|
lDS.First;
|
|
|
|
|
|
|
|
ViewData['people'] := lDS;
|
|
|
|
LoadView(['people_list_test','people_list_test']);
|
|
|
|
RenderResponseStream;
|
|
|
|
finally
|
|
|
|
lDS.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2024-01-23 17:20:15 +01:00
|
|
|
function TWebSiteController.MustacheTemplateShowCase: String;
|
2020-11-05 17:14:39 +01:00
|
|
|
var
|
|
|
|
LDAL: IPeopleDAL;
|
|
|
|
lPeople, lPeople2: TPeople;
|
|
|
|
lMyObj: TMyObj;
|
|
|
|
begin
|
|
|
|
LDAL := TServicesFactory.GetPeopleDAL;
|
|
|
|
lPeople := LDAL.GetPeople;
|
|
|
|
try
|
|
|
|
lPeople2 := TPeople.Create;
|
|
|
|
try
|
|
|
|
lMyObj := TMyObj.Create;
|
|
|
|
try
|
|
|
|
lMyObj.RawHTML := '<h1>This is</h1>Raw<br><span>HTML</span>';
|
|
|
|
ViewData['people'] := lPeople;
|
|
|
|
ViewData['people2'] := lPeople2;
|
|
|
|
ViewData['myobj'] := lMyObj;
|
2024-01-23 17:20:15 +01:00
|
|
|
Result := Page(['showcase'], False);
|
2020-11-05 17:14:39 +01:00
|
|
|
finally
|
|
|
|
lMyObj.Free;
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
lPeople2.Free;
|
|
|
|
end;
|
|
|
|
finally
|
|
|
|
lPeople.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2023-10-06 12:30:40 +02:00
|
|
|
function TWebSiteController.NewPerson: String;
|
2019-01-08 12:48:27 +01:00
|
|
|
var
|
2020-04-13 16:22:15 +02:00
|
|
|
LDAL: IPeopleDAL;
|
2023-10-06 12:30:40 +02:00
|
|
|
lDevices: TArray<String>;
|
|
|
|
lJObj: TJsonObject;
|
2019-01-08 12:48:27 +01:00
|
|
|
begin
|
2020-04-13 16:22:15 +02:00
|
|
|
LDAL := TServicesFactory.GetPeopleDAL;
|
2019-01-08 12:48:27 +01:00
|
|
|
lDevices := LDAL.GetDevicesList;
|
2023-10-06 12:30:40 +02:00
|
|
|
lJObj := TJsonObject.Create;
|
2019-01-08 12:48:27 +01:00
|
|
|
try
|
2023-10-06 12:30:40 +02:00
|
|
|
var lJDevices := lJObj.A['devices'];
|
|
|
|
for var lItem in lDevices do
|
|
|
|
begin
|
|
|
|
var lJItm := lJDevices.AddObject;
|
|
|
|
lJItm.S['name'] := lItem;
|
|
|
|
lJItm.B['selected'] := False;
|
|
|
|
end;
|
2024-01-23 17:20:15 +01:00
|
|
|
Result := Page(['editperson'], lJObj);
|
2019-01-08 12:48:27 +01:00
|
|
|
finally
|
2023-10-06 12:30:40 +02:00
|
|
|
lJObj.Free;
|
2019-01-08 12:48:27 +01:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TWebSiteController.OnBeforeAction(Context: TWebContext;
|
|
|
|
const AActionNAme: string; var Handled: Boolean);
|
|
|
|
begin
|
|
|
|
inherited;
|
2024-01-23 17:20:15 +01:00
|
|
|
SetPagesCommonHeaders(['header']);
|
|
|
|
SetPagesCommonFooters(['footer']);
|
2024-03-08 16:17:30 +01:00
|
|
|
if not AActionNAme.ToLower.Contains('test') then ContentType := 'text/html';
|
2019-01-08 12:48:27 +01:00
|
|
|
Handled := False;
|
|
|
|
end;
|
|
|
|
|
2024-01-23 17:20:15 +01:00
|
|
|
function TWebSiteController.PeopleList: String;
|
2019-01-08 12:48:27 +01:00
|
|
|
var
|
|
|
|
LDAL: IPeopleDAL;
|
|
|
|
lPeople: TPeople;
|
|
|
|
begin
|
|
|
|
LDAL := TServicesFactory.GetPeopleDAL;
|
|
|
|
lPeople := LDAL.GetPeople;
|
|
|
|
try
|
|
|
|
ViewData['people'] := lPeople;
|
2024-01-23 17:20:15 +01:00
|
|
|
Result := Page(['people_list']);
|
2019-01-08 12:48:27 +01:00
|
|
|
finally
|
|
|
|
lPeople.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2023-10-06 12:30:40 +02:00
|
|
|
procedure TWebSiteController.SavePerson(const [MVCFromBody] Person: TPerson);
|
2019-01-08 12:48:27 +01:00
|
|
|
var
|
|
|
|
LPeopleDAL: IPeopleDAL;
|
|
|
|
begin
|
2023-10-06 12:30:40 +02:00
|
|
|
if Person.FirstName.IsEmpty or Person.LastName.IsEmpty or (Person.Age <= 0) then
|
2019-01-08 12:48:27 +01:00
|
|
|
begin
|
|
|
|
{ TODO -oDaniele -cGeneral : Show how to properly render an exception }
|
|
|
|
raise EMVCException.Create('Invalid data',
|
|
|
|
'First name, last name and age are not optional', 0);
|
|
|
|
end;
|
|
|
|
|
|
|
|
LPeopleDAL := TServicesFactory.GetPeopleDAL;
|
2023-10-06 12:30:40 +02:00
|
|
|
LPeopleDAL.AddPerson(Person.FirstName, Person.LastName,
|
|
|
|
Person.Age, Person.Devices);
|
2019-01-08 12:48:27 +01:00
|
|
|
Redirect('/people');
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|