unit uDeleteServer;
{< will delete items from dataBase by ID and files
 and folders from disk}

interface

uses
  System.SysUtils, System.Classes,System.IOUtils;

type
  TDelete = class(TDataModule)
  private
    { Private declarations }

    FIDListToDelete:TStringList;

  public
    { Public declarations }

    procedure Delete(AIDList:TStringList);
    {< recusievely deleting records from database and files of records}


    property IDListToDelete:TStringList read FIDListToDelete
    write FIDListToDelete;

    constructor Create(AOwner: TComponent); override;

  end;

//var
//  Delete: TDelete;

implementation

{%CLASSGROUP 'Vcl.Controls.TControl'}

{$R *.dfm}


uses uHTTPServer_Main;

var HTTPServer_Main_UnitVar:TfHTTPServer;


{ TDelete }

constructor TDelete.Create(AOwner: TComponent);
begin
  inherited;


if (AOwner is TfHTTPServer) then HTTPServer_Main_UnitVar:=(AOwner as TfHTTPServer);




end;

procedure TDelete.Delete(AIDList: TStringList);
var
  i: Integer;

  IDChildrenList:TStringList;
  j: Integer;

  currentAbsAdress:string;
  currentID:integer;

begin

IDChildrenList:=TStringList.Create;


    for i := AIDList.Count-1  downto 0 do begin

    //check by DataBase if element has children,

     with HTTPServer_Main_UnitVar.DataBaseUnitServer.qSelectChildren do begin

     params.ParamValues['idParent']:=AIDList[i];

     Open();

      while not EOF do begin

      IDChildrenList.Add( FieldByName('id').AsInteger.ToString() );


      Next();
      end;

     Close();
     end;


  //Delete all children and itself


     //Delete files or folders

     currentID:=AIDList[i].ToInteger();

     currentAbsAdress:=HTTPServer_Main_UnitVar.
     DataBaseUnitServer.GetAbsAdressByID(currentID);

           // if isFolder by DataBase
           if HTTPServer_Main_UnitVar.
           DataBaseUnitServer.IsFolder(AIDList[i].ToInteger()) then

           begin
           if TDirectory.Exists(currentAbsAdress) then
           TDirectory.Delete(currentAbsAdress,true)
           end

             else

           //if isFile by DataBase
           if TFile.Exists(currentAbsAdress) then
           TFile.Delete(currentAbsAdress);


         //Delete records from database

          HTTPServer_Main_UnitVar.
          DataBaseUnitServer.
          DeleteRecord(AIDList[i].ToInteger());


   // Recursion
  //if Yes - recursievely call Delete for all children
   if IDChildrenList.Count>0 then Delete(IDChildrenList);


  end;


FreeAndNil(IDChildrenList);

end;



end.
