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

88 lines
2.1 KiB
C#
Raw Normal View History

2023-03-08 12:20:33 +00:00
using InnovEnergy.App.Backend.Model;
using InnovEnergy.App.Backend.Utils;
2023-02-16 12:57:06 +00:00
using InnovEnergy.Lib.Utils;
using SQLite;
2023-03-08 12:20:33 +00:00
namespace InnovEnergy.App.Backend.Database;
2023-02-16 12:57:06 +00:00
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)
2023-02-16 12:57:06 +00:00
{
return Folders.Where(f => f.ParentId == parent.Id);
2023-02-16 12:57:06 +00:00
}
public IEnumerable<Folder> GetDescendantFolders(Folder parent)
2023-02-16 12:57:06 +00:00
{
return parent.Traverse(GetChildFolders);
2023-02-16 12:57:06 +00:00
}
public IEnumerable<Installation> GetChildInstallations(Folder parent)
2023-02-16 12:57:06 +00:00
{
return Installations.Where(f => f.ParentId == parent.Id);
2023-02-16 12:57:06 +00:00
}
2023-02-24 11:58:47 +00:00
public IEnumerable<User> GetChildUsers(User parent)
{
return Users.Where(f => f.ParentId == parent.Id);
}
public IEnumerable<User> GetDescendantUsers(User parent)
{
return parent.Traverse(GetChildUsers);
}
2023-02-16 12:57:06 +00:00
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)
{
// 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);
2023-02-16 12:57:06 +00:00
return Delete(folder);
}
}