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

100 lines
2.8 KiB
C#
Raw Normal View History

2023-03-15 13:38:06 +00:00
using InnovEnergy.App.Backend.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
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
public static void CreateFakeRelations()
2023-02-16 12:57:06 +00:00
{
Connection.RunInTransaction(() =>
2023-02-16 12:57:06 +00:00
{
CreateFakeUserTree();
CreateFakeFolderTree();
LinkFakeInstallationsToFolders();
GiveFakeUsersAccessToFolders();
GiveFakeUsersAccessToInstallations();
});
}
2023-03-13 10:36:50 +00:00
private static void CreateFakeUserTree()
2023-02-16 12:57:06 +00:00
{
2023-03-15 13:38:06 +00:00
foreach (var userId in Enumerable.Range(1, Users.Count()))
2023-02-16 12:57:06 +00:00
{
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);
}
}
2023-03-13 10:36:50 +00:00
private static void CreateFakeFolderTree()
2023-02-16 12:57:06 +00:00
{
2023-03-15 13:38:06 +00:00
foreach (var folderId in Enumerable.Range(1, Folders.Count()))
2023-02-16 12:57:06 +00:00
{
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);
}
}
2023-03-13 10:36:50 +00:00
private static void LinkFakeInstallationsToFolders()
2023-02-16 12:57:06 +00:00
{
2023-03-15 13:38:06 +00:00
var nFolders = Folders.Count();
2023-02-16 12:57:06 +00:00
foreach (var installation in Installations)
{
installation.ParentId = Random.Shared.Next(nFolders) + 1;
Update(installation);
}
}
2023-03-13 10:36:50 +00:00
private static void GiveFakeUsersAccessToFolders()
2023-02-16 12:57:06 +00:00
{
foreach (var uf in FolderAccess) // remove existing relations
Connection.Delete(uf);
2023-02-16 12:57:06 +00:00
2023-03-15 13:38:06 +00:00
var nFolders = Folders.Count();
var nUsers = Users.Count();
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 FolderAccess
2023-02-16 12:57:06 +00:00
{
UserId = user.Id,
FolderId = Random.Shared.Next(nFolders) + 1
};
Connection.Insert(relation);
2023-02-16 12:57:06 +00:00
}
}
2023-03-13 10:36:50 +00:00
private static void GiveFakeUsersAccessToInstallations()
2023-02-16 12:57:06 +00:00
{
foreach (var ui in InstallationAccess) // remove existing relations
Connection.Delete(ui);
2023-02-16 12:57:06 +00:00
2023-03-15 13:38:06 +00:00
var nbInstallations = Installations.Count();
2023-02-16 12:57:06 +00:00
foreach (var user in Users)
while (Random.Shared.Next(5) != 0)
{
var relation = new InstallationAccess
2023-02-16 12:57:06 +00:00
{
UserId = user.Id,
InstallationId = Random.Shared.Next(nbInstallations) + 1
};
Connection.Insert(relation);
2023-02-16 12:57:06 +00:00
}
}
}