2016-02-14 18:15:56 +01:00
|
|
|
unit WebSiteControllerU;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2017-05-25 16:57:49 +02:00
|
|
|
MVCFramework, MVCFramework.Commons, System.Diagnostics, System.JSON,
|
|
|
|
MVCFramework.Serializer.Commons;
|
2016-02-14 18:15:56 +01:00
|
|
|
|
|
|
|
type
|
|
|
|
|
2017-05-25 16:57:49 +02:00
|
|
|
[MVCNameCase(ncLowerCase)]
|
|
|
|
TSpeedValue = class
|
|
|
|
private
|
|
|
|
FValue: string;
|
|
|
|
procedure SetValue(const Value: string);
|
|
|
|
public
|
|
|
|
property Value: string read FValue write SetValue;
|
|
|
|
constructor Create(aValue: string);
|
|
|
|
end;
|
|
|
|
|
2016-02-14 18:15:56 +01:00
|
|
|
[MVCPath('/')]
|
|
|
|
TWebSiteController = class(TMVCController)
|
|
|
|
private
|
|
|
|
FStopWatch: TStopwatch;
|
2017-05-25 16:57:49 +02:00
|
|
|
function GetSpeed: TSpeedValue;
|
2016-02-14 18:15:56 +01:00
|
|
|
protected
|
|
|
|
procedure OnBeforeAction(Context: TWebContext; const AActionNAme: string;
|
|
|
|
var Handled: Boolean); override;
|
|
|
|
public
|
|
|
|
[MVCPath('/people')]
|
|
|
|
[MVCHTTPMethods([httpGET])]
|
2017-05-08 17:26:19 +02:00
|
|
|
procedure PeopleList;
|
2016-11-27 23:17:20 +01:00
|
|
|
|
|
|
|
[MVCPath('/people/($guid)')]
|
|
|
|
[MVCHTTPMethods([httpDELETE])]
|
|
|
|
procedure DeletePerson(const guid: string);
|
|
|
|
|
2016-02-14 18:15:56 +01:00
|
|
|
[MVCPath('/people')]
|
|
|
|
[MVCHTTPMethods([httpPOST])]
|
|
|
|
[MVCConsumes('application/x-www-form-urlencoded')]
|
2017-05-08 17:26:19 +02:00
|
|
|
procedure SavePerson;
|
2016-02-14 18:15:56 +01:00
|
|
|
[MVCPath('/newperson')]
|
|
|
|
[MVCHTTPMethods([httpGET])]
|
2017-05-08 17:26:19 +02:00
|
|
|
procedure NewPerson;
|
2016-02-14 18:15:56 +01:00
|
|
|
[MVCPath('/')]
|
|
|
|
[MVCHTTPMethods([httpGET])]
|
2017-05-08 17:26:19 +02:00
|
|
|
procedure Index;
|
2016-02-14 18:15:56 +01:00
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
{ TWebSiteController }
|
|
|
|
|
2017-01-18 21:53:53 +01:00
|
|
|
uses DAL, System.SysUtils, Web.HTTPApp;
|
2016-02-14 18:15:56 +01:00
|
|
|
|
2016-11-27 23:17:20 +01:00
|
|
|
procedure TWebSiteController.DeletePerson(const guid: string);
|
|
|
|
var
|
|
|
|
LDAL: IPeopleDAL;
|
|
|
|
begin
|
|
|
|
LDAL := TServicesFactory.GetPeopleDAL;
|
|
|
|
LDAL.DeleteByGUID(guid);
|
|
|
|
end;
|
|
|
|
|
2017-05-25 16:57:49 +02:00
|
|
|
function TWebSiteController.GetSpeed: TSpeedValue;
|
2016-02-14 18:15:56 +01:00
|
|
|
begin
|
2017-05-25 16:57:49 +02:00
|
|
|
Result := TSpeedValue.Create(FStopWatch.Elapsed.TotalMilliseconds.ToString);
|
2016-02-14 18:15:56 +01:00
|
|
|
end;
|
|
|
|
|
2017-05-08 17:26:19 +02:00
|
|
|
procedure TWebSiteController.Index;
|
2016-02-14 18:15:56 +01:00
|
|
|
begin
|
|
|
|
Redirect('/people');
|
|
|
|
end;
|
|
|
|
|
2017-05-08 17:26:19 +02:00
|
|
|
procedure TWebSiteController.NewPerson;
|
2016-02-14 18:15:56 +01:00
|
|
|
begin
|
2017-05-25 16:57:49 +02:00
|
|
|
PushObjectToView('speed', GetSpeed);
|
2016-02-14 18:15:56 +01:00
|
|
|
LoadView(['header', 'editperson', 'footer']);
|
2016-11-27 23:17:20 +01:00
|
|
|
RenderResponseStream;
|
2016-02-14 18:15:56 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TWebSiteController.OnBeforeAction(Context: TWebContext;
|
|
|
|
const AActionNAme: string; var Handled: Boolean);
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
ContentType := 'text/html';
|
|
|
|
Handled := False;
|
|
|
|
FStopWatch := TStopwatch.StartNew;
|
|
|
|
end;
|
|
|
|
|
2017-05-08 17:26:19 +02:00
|
|
|
procedure TWebSiteController.PeopleList;
|
2016-02-14 18:15:56 +01:00
|
|
|
var
|
|
|
|
LDAL: IPeopleDAL;
|
2016-04-24 19:08:21 +02:00
|
|
|
lCookie: TCookie;
|
2016-02-14 18:15:56 +01:00
|
|
|
begin
|
|
|
|
LDAL := TServicesFactory.GetPeopleDAL;
|
2017-05-25 16:57:49 +02:00
|
|
|
PushObjectToView('people', LDAL.GetPeople);
|
|
|
|
PushObjectToView('speed', GetSpeed);
|
2016-02-14 18:15:56 +01:00
|
|
|
LoadView(['header', 'people_list', 'footer']);
|
2016-04-24 19:08:21 +02:00
|
|
|
|
|
|
|
// send a cookie with the server datetime at the page rendering
|
2017-05-08 17:26:19 +02:00
|
|
|
lCookie := Context.Response.Cookies.Add;
|
2016-04-24 19:08:21 +02:00
|
|
|
lCookie.Name := 'lastresponse';
|
|
|
|
lCookie.Value := DateTimeToStr(now);
|
|
|
|
lCookie.Expires := 0; // session cookie
|
|
|
|
// END cookie sending
|
|
|
|
|
2016-11-27 23:17:20 +01:00
|
|
|
RenderResponseStream; // rember to call render!!!
|
2016-02-14 18:15:56 +01:00
|
|
|
end;
|
|
|
|
|
2017-05-08 17:26:19 +02:00
|
|
|
procedure TWebSiteController.SavePerson;
|
2016-02-14 18:15:56 +01:00
|
|
|
var
|
2017-05-08 17:26:19 +02:00
|
|
|
lFirstName: string;
|
|
|
|
lLastName: string;
|
2017-05-25 16:57:49 +02:00
|
|
|
lAge: string;
|
2017-05-08 17:26:19 +02:00
|
|
|
lPeopleDAL: IPeopleDAL;
|
|
|
|
lItems: TArray<string>;
|
2016-02-14 18:15:56 +01:00
|
|
|
begin
|
2017-05-08 17:26:19 +02:00
|
|
|
lItems := Context.Request.ParamsMulti['items'];
|
|
|
|
lFirstName := Context.Request.Params['first_name'].Trim;
|
|
|
|
lLastName := Context.Request.Params['last_name'].Trim;
|
|
|
|
lAge := Context.Request.Params['age'];
|
2016-02-14 18:15:56 +01:00
|
|
|
|
|
|
|
if LFirstName.IsEmpty or LLastName.IsEmpty or LAge.IsEmpty then
|
|
|
|
begin
|
2016-04-24 19:08:21 +02:00
|
|
|
{ TODO -oDaniele -cGeneral : Show how to properly render an exception }
|
2016-02-14 18:15:56 +01:00
|
|
|
raise EMVCException.Create('Invalid data',
|
|
|
|
'First name, last name and age are not optional', 0);
|
|
|
|
end;
|
|
|
|
|
|
|
|
LPeopleDAL := TServicesFactory.GetPeopleDAL;
|
2017-05-08 17:26:19 +02:00
|
|
|
LPeopleDAL.AddPerson(LFirstName, LLastName, LAge.ToInteger(), lItems);
|
2016-04-24 19:08:21 +02:00
|
|
|
|
2016-02-14 18:15:56 +01:00
|
|
|
Redirect('/people');
|
|
|
|
end;
|
|
|
|
|
2017-05-25 16:57:49 +02:00
|
|
|
{ TSpeedValue }
|
|
|
|
|
|
|
|
constructor TSpeedValue.Create(aValue: string);
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
FValue := aValue;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TSpeedValue.SetValue(const Value: string);
|
|
|
|
begin
|
|
|
|
FValue := Value;
|
|
|
|
end;
|
|
|
|
|
2016-02-14 18:15:56 +01:00
|
|
|
end.
|