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

99 lines
2.6 KiB
C#
Raw Normal View History

2023-02-16 12:57:06 +00:00
using Backend.Model.Relations;
namespace Backend.Database;
public partial class Db
{
public void CreateFakeRelations()
{
_Db.RunInTransaction(() =>
{
CreateFakeUserTree();
CreateFakeFolderTree();
LinkFakeInstallationsToFolders();
GiveFakeUsersAccessToFolders();
GiveFakeUsersAccessToInstallations();
});
}
private void CreateFakeUserTree()
{
foreach (var userId in Enumerable.Range(1, NbUsers))
{
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 void CreateFakeFolderTree()
{
foreach (var folderId in Enumerable.Range(1, NbFolders))
{
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 void LinkFakeInstallationsToFolders()
{
var nFolders = NbFolders;
foreach (var installation in Installations)
{
installation.ParentId = Random.Shared.Next(nFolders) + 1;
Update(installation);
}
}
private void GiveFakeUsersAccessToFolders()
{
foreach (var uf in User2Folder) // remove existing relations
_Db.Delete(uf);
var nFolders = NbFolders;
foreach (var user in Users)
while (Random.Shared.Next(5) != 0)
{
var relation = new User2Folder
{
UserId = user.Id,
FolderId = Random.Shared.Next(nFolders) + 1
};
_Db.Insert(relation);
}
}
private void GiveFakeUsersAccessToInstallations()
{
foreach (var ui in User2Installation) // remove existing relations
_Db.Delete(ui);
var nbInstallations = NbInstallations;
foreach (var user in Users)
while (Random.Shared.Next(5) != 0)
{
var relation = new User2Installation
{
UserId = user.Id,
InstallationId = Random.Shared.Next(nbInstallations) + 1
};
_Db.Insert(relation);
}
}
}