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

101 lines
2.8 KiB
C#

using InnovEnergy.App.Backend.Relations;
namespace InnovEnergy.App.Backend.Database;
public static partial class Db
{
public static void CreateFakeRelations()
{
Connection.RunInTransaction(() =>
{
CreateFakeUserTree();
CreateFakeFolderTree();
LinkFakeInstallationsToFolders();
GiveFakeUsersAccessToFolders();
GiveFakeUsersAccessToInstallations();
});
}
private static void CreateFakeUserTree()
{
foreach (var userId in Enumerable.Range(1, Users.Count()))
{
var user = GetUserById(userId);
if (user is null)
continue;
user.ParentId = userId > 1
? Random.Shared.Next(userId - 1) + 1
: 0; // root has parentId 0
Update(user);
}
}
private static void CreateFakeFolderTree()
{
foreach (var folderId in Enumerable.Range(1, Folders.Count()))
{
var folder = GetFolderById(folderId);
if (folder is null)
continue;
folder.ParentId = folderId > 1
? Random.Shared.Next(folderId - 1) + 1
: 0; // root has parentId 0
Update(folder);
}
}
private static void LinkFakeInstallationsToFolders()
{
var nFolders = Folders.Count();
foreach (var installation in Installations)
{
installation.ParentId = Random.Shared.Next(nFolders) + 1;
Update(installation);
}
}
private static void GiveFakeUsersAccessToFolders()
{
foreach (var uf in FolderAccess) // remove existing relations
Connection.Delete(uf);
var nFolders = Folders.Count();
var nUsers = Users.Count();
foreach (var user in Users)
while (Random.Shared.Next((Int32)(nUsers - user.Id + 1)) != 0)
{
var relation = new FolderAccess
{
UserId = user.Id,
FolderId = Random.Shared.Next(nFolders) + 1
};
Connection.Insert(relation);
}
}
private static void GiveFakeUsersAccessToInstallations()
{
foreach (var ui in InstallationAccess) // remove existing relations
Connection.Delete(ui);
var nbInstallations = Installations.Count();
foreach (var user in Users)
while (Random.Shared.Next(5) != 0)
{
var relation = new InstallationAccess
{
UserId = user.Id,
InstallationId = Random.Shared.Next(nbInstallations) + 1
};
Connection.Insert(relation);
}
}
//TODO fake OrderNumbers
}