unit uCtrlObecne;

interface
{$I 'GlobalDefs.inc'}

uses
  System.Generics.Collections,
  mvcframework,
  mvcframework.Commons,
  mvcframework.Serializer.Commons,
  uCtrlBase,
  uHeoObj_Base
{$IFDEF SWAGGER}
  , MVCFramework.Swagger.Commons
{$ENDIF}
;


const
{$I globalConsts.inc}

type
  [MVCPath('/aktivity')]
  TAktivitaController = class(TBaseController)
  public
    [MVCPath('/meta')]
    [MVCHTTPMethod([httpGET])]
    [MVCSwagSummary('Aktivity', 'Vrátí metadata', 'AktivityGetMeta')]
    procedure GetMeta;

    [MVCPath('/($id)')]
    [MVCHTTPMethod([httpGET])]
//    [MVCProduces('application/json')]
    [MVCSwagSummary('Aktivity', 'Vratí detail aktivity', 'AktivityGetByID')]
    [MVCSwagParam(plPath, 'id', 'ID aktivity', ptString, true)]
    [MVCSwagParam(plQuery, 'kateg', 'Kategorie', ptString, false)]
    [MVCSwagParam(plQuery, 'od', 'Datum od ve formátu RRRRMMDD', ptString, false)]
    [MVCSwagParam(plQuery, 'do', 'Datum do ve formátu RRRRMMDD', ptString, false)]
    [MVCSwagResponses(200, 'Success', TAktivita, false)]
    procedure GetByID (id: string='';
                       [MVCFromQueryString('kateg', '')] kateg: string='';
                       [MVCFromQueryString('od', '')] odDatum: string='';
                       [MVCFromQueryString('do', '')] doDatum: string=''
                      );

    [MVCPath('')]
    [MVCHTTPMethod([httpGET])]
    [MVCSwagSummary('Aktivity', 'Vratí seznam aktivit', 'GetAll_Aktivity')]
    [MVCSwagParam(plQuery, 'kateg', 'Kategorie', ptString, false)]
    [MVCSwagParam(plQuery, 'od', 'Datum od ve formátu RRRRMMDD', ptString, false)]
    [MVCSwagParam(plQuery, 'do', 'Datum do ve formátu RRRRMMDD', ptString, false)]
    [MVCSwagResponses(200, 'Success', TAktivita, true)]
    procedure GetAll ([MVCFromQueryString('kateg', '')] kateg: string='';
                      [MVCFromQueryString('od', '')] odDatum: string='';
                      [MVCFromQueryString('do', '')] doDatum: string=''
                     );
  end;




  [MVCPath('/ukoly')]
  TUkolAktivityController = class(TBaseController)
  public
    [MVCPath('/meta')]
    [MVCHTTPMethod([httpGET])]
    [MVCSwagSummary('Úkoly', 'Vrátí metadata', 'UkolAktivityGetMeta')]
    procedure GetMeta;

    [MVCPath('/($id)')]
    [MVCHTTPMethod([httpGET])]
//    [MVCProduces('application/json')]
    [MVCSwagSummary('Úkoly', 'Vratí detail úkolu', 'UkolAktivityGetByID')]
    [MVCSwagParam(plPath, 'id', 'ID úkolu', ptString, true)]
    [MVCSwagResponses(200, 'Success', TUkolAktivity, false)]
    procedure GetByID (id: string='');

    [MVCPath('')]
    [MVCHTTPMethod([httpGET])]
    [MVCSwagSummary('Úkoly', 'Vratí seznam úkolů', 'UkolAktivityGetAll')]
    [MVCSwagParam(plQuery, 'idkj', 'ID aktivity', ptString, false)]
    [MVCSwagResponses(200, 'Success', TUkolAktivity, true)]
    procedure GetAll ([MVCFromQueryString('idkj', '')] sIdKj: string='0'
                     );
  end;





  [MVCPath('/dokumenty')]
  [MVCSwagSummary('Dokumenty', 'Dokumenty', '')]
  TDokumentController = class(TBaseController)
  public
    [MVCPath('/meta')]
    [MVCHTTPMethod([httpGET])]
    [MVCSwagSummary('Dokument', 'Vrátí metadata', 'DokumentyGetMeta')]
    procedure GetMeta;

    [MVCPath('/strom')]
    [MVCHTTPMethod([httpGET])]
    [MVCSwagSummary('Dokument', 'Vrátí stromovou strukturu dokumentu', 'DokumentyGetDokumStrom')]
    procedure GetDokumStrom;


    [MVCPath('/($id)')]
    [MVCHTTPMethod([httpGET])]
//    [MVCProduces('application/json')]
    [MVCSwagSummary('Dokument', 'Vratí dokument', 'DokumentyGetByID')]
    [MVCSwagParam(plPath, 'id', 'ID dokumentu', ptString, true)]
    [MVCSwagParam(plQuery, 'jenHlavicka', '1=jen hlavičku (bez obsahu dokumetu)', ptString, false)]
    [MVCSwagResponses(200, 'Success', TDokument, true)]
    procedure GetByID (id: string='';
                       [MVCFromQueryString('jenHlavicka', '1')] jenHlavicka: string='1'
                      );

    [MVCPath('')]
    [MVCHTTPMethod([httpGET])]
    [MVCSwagSummary('Dokument', 'Vratí dokument', 'DokumentyGetAll')]
    [MVCSwagParam(plQuery, 'jenHlavicka', '1=jen hlavičku (bez obsahu dokumetu)', ptString, false)]
    [MVCSwagParam(plQuery, 'pripona', 'Přípona názvu souboru dokumetu', ptString, false)]
    [MVCSwagParam(plQuery, 'iddokstrom', 'ID stromové struktury dokumetu', ptString, false)]
    [MVCSwagParam(plQuery, 'iddoktyp', 'ID typu dokumetu', ptString, false)]
    [MVCSwagResponses(200, 'Success', TDokument, true)]
    procedure GetAll ([MVCFromQueryString('jenHlavicka', '1')] jenHlavicka: string='1';
                      [MVCFromQueryString('pripona', '')] pripona: string='';
                      [MVCFromQueryString('iddokstrom', '')] idDokStrom: string='';
                      [MVCFromQueryString('iddoktyp', '')] idDokTyp: string=''
                     );
  end;




  [MVCPath('/ciselnik/plankalendar')]
  TPlanKalendarController = class(TBaseController)
  public
    [MVCPath('/meta')]
    [MVCHTTPMethod([httpGET])]
    [MVCSwagSummary('Plánovací kalendář', 'Vrátí metadata', 'TPVPlanKalendarGetMeta')]
    procedure GetMeta;

    [MVCPath('/($id)')]
    [MVCHTTPMethod([httpGET])]
//    [MVCProduces('application/json')]
    [MVCSwagSummary('Plánovací kalendář', 'Vratí plánovací kalendář', 'TPVPlanKalendarGetByID')]
    [MVCSwagParam(plPath, 'id', 'ID plánovacího kalendáře', ptString, true)]
    [MVCSwagParam(plQuery, 'polozky', '1=vrátí kalendář včetně položek', ptString, false)]
    [MVCSwagParam(plQuery, 'idstroj', 'ID stroje', ptString, false)]
    [MVCSwagParam(plQuery, 'idkooperace', 'ID kooperace', ptString, false)]
    [MVCSwagParam(plQuery, 'od', 'Datum od ve formátu RRRRMMDD / RRRRMMDDHHMM', ptString, false)]
    [MVCSwagParam(plQuery, 'do', 'Datum do ve formátu RRRRMMDD / RRRRMMDDHHMM', ptString, false)]
    [MVCSwagResponses(200, 'Success', TPlanKalendar, true)]
    procedure GetByID (id: string='';
                       [MVCFromQueryString('polozky', '')] polozky: string='';
                       [MVCFromQueryString('od', '')] odDatum: string='';
                       [MVCFromQueryString('do', '')] doDatum: string='';
                       [MVCFromQueryString('idstroj', '')] idstroj: string='';
                       [MVCFromQueryString('idkooperace', '')] idkooperace: string=''
                      );

    [MVCPath('')]
    [MVCHTTPMethod([httpGET])]
    [MVCSwagSummary('Plánovací kalendář', 'Vratí dokument', 'TPVPlanKalendarGetAll')]
    [MVCSwagParam(plQuery, 'oznaceni', 'Označení kalendáře', ptString, false)]
    [MVCSwagResponses(200, 'Success', TPlanKalendar, true)]
    procedure GetAll ([MVCFromQueryString('oznaceni', '')] oznaceni: string='');

  end;

implementation


uses
  uSvc_Base,
  uCommons,
  mvcframework.Serializer.Intf,
  System.RegularExpressions,
  System.StrUtils,
  System.SysUtils;


{ TDokumentController }

 procedure TDokumentController.GetMeta;
  begin
    try
      Render(ObjectDict().Add('data', GetDokumentService.GetMeta));
     except
{$IFDEF NORENDER400}
      RenderStatusMessage (200);
{$ELSE}
      on E: EServiceException do
      begin
        raise EMVCException.Create(E.Message, '', 0, 200);
      end
      else
        raise;
{$ENDIF}
    end;
  end;





 procedure TDokumentController.GetDokumStrom;
  begin
    try
      Render(ObjectDict().Add('data', GetDokumentService.GetDokumStrom));
     except
{$IFDEF NORENDER400}
      RenderStatusMessage (200);
{$ELSE}
      on E: EServiceException do
      begin
        raise EMVCException.Create(E.Message, '', 0, 200);
      end
      else
        raise;
{$ENDIF}
    end;
  end;





 procedure TDokumentController.GetAll (jenHlavicka: string='1'; pripona: string=''; idDokStrom: string=''; idDokTyp: string='');
  var  params: TDictionary<string, string>;
       i1, i2: integer;
  begin
   params:= TDictionary<string, string>.Create;

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

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

   i1:= 0;
   idDokStrom:= sanitizeSQLString(idDokStrom);
   if not(TryStrToInt(idDokStrom, i1)) then
    i1:= 0;
   if (i1>0) then
    params.Add('iddokstrom', i1.ToString);

   i2:= 0;
   idDokTyp:= sanitizeSQLString(idDokTyp);
   if not(TryStrToInt(idDokTyp, i2)) then
    i2:= 0;
   if (i2>0) then
    params.Add('iddoktyp', i2.ToString);


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

   params.Free;
  end;




 procedure TDokumentController.GetByID (id: string=''; jenHlavicka: string='1');
 var iId: integer;
     h: boolean;
  begin

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

   h:= false;
   if (jenHlavicka='1') then
    h:= true;

   try
     Render(ObjectDict().Add('data', GetDokumentService.GetByID (iId, h)))
    except
{$IFDEF NORENDER400}
     RenderStatusMessage (200);
{$ELSE}
     on E: EServiceException do
      begin
       raise EMVCException.Create(E.Message, '', 0, 200);
      end
     else
       raise;
{$ENDIF}
   end;
  end;






{ TPlanKalendarController }
 procedure TPlanKalendarController.GetMeta;
  begin
    try
      Render(ObjectDict().Add('data', GetPlanKalendarService.GetMeta));
     except
{$IFDEF NORENDER400}
      RenderStatusMessage (200);
{$ELSE}
      on E: EServiceException do
      begin
        raise EMVCException.Create(E.Message, '', 0, 200);
      end
      else
        raise;
{$ENDIF}
    end;
  end;



 procedure TPlanKalendarController.GetByID (id: string=''; polozky: string=''; odDatum: string=''; doDatum: string=''; idStroj: string=''; idKooperace: string='');
 var iId, iIdStroj, iIdKooperace: integer;
     bPolozky: boolean;
     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);

   iIdStroj:= 0;
   idStroj:= sanitizeSQLString(idStroj);
   if (idStroj<>'') then
    if not(TryStrToInt(idStroj, iIdStroj)) then
     iIdStroj:= 0;
   if (iIdStroj>0) then
    params.Add('idstroj', iIdStroj.ToString);

   iIdKooperace:= 0;
   idKooperace:= sanitizeSQLString(idKooperace);
   if (idKooperace<>'') then
    if not(TryStrToInt(idKooperace, iIdKooperace)) then
     iIdKooperace:= 0;
   if (iIdKooperace>0) then
    params.Add('idkooperace', iIdKooperace.ToString);

   bPolozky:= False;
   polozky:= sanitizeSQLString(polozky);
   if (polozky='1') then
    bPolozky:= true;
   if (bPolozky) then
    params.Add('polozky', '1');

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

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

   try
     try
//       Render(ObjectDict().Add('data', GetPlanKalendarService.GetByParams (params)) );
       Render(ObjectDict().Add('data', GetPlanKalendarService.GetByID (iId, bPolozky, params)))
      except
{$IFDEF NORENDER400}
       RenderStatusMessage (200);
{$ELSE}
       on E: EServiceException do
        begin
         raise EMVCException.Create(E.Message, '', 0, 200);
        end
       else
         raise;
{$ENDIF}
     end;
    finally
     params.Free;
   end;
  end;



 procedure TPlanKalendarController.GetAll (oznaceni: string='');
  var params: TDictionary<string, string>;
  begin
   params:= TDictionary<string, string>.Create;

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

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

   params.Free;
  end;







{ TUkolAktivityController }
 procedure TUkolAktivityController.GetMeta;
  begin
    try
      Render(ObjectDict().Add('data', GetUkolAktivityService.GetMeta));
     except
{$IFDEF NORENDER400}
      RenderStatusMessage (200);
{$ELSE}
      on E: EServiceException do
      begin
        raise EMVCException.Create(E.Message, '', 0, 200);
      end
      else
        raise;
{$ENDIF}
    end;
  end;




 procedure TUkolAktivityController.GetAll (sIdKj: string = '0');
  var idKJ: Integer;
      params: TDictionary<string, string>;
  begin
   params:= TDictionary<string, string>.Create;

   idKJ:= 0;
   sIdKj:= sanitizeSQLString(sIdKj);
   if (sIdKj<>'') then
    if not(TryStrToInt(sIdKj, idKJ)) then
     idKJ:= 0;
   if (idKJ>0) then
    params.Add('idKontaktJedn', idKJ.ToString);

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




 procedure TUkolAktivityController.GetByID (id: 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', GetUkolAktivityService.GetByParams (params) )); // viz uSvc_Obecne.pas
    except
{$IFDEF NORENDER400}
     RenderStatusMessage (200);
{$ELSE}
     on E: EServiceException do
      begin
       raise EMVCException.Create(E.Message, '', 0, 200);
      end
     else
       raise;
{$ENDIF}
   end;
  end;







{ TAktivitaController }
 procedure TAktivitaController.GetMeta;
  begin
    try
      Render(ObjectDict().Add('data', GetAktivitaService.GetMeta));
     except
{$IFDEF NORENDER400}
      RenderStatusMessage (200);
{$ELSE}
      on E: EServiceException do
      begin
        raise EMVCException.Create(E.Message, '', 0, 200);
      end
      else
        raise;
{$ENDIF}
    end;
  end;




 procedure TAktivitaController.GetAll (kateg: string = ''; odDatum: string=''; doDatum: string='');
  var params: TDictionary<string, string>;
  begin
   params:= TDictionary<string, string>.Create;

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

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

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


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




 procedure TAktivitaController.GetByID (id: string=''; kateg: string = ''; odDatum: string=''; doDatum: 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);

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

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

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



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


end.
