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

100 lines
2.7 KiB
C#
Raw Normal View History

2023-03-08 12:20:33 +00:00
using InnovEnergy.App.Backend.Model.Relations;
2023-02-16 12:57:06 +00:00
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
{
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;
2023-02-16 12:57:06 +00:00
foreach (var user in Users)
while (Random.Shared.Next((Int32)(nUsers - user.Id + 1)) != 0)
2023-02-16 12:57:06 +00:00
{
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);
}
}
}