unit uCtrlCustomSDG;

interface

uses
  MVCframework,
  MVCframework.Commons,
  MVCframework.Serializer.Commons,
//  MVCframework.Serializer.Intf,
  System.Generics.Collections, // kvuli TDictionary
  System.RegularExpressions,
//  JsonDataObjects,
  uCommons, // kvuli EServiceException
  uCtrlBase,
//  uSvc_Base,
  uHeoObj_Base,
//  uHeOObj_Custom,
  uSvcCustomSDG,
  uHeOObj_CustomSDG,
  MVCFramework.Swagger.Commons; // kvuli dokumentaci

type

  [MVCPath('/sdserver/doklad')]
  TGatemaSDDokladController = class(TBaseController)
   strict private
    FSelfSvc: TGatemaSDDokladService;
   strict protected
    function GetGatemaSDDokladService: TGatemaSDDokladService;
   public
    destructor Destroy; override;

    [MVCPath('')]
    [MVCHTTPMethod([httpGET])]
    [MVCSwagSummary('GatemaSD_Doklad', 'Vrací hlavičku dokladů SD Serveru', 'GatemaSDDokladyGetAll')]
    [MVCSwagParam(plQuery, 'od', 'Datum od ve formátu RRRRMMDD', ptString, false)]
    procedure GetAll([MVCFromQueryString('od', '')] odDatum: string=''
                    );

    [MVCPath('/meta')]
    [MVCHTTPMethod([httpGET])]
    [MVCSwagSummary('GatemaSD_Doklad', 'Vrací metadata', 'GatemaSDDokladyGetMeta')]
    procedure GetMeta;

    [MVCPath('/($id)')]
    [MVCHTTPMethod([httpGET])]
    [MVCSwagSummary('GatemaSD_Doklad', 'Vrací data dokladu SD Serveru', 'GatemaSDDokladyGetByID')]
    [MVCSwagParam(plPath, 'id', 'ID dokladu', ptString, false)]
    [MVCSwagParam(plQuery, 'typdokl', 'Typ dokladu', ptString, false)]
    [MVCSwagParam(plQuery, 'druhpohybu', 'Druh pohybu zboží', ptString, false)]
    [MVCSwagParam(plQuery, 'sklad', 'Sklad', ptString, false)]
    [MVCSwagParam(plQuery, 'format', 'Formát (1=dokladOZ)', ptString, false)]
    [MVCSwagParam(plQuery, 'od', 'Datum od ve formátu RRRRMMDD', ptString, false)]
    [MVCSwagParam(plQuery, 'do', 'Datum do ve formátu RRRRMMDD', ptString, false)]
    [MVCSwagParam(plQuery, 'seznam', '1=jen seznam se základními údaji', ptString, false)]
    [MVCSwagResponses(200, 'Success', TGatemaSDDoklad, true)]
    procedure GetByID (id: string='';
                       [MVCFromQueryString('typdokl', '')] typDokl: string='';
                       [MVCFromQueryString('druhpohybu', '')] druhPohybu: string='';
                       [MVCFromQueryString('sklad', '')] sklad: string='';
                       [MVCFromQueryString('format', '')] formatX: string='';
                       [MVCFromQueryString('od', '')] odDatum: string='';
                       [MVCFromQueryString('do', '')] doDatum: string='';
                       [MVCFromQueryString('seznam', '')] jenSeznam: string='0'
                      );

  end;



  [MVCPath('/sdserver/scandata')]
  TGatemaSDScanDataController = class(TBaseController)
   strict private
    FSelfSvc: TGatemaSDScanDataService;
   strict protected
    function GetGatemaSDScanDataService: TGatemaSDScanDataService;
   public
    destructor Destroy; override;

    [MVCPath('/meta')]
    [MVCHTTPMethod([httpGET])]
    [MVCSwagSummary('GatemaSD_ScanData', 'Vrací metadata', 'GatemaSDScanDataGetMeta')]
    procedure GetMeta;

    [MVCPath('/($id)')]
    [MVCHTTPMethod([httpGET])]
    [MVCSwagSummary('GatemaSD_ScanData', 'Vrací naskenovaná data', 'GatemaSDScanDataGetByID')]
    [MVCSwagParam(plPath, 'id', 'ID scan data', ptString, false)]
    [MVCSwagParam(plQuery, 'iddoklad', 'ID dokladu SD', ptString, false)]
    [MVCSwagResponses(200, 'Success', TGatemaSDScanData, true)]
    procedure GetByID(id: string='';
                      [MVCFromQueryString('iddoklad', '')] iddoklad: string=''
                     );
  end;




implementation
uses
 FireDAC.Stan.Option,
 FireDAC.Comp.Client,
 FireDAC.Stan.Param,
 MVCFramework.FireDAC.Utils,
 MVCFramework.DataSet.Utils,
 System.StrUtils,
 System.SysUtils,
 uDataMod;

const
 selSloupce = '';



{ TGatemaSDDokladController }

 destructor TGatemaSDDokladController.Destroy;
  begin
   FSelfSvc.Free;
   inherited;
  end;


 function TGatemaSDDokladController.GetGatemaSDDokladService: TGatemaSDDokladService;
  begin
   inherited;
   if not Assigned(FSelfSvc) then
    FSelfSvc:= TGatemaSDDokladService.Create(datMod);
   result:= FSelfSvc;
  end;




 procedure TGatemaSDDokladController.GetAll(odDatum: string='');
  var params: TDictionary<string, string>;
//      odDatum: string;
  begin
   params:= TDictionary<string, string>.Create;
   odDatum:= sanitizeSQLString(odDatum);
   if (odDatum<>'') then
    params.Add('odDatum', odDatum);

   try
     try
       Render(ObjectDict().Add('data', GetGatemaSDDokladService.GetByParams(params))); // viz uSvcCustom.pas
      except
       on E: EServiceException do
        begin
         raise EMVCException.Create(E.Message, '', 0, 404);
        end;
       on E:Exception do
        begin
         raise EMVCException.Create(E.Message, '', 0, 404);
        end;
     end;
    finally
     params.Free;
   end;
  end;




 procedure TGatemaSDDokladController.GetByID(id: string=''; typDokl: string=''; druhPohybu: string=''; sklad: string=''; formatX: string=''; odDatum: string=''; doDatum: string='';
                                             jenSeznam: string='0');
  var iId: integer;
      params: TDictionary<string, string>;
  begin
   params:= TDictionary<string, string>.Create;

   iId:= 0;
   id:= sanitizeSQLString(id);
   if (id<>'') then
    if not(TryStrToInt(id, iId)) then
     iId:= 0;
   if (iId>0) then
    params.Add('id', iId.ToString);

   typDokl:= sanitizeSQLString(sklad);
   if (typDokl<>'') then
    params.Add('typDokladu', typDokl);

   druhPohybu:= sanitizeSQLString(sklad);
   if (druhPohybu<>'') then
    params.Add('druhPohybu', druhPohybu);

   formatX:= sanitizeSQLString(formatX); // pro zobrazeni jako DokladOZ
   if (formatX<>'') then
    if (formatX='1') then
     params.Add('format', formatX);

   sklad:= sanitizeSQLString(sklad);
   if (sklad<>'') then
    params.Add('sklad', sklad);

   odDatum:= sanitizeSQLString(odDatum);
   if (odDatum<>'') then
    params.Add('odDatum', odDatum);

   doDatum:= sanitizeSQLString(doDatum);
   if (doDatum<>'') then
    params.Add('doDatum', doDatum);

   jenSeznam:= sanitizeSQLString(jenSeznam);
   if (jenSeznam='1') then
    params.Add('seznam', '1');

   try
     try
       Render(ObjectDict().Add('data', GetGatemaSDDokladService.GetByParams(params))); // viz uSvcCustom.pas
      except
       on E: EServiceException do
        begin
         raise EMVCException.Create(E.Message, '', 0, 404);
        end;
       on E:Exception do
        begin
         raise EMVCException.Create(E.Message, '', 0, 404);
        end;
     end;
    finally
     params.Free;
   end;
  end;




 procedure TGatemaSDDokladController.GetMeta;
  begin
   try
     Render(ObjectDict().Add('data', GetGatemaSDDokladService.GetMeta));
    except
     on E: EServiceException do
     begin
       raise EMVCException.Create(E.Message, '', 0, 404);
     end;
     on E:Exception do
      begin
       raise EMVCException.Create(E.Message, '', 0, 404);
      end;
   end;
  end;





{ TGatemaSDScanDataController }

 destructor TGatemaSDScanDataController.Destroy;
  begin
   FSelfSvc.Free;
  end;

 function TGatemaSDScanDataController.GetGatemaSDScanDataService: TGatemaSDScanDataService;
  begin
   inherited Create;
   if not Assigned(FSelfSvc) then
    FSelfSvc:= TGatemaSDScanDataService.Create(datMod);
   result:= FSelfSvc;
  end;



 procedure TGatemaSDScanDataController.GetByID(id: string=''; iddoklad: string='');
  var iId: integer;
      params: TDictionary<string, string>;
  begin
   params:= TDictionary<string, string>.Create;

   iId:= 0;
   id:= sanitizeSQLString(id);
   if (id<>'') then
    if not(TryStrToInt(id, iId)) then
     iId:= 0;
   if (iId>0) then
    params.Add('id', iId.ToString);

   try
     Render(ObjectDict().Add('data', GetGatemaSDScanDataService.GetByParams(params))); // viz uSvcCustom.pas
    except
     on E: EServiceException do
      begin
       raise EMVCException.Create(E.Message, '', 0, 404);
      end;
     on E:Exception do
      begin
       raise EMVCException.Create(E.Message, '', 0, 404);
      end;
   end;
  end;



 procedure TGatemaSDScanDataController.GetMeta;
  begin
   try
     Render(ObjectDict().Add('data', GetGatemaSDScanDataService.GetMeta));
   except
     on E: EServiceException do
     begin
       raise EMVCException.Create(E.Message, '', 0, 404);
     end;
     on E:Exception do
      begin
       raise EMVCException.Create(E.Message, '', 0, 404);
      end;
   end;
  end;

end.
