From 1dcfc6b90378d2bea7fb86053a56b72406b74cc5 Mon Sep 17 00:00:00 2001 From: Daniele Teti Date: Sat, 25 Nov 2017 15:36:16 +0100 Subject: [PATCH] Merged 2 SSV samples into one --- samples/serversideviews/DAL.pas | 70 ++- samples/serversideviews/ServerSideViewsEx.dpr | 57 -- .../serversideviews/ServerSideViewsEx.dproj | 502 ------------------ .../serversideviews/WebSiteControllerU.pas | 99 ++-- .../bin/templates/editperson.mustache | 14 + .../bin/templates/footer.mustache | 2 +- 6 files changed, 133 insertions(+), 611 deletions(-) delete mode 100644 samples/serversideviews/ServerSideViewsEx.dpr delete mode 100644 samples/serversideviews/ServerSideViewsEx.dproj diff --git a/samples/serversideviews/DAL.pas b/samples/serversideviews/DAL.pas index 94fcb31e..c551655b 100644 --- a/samples/serversideviews/DAL.pas +++ b/samples/serversideviews/DAL.pas @@ -36,12 +36,29 @@ type TPeople = class(TObjectList) end; + TDevice = class + private + fDeviceName: string; + fSelected: Boolean; + public + property DeviceName: string read fDeviceName write fDeviceName; + property Selected: Boolean read fSelected write fSelected; + constructor Create(aDeviceName: string; aSelected: Boolean); + end; + + TDeviceList = class(TObjectList) + public + function Contains(const aDeviceName: string): Boolean; + function IndexOf(const aDeviceName: string): Integer; + end; + IPeopleDAL = interface ['{3E534A3E-EAEB-44ED-B74E-EFBBAAAE11B4}'] function GetPeople: TPeople; procedure AddPerson(FirstName, LastName: string; Age: Integer; Items: TArray); procedure DeleteByGUID(GUID: string); function GetPersonByGUID(GUID: string): TPerson; + function GetDevicesList: TDeviceList; end; TPeopleDAL = class(TInterfacedObject, IPeopleDAL) @@ -52,6 +69,7 @@ type procedure AddPerson(FirstName, LastName: string; Age: Integer; Items: TArray); procedure DeleteByGUID(GUID: string); function GetPersonByGUID(GUID: string): TPerson; + function GetDevicesList: TDeviceList; end; TServicesFactory = class sealed @@ -128,6 +146,15 @@ begin end; end; +function TPeopleDAL.GetDevicesList: TDeviceList; +begin + Result := TDeviceList.Create(true); + Result.Add(TDevice.Create('smartphone', false)); + Result.Add(TDevice.Create('dumbphone', false)); + Result.Add(TDevice.Create('laptop', false)); + Result.Add(TDevice.Create('desktop', false)); +end; + function TPeopleDAL.GetPeople: TPeople; var LData: string; @@ -153,14 +180,17 @@ var begin Result := nil; lPeople := GetPeople; - for lPerson in lPeople do - begin - if lPerson.GUID = GUID then + try + for lPerson in lPeople do begin - Result := lPeople.Extract(lPerson); - Break; + if lPerson.GUID = GUID then + begin + Result := lPeople.Extract(lPerson); + Break; + end; end; - + finally + lPeople.Free; end; end; @@ -191,6 +221,34 @@ begin FLastName := Value; end; +{ TDevice } + +constructor TDevice.Create(aDeviceName: string; aSelected: Boolean); +begin + inherited Create; + fDeviceName := aDeviceName; + fSelected := aSelected; +end; + +{ TDeviceList } + +function TDeviceList.Contains(const aDeviceName: string): Boolean; +begin + Result := IndexOf(aDeviceName) > -1; +end; + +function TDeviceList.IndexOf(const aDeviceName: string): Integer; +var + I: Integer; +begin + Result := -1; + for I := 0 to Self.Count - 1 do + begin + if SameText(Self[i].DeviceName, aDeviceName) then + Exit(i); + end; +end; + initialization _CS := TCriticalSection.Create; diff --git a/samples/serversideviews/ServerSideViewsEx.dpr b/samples/serversideviews/ServerSideViewsEx.dpr deleted file mode 100644 index 383a7364..00000000 --- a/samples/serversideviews/ServerSideViewsEx.dpr +++ /dev/null @@ -1,57 +0,0 @@ -program ServerSideViewsEx; - -{$APPTYPE CONSOLE} - - -uses - System.SysUtils, - {$IFDEF MSWINDOWS} - Winapi.ShellAPI, - Winapi.Windows, - {$ENDIF } - IdHTTPWebBrokerBridge, - Web.WebReq, - Web.WebBroker, - WebModuleU in 'WebModuleU.pas' {WebModule1: TWebModule}, - WebSiteControllerU in 'WebSiteControllerU.pas', - DAL in 'DAL.pas', - MyDataModuleU in '..\renders\MyDataModuleU.pas' {MyDataModule: TDataModule}; - -{$R *.res} - - -procedure RunServer(APort: Integer); -var - LServer: TIdHTTPWebBrokerBridge; -begin - ReportMemoryLeaksOnShutdown := True; - Writeln(Format('Starting HTTP Server on port %d', [APort])); - LServer := TIdHTTPWebBrokerBridge.Create(nil); - try - LServer.DefaultPort := APort; - LServer.Active := True; - Writeln('Press RETURN to stop the server'); - - {$IFDEF MSWINDOWS} - - ShellExecute(0, 'open', 'http://localhost:8080', nil, nil, SW_SHOW); - - {$ENDIF} - - ReadLn; - finally - LServer.Free; - end; -end; - -begin - try - if WebRequestHandler <> nil then - WebRequestHandler.WebModuleClass := WebModuleClass; - RunServer(8080); - except - on E: Exception do - Writeln(E.ClassName, ': ', E.Message); - end; - -end. diff --git a/samples/serversideviews/ServerSideViewsEx.dproj b/samples/serversideviews/ServerSideViewsEx.dproj deleted file mode 100644 index d09dd836..00000000 --- a/samples/serversideviews/ServerSideViewsEx.dproj +++ /dev/null @@ -1,502 +0,0 @@ - - - {C829684B-145E-49F2-8C37-2562C6C5904E} - 18.2 - VCL - ServerSideViewsEx.dpr - True - Debug - Win32 - 1 - Console - - - true - - - true - Base - true - - - true - Base - true - - - true - Base - true - - - true - Cfg_1 - true - true - - - true - Base - true - - - $(BDS)\bin\delphi_PROJECTICNS.icns - $(BDS)\bin\delphi_PROJECTICON.ico - System;Xml;Data;Datasnap;Web;Soap;Vcl;Vcl.Imaging;Vcl.Touch;Vcl.Samples;Vcl.Shell;$(DCC_Namespace) - ServerSideViewsEx - .\$(Platform)\$(Config) - .\$(Platform)\$(Config) - false - false - false - false - false - - - Winapi;System.Win;Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;Bde;$(DCC_Namespace) - 1033 - .\bin - CompanyName=;FileVersion=1.0.0.0;InternalName=;LegalCopyright=;LegalTrademarks=;OriginalFilename=;ProductVersion=1.0.0.0;Comments=;ProgramID=com.embarcadero.$(ModuleName);FileDescription=$(ModuleName);ProductName=$(ModuleName) - None - DBXSqliteDriver;RESTComponents;DataSnapServerMidas;DBXDb2Driver;DBXInterBaseDriver;vclactnband;frxe23;vclFireDAC;emsclientfiredac;DataSnapFireDAC;svnui;tethering;FireDACADSDriver;DBXMSSQLDriver;DatasnapConnectorsFreePascal;FireDACMSSQLDriver;vcltouch;vcldb;bindcompfmx;svn;Intraweb;DBXOracleDriver;ipstudiowinwordxp;inetdb;FmxTeeUI;FireDACIBDriver;fmx;fmxdae;DelphiCookbookListViewAppearance;vclib;FireDACDBXDriver;dbexpress;IndyCore;vclx;dsnap;DataSnapCommon;emsclient;FireDACCommon;bdertl;RESTBackendComponents;DataSnapConnectors;VCLRESTComponents;soapserver;frxTee23;JclDeveloperTools;vclie;CPortLibDXE;bindengine;DBXMySQLDriver;FireDACOracleDriver;CloudService;FireDACMySQLDriver;DBXFirebirdDriver;FireDACCommonDriver;DataSnapClient;inet;bindcompdbx;IndyIPCommon;vcl;DBXSybaseASEDriver;IndyIPServer;IndySystem;FireDACDb2Driver;dsnapcon;FireDACMSAccDriver;fmxFireDAC;FireDACInfxDriver;vclimg;TeeDB;FireDAC;Jcl;FireDACSqliteDriver;FireDACPgDriver;ibmonitor;FireDACASADriver;DBXOdbcDriver;FireDACTDataDriver;FMXTee;ipstudiowinclient;soaprtl;DbxCommonDriver;ibxpress;Tee;DataSnapServer;xmlrtl;soapmidas;DataSnapNativeClient;fmxobj;vclwinx;ibxbindings;rtl;FireDACDSDriver;DbxClientDriver;DTKANPRPackage;DBXSybaseASADriver;CustomIPTransport;vcldsnap;CodeSiteExpressPkg;SampleListViewMultiDetailAppearancePackage;bindcomp;appanalytics;ipstudiowin;DBXInformixDriver;officeXPrt;IndyIPClient;bindcompvcl;frxDB23;vcldbx;TeeUI;vclribbon;dbxcds;VclSmp;adortl;FireDACODBCDriver;JclVcl;DataSnapIndy10ServerTransport;dsnapxml;DataSnapProviderClient;dbrtl;inetdbxpress;FireDACMongoDBDriver;IndyProtocols;frx23;JclContainers;fmxase;$(DCC_UsePackage) - - - DBXSqliteDriver;RESTComponents;DataSnapServerMidas;DBXDb2Driver;DBXInterBaseDriver;vclactnband;vclFireDAC;emsclientfiredac;DataSnapFireDAC;tethering;FireDACADSDriver;DBXMSSQLDriver;DatasnapConnectorsFreePascal;FireDACMSSQLDriver;vcltouch;vcldb;bindcompfmx;Intraweb;DBXOracleDriver;inetdb;FmxTeeUI;FireDACIBDriver;fmx;fmxdae;vclib;FireDACDBXDriver;dbexpress;IndyCore;vclx;dsnap;DataSnapCommon;emsclient;FireDACCommon;RESTBackendComponents;DataSnapConnectors;VCLRESTComponents;soapserver;vclie;bindengine;DBXMySQLDriver;FireDACOracleDriver;CloudService;FireDACMySQLDriver;DBXFirebirdDriver;FireDACCommonDriver;DataSnapClient;inet;bindcompdbx;IndyIPCommon;vcl;DBXSybaseASEDriver;IndyIPServer;IndySystem;FireDACDb2Driver;dsnapcon;FireDACMSAccDriver;fmxFireDAC;FireDACInfxDriver;vclimg;TeeDB;FireDAC;FireDACSqliteDriver;FireDACPgDriver;ibmonitor;FireDACASADriver;DBXOdbcDriver;FireDACTDataDriver;FMXTee;ipstudiowinclient;soaprtl;DbxCommonDriver;ibxpress;Tee;DataSnapServer;xmlrtl;soapmidas;DataSnapNativeClient;fmxobj;vclwinx;ibxbindings;rtl;FireDACDSDriver;DbxClientDriver;DBXSybaseASADriver;CustomIPTransport;vcldsnap;bindcomp;appanalytics;ipstudiowin;DBXInformixDriver;officeXPrt;IndyIPClient;bindcompvcl;TeeUI;vclribbon;dbxcds;VclSmp;adortl;FireDACODBCDriver;DataSnapIndy10ServerTransport;dsnapxml;DataSnapProviderClient;dbrtl;inetdbxpress;FireDACMongoDBDriver;IndyProtocols;fmxase;$(DCC_UsePackage) - - - DEBUG;$(DCC_Define) - true - false - true - true - true - - - 1033 - None - false - - - false - RELEASE;$(DCC_Define) - 0 - 0 - - - - MainSource - - -
WebModule1
- TWebModule -
- - - -
MyDataModule
- dfm - TDataModule -
- - Cfg_2 - Base - - - Base - - - Cfg_1 - Base - -
- - Delphi.Personality.12 - Console - - - - ServerSideViewsEx.dpr - - - Embarcadero C++Builder Office 2000 Servers Package - Embarcadero C++Builder Office XP Servers Package - Microsoft Office 2000 Sample Automation Server Wrapper Components - - - - - - ServerSideViewsEx.exe - true - - - - - 1 - - - Contents\MacOS - 0 - - - - - classes - 1 - - - - - library\lib\armeabi-v7a - 1 - - - - - library\lib\armeabi - 1 - - - - - library\lib\mips - 1 - - - - - - library\lib\armeabi-v7a - 1 - - - - - res\drawable - 1 - - - - - res\values - 1 - - - - - res\drawable - 1 - - - - - res\drawable-xxhdpi - 1 - - - - - res\drawable-ldpi - 1 - - - - - res\drawable-mdpi - 1 - - - - - res\drawable-hdpi - 1 - - - - - res\drawable-xhdpi - 1 - - - - - res\drawable-small - 1 - - - - - res\drawable-normal - 1 - - - - - res\drawable-large - 1 - - - - - res\drawable-xlarge - 1 - - - - - 1 - - - 1 - - - 0 - - - - - 1 - .framework - - - 0 - - - - - 1 - .dylib - - - 0 - .dll;.bpl - - - - - 1 - .dylib - - - 1 - .dylib - - - 1 - .dylib - - - 1 - .dylib - - - 0 - .bpl - - - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - 0 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - 1 - - - 1 - - - - - 1 - - - - - ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF - 1 - - - ..\$(PROJECTNAME).app.dSYM\Contents\Resources\DWARF - 1 - - - - - - - - 1 - - - 1 - - - 1 - - - - - - - Contents\Resources - 1 - - - - - library\lib\armeabi-v7a - 1 - - - 1 - - - 1 - - - 1 - - - 1 - - - 1 - - - 0 - - - - - 1 - - - 1 - - - - - Assets - 1 - - - Assets - 1 - - - - - Assets - 1 - - - Assets - 1 - - - - - - - - - - - - - True - False - - - 12 - - - - -
diff --git a/samples/serversideviews/WebSiteControllerU.pas b/samples/serversideviews/WebSiteControllerU.pas index 5752f548..e88ee649 100644 --- a/samples/serversideviews/WebSiteControllerU.pas +++ b/samples/serversideviews/WebSiteControllerU.pas @@ -6,20 +6,9 @@ uses MVCFramework, System.Diagnostics, System.JSON, MVCFramework.Commons; type - TSpeedValue = class - private - FValue: string; - procedure SetValue(const Value: string); - public - property Value: string read FValue write SetValue; - constructor Create(const aValue: string); - end; [MVCPath('/')] TWebSiteController = class(TMVCController) - private - FStopWatch: TStopwatch; - function GetSpeed: TSpeedValue; protected procedure OnBeforeAction(Context: TWebContext; const AActionNAme: string; var Handled: Boolean); override; @@ -89,13 +78,29 @@ procedure TWebSiteController.EditPerson(guid: string); var LDAL: IPeopleDAL; lPerson: TPerson; + lDevices: TDeviceList; + lItem: TDevice; + lIdx: Integer; begin LDAL := TServicesFactory.GetPeopleDAL; lPerson := LDAL.GetPersonByGUID(guid); - ViewData['person'] := lPerson; - ViewData['speed'] := GetSpeed; - LoadView(['header', 'editperson', 'footer']); - RenderResponseStream; + try + lDevices := LDAL.GetDevicesList; + try + ViewData['person'] := lPerson; + for lItem in lDevices do + begin + lItem.Selected := lPerson.Items.Contains(lItem.DeviceName); + end; + ViewData['deviceslist'] := lDevices; + LoadView(['header', 'editperson', 'footer']); + RenderResponseStream; + finally + lDevices.Free; + end; + finally + lPerson.Free; + end; end; procedure TWebSiteController.ExportPeopleListAsCSV; @@ -115,16 +120,17 @@ end; procedure TWebSiteController.GeneratePeopleListAsCSV; var LDAL: IPeopleDAL; + lPeople: TPeople; begin LDAL := TServicesFactory.GetPeopleDAL; - ViewData['people'] := LDAL.GetPeople; - LoadView(['people_header.csv', 'people_list.csv']); - RenderResponseStream; // rember to call RenderResponseStream!!! -end; - -function TWebSiteController.GetSpeed: TSpeedValue; -begin - Result := TSpeedValue.Create(FStopWatch.Elapsed.TotalMilliseconds.ToString); + lPeople := LDAL.GetPeople; + try + ViewData['people'] := lPeople; + LoadView(['people_header.csv', 'people_list.csv']); + RenderResponseStream; // rember to call RenderResponseStream!!! + finally + lPeople.Free; + end; end; procedure TWebSiteController.Index; @@ -133,10 +139,19 @@ begin end; procedure TWebSiteController.NewPerson; +var + lDAL: IPeopleDAL; + lDevices: TDeviceList; begin - ViewData['speed'] := GetSpeed; - LoadView(['header', 'editperson', 'footer']); - RenderResponseStream; + lDAL := TServicesFactory.GetPeopleDAL; + lDevices := LDAL.GetDevicesList; + try + ViewData['deviceslist'] := lDevices; + LoadView(['header', 'editperson', 'footer']); + RenderResponseStream; + finally + lDevices.Free; + end; end; procedure TWebSiteController.OnBeforeAction(Context: TWebContext; @@ -145,18 +160,23 @@ begin inherited; ContentType := 'text/html'; Handled := False; - FStopWatch := TStopwatch.StartNew; end; procedure TWebSiteController.PeopleList; var LDAL: IPeopleDAL; + lPeople: TPeople; begin LDAL := TServicesFactory.GetPeopleDAL; - ViewData['people'] := LDAL.GetPeople; - ViewData['speed'] := GetSpeed; - LoadView(['header', 'people_list', 'footer']); - RenderResponseStream; // rember to call RenderResponseStream!!! + lPeople := LDAL.GetPeople; + try + ViewData['people'] := lPeople; + LoadView(['header', 'people_list', 'footer']); + RenderResponseStream; // rember to call RenderResponseStream!!! + finally + lPeople.Free; + end; + end; procedure TWebSiteController.SavePerson; @@ -165,10 +185,12 @@ var LLastName: string; LAge: string; LPeopleDAL: IPeopleDAL; + lDevices: TArray; begin LFirstName := Context.Request.Params['first_name'].Trim; LLastName := Context.Request.Params['last_name'].Trim; LAge := Context.Request.Params['age']; + lDevices := Context.Request.ParamsMulti['items']; if LFirstName.IsEmpty or LLastName.IsEmpty or LAge.IsEmpty then begin @@ -178,22 +200,9 @@ begin end; LPeopleDAL := TServicesFactory.GetPeopleDAL; - LPeopleDAL.AddPerson(LFirstName, LLastName, LAge.ToInteger(), []); + LPeopleDAL.AddPerson(LFirstName, LLastName, LAge.ToInteger(), lDevices); Redirect('/people'); end; -{ TSpeedValue } - -constructor TSpeedValue.Create(const aValue: string); -begin - inherited Create; - FValue := aValue; -end; - -procedure TSpeedValue.SetValue(const Value: string); -begin - FValue := Value; -end; - end. diff --git a/samples/serversideviews/bin/templates/editperson.mustache b/samples/serversideviews/bin/templates/editperson.mustache index 2267795e..96846a20 100644 --- a/samples/serversideviews/bin/templates/editperson.mustache +++ b/samples/serversideviews/bin/templates/editperson.mustache @@ -49,6 +49,20 @@ +
+
+ +
+ + (Ctrl+Click to select multiple devices) +
+
+
+
diff --git a/samples/serversideviews/bin/templates/footer.mustache b/samples/serversideviews/bin/templates/footer.mustache index 445ed0d2..6579c5ee 100644 --- a/samples/serversideviews/bin/templates/footer.mustache +++ b/samples/serversideviews/bin/templates/footer.mustache @@ -12,7 +12,7 @@ Powered by DMVCFramework
- Page generated in {{speed.value}}ms + Server Side Views