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

107 lines
2.7 KiB
C#
Raw Normal View History

2023-03-08 12:20:33 +00:00
using InnovEnergy.App.Backend.Model;
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
2023-03-13 10:36:50 +00:00
public static partial class Db
2023-02-16 12:57:06 +00:00
{
2023-03-13 10:36:50 +00:00
private static TableQuery<Folder> Folders => _Db.Table<Folder>();
2023-02-16 12:57:06 +00:00
2023-03-13 10:36:50 +00:00
public static Int32 NbFolders => Folders.Count();
2023-02-16 12:57:06 +00:00
2023-03-13 10:36:50 +00:00
public static Folder? GetFolderById(Int64 id)
2023-02-16 12:57:06 +00:00
{
return Folders.FirstOrDefault(u => u.Id == id);
// if (folder is null)
// return null;
//return PopulateDescendants(folder);
}
2023-03-13 10:36:50 +00:00
public static 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
}
2023-03-13 10:36:50 +00:00
public static IEnumerable<Installation> GetChildInstallations(Folder parent)
{
return Installations.Where(f => f.ParentId == parent.Id);
}
2023-02-16 12:57:06 +00:00
2023-03-13 10:36:50 +00:00
public static 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
}
2023-03-13 10:36:50 +00:00
public static Boolean IsDescendantOf(Folder folder, Int64 ancestorFolderId)
{
return Ancestors(folder)
.Any(u => u.Id == ancestorFolderId);
}
2023-02-16 12:57:06 +00:00
2023-03-13 10:36:50 +00:00
public static Boolean IsDescendantOf(Folder folder, Folder ancestor)
2023-02-16 12:57:06 +00:00
{
2023-03-13 10:36:50 +00:00
return IsDescendantOf(folder, ancestor.Id);
2023-02-16 12:57:06 +00:00
}
2023-03-13 10:36:50 +00:00
private static IEnumerable<Folder> Ancestors(Folder child)
2023-02-24 11:58:47 +00:00
{
2023-03-13 10:36:50 +00:00
return child.Unfold(GetParent);
2023-02-24 11:58:47 +00:00
}
2023-03-13 10:36:50 +00:00
public static Folder? GetParent(Folder f)
2023-02-24 11:58:47 +00:00
{
2023-03-13 10:36:50 +00:00
return IsRoot(f)
? null
: GetFolderById(f.ParentId);
}
public static Boolean IsRoot(Folder f)
{
return f.ParentId == 0; // root has ParentId 0 by definition
2023-02-24 11:58:47 +00:00
}
2023-02-16 12:57:06 +00:00
2023-03-13 10:36:50 +00:00
public static Int64 CreateFolder(Folder folder)
2023-02-16 12:57:06 +00:00
{
2023-02-16 14:08:50 +00:00
return Create(folder);
2023-02-16 12:57:06 +00:00
}
2023-03-13 10:36:50 +00:00
public static Boolean UpdateFolder(Folder folder)
2023-02-16 12:57:06 +00:00
{
// TODO: no circles in path
return Update(folder);
}
2023-03-13 10:36:50 +00:00
// These should not be necessary, just Update folder/installation with new parentId
// public Boolean ChangeParent(Installation child, Int64 parentId)
// {
// child.ParentId = parentId;
// return UpdateInstallation(child);
// }
//
// public Boolean ChangeParent(Folder child, Int64 parentId)
// {
// child.ParentId = parentId;
// return UpdateFolder(child);
// }
2023-02-16 12:57:06 +00:00
2023-03-13 10:36:50 +00:00
public static Boolean DeleteFolder(Folder folder)
2023-02-16 12:57:06 +00:00
{
// 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);
}
}