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

245 lines
6.4 KiB
C#
Raw Normal View History

2023-02-16 12:57:06 +00:00
using System.Diagnostics.CodeAnalysis;
2023-03-08 12:20:33 +00:00
using InnovEnergy.App.Backend.Model;
using InnovEnergy.App.Backend.Model.Relations;
2023-02-16 12:57:06 +00:00
using InnovEnergy.Lib.Utils;
using SQLite;
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
{
internal const String DbPath = "./db.sqlite";
2023-03-09 15:32:11 +00:00
public static SQLiteConnection Connection { get; } = new SQLiteConnection(DbPath);
public static TableQuery<Session> Sessions => Connection.Table<Session>();
public static TableQuery<Folder> Folders => Connection.Table<Folder>();
public static TableQuery<Installation> Installations => Connection.Table<Installation>();
public static TableQuery<User> Users => Connection.Table<User>();
public static TableQuery<User2Folder> User2Folder => Connection.Table<User2Folder>();
public static TableQuery<User2Installation> User2Installation => Connection.Table<User2Installation>();
public static Int32 NbUser2Installation => User2Installation.Count();
public static Int32 NbUser2Folder => User2Folder.Count();
public static Int32 NbFolders => Folders.Count();
public static Int32 NbInstallations => Installations.Count();
public static Int32 NbUsers => Users.Count();
public static Folder? GetFolderById(Int64 id)
{
return Folders
.FirstOrDefault(f => f.Id == id);
}
public static Installation? GetInstallationById(Int64 id)
{
return Installations
.FirstOrDefault(i => i.Id == id);
}
public static User? GetUserById(Int64 id)
{
return Users
.FirstOrDefault(u => u.Id == id);
}
2023-03-09 15:32:11 +00:00
2023-02-16 12:57:06 +00:00
[SuppressMessage("ReSharper", "AccessToDisposedClosure")]
static Db()
{
// on startup create/migrate tables
2023-03-09 15:32:11 +00:00
2023-02-16 12:57:06 +00:00
using var db = new SQLiteConnection(DbPath);
db.RunInTransaction(() =>
{
db.CreateTable<User>();
db.CreateTable<Installation>();
db.CreateTable<Folder>();
db.CreateTable<User2Folder>();
db.CreateTable<User2Installation>();
db.CreateTable<Session>();
});
}
2023-03-09 15:32:11 +00:00
2023-02-16 12:57:06 +00:00
// the C in CRUD
2023-03-13 10:36:50 +00:00
private static Int64 Create(TreeNode treeNode)
2023-02-16 12:57:06 +00:00
{
try
{
Connection.Insert(treeNode);
return SQLite3.LastInsertRowid(Connection.Handle);
2023-02-16 12:57:06 +00:00
}
catch (Exception e)
{
2023-03-09 15:32:11 +00:00
return -1;
2023-02-16 12:57:06 +00:00
}
2023-03-09 15:32:11 +00:00
}
2023-03-13 10:36:50 +00:00
private static Boolean Create(Session session)
2023-03-09 15:32:11 +00:00
{
try
{
Connection.Insert(session);
2023-03-09 15:32:11 +00:00
return true;
}
catch (Exception e)
{
return false;
}
}
2023-02-16 12:57:06 +00:00
// the U in CRUD
2023-03-13 10:36:50 +00:00
private static Boolean Update(TreeNode treeNode)
2023-02-16 12:57:06 +00:00
{
try
{
Connection.InsertOrReplace(treeNode);
2023-03-09 15:32:11 +00:00
return true;
2023-02-16 12:57:06 +00:00
}
catch (Exception e)
{
2023-03-09 15:32:11 +00:00
return false;
2023-02-16 12:57:06 +00:00
}
}
2023-03-09 15:32:11 +00:00
2023-02-16 12:57:06 +00:00
// the D in CRUD
2023-03-13 10:36:50 +00:00
private static Boolean Delete(TreeNode treeNode)
2023-02-16 12:57:06 +00:00
{
try
{
Connection.Delete(treeNode);
2023-03-09 15:32:11 +00:00
return true;
2023-02-16 12:57:06 +00:00
}
catch (Exception e)
{
2023-03-09 15:32:11 +00:00
return false;
2023-02-16 12:57:06 +00:00
}
}
2023-03-09 15:32:11 +00:00
2023-03-13 10:36:50 +00:00
public static IEnumerable<Installation> GetAllAccessibleInstallations(User user)
2023-02-16 12:57:06 +00:00
{
2023-03-13 10:36:50 +00:00
var direct = GetDirectlyAccessibleInstallations(user);
2023-02-16 12:57:06 +00:00
var fromFolders = GetAllAccessibleFolders(user)
2023-03-13 10:36:50 +00:00
.SelectMany(GetChildInstallations);
2023-02-16 12:57:06 +00:00
2023-03-08 14:45:53 +00:00
return direct
2023-03-13 10:36:50 +00:00
.Concat(fromFolders)
.Distinct();
2023-02-16 12:57:06 +00:00
}
2023-03-09 15:32:11 +00:00
2023-03-13 10:36:50 +00:00
public static IEnumerable<Folder> GetAllAccessibleFolders(User user)
2023-02-16 12:57:06 +00:00
{
return GetDirectlyAccessibleFolders(user)
2023-03-13 10:36:50 +00:00
.SelectMany(GetDescendantFolders)
.Distinct();
2023-03-09 15:32:11 +00:00
2023-03-08 14:45:53 +00:00
// Distinct because the user might have direct access
// to a child folder of a folder he has already access to
2023-02-16 12:57:06 +00:00
}
2023-03-09 15:32:11 +00:00
2023-03-13 10:36:50 +00:00
public static IEnumerable<Installation> GetDirectlyAccessibleInstallations(User user)
2023-02-16 12:57:06 +00:00
{
return User2Installation
2023-03-13 10:36:50 +00:00
.Where(r => r.UserId == user.Id)
.Select(r => r.InstallationId)
.Select(GetInstallationById)
.NotNull()
.Do(i => i.ParentId = 0); // hide inaccessible parents from calling user
2023-02-16 12:57:06 +00:00
}
2023-03-09 15:32:11 +00:00
2023-03-13 10:36:50 +00:00
public static IEnumerable<Folder> GetDirectlyAccessibleFolders(User user)
2023-02-16 12:57:06 +00:00
{
return User2Folder
2023-03-13 10:36:50 +00:00
.Where(r => r.UserId == user.Id)
.Select(r => r.FolderId)
.Select(GetFolderById)
.NotNull()
.Do(i => i.ParentId = 0); // hide inaccessible parents from calling user;
2023-02-16 12:57:06 +00:00
}
2023-03-09 15:32:11 +00:00
2023-03-13 10:36:50 +00:00
public static Boolean AddToAccessibleInstallations(Int64 userId, Int64 updatedInstallationId)
2023-02-16 12:57:06 +00:00
{
var con = new User2Installation
{
UserId = userId,
InstallationId = updatedInstallationId
};
2023-02-16 12:57:06 +00:00
try
{
Connection.Insert(con);
2023-03-09 15:32:11 +00:00
return true;
2023-02-16 12:57:06 +00:00
}
catch (Exception e)
{
2023-03-09 15:32:11 +00:00
return false;
2023-02-16 12:57:06 +00:00
}
}
2023-03-09 15:32:11 +00:00
2023-03-13 10:36:50 +00:00
public static Boolean AddToAccessibleFolders(Int64 userId, Int64 updatedFolderId)
2023-02-16 12:57:06 +00:00
{
var con = new User2Folder
{
UserId = userId,
FolderId = updatedFolderId
};
2023-02-16 12:57:06 +00:00
try
{
Connection.Insert(con);
2023-03-09 15:32:11 +00:00
return true;
2023-02-16 12:57:06 +00:00
}
catch (Exception e)
{
2023-03-09 15:32:11 +00:00
return false;
2023-02-16 12:57:06 +00:00
}
}
2023-03-13 10:36:50 +00:00
public static User? GetUserByToken(String token)
2023-02-16 12:57:06 +00:00
{
return Sessions
.Where(s => s.Token == token).ToList()
.Where(s => s.Valid)
.Select(s => s.UserId)
.Select(GetUserById)
.FirstOrDefault();
}
2023-03-13 10:36:50 +00:00
public static Boolean NewSession(Session ses) => Create(ses);
2023-03-13 10:36:50 +00:00
public static Boolean DeleteSession(Int64 id)
{
try
{
Sessions.Delete(u => u.UserId == id);
2023-03-09 15:32:11 +00:00
return true;
}
catch (Exception e)
{
2023-03-09 15:32:11 +00:00
return false;
}
}
2023-03-09 11:50:21 +00:00
2023-03-13 10:36:50 +00:00
public static String? GetInstallationS3Key(Int64 installationId)
2023-03-09 11:50:21 +00:00
{
return Installations
2023-03-13 10:36:50 +00:00
.FirstOrDefault(i => i.Id == installationId)?
.S3Key;
2023-03-09 11:50:21 +00:00
}
2023-03-13 10:36:50 +00:00
public static void DeleteAllS3Keys()
{
foreach (var installation in Installations.ToList())
{
installation.S3Key = null;
Update(installation);
}
}
2023-03-09 15:32:11 +00:00
}