Advertisement
danieleteti

DMVCFramework:TMVCActiveRecordController + other controllers

Feb 11th, 2020
763
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.75 KB | None | 0 0
  1. unit WebModuleU;
  2.  
  3. interface
  4.  
  5. uses
  6.   System.SysUtils,
  7.   System.Classes,
  8.   Web.HTTPApp,
  9.   MVCFramework,
  10.   FireDAC.Stan.Intf,
  11.   FireDAC.Stan.Option,
  12.   FireDAC.Stan.Error,
  13.   FireDAC.UI.Intf,
  14.   FireDAC.Phys.Intf,
  15.   FireDAC.Stan.Def,
  16.   FireDAC.Stan.Pool,
  17.   FireDAC.Stan.Async,
  18.   FireDAC.Phys,
  19.   FireDAC.Phys.MySQL,
  20.   FireDAC.Phys.MySQLDef,
  21.   FireDAC.VCLUI.Wait,
  22.   Data.DB,
  23.   FireDAC.Comp.Client,
  24.   FireDAC.Stan.Param,
  25.   FireDAC.DatS,
  26.   FireDAC.DApt.Intf,
  27.   FireDAC.DApt,
  28.   FireDAC.Comp.DataSet;
  29.  
  30. type
  31.   TMyWebModule = class(TWebModule)
  32.     FDQuery1: TFDQuery;
  33.     FDConnection1: TFDConnection;
  34.     procedure WebModuleCreate(Sender: TObject);
  35.     procedure WebModuleDestroy(Sender: TObject);
  36.   private
  37.     FMVC: TMVCEngine;
  38.   public
  39.     { Public declarations }
  40.   end;
  41.  
  42. var
  43.   WebModuleClass: TComponentClass = TMyWebModule;
  44.   ConnectionDefinitionName: string = '';
  45.  
  46. implementation
  47.  
  48. {$R *.dfm}
  49.  
  50.  
  51. uses
  52.   System.IOUtils,
  53.   MVCFramework.Commons,
  54.   MVCFramework.ActiveRecordController,
  55.   MVCFramework.ActiveRecord,
  56.   FDConnectionConfigU, CustomControllerU;
  57.  
  58. procedure TMyWebModule.WebModuleCreate(Sender: TObject);
  59. begin
  60.   FMVC := TMVCEngine.Create(Self,
  61.     procedure(Config: TMVCConfig)
  62.     begin
  63.       // enable static files
  64.       Config[TMVCConfigKey.DocumentRoot] := TPath.Combine(ExtractFilePath(GetModuleName(HInstance)), 'www');
  65.       // session timeout (0 means session cookie)
  66.       Config[TMVCConfigKey.SessionTimeout] := '0';
  67.       // default content-type
  68.       Config[TMVCConfigKey.DefaultContentType] := TMVCConstants.DEFAULT_CONTENT_TYPE;
  69.       // default content charset
  70.       Config[TMVCConfigKey.DefaultContentCharset] := TMVCConstants.DEFAULT_CONTENT_CHARSET;
  71.       // unhandled actions are permitted?
  72.       Config[TMVCConfigKey.AllowUnhandledAction] := 'false';
  73.       // default view file extension
  74.       Config[TMVCConfigKey.DefaultViewFileExtension] := 'html';
  75.       // view path
  76.       Config[TMVCConfigKey.ViewPath] := 'templates';
  77.       // Enable Server Signature in response
  78.       Config[TMVCConfigKey.ExposeServerSignature] := 'true';
  79.       // Define a default URL for requests that don't map to a route or a file (useful for client side web app)
  80.       Config[TMVCConfigKey.FallbackResource] := 'index.html';
  81.     end);
  82.  
  83.  
  84.   // ********************************************************************
  85.   //  Just register other controllers before TMVCActiveRecordController with
  86.   //  compatible paths. The router will works as usual.
  87.   // ********************************************************************
  88.   FMVC.AddController(TCustomPeopleController, '/api/entities/people');
  89.   FMVC.AddController(TCitiesController, '/api/cities');
  90.   FMVC.AddController(TContriesController, '/api/countries');
  91.  
  92.   // ********************************************************************
  93.   //  As last controller, register TMVCActiveRecordController.
  94.   // ********************************************************************
  95.   FMVC.AddController(TMVCActiveRecordController,
  96.     function: TMVCController
  97.     begin
  98.       Result := TMVCActiveRecordController.Create(
  99.         function: TFDConnection
  100.         begin
  101.           Result := TFDConnection.Create(nil);
  102.           Result.ConnectionDefName := ConnectionDefinitionName;
  103.         end,
  104.         function(aContext: TWebContext; aClass: TMVCActiveRecordClass; aAction: TMVCActiveRecordAction): Boolean
  105.         begin
  106.           if aContext.LoggedUser.IsValid then
  107.           begin
  108.             Result := True;
  109.           end
  110.           else
  111.           begin
  112.             Result := True; // not(aAction in [TMVCActiveRecordAction.Delete]);
  113.           end;
  114.         end);
  115.     end, '/api/entities');
  116. end;
  117.  
  118. procedure TMyWebModule.WebModuleDestroy(Sender: TObject);
  119. begin
  120.   FMVC.Free;
  121. end;
  122.  
  123. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement