100 lines
2.7 KiB
C#
100 lines
2.7 KiB
C#
|
using Innovenergy.Backend.Model.Relations;
|
||
|
|
||
|
namespace Innovenergy.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;
|
||
|
var nUsers = NbUsers;
|
||
|
|
||
|
foreach (var user in Users)
|
||
|
while (Random.Shared.Next((Int32)(nUsers - user.Id + 1)) != 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);
|
||
|
}
|
||
|
}
|
||
|
}
|