80 lines
1.8 KiB
C#
80 lines
1.8 KiB
C#
using Backend.Model;
|
|
using Backend.Utils;
|
|
using InnovEnergy.Lib.Utils;
|
|
using SQLite;
|
|
|
|
namespace Backend.Database;
|
|
|
|
public partial class Db
|
|
{
|
|
private TableQuery<Folder> Folders => _Db.Table<Folder>();
|
|
|
|
public Int32 NbFolders => Folders.Count();
|
|
|
|
public Folder? GetFolderById(Int64 id)
|
|
{
|
|
return Folders.FirstOrDefault(u => u.Id == id);
|
|
|
|
// if (folder is null)
|
|
// return null;
|
|
|
|
//return PopulateDescendants(folder);
|
|
}
|
|
|
|
public IEnumerable<Folder> GetChildFolders(Folder parent)
|
|
{
|
|
return Folders.Where(f => f.ParentId == parent.Id);
|
|
}
|
|
|
|
|
|
public IEnumerable<Folder> GetDescendantFolders(Folder parent)
|
|
{
|
|
return parent.Traverse(GetChildFolders);
|
|
}
|
|
|
|
public IEnumerable<Installation> GetChildInstallations(Folder parent)
|
|
{
|
|
return Installations.Where(f => f.ParentId == parent.Id);
|
|
}
|
|
|
|
|
|
public Result CreateFolder(Folder folder)
|
|
{
|
|
return Create(folder);
|
|
}
|
|
|
|
public Result UpdateFolder(Folder folder)
|
|
{
|
|
// TODO: no circles in path
|
|
|
|
return Update(folder);
|
|
}
|
|
|
|
public Result ChangeParent(Installation child, Int64 parentId)
|
|
{
|
|
child.ParentId = parentId;
|
|
return UpdateInstallation(child);
|
|
}
|
|
|
|
public Result ChangeParent(Folder child, Int64 parentId)
|
|
{
|
|
child.ParentId = parentId;
|
|
return UpdateFolder(child);
|
|
}
|
|
|
|
public Result DeleteFolder(Folder folder)
|
|
{
|
|
// Delete direct children
|
|
User2Folder .Delete(f => f.FolderId == folder.Id);
|
|
Installations.Delete(i => i.ParentId == folder.Id);
|
|
|
|
// recursion
|
|
Folders.Where(f => f.ParentId == folder.Id)
|
|
.ForEach(DeleteFolder);
|
|
|
|
return Delete(folder);
|
|
}
|
|
|
|
}
|
|
|