unit uCtrlOrganizace;

interface
{$I 'GlobalDefs.inc'}

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


const
{$I globalConsts.inc}

type
  [MVCPath('/organizace')]
  TOrganizaceController = class(TBaseController)
  public
{
    procedure GetByICODIC(const [MVCFromQueryString('ico', '')] ico: string;
                          const [MVCFromQueryString('dic', '')] dic: string
                         );
}
    [MVCPath('/meta')]
    [MVCHTTPMethod([httpGET])]
    [MVCSwagSummary('Organizace', 'Vraci metadata kmenové karty', 'OrganizaceGetMeta')]
    procedure GetMeta;

    [MVCPath]
    [MVCHTTPMethod([httpGET])]
    [MVCSwagSummary('Organizace', 'Vrací seznam všech organizací', 'OrganizaceGetAll')]
    [MVCSwagResponses(200, 'Success', TOrganizace)]
    procedure GetAll;

    [MVCPath('/($id)')]
    [MVCHTTPMethod([httpGET])]
    [MVCSwagSummary('Organizace', 'Vrací detail organizace', 'OrganizaceGetByID')]
    [MVCSwagParam(plPath, 'id', 'ID organizace', ptString, false)]
    [MVCSwagParam(plQuery, 'cislo', 'Číslo organizace', ptString, false)]
    [MVCSwagParam(plQuery, 'ico', 'IČO organizace', ptString, false)]
    [MVCSwagParam(plQuery, 'dic', 'DIČ organizace', ptString, false)]
    [MVCSwagResponses(200, 'Success', TOrganizace)]
    procedure GetByID (id: string='0';
                       [MVCFromQueryString('cislo', '')] cislo: string='';
                       [MVCFromQueryString('ico', '')] ico: string='';
                       [MVCFromQueryString('dic', '')] dic: string=''
                      );
  end;

implementation

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


{ TKmenZboziController }


 procedure TOrganizaceController.GetAll;
  begin
   try
     Render(ObjectDict().Add('data', GetOrganizaceService.GetAll));  // viz uSvc_Organizace.pas
    except
     on E: EServiceException do
      begin
       raise EMVCException.Create(E.Message, '', 0, 404);
      end
     else
       raise;
   end;
  end;


{
 procedure TOrganizaceController.GetByICODIC(const ico, dic: string);
  begin
    try
      if (dic.Trim<>'')then
          Render(ObjectDict().Add('data', GetOrganizaceService.GetByDIC(dic)))
       else
        if (ico.Trim<>'')then
          Render(ObjectDict().Add('data', GetOrganizaceService.GetByICO(ico))) // viz uSvc_Organizace.pas
         else
          Render(ObjectDict().Add('data', GetOrganizaceService.GetAll))
     except
      on E: EServiceException do
       begin
        raise EMVCException.Create(E.Message, '', 0, 404);
       end
      else
        raise;
    end;
  end;
}

 procedure TOrganizaceController.GetByID(id: string='0'; cislo: string=''; ico: string=''; dic: string='');
  var iId, iCislo: integer;
      typ: Byte;
      params: TDictionary<string, string>;
  begin

   params:= TDictionary<string, string>.Create;

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


   iCislo:= -1;
   cislo:= sanitizeSQLString(cislo);
   if not(TryStrToInt(cislo, iCislo)) then
    iCislo:= -1;
   if (iCislo>-1) then
    params.Add('cislo', iCislo.ToString);

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

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

   typ:= 0;
   if (iId>-1) then
    typ:= 0;
   if (iCislo>-1) then
    typ:= 1;
   if (ico<>'') then
    typ:= 2;
   if (dic<>'') then
    typ:= 3;

   typ:= 4;

   try
    case typ of
     0: Render(ObjectDict().Add('data', GetOrganizaceService.GetByID(iId))); // viz uSvc_Organizace.pas
     1: Render(ObjectDict().Add('data', GetOrganizaceService.GetByCislo(iCislo)));
     2: Render(ObjectDict().Add('data', GetOrganizaceService.GetByICO(ico)));
     3: Render(ObjectDict().Add('data', GetOrganizaceService.GetByDIC(dic)));
     4: Render(ObjectDict().Add('data', GetOrganizaceService.GetByParams(params)));
    end;
    except
     on E: EServiceException do
      begin
       raise EMVCException.Create(E.Message, '', 0, 404);
      end
     else
       raise;
   end;

   params.Free;

  end;



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

end.
