unit uCtrlBase;

interface
{$I 'GlobalDefs.inc'}


uses
  MVCFramework,
  MVCFramework.Commons,
  MVCFramework.Serializer.Commons,
  MVCFramework.Nullables,
  MVCFramework.Controllers.Register,
  FireDAC.Stan.Intf, FireDAC.Stan.Option,
  FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def,
  FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.VCLUI.Wait,
  Data.DB, FireDAC.Comp.Client, FireDAC.Comp.UI, FireDAC.Phys.MSSQLDef,
  FireDAC.Phys.ODBCBase, FireDAC.Phys.MSSQL, FireDAC.Stan.Param, FireDAC.DatS,
  FireDAC.DApt.Intf, FireDAC.DApt, FireDAC.Comp.DataSet, FireDAC.Stan.Consts,
  uDataMod,
  uCommons,
  uSvc_Base,
  uSvc_Obecne,
  uSvc_Organizace,
  uSvc_Zamestnanci,
  uSvc_ObehZbozi,
  uSvc_KmenZbozi,
  uSvc_Vyroba,
  uSvc_QMS,
  MVCFramework.Swagger.Commons
{$IF DEFINED(CUSTOM_CTRL_Rootvin)}, uSvcCustom {$ENDIF}
{$IF DEFINED(CUSTOM_CTRL_INCOSystems)}, uSvcCustom {$ENDIF}
{$IF DEFINED(CUSTOM_CTRL_Gornicky)}, uSvcCustom {$ENDIF}
{$IF DEFINED(CUSTOM_CTRL_Westra)}, uSvcCustom {$ENDIF}
{$IF DEFINED(CUSTOM_CTRL_EMPolar)}, uSvcCustom {$ENDIF}
{$IF DEFINED(CUSTOM_CTRL_GatemaSD)}, uSvcCustomSDG {$ENDIF}
  ;


const
{$I globalConsts.inc}

type
  TBaseController = class abstract(TMVCController)
   private
    FConn: TFDConnection;
   strict private
    FObecnyService: TObecnyService;

    FAktivitaService: TAktivitaService;
    FUkolAktivityService: TUkolAktivityService;

    FZamestnanciService: TZamestnanciService;
    FDokumentService: TDokumentService;
    FPlanKalendarService: TPlanKalendarService;
    FPolPlanKalendarService: TPolPlanKalendarService;

    FSkupinaZboziService: TSkupinaZboziService;
    FKmenZboziService: TKmenZboziService;
    FDokladOZService: TDokladOZService;
    FPolozkaOZService: TPolozkaOZService;
    FStavSkladuService: TStavSkladuService;

    FTPVOperaceDilceService: TTPVOperaceDilceService;
    FTPVKusovnikDilceService: TTPVKusovnikDilceService;

    FVyrobniPrikazService: TVyrobniPrikazService;
    FVyrobaVyrCisPrikazService: TVyrobaVyrCisPrikazService;
    FMaterialPrikazuService: TMaterialPrikazuService;
    FVyrobniOperaceService: TVyrobniOperaceService;
    FKooperacniObjednavkaService: TKooperacniObjednavkaService;

    FVyrobaEvidRozpracOperaciService: TVyrobaEvidRozpracOperaciService;
    FVyrobaEvidRozpracOperaciOperaceService: TVyrobaEvidRozpracOperaciOperaceService;
    FVyrobaEvidenceOperaciService: TVyrobaEvidenceOperaciService;

    FQMSUdrzbaStrojuAZarizeniService: TQMSUdrzbaStrojuAZarizeniService;
    FQMSObecneService: TQMSObecneService;
    FQMSKontrolniPostupyService: TQMSKontrolniPostupyService;
    FQMSKontrolniPlanService: TQMSKontrolniPlanService;

    FOrganizaceService: TOrganizaceService;
    FTPVPracovisteService: TTPVPracovisteService;
    FTPVStrojService: TTPVStrojService;
    FTPVCisKoopService: TTPVCisKoopService;
    FTPVCiselnikZmenService: TTPVCiselnikZmenService;
    FTPVPrednastaveniOperaciService: TTPVPrednastaveniOperaciService;
    FTPVZakazkoveModifikaceService: TTPVZakazkoveModifikaceService;
    FTPVZakazkoveModifikaceDilceService: TTPVZakazkoveModifikaceDilceService;

{$IFDEF CUSTOM_CTRL_Rootvin}
    FRTNService: TRTNService;
{$ENDIF}
{$IFDEF CUSTOM_CTRL_INCOSystems}
    FINCOSysService: TINCOSysService;
{$ENDIF}
{$IFDEF CUSTOM_CTRL_Gornicky}
    FGornickyService: TGornickyService;
{$ENDIF}
{$IFDEF CUSTOM_CTRL_Westra}
    FWestraService: TWestraService;
{$ENDIF}
{$IFDEF CUSTOM_CTRL_EMPolar}
    FEMPNadobaService: TEMPNadobaService;
    FEMPVytezeniDoklService: TEMPVytezeniDoklService;
{$ENDIF}
    function GetDataModule: TdatMod;
   strict protected
    FDM: TdatMod;

    function GetObecnyService: TObecnyService;

    function GetAktivitaService: TAktivitaService;
    function GetUkolAktivityService: TUkolAktivityService;

    function GetQMSUdrzbaStrojuAZarizeniService: TQMSUdrzbaStrojuAZarizeniService;
    function GetQMSObecneService: TQMSObecneService;

    function GetZamestnanciService: TZamestnanciService;
    function GetOrganizaceService: TOrganizaceService;
    function GetDokumentService: TDokumentService;
    function GetPlanKalendarService: TPlanKalendarService;
    function GetPolPlanKalendarService: TPolPlanKalendarService;

    function GetSkupinaZboziService: TSkupinaZboziService;
    function GetKmenZboziService: TKmenZboziService;
    function GetDokladOZService: TDokladOZService;
    function GetPolozkaOZService: TPolozkaOZService;
    function GetStavSkladuService: TStavSkladuService;

    function GetTPVOperaceDilceService: TTPVOperaceDilceService;
    function GetTPVKusovnikDilceService: TTPVKusovnikDilceService;

    function GetVyrobniPrikazService: TVyrobniPrikazService;
    function GetVyrobaVyrCisPrikazService: TVyrobaVyrCisPrikazService;
    function GetMaterialPrikazuService: TMaterialPrikazuService;
    function GetVyrobniOperaceService: TVyrobniOperaceService;
    function GetKooperacniObjednavkaService: TKooperacniObjednavkaService;

    function GetVyrobaEvidRozpracOperaciService: TVyrobaEvidRozpracOperaciService;
    function GetVyrobaEvidRozpracOperaciOperaceService: TVyrobaEvidRozpracOperaciOperaceService;
    function GetVyrobaEvidenceOperaciService: TVyrobaEvidenceOperaciService;


    function GetQMSKontrolniPostupyService: TQMSKontrolniPostupyService;
    function GetQMSKontrolniPlanService: TQMSKontrolniPlanService;

    function GetTPVPracovisteService: TTPVPracovisteService;
    function GetTPVStrojService: TTPVStrojService;
    function GetTPVCisKoopService: TTPVCisKoopService;
    function GetTPVCiselnikZmenService: TTPVCiselnikZmenService;
    function GetTPVPrednastaveniOperaciService: TTPVPrednastaveniOperaciService;
    function GetTPVZakazkoveModifikaceService: TTPVZakazkoveModifikaceService;
    function GetTPVZakazkoveModifikaceDilceService: TTPVZakazkoveModifikaceDilceService;

{$IFDEF CUSTOM_CTRL_Rootvin}
    function GetRTNService: TRTNService;
{$ENDIF}
{$IFDEF CUSTOM_CTRL_INCOSystems}
    function GetINCOSysService: TINCOSysService;
{$ENDIF}
{$IFDEF CUSTOM_CTRL_Gornicky}
    function GetGornickyService: TGornickyService;
{$ENDIF}
{$IFDEF CUSTOM_CTRL_Westra}
    function GetWestraService: TWestraService;
{$ENDIF}
{$IFDEF CUSTOM_CTRL_EMPolar}
    function GetEMPNadobaService: TEMPNadobaService;
    function GetEMPVytezeniDoklService: TEMPVytezeniDoklService;
{$ENDIF}

    procedure OnBeforeAction (aContext: TWebContext; const aActionName: string; var AHandled: Boolean); override;
    procedure OnAfterAction (aContext: TWebContext; const aActionName: string); override;
   public
    property Conn: TFDConnection read FConn;
    constructor Create; override;
    destructor Destroy; override;
    function sanitizeSQLString (s: string): string;
  end;


  [MVCPath('')]
  TObecnyController = class(TBaseController)
  public
    [MVCPath('/test')]
    [MVCHTTPMethod([httpGET])]
    [MVCSwagSummary('Obecny kontroler', 'Test dostupnosti DB', 'BaseTestDB')]
//    [MVCProduces('text/plain')]
    procedure TestDB;

    [MVCDoc('Pomocí POST zapíše obecná json data do Heliosu')]
    [MVCPath('/zapisjson')] // zapis obecnych json data do Heliosu
    [MVCHTTPMethod([httpPOST])]
    [MVCSwagSummary('Obecny kontroler', 'Zápis JSON dat do db Heliosu', 'BaseZapisJSONDoHeliosu')]
    [MVCConsumes(TMVCMediaType.APPLICATION_JSON)]
    procedure ZapisJSONDoHeliosu (CTX: TWebContext);

  end;





  [MVCPatch]




  [MVCSwagIgnorePath]
  [MVCPath]
  TRedirectController = class(TMVCController)
  public
    [MVCPath('/')]
    [MVCHTTPMethod([httpGET])]
    procedure DoRedirect;
  end;


implementation

uses
  Winapi.ActiveX,
  System.SysUtils,
  System.DateUtils;


{ TBaseController }

 procedure TBaseController.OnBeforeAction (aContext: TWebContext; const aActionName: string; var AHandled: Boolean);
  begin
   inherited;
   AHandled := true;

   FConn := TFDConnection.Create (nil);
   FConn.ConnectionDefName := sqlPoolName;
   FConn.Connected := true;
  end;



 procedure TBaseController.OnAfterAction (aContext: TWebContext; const aActionName: string);
  begin
   try
     if Assigned(FConn) then
      FConn.Close;
    finally
     FConn.Free;
   end;
  end;



 constructor TBaseController.Create;
  begin
   inherited;
   if not(Assigned(FDM)) then
    FDM:= GetDataModule;
  end;



 destructor TBaseController.Destroy;
  begin
   FObecnyService.Free;
   FZamestnanciService.Free;
   FOrganizaceService.Free;
   FDokumentService.Free;
   FPlanKalendarService.Free;

   FKmenZboziService.Free;
   FStavSkladuService.Free;

   FDokladOZService.Free;
   FPolozkaOZService.Free;

   FTPVOperaceDilceService.Free;
   FTPVKusovnikDilceService.Free;

   FVyrobniPrikazService.Free;
   FVyrobaVyrCisPrikazService.Free;
   FVyrobniOperaceService.Free;
   FKooperacniObjednavkaService.Free;

   FVyrobaEvidRozpracOperaciService.Free;
   FVyrobaEvidRozpracOperaciOperaceService.Free;
   FVyrobaEvidenceOperaciService.Free;

   FQMSObecneService.Free;
   FQMSUdrzbaStrojuAZarizeniService.Free;
   FQMSKontrolniPostupyService.Free;
   FQMSKontrolniPlanService.Free;


   FTPVPracovisteService.Free;
   FTPVStrojService.Free;
   FTPVCisKoopService.Free;
   FTPVCiselnikZmenService.Free;
   FTPVPrednastaveniOperaciService.Free;
   FTPVZakazkoveModifikaceService.Free;
   FTPVZakazkoveModifikaceDilceService.Free;

{$IFDEF CUSTOM_CTRL_Rootvin}
   FRTNService.Free;
{$ENDIF}

{$IFDEF CUSTOM_CTRL_INCOSystems}
   FINCOSysService.Free;
{$ENDIF}

{$IFDEF CUSTOM_CTRL_Gornicky}
   FGornickyService.Free;
{$ENDIF}

{$IFDEF CUSTOM_CTRL_Westra}
   FWestraService.Free;
{$ENDIF}

{$IFDEF CUSTOM_CTRL_EMPolar}
   FEMPNadobaService.Free;
   FEMPVytezeniDoklService.Free;
{$ENDIF}

   if Assigned(FDM) then
    FDM.Free;

   inherited;
  end;



 function TBaseController.GetDataModule: TdatMod;
  begin
   if not Assigned(FDM) then
    FDM:= TdatMod.Create(nil);

   with FDM do
    try
      sqlConn.Params.Clear;
      sqlConn.Params.Add(S_FD_ConnParam_Common_DriverID + '=MSSQL');
      sqlConn.Params.Add(S_FD_ConnParam_Common_Server + '=' + datMod.dbServer);
      sqlConn.Params.Add(S_FD_ConnParam_Common_Port + '=' + datMod.dbPort.ToString);
      sqlConn.Params.Add(S_FD_ConnParam_Common_Database + '=' + datMod.dbName);
      sqlConn.Params.Add(S_FD_ConnParam_Common_UserName + '=' + datMod.dbUser);
      sqlConn.Params.Add(S_FD_ConnParam_Common_Password + '=' + datMod.dbPwd);
      sqlConn.Params.Add(S_FD_ConnParam_Common_OSAuthent + '=No');
      sqlConn.Params.Add(S_FD_ConnParam_Common_MetaDefSchema + '=dbo');
      sqlConn.Params.Add(S_FD_ConnParam_ODBC_ODBCAdvanced + '=TrustServerCertificate=yes');
      sqlConn.Params.Add(S_FD_ConnParam_Common_ApplicationName + '=hdcdzAPIsvc');
//      sqlConn.Open;
     except on E:Exception do
    end;

   result:= FDM;
  end;



 function TBaseController.GetObecnyService: TObecnyService;
  begin
   if not Assigned(FObecnyService) then
    FObecnyService:= TObecnyService.Create (GetDataModule);
   result:= FObecnyService;
  end;


 function TBaseController.GetUkolAktivityService: TUkolAktivityService;
  begin
   if not Assigned(FUkolAktivityService) then
    FUkolAktivityService:= TUkolAktivityService.Create (GetDataModule);
   result:= FUkolAktivityService;
  end;

 function TBaseController.GetAktivitaService: TAktivitaService;
  begin
   if not Assigned(FAktivitaService) then
    FAktivitaService:= TAktivitaService.Create (GetDataModule);
   result:= FAktivitaService;
  end;




 function TBaseController.GetQMSUdrzbaStrojuAZarizeniService: TQMSUdrzbaStrojuAZarizeniService;
  begin
   if not Assigned(FQMSUdrzbaStrojuAZarizeniService) then
    FQMSUdrzbaStrojuAZarizeniService:= TQMSUdrzbaStrojuAZarizeniService.Create (GetDataModule);
   result:= FQMSUdrzbaStrojuAZarizeniService;
  end;



 function TBaseController.GetQMSObecneService: TQMSObecneService;
  begin
   if not Assigned(FQMSObecneService) then
    FQMSObecneService:= TQMSObecneService.Create (GetDataModule);
   result:= FQMSObecneService;
  end;



 function TBaseController.GetZamestnanciService: TZamestnanciService;
  begin
   if not Assigned(FZamestnanciService) then
    FZamestnanciService:= TZamestnanciService.Create (GetDataModule);
   result:= FZamestnanciService;
  end;


 function TBaseController.GetDokumentService: TDokumentService;
  begin
   if not Assigned(FDokumentService) then
    FDokumentService:= TDokumentService.Create (GetDataModule);
   result:= FDokumentService;
  end;


 function TBaseController.GetPlanKalendarService: TPlanKalendarService;
  begin
   if not Assigned(FPlanKalendarService) then
    FPlanKalendarService:= TPlanKalendarService.Create (GetDataModule);
   result:= FPlanKalendarService;
  end;

 function TBaseController.GetPolPlanKalendarService: TPolPlanKalendarService;
  begin
   if not Assigned(FPolPlanKalendarService) then
    FPolPlanKalendarService:= TPolPlanKalendarService.Create (GetDataModule);
   result:= FPolPlanKalendarService;
  end;



 function TBaseController.GetKmenZboziService: TKmenZboziService;
  begin
   if not Assigned(FKmenZboziService) then
    FKmenZboziService:= TKmenZboziService.Create (GetDataModule);
   result:= FKmenZboziService;
  end;



 function TBaseController.GetDokladOZService: TDokladOZService;
  begin
   if not Assigned(FDokladOZService) then
    FDokladOZService:= TDokladOZService.Create(GetDataModule);
   result:= FDokladOZService;
  end;



 function TBaseController.GetPolozkaOZService: TPolozkaOZService;
  begin
   if not Assigned(FPolozkaOZService) then
    FPolozkaOZService:= TPolozkaOZService.Create(GetDataModule);
   result:= FPolozkaOZService;
  end;



 function TBaseController.GetStavSkladuService: TStavSkladuService;
  begin
   if not Assigned(FStavSkladuService) then
    FStavSkladuService:= TStavSkladuService.Create(GetDataModule);
   result:= FStavSkladuService;
  end;



 function TBaseController.GetSkupinaZboziService: TSkupinaZboziService;
  begin
   if not Assigned(FSkupinaZboziService) then
    FSkupinaZboziService:= TSkupinaZboziService.Create(GetDataModule);
   result:= FSkupinaZboziService;
  end;





 function TBaseController.GetVyrobniPrikazService: TVyrobniPrikazService;
  begin
   if not Assigned(FVyrobniPrikazService) then
    FVyrobniPrikazService:= TVyrobniPrikazService.Create(GetDataModule);
   result:= FVyrobniPrikazService;
  end;



 function TBaseController.GetVyrobaVyrCisPrikazService: TVyrobaVyrCisPrikazService;
  begin
   if not Assigned(FVyrobaVyrCisPrikazService) then
    FVyrobaVyrCisPrikazService:= TVyrobaVyrCisPrikazService.Create(GetDataModule);
   result:= FVyrobaVyrCisPrikazService;
  end;



 function TBaseController.GetMaterialPrikazuService: TMaterialPrikazuService;
  begin
   if not Assigned(FMaterialPrikazuService) then
    FMaterialPrikazuService:= TMaterialPrikazuService.Create (GetDataModule);
   result:= FMaterialPrikazuService;
  end;



 function TBaseController.GetVyrobniOperaceService: TVyrobniOperaceService;
  begin
   if not Assigned(FVyrobniOperaceService) then
    FVyrobniOperaceService:= TVyrobniOperaceService.Create (GetDataModule);
   result:= FVyrobniOperaceService;
  end;



 function TBaseController.GetVyrobaEvidenceOperaciService: TVyrobaEvidenceOperaciService;
  begin
   if not Assigned(FVyrobaEvidenceOperaciService) then
    FVyrobaEvidenceOperaciService:= TVyrobaEvidenceOperaciService.Create (GetDataModule);
   result:= FVyrobaEvidenceOperaciService;
  end;



 function TBaseController.GetVyrobaEvidRozpracOperaciService: TVyrobaEvidRozpracOperaciService;
  begin
   if not Assigned(FVyrobaEvidRozpracOperaciService) then
    FVyrobaEvidRozpracOperaciService:= TVyrobaEvidRozpracOperaciService.Create (GetDataModule);
   result:= FVyrobaEvidRozpracOperaciService;
  end;


 function TBaseController.GetVyrobaEvidRozpracOperaciOperaceService: TVyrobaEvidRozpracOperaciOperaceService;
  begin
   if not Assigned(FVyrobaEvidRozpracOperaciOperaceService) then
    FVyrobaEvidRozpracOperaciOperaceService:= TVyrobaEvidRozpracOperaciOperaceService.Create (GetDataModule);
   result:= FVyrobaEvidRozpracOperaciOperaceService;
  end;



 function TBaseController.GetKooperacniObjednavkaService: TKooperacniObjednavkaService;
  begin
   if not Assigned(FKooperacniObjednavkaService) then
    FKooperacniObjednavkaService:= TKooperacniObjednavkaService.Create (GetDataModule);
   result:= FKooperacniObjednavkaService;
  end;



 function TBaseController.GetOrganizaceService: TOrganizaceService;
  begin
   if not Assigned(FOrganizaceService) then
    FOrganizaceService:= TOrganizaceService.Create (GetDataModule);
   result:= FOrganizaceService;
  end;


 function TBaseController.GetQMSKontrolniPostupyService: TQMSKontrolniPostupyService;
  begin
   if not Assigned(FQMSKontrolniPostupyService) then
    FQMSKontrolniPostupyService:= TQMSKontrolniPostupyService.Create (GetDataModule);
   result:= FQMSKontrolniPostupyService;
  end;


 function TBaseController.GetQMSKontrolniPlanService: TQMSKontrolniPlanService;
  begin
   if not Assigned(FQMSKontrolniPlanService) then
    FQMSKontrolniPlanService:= TQMSKontrolniPlanService.Create (GetDataModule);
   result:= FQMSKontrolniPlanService;
  end;



 function TBaseController.GetTPVCiselnikZmenService: TTPVCiselnikZmenService;
  begin
   if not Assigned(FTPVCiselnikZmenService) then
    FTPVCiselnikZmenService:= TTPVCiselnikZmenService.Create(GetDataModule);
   result:= FTPVCiselnikZmenService;
  end;


 function TBaseController.GetTPVPracovisteService: TTPVPracovisteService;
  begin
   if not Assigned(FTPVPracovisteService) then
    FTPVPracovisteService:= TTPVPracovisteService.Create(GetDataModule);
   result:= FTPVPracovisteService;
  end;


 function TBaseController.GetTPVStrojService: TTPVStrojService;
  begin
   if not Assigned(FTPVStrojService) then
    FTPVStrojService:= TTPVStrojService.Create(GetDataModule);
   result:= FTPVStrojService;
  end;


 function TBaseController.GetTPVCisKoopService: TTPVCisKoopService;
  begin
   if not Assigned(FTPVCisKoopService) then
    FTPVCisKoopService:= TTPVCisKoopService.Create(GetDataModule);
   result:= FTPVCisKoopService;
  end;



 function TBaseController.GetTPVPrednastaveniOperaciService: TTPVPrednastaveniOperaciService;
  begin
   if not Assigned(FTPVPrednastaveniOperaciService) then
    FTPVPrednastaveniOperaciService:= TTPVPrednastaveniOperaciService.Create (GetDataModule);
   result:= FTPVPrednastaveniOperaciService;
  end;



 function TBaseController.GetTPVZakazkoveModifikaceService: TTPVZakazkoveModifikaceService;
  begin
   if not Assigned(FTPVZakazkoveModifikaceService) then
    FTPVZakazkoveModifikaceService:= TTPVZakazkoveModifikaceService.Create (GetDataModule);
   result:= FTPVZakazkoveModifikaceService;
  end;



 function TBaseController.GetTPVZakazkoveModifikaceDilceService: TTPVZakazkoveModifikaceDilceService;
  begin
   if not Assigned(FTPVZakazkoveModifikaceDilceService) then
    FTPVZakazkoveModifikaceDilceService:= TTPVZakazkoveModifikaceDilceService.Create (GetDataModule);
   result:= FTPVZakazkoveModifikaceDilceService;
  end;



 function TBaseController.GetTPVOperaceDilceService: TTPVOperaceDilceService;
  begin
   if not Assigned(FTPVOperaceDilceService) then
    FTPVOperaceDilceService:= TTPVOperaceDilceService.Create (GetDataModule);
   result:= FTPVOperaceDilceService;
  end;


 function TBaseController.GetTPVKusovnikDilceService: TTPVKusovnikDilceService;
  begin
   if not Assigned(FTPVKusovnikDilceService) then
    FTPVKusovnikDilceService:= TTPVKusovnikDilceService.Create (GetDataModule);
   result:= FTPVKusovnikDilceService;
  end;


{$IFDEF CUSTOM_CTRL_Rootvin}
 function TBaseController.GetRTNService: TRTNService;
  begin
   if not Assigned(FRTNService) then
    FRTNService:= TRTNService.Create(GetDataModule);
   result:= FRTNService;
  end;
{$ENDIF}


{$IFDEF CUSTOM_CTRL_INCOSystems}
 function TBaseController.GetINCOSysService: TINCOSysService;
  begin
   if not Assigned(FINCOSysService) then
    FINCOSysService:= TINCOSysService.Create (GetDataModule);
   result:= FINCOSysService;
  end;
{$ENDIF}


{$IFDEF CUSTOM_CTRL_Gornicky}
 function TBaseController.GetGornickyService: TGornickyService;
  begin
   if not Assigned(FGornickyService) then
    FGornickyService:= TGornickyService.Create (GetDataModule);
   result:= FGornickyService;
  end;
{$ENDIF}


{$IFDEF CUSTOM_CTRL_Westra}
 function TBaseController.GetWestraService: TWestraService;
  begin
   if not Assigned(FWestraService) then
    FWestraService:= TWestraService.Create (GetDataModule);
   result:= FWestraService;
  end;
{$ENDIF}


{$IFDEF CUSTOM_CTRL_EMPolar}
 function TBaseController.GetEMPNadobaService: TEMPNadobaService;
  begin
   if not Assigned(FEMPNadobaService) then
    FEMPNadobaService:= TEMPNadobaService.Create (GetDataModule);
   result:= FEMPNadobaService;
  end;

 function TBaseController.GetEMPVytezeniDoklService: TEMPVytezeniDoklService;
  begin
   if not Assigned(FEMPVytezeniDoklService) then
    FEMPVytezeniDoklService:= TEMPVytezeniDoklService.Create (GetDataModule);
   result:= FEMPVytezeniDoklService;
  end;
{$ENDIF}


 function TBaseController.sanitizeSQLString (s: string): string;
  begin
   result:= s.Replace(' ', '').Replace(';', '').Replace('--', '').Replace(' OR', '').Trim;
  end;





{ TRedirectController }
  procedure TRedirectController.DoRedirect;
   begin
    Redirect('/swagger');
   end;






{ TObecnyController }

 procedure TObecnyController.TestDB;
  begin

   try
     if not(FDM.sqlConn.Connected) then
      FDM.sqlConn.Open;
    finally
   end;
   if not(FDM.sqlConn.Connected) then
    begin
     RenderStatusMessage(200, FDM.ErrorStatusJsonText('NO_DB_CONNECTION'));
{$IFDEF NORENDER400}
//     RenderStatusMessage(200);
{$ENDIF}
     Exit;
    end;


   try
     Render(ObjectDict().Add('data', GetObecnyService.DoTesty)); // viz uSvc_KmenZbozi.pas
    except
{$IFDEF NORENDER400}
     RenderStatusMessage(200);
{$ELSE}
     on E: EServiceException do
      begin
       raise EMVCException.Create(E.Message, '', 0, 404);
      end
     else
       raise;
{$ENDIF}
   end;
  end;




 procedure TObecnyController.ZapisJSONDoHeliosu (CTX: TWebContext);
  var respData, jsonData: string;
//      o: System.JSON.TJSONObject;
  begin
   try
     if not(FDM.sqlConn.Connected) then
      FDM.sqlConn.Open;
    finally
   end;
   if not(FDM.sqlConn.Connected) then
    begin
     RenderStatusMessage(200, FDM.ErrorStatusJsonText('NO_DB_CONNECTION'));
{$IFDEF NORENDER400}
//     RenderStatusMessage(200);
{$ENDIF}
     Exit;
    end;


   respData:= '';
   try
     jsonData:= CTX.Request.Body.Trim;
     if (jsonData<>'') then
      GetObecnyService.ZapisJsonDoHeliosu (jsonData, respData);
     ResponseStatus(HTTP_STATUS.OK, 'OK');
     Render(respData);
    except
{$IFDEF NORENDER400}
     RenderStatusMessage(200);
{$ELSE}
     on E: EServiceException do
      begin
       raise EMVCException.Create(E.Message, '', 0, 404);
      end
     else
       raise;
{$ENDIF}
   end;
  end;


end.
