Innovenergy_trunk/csharp/app/Backend/Database/Folder.cs

83 lines
1.8 KiB
C#
Raw Normal View History

2023-02-16 12:57:06 +00:00
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 folder)
{
return Folders.Where(f => f.ParentId == f.Id);
}
public IEnumerable<Folder> GetDescendantFolders(Folder folder)
{
return folder.Traverse(GetChildFolders);
}
public IEnumerable<Installation> GetChildInstallations(Folder folder)
{
return Installations.Where(f => f.ParentId == f.Id);
}
public Result CreateFolder(Folder folder)
{
2023-02-16 14:08:50 +00:00
return Create(folder);
2023-02-16 12:57:06 +00:00
}
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)
{
User2Folder
.Where(f => f.FolderId == folder.Id)
.Delete();
2023-02-16 15:03:00 +00:00
// TODO: delete descendants? Here they are just adopted one level up
2023-02-16 12:57:06 +00:00
foreach (var l in Installations
.Where(i => i.ParentId == folder.Id))
{
ChangeParent(l, folder.ParentId);
}
return Delete(folder);
}
}