delphimvcframework/samples/serversideviewsprimer/WebSiteControllerU.pas

125 lines
3.1 KiB
ObjectPascal
Raw Normal View History

2016-02-14 18:15:56 +01:00
unit WebSiteControllerU;
interface
uses
MVCFramework, System.Diagnostics, System.JSON;
type
[MVCPath('/')]
TWebSiteController = class(TMVCController)
private
FStopWatch: TStopwatch;
function GetSpeed: TJSONString;
protected
procedure OnBeforeAction(Context: TWebContext; const AActionNAme: string;
var Handled: Boolean); override;
public
[MVCPath('/people')]
[MVCHTTPMethods([httpGET])]
procedure PeopleList(CTX: TWebContext);
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')]
procedure SavePerson(CTX: TWebContext);
[MVCPath('/newperson')]
[MVCHTTPMethods([httpGET])]
procedure NewPerson(CTX: TWebContext);
[MVCPath('/')]
[MVCHTTPMethods([httpGET])]
procedure Index(CTX: TWebContext);
end;
implementation
{ TWebSiteController }
uses DAL, System.SysUtils, MVCFramework.Commons, 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;
2016-02-14 18:15:56 +01:00
function TWebSiteController.GetSpeed: TJSONString;
begin
Result := TJSONString.Create(FStopWatch.Elapsed.TotalMilliseconds.ToString);
end;
procedure TWebSiteController.Index(CTX: TWebContext);
begin
Redirect('/people');
end;
procedure TWebSiteController.NewPerson(CTX: TWebContext);
begin
PushJSONToView('speed', GetSpeed);
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;
procedure TWebSiteController.PeopleList(CTX: TWebContext);
var
LDAL: IPeopleDAL;
lCookie: TCookie;
2016-02-14 18:15:56 +01:00
begin
LDAL := TServicesFactory.GetPeopleDAL;
PushJSONToView('people', LDAL.GetPeople);
PushJSONToView('speed', GetSpeed);
LoadView(['header', 'people_list', 'footer']);
// send a cookie with the server datetime at the page rendering
lCookie := CTX.Response.Cookies.Add;
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;
procedure TWebSiteController.SavePerson(CTX: TWebContext);
var
LFirstName: string;
LLastName: string;
LAge: String;
LPeopleDAL: IPeopleDAL;
begin
LFirstName := CTX.Request.Params['first_name'].Trim;
LLastName := CTX.Request.Params['last_name'].Trim;
LAge := CTX.Request.Params['age'];
if LFirstName.IsEmpty or LLastName.IsEmpty or LAge.IsEmpty then
begin
{ 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;
LPeopleDAL.AddPerson(LFirstName, LLastName, LAge.ToInteger());
2016-02-14 18:15:56 +01:00
Redirect('/people');
end;
end.