unit uCtrlZamestnanci;

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('/zamestnanec')]
  [MVCSwagSummary('Zaměstnanec', 'Zaměstnanci', '')]
  TZamestnanciController = class(TBaseController)
  public
    [MVCPath('/meta')]
    [MVCHTTPMethod([httpGET])]
    [MVCSwagSummary('Zaměstnanec', 'Vratí metadata', 'ZamestnanciGetMeta')]
    procedure GetMeta;

//    [MVCDoc('Vrati udaje zamestnance dle jeho ID')]
    [MVCPath('/($id)')]
    [MVCHTTPMethod([httpGET])]
    [MVCSwagSummary('Zaměstnanec', 'Vratí detail zaměstnance (jen jméno/příjmení/tituly/středisko/zakázka)', 'ZamestnanciGetByID')]
    [MVCSwagParam(plPath, 'id', 'ID zaměstnance', ptString, true)]
    [MVCSwagParam(plQuery, 'cislo', 'Číslo zaměstnance', ptString, false)]
    [MVCSwagParam(plQuery, 'prijm', 'Příjmení zaměstnance', ptString, false)]
    [MVCSwagParam(plQuery, 'vestavu', '1=zaměstnanci ve stavu', ptString, false)]
    [MVCSwagParam(plQuery, 'kodcipu', 'Kód čipu zaměstnance (max 36 znaků)', ptString, false)]
    [MVCSwagResponses(200, 'Success', TZamestnanec)]
    procedure GetByID (id: string='';
                       [MVCFromQueryString('cislo', '')] cislo: string='';
                       [MVCFromQueryString('prijm', '')] prijm: string='';
                       [MVCFromQueryString('vestavu', '')] veStavu: string='';
                       [MVCFromQueryString('kodcipu', '')] kodCipu: string=''
                      );

    [MVCPath('')]
    [MVCHTTPMethod([httpGET])]
    [MVCSwagSummary('Zaměstnanec', 'Vratí seznam zaměstnanců', 'ZamestnanciGetAll')]
    [MVCSwagParam(plQuery, 'vestavu', '1=jen zaměstnance ve stavu', ptString, false)]
    [MVCSwagResponses(200, 'Success', TZamestnanec, true)]
    procedure GetAll ([MVCFromQueryString('vestavu', '')] veStavu: string=''
                     );
  end;


implementation


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



 procedure TZamestnanciController.GetAll (veStavu: string= '');
  var idZam, cisZam: integer;
      params: TDictionary<string, string>;
  begin

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

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


   try
     Render(ObjectDict().Add('data', GetZamestnanciService.GetByParams (params)))
    except
     RenderStatusMessage (200);
{
     on E: EServiceException do
      begin
       raise EMVCException.Create(E.Message, '', 0, 200);
      end
     else
       raise;
}
   end;

   params.Free;

  end;





 procedure TZamestnanciController.GetByID (id: string=''; cislo: string=''; prijm: string=''; veStavu: string=''; kodCipu: string='');
  var idZam, cisZam: integer;
      params: TDictionary<string, string>;
  begin

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

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

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

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

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

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


   try
     Render(ObjectDict().Add('data', GetZamestnanciService.GetByParams (params)))
    except
     RenderStatusMessage (200);
{
     on E: EServiceException do
      begin
       raise EMVCException.Create(E.Message, '', 0, 200);
      end
     else
       raise;
}
   end;

   params.Free;

  end;



 procedure TZamestnanciController.GetMeta;
  begin
    try
      Render(ObjectDict().Add('data', GetZamestnanciService.GetMeta));
     except
      RenderStatusMessage (200);
{
      on E: EServiceException do
      begin
        raise EMVCException.Create(E.Message, '', 0, 200);
      end
      else
        raise;
}
    end;
  end;


end.

